-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (33 loc) · 1.72 KB
/
index.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
const config = require('./config.js');
const RtmClient = require('@slack/client').RtmClient;
const CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS;
const RTM_EVENTS = require('@slack/client').RTM_EVENTS;
const makeMessageResponder = require('personality-helper');
config.bots.forEach(async(botConfig) => {
const messageResponder = makeMessageResponder(config.personalityServer, botConfig.language, botConfig.personality);
const rtm = new RtmClient(botConfig.bot_token);
// let channel;
// // The client will emit an RTM.AUTHENTICATED event on successful connection, with the `rtm.start` payload
// rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, (rtmStartData) => {
// for (const c of rtmStartData.channels) {
// if (c.is_member && c.name === 'general') {
// channel = c.id
// }
// }
// });
// // you need to wait for the client to fully connect before you can send messages
// rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, function () {
// rtm.sendMessage("Hello!", channel);
// });
rtm.on(RTM_EVENTS.MESSAGE, async(message) => {
if (message.type === 'message' && message.user !== rtm.activeUserId) {
const response = await messageResponder(message.text, 'slack', message.team + '-' + message.channel, `slack-${message.team}-${message.ts}`, Number(message.ts.split('.')[0]));
if (response && response.response) {
rtm.sendMessage(`<@${message.user}> ${response.response}`, message.channel);
} else if (response && response.error) {
console.warn(`Got an error from server for message [${messageBody}]`, response.error);
}
}
});
rtm.start();
});