-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
139 lines (121 loc) · 3.65 KB
/
bot.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const superagent = require('superagent');
const {App} = require('@slack/bolt');
let botConfig = {};
let app;
const sendCommand = async (commandName, payload) => {
try {
return await superagent.post(`https://slack.com/api/${commandName}`)
.set('Authorization', `Bearer ${botConfig.botUserOAuthToken}`)
.set('Content-Type', 'application/json')
.send(payload);
} catch (e) {
console.error('Error sending command');
console.error(e);
throw e;
}
}
const postMessage = async (target, text, blocks = []) => {
const message = {
channel: target,
text: text,
blocks: blocks
};
return await app.client.chat.postMessage(message);
}
const updateMessage = async (timestamp, channel, newText, newBlocks = []) => {
const message = {
channel,
ts: timestamp,
text: newText,
blocks: newBlocks
};
return await app.client.chat.update(message)
}
const reactToMessage = async (timestamp, channel, emoji) => {
const reaction = {
channel, timestamp, name: emoji
}
return await app.client.reactions.add(reaction);
}
const bot = {
postMessage,
updateMessage,
sendCommand,
reactToMessage
};
const plugins = [];
const messageQueue = [];
const start = async (config) => {
const {
botUserOAuthToken,
botSigningSecret,
botAppToken
} = config;
app = new App({
token: botUserOAuthToken,
signingSecret: botSigningSecret,
appToken: botAppToken,
socketMode: true
})
await app.start();
app.message((message) => {
messageQueue.push(message);
});
plugins.push(...config.plugins);
plugins.forEach(async (plugin) => {
await plugin.init(bot)
});
const handleMessage = (message) => {
const {
appId
} = message;
// Ignore own messages
if (appId === botConfig.botAppId) {
return;
}
console.log(message);
plugins.forEach(plugin => plugin.handleMessage(message));
}
const processQueue = () => {
if (messageQueue.length > 0) {
const event = messageQueue.shift();
const rawMessage = event.message;
const botAuthorization = event.body.authorizations.find(authorization => authorization.is_bot);
const type = event.type;
const rawText = event.message.text;
if (!rawText) {
// Happens on image only messages, etc
setTimeout(processQueue, 10);
return;
}
const botUserRegex = new RegExp(`\<@${botAuthorization.user_id}\> ?`);
const trimmedText = rawText.replace(botUserRegex, '').trim();
const fromUser = event.message.user;
const fromChannel = event.message.channel;
const eventId = event.message.client_msg_id;
const appId = event.body.api_app_id || undefined;
console.log(`Processing message ${eventId}`);
try {
handleMessage({
type,
rawText,
trimmedText,
fromUser,
fromChannel,
eventId,
appId,
rawMessage
});
} catch (e) {
console.log(`Couldn't process message - ${e}`);
postMessage(fromChannel, `Couldn't process message. \`\`\`${e}\`\`\``);
}
}
setTimeout(processQueue, 10);
}
setTimeout(processQueue, 10);
return bot;
}
module.exports = {
start
};