-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (50 loc) · 1.94 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'use strict';
const client = require('@slack/client');
const bluebird = require('bluebird');
class slack {
constructor() {
this.name = 'slack';
this.displayname = 'Slack Chat';
this.description = 'Send messages to woodhouse via Slack';
this.defaultPrefs = {
token: {
displayname: 'Token',
type: 'text',
value: ''
}
};
}
init() {
bluebird.all([
this.getSystemPref('name'),
this.getPref('token')
]).then(([name, token]) => {
const connection = new client.RtmClient(token, {
dataStore: new client.MemoryDataStore()
});
let botId;
connection.start();
connection.on(client.CLIENT_EVENTS.RTM.AUTHENTICATED, (rtmStartData) => {
botId = rtmStartData.self.id;
});
connection.on(client.RTM_EVENTS.MESSAGE, (message) => {
const user = connection.dataStore.getUserById(message.user);
const nameRegex = new RegExp('^[<]{0,1}[@]{0,1}' + botId + '[>]{0,1}[:]{0,1}');
const mentionRegex = new RegExp('<@([0-9a-z]+)>', 'gi');
if (message.type === 'message' && message.text) {
message.text = message.text.replace(nameRegex, name);
let mention;
while (mention = mentionRegex.exec(message.text)) {
let mentionUser = connection.dataStore.getUserById(mention[1]);
message.text = message.text.replace(mention[0], mentionUser.name);
}
this.messageRecieved(message.channel, message.text, user.name);
}
});
this.addMessageSender(function(to, message){
connection.sendMessage(message, to);
});
});
}
}
module.exports = slack;