-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWebHooks.ts
105 lines (95 loc) · 3.87 KB
/
WebHooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function doGet(e) {
WebHookHandler.handleActionEvent();
return ContentService.createTextOutput('handled doGet');
}
function doPost(e) {
WebHookHandler.handleMessageEvent(e);
return ContentService.createTextOutput('handled doPost');
}
class WebHookHandler {
static handleMessageEvent(event) {
const eventData = event.postData.contents
try {
const messageData = JSON.parse(eventData).data;
if (messageData.personEmail !== MainConfig.botEmail) {
const webexClient = WebexClient();
const messageFull = webexClient.getMessage(messageData.id);
if (messageData.roomType === 'group') {
webexClient.DEBUG = messageFull.roomId === MainConfig.debugRoomId;
WebHookHandler.handlePublicCommandMessages([messageFull], webexClient);
} else {
// console.log('Handled one-to-one message: ', messageFull);
WebHookHandler.handlePrivateCommandMessage(messageFull, webexClient);
}
}
} catch (e) {
console.error(e);
console.log(event.postData.contents)
console.log(event.postData)
console.log(event)
debugMessage(`Failed to handle message event: ${e.message}. See detailed logs at google apps script.`);
}
}
static handlePublicCommandMessages(directMessages, webexClient) {
const publicCommands = PublicCommands();
directMessages.forEach(message => {
const user = message.personEmail;
// rememory bot support
if (message.text.match(/Rememory remind/)) {
// ignore when bot is mentioned in a message to Rememory bot
return;
}
if (BotUtils.isBot(user)) {
// example: Tbot Reminder: "shuffle" -created by Bogdan Kovalev
let rememoryCommand = message.text.match(/Tbot Reminder: "(.+)" -/);
if (rememoryCommand) {
rememoryCommand = rememoryCommand[1];
debugMessage(`matched Rememory bot command: '${rememoryCommand}'\noriginal: '${message.text}'`);
message.text = `Tbot ${rememoryCommand}`;
// TODO: update sender using created by
}
}
try {
publicCommands.some(command => {
if (command.match(message.text)) {
if (command.adminsOnly && !BotUtils.isMessageFromAdmin(user, webexClient)) {
webexClient.sendMessageToRoom(message.roomId,
mention(user) + getRandomFromList(Constants.funnyRestrictions));
} else if (command.workDayOnly && BotUtils.isBot(user) && !isWorkDay(new Date())) {
// If sent from any bot, do not run workDayOnly commands on weekends/holidays
webexClient.sendMessageToRoom(message.roomId, `I'm ignoring this reminder because today is a day off.`);
} else {
console.log('Evaluating public command', command, message);
command.evaluate(message, webexClient);
}
return true;
}
});
} catch (e) {
debugMessage(`Failed to handle public command: ${message.text} \nError: ${e.message}`);
console.error(e);
console.error(e.stack);
webexClient.sendMessageToRoom(message.roomId,
mention(user) + ', sorry, but something wrong happened. I cannot handle your command: ' + message.text);
}
});
}
static handlePrivateCommandMessage(message, webexClient) {
try {
PrivateCommands().some(command => {
if (command.match(message.text, message, webexClient)) {
console.log('Evaluating private command', command, message);
command.evaluate(message, webexClient);
return true;
}
});
} catch (e) {
debugMessage('Failed to handle private command: ' + message.text);
console.error(e);
webexClient.sendMessageToPerson(message.personEmail,
'Sorry, but something wrong happened. I cannot handle your latest message.');
}
}
static handleActionEvent() {
}
}