-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtm.js
57 lines (47 loc) · 1.48 KB
/
rtm.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
const Botkit = require('botkit');
const {RtmClient, MemoryDataStore, CLIENT_EVENTS} = require('@slack/client');
const token = process.env.TOKEN;
const channelName = process.env.CHANNEL || "games";
let channel_id;
const controller = Botkit.slackbot();
let users = [];
const rtm = new RtmClient(token, {
logLevel: 'error',
dataStore: new MemoryDataStore(),
autoReconnect: true,
autoMark: true
});
rtm.start();
rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, function () {
users = rtm.dataStore.users;
const channels = process.env.NODE_ENV === "development"
? rtm.dataStore.groups
: rtm.dataStore.channels;
for (let key of Object.keys(channels)) {
let channel = channels[key];
if (channel.name === channelName) {
channel_id = channel.id;
break;
}
}
if (!channel_id) {
console.error(`Channel with name "${channelName}" required`);
process.exit(-1);
}
rtm.disconnect();
});
rtm.on(CLIENT_EVENTS.RTM.DISCONNECT, function () {
const hangBot = controller.spawn({
token: token
}).startRTM();
hangBot.replyToUser = function (src, rsp, attachments = null) {
src.channel = channel_id;
let msg = `${users[src.user].name}: ${rsp}`;
if (attachments) {
this.reply(src, { text: msg, attachments: attachments });
} else {
this.reply(src, msg);
}
};
});
module.exports = controller;