1
|
|
|
import {HttpFunction} from '@google-cloud/functions-framework'; |
2
|
|
|
import {chat_v1 as chatV1} from '@googleapis/chat'; |
3
|
|
|
import CommandHandler from './handlers/CommandHandler'; |
4
|
|
|
import MessageHandler from './handlers/MessageHandler'; |
5
|
|
|
import ActionHandler from './handlers/ActionHandler'; |
6
|
|
|
import {generateHelpText, welcomeButtonCard} from './helpers/helper'; |
7
|
|
|
import TaskHandler from './handlers/TaskHandler'; |
8
|
|
|
|
9
|
|
|
export const app: HttpFunction = async (req, res) => { |
10
|
|
|
if (!(req.method === 'POST' && req.body)) { |
11
|
|
|
console.log('unknown access', req.hostname, req.ips.join(','), req.method, JSON.stringify(req.body)); |
12
|
|
|
res.status(400).send(''); |
13
|
|
|
} |
14
|
|
|
const event = req.body; |
15
|
|
|
if (event.type === 'TASK') { |
16
|
|
|
await new TaskHandler(event).process(); |
17
|
|
|
res.json(''); |
18
|
|
|
} |
19
|
|
|
// console.log(JSON.stringify(event)); |
20
|
|
|
console.log(event.type, |
21
|
|
|
event.common?.invokedFunction || event.message?.slashCommand?.commandId || event.message?.argumentText, |
22
|
|
|
event.user.displayName, event.user.email, event.space.type, event.space.name); |
23
|
|
|
let reply: chatV1.Schema$Message = { |
24
|
|
|
thread: event.message?.thread, |
25
|
|
|
actionResponse: { |
26
|
|
|
type: 'NEW_MESSAGE', |
27
|
|
|
}, |
28
|
|
|
text: 'Hi! To create a poll, you can use the */poll* command. \n \n' + |
29
|
|
|
'Alternatively, you can create poll by mentioning me with question and answers. ' + |
30
|
|
|
'e.g *@Absolute Poll "Your Question" "Answer 1" "Answer 2"*', |
31
|
|
|
}; |
32
|
|
|
|
33
|
|
|
// Dispatch slash and action events |
34
|
|
|
if (event.type === 'MESSAGE') { |
35
|
|
|
const message = event.message; |
36
|
|
|
if (message.slashCommand?.commandId) { |
37
|
|
|
reply = new CommandHandler(event).process(); |
38
|
|
|
} else if (message.text) { |
39
|
|
|
reply = new MessageHandler(event).process(); |
40
|
|
|
} |
41
|
|
|
} else if (event.type === 'CARD_CLICKED') { |
42
|
|
|
reply = await (new ActionHandler(event).process()); |
43
|
|
|
} else if (event.type === 'ADDED_TO_SPACE') { |
44
|
|
|
const message: chatV1.Schema$Message = { |
45
|
|
|
text: undefined, |
46
|
|
|
cardsV2: undefined, |
47
|
|
|
}; |
48
|
|
|
const spaceType = event.space.type; |
49
|
|
|
if (spaceType === 'ROOM') { |
50
|
|
|
message.text = generateHelpText(); |
51
|
|
|
} else if (spaceType === 'DM') { |
52
|
|
|
message.text = generateHelpText(true); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
message.cardsV2 = [welcomeButtonCard]; |
56
|
|
|
|
57
|
|
|
reply = { |
58
|
|
|
actionResponse: { |
59
|
|
|
type: 'NEW_MESSAGE', |
60
|
|
|
}, |
61
|
|
|
...message, |
62
|
|
|
}; |
63
|
|
|
} |
64
|
|
|
res.json(reply); |
65
|
|
|
}; |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
|