-
Notifications
You must be signed in to change notification settings - Fork 1
/
messaging.js
executable file
·223 lines (184 loc) · 6.73 KB
/
messaging.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
This file contains functions that help with communication with slack
*/
const https = require("https");
const qs = require("querystring");
const botToken = require("./secrets")["bot-token"];
const events = require("./events");
module.exports = {
setDefaultChannelID: setDefaultChannelID,
dmUser: dmUser,
groupMessage: groupMessage,
channelMsg: channelMsg,
getDefaultChannelID: getDefaultChannelID,
ephemeralMsg: ephemeralMsg,
closeConversation: closeConversation
};
let defaultChannelID;
function setDefaultChannelID(id) {
defaultChannelID = id;
}
function getDefaultChannelID() {
return defaultChannelID;
}
//DM a specific user
//callback will be called with the first reply from the user
function dmUser(userID, message, cb) {
//First, open a DM channel with the user
//Setup request options
const requestData = qs.stringify({
token: botToken,
users: userID
});
const options = {
hostname: "slack.com",
path: "/api/conversations.open?"+requestData
};
https.get(options, function(res) {
let fullData = "";
res.on("data", function(data) {
fullData += data;
});
res.on("end", function() {
//extract the conversation ID
let convID = JSON.parse(fullData).channel.id;
//Setup call to the slack API to send the message
const msgReqData = qs.stringify({
token: botToken,
channel: convID,
text: message
});
const msgReqOptions = {
hostname: "slack.com",
path: "/api/chat.postMessage?"+msgReqData
};
https.get(msgReqOptions, function(res) {
//We don't really care about the info we get back (we will just assume message send success for now)
//debug only
res.on("data", function(data) {
//console.log("recv data:", data);
});
res.on("end", function() {
//The request is finished, we can register a callback for the user reply so that we can send it back to our callback
let cbUUID = events.registerCallbackUserChannelReply(userID, convID, function (reply) {
//Deregister the callback so that we only get the first reply, not any others
events.deregisterCallback(cbUUID);
if(cb){cb(reply);}
});
})
});
});
res.on("error", function(err) {
console.log("Error with request to open conversation:", err);
});
})
}
//Starts a group message with the people in userArr
//Callback gets called every time *any* user in the group message channel replies to the channel
//This way, we can easily tally the votes/kill mentions/etc
//The callback also is given the UUID for cb registration as its second parameter, that way it can cancel notifications for any further replies
function groupMessage(userArr, message, cb) {
//First transform the userArr into a comma separated list of userids for the slack api
let userList = "";
for(let i=0; i<userArr.length; i++) {
userList += ","+userArr[i];
}
//Take the first comma off the userList
userList.slice(0,1);
//Now setup a call to slack api for creating conversation with these users
const reqData = qs.stringify({
token: botToken,
users: userList
});
const options = {
hostname: "slack.com",
path: "/api/conversations.open?"+reqData
};
https.get(options, function(res) {
let fullData = "";
res.on("data", function(data) {
fullData += data;
});
res.on("end", function() {
//We have all the data, grab the conversation ID and send the message
console.log(fullData);
let convID = JSON.parse(fullData).channel.id;
//Setup call to the slack API to send the message
const msgReqData = qs.stringify({
token: botToken,
channel: convID,
text: message
});
const msgReqOptions = {
hostname: "slack.com",
path: "/api/chat.postMessage?"+msgReqData
};
https.get(msgReqOptions, function(res) {
console.log("INITIAL GROUP MSG POST MAIN CB");
res.on("end", function() {
console.log("INITIAL GROUP MESSAGE SEND REQ END");
//register callback with the events system here
let cbUUID = events.registerCallbackChannelReply(convID, function(reply) {
console.log("CHANNEL REPLY ON GROUP MESSAGE CHANNEL!!!");
cb(reply, convID, cbUUID);
});
});
res.on("data", function(data) {
console.log("GM RECV DATA, DO NOTHING");
});
res.on("error", function(err) {
console.log("INITIAL GROUP MESSAGE SEND REQ ERROR:", err);
});
});
});
res.on("error", function(err) {
console.log("ERROR setting up group message:", err);
})
});
}
//Send a message to the entire channel
function channelMsg(channelID, message) {
//Much simpler than user dm code, we just use the chat.postMessage endpoint
if(channelID === undefined) {
channelID = defaultChannelID;
}
const msgReqData = qs.stringify({
token: botToken,
channel: channelID,
text: message
});
const msgReqOptions = {
hostname: "slack.com",
path: "/api/chat.postMessage?"+msgReqData
};
https.get(msgReqOptions); //no need for a callback, we don't really care about whether the message got sent correctly or not
}
function ephemeralMsg(channelID, userID, message) {
//If channel not specified, use default channel
if(channelID === undefined) {
channelID = defaultChannelID;
}
const reqData = qs.stringify({
token: botToken,
channel: channelID,
user: userID,
text: message
});
const msgReqOptions = {
hostname: "slack.com",
path: "/api/chat.postEphemeral?"+reqData
};
https.get(msgReqOptions);
}
function closeConversation(convID) {
//Setup request to the slack API
const reqData = qs.stringify({
token: botToken,
channel: convID
});
const options = {
hostname: "slack.com",
path: "/api/conversations.close?"+reqData
};
https.get(options); //we don't really care if the conversation was closed properly or not, just do it
}