-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
328 lines (293 loc) · 9.2 KB
/
Copy pathmain.js
File metadata and controls
328 lines (293 loc) · 9.2 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var S = require("string");
var fs = require("fs");
var options, optionsFilename;
var app, server, response;
main();
function main() {
loadOptions();
app = express();
app.use(bodyParser.json());
app.use(function(req, res, next) {
console.log(' in> %s %s %s', req.method, req.path, JSON.stringify(req.body));
next();
});
endpoint("/resolved", doResolved);
endpoint("/added", doAdded);
endpoint("/removed", doRemoved);
endpoint("/message", doMessage);
endpoint("/offer", doOffer);
server = app.listen(options.port, function () {
console.log('Example app listening on port %s', server.address().port);
});
// do something asynchronously
setInterval(doSomething, 1000000);
}
function endpoint(endpoint, handler) {
app.post(endpoint, function(req, res) {
res.succeed = function (obj) {
obj = obj || {};
obj.success = "true";
console.log('resp> SUCCESS %s', JSON.stringify(obj));
this.status(200).send(obj);
};
res.fail = function (text) {
console.log('resp> FAIL %s', text);
this.status(200).send({success: "false", text: text});
};
response = res;
if (!req.body) {
res.fail("Missing comand or action");
return;
}
handler(req.body);
});
}
//---------------- asynchronous part of the protocol
function doSomething() {
var id = getRandomRoom();
if (id) {
request('http://api.icndb.com/jokes/random', function(error, response, body) {
body = JSON.parse(body);
var joke = body.type == "success" ? body.value.joke : "fail"
asyncSay(joke, id);
});
}
}
function asyncSay(text, roomId, mediaType) {
var data = {
token: options.chatboxToken,
content: text,
roomId: roomId,
mediaType: mediaType || "html",
id: new Date().getTime()
};
asyncSend(data, "/chat");
}
function asyncWhisper(text, roomId, mediaType) {
var data = {
token: options.chatboxToken,
content: text,
roomId: roomId,
mediaType: mediaType || "html",
id: new Date().getTime()
};
asyncSend(data, "/whisper");
}
function asyncHandoff(queueId, roomId) {
var data = {
token: options.chatboxToken,
queueId: queueId,
roomId: roomId
};
asyncSend(data, "/handoff");
}
function asyncResolve(queueId, roomId) {
var data = {
token: options.chatboxToken,
queueId: queueId,
roomId: roomId
};
asyncSend(data, "/resolve");
}
function asyncLeave(roomId) {
var data = {
token: options.chatboxToken,
roomId: roomId
};
asyncSend(data, "/leaveRoom", function() {
console.log("==== Left Room " + roomId + " ====");
removeRoom(roomId);
});
}
function asyncHistory(roomId) {
var data = {
token: options.chatboxToken,
roomId: roomId
};
asyncSend(data, "/history", function(body) {
console.log("==== History for Room " + roomId + " ====");
console.log(body);
});
}
function asyncAddInteractive(roomId) {
var data = {
token: options.chatboxToken,
customerId: options.byRoom[roomId].userId,
interactiveId: options.defaultInteractiveId
};
var action;
var mediaType = options.byRoom[roomId].mediaType;
if (mediaType && mediaType == "sms") {
action = "/smsInteractive";
data.message = "Come take a look at this {link}";
} else {
action = "/makeInteractive"
}
asyncSend(data, action, function(body) {
console.log("==== Adding interactive to " + roomId + " ====");
});
}
function asyncSend(data, location, callback, tries) {
tries = tries || 1;
console.log("POST " + location + " " + JSON.stringify(data));
request({
method: 'POST',
uri: options.chatboxURL + location,
json: true,
gzip: true,
body: data
}, function(error, response, body) {
if (!response) {
// unable to complete request successfully, try later
console.log("fail> empty");
retry();
} else {
switch (response.statusCode) {
case 200:
// success!
console.log("success> " + JSON.stringify(body));
if (callback) callback(body);
break;
case 401:
// maintenance, try later
console.log("fail-401> " + JSON.stringify(body));
retry();
break;
case 404:
// insufficient rights
console.log("fail-404> " + JSON.stringify(body));
console.log(" err> insufficient rights to post to %s", options.chatboxURL + location)
break;
default:
console.log("fail-" + response.statusCode + "> " + JSON.stringify(body));
break;
}
}
});
function retry() {
// this retries for about 6 minutes, which is probably more patience than customers will show
if (tries < 10) {
var tm = tries * tries * 1000;
setTimeout(function() { asyncSend(data, location, callback, tries+1)}, tm);
} else {
console.log(" err> ran out of tries to post to %s", options.chatboxURL + location);
}
}
}
//----------------- synchronous part of the protocol
function doResolved(body) {
response.succeed();
console.log("Room marked as resolved, history:");
console.log(body.messages);
console.log("Going to leave the room");
asyncLeave(body.roomId);
}
function doAdded(body) {
response.succeed();
if(addRoom(body.roomId, body.userId)){
asyncSay("Thank you for having me, it's great to be here!", { suppressWelcome: true }, body.roomId);
}
}
function doRemoved(body) {
response.succeed();
removeRoom(body.roomId);
}
function doMessage(body) {
response.succeed();
if (body.content.indexOf("#handoff") >= 0) {
asyncWhisper("I am asking for some human assistance, sync", body.roomId);
asyncHandoff(body.queueId, body.roomId);
} else if (body.content.indexOf("#resolve") >= 0) {
asyncWhisper("I am resolving, sync", body.roomId);
asyncResolve(body.queueId, body.roomId);
} else if (body.content.indexOf("#history") >= 0) {
asyncWhisper("I am asking for the history", body.roomId);
asyncHistory(body.roomId);
} else if (body.content.indexOf("#interactive") >= 0) {
asyncWhisper("I am adding an interactive", body.roomId);
asyncAddInteractive(body.roomId);
} else {
if (body.visibility == "whisper") {
asyncWhisper("Sssh <b>" + body.userName + "</b>, you I can hear you ", body.roomId);
} else {
if (body.mediaType == "sms") {
asyncSay("Hello there " + body.userName + " you spoke and I heard " + body.content, body.roomId, "sms");
} else {
asyncSay("Hello there <b>" + body.userName + "</b> you spoke and I heard <i title=\'" + JSON.stringify(body) + "\'>" + body.content + "</i>", body.roomId);
}
}
}
}
function doOffer(body) {
var routing = body.routing;
var contents = body.contents;
var username = body.userName;
var userId = body.userId;
var roomId = body.roomId;
var channel = body.channel == "sms" ? "sms": undefined;
if (contents.length == 0) {
response.fail("offer with empty contents");
return;
}
// fake auto-ignore and auto-skip by just looking for #skip and #ignore in message
if (routing == "automatic") {
if (S(contents[0]).contains("#skip")) {
response.succeed({ action: "skip" });
return;
}
if (S(contents[0]).contains("#ignore")) {
response.succeed({ action: "ignore" });
return;
}
}
response.succeed({ action: "take" });
addRoom(body.roomId, userId, channel);
asyncSay("Enter, if you dare {chatbox}",roomId, channel);
}
//--------------------- misc functions
function loadOptions() {
var args = process.argv.slice(2);
optionsFilename = args.length > 0 ? args[0] : "examplebot.opts";
if (fs.existsSync(optionsFilename)) {
var text = fs.readFileSync(optionsFilename, "utf8");
options = JSON.parse(text);
} else {
options = {};
options.chatboxToken = "unknown";
options.chatboxURL = "https://unknown/api/v1";
options.rooms = [];
options.byRoom = {};
options.port = 7111;
options.defaultInteractiveId = 1;
saveOptions();
console.log("Please edit " + optionsFilename + " to have the proper values for chatbox URL and token");
process.exit(0);
}
}
function saveOptions() {
fs.writeFileSync(optionsFilename, JSON.stringify(options, null, 3), "utf8");
}
function getRandomRoom() {
if (options.rooms.length > 0) {
var ix = Math.floor(Math.random() * options.rooms.length);
return options.rooms[ix];
}
}
function addRoom(roomId, userId, mediaType) {
if (!options.byRoom[roomId]) {
options.byRoom[roomId] = {userId: userId, mediaType: mediaType};
options.rooms.push(roomId);
saveOptions();
return true;
}
return false;
}
function removeRoom(roomId) {
options.byRoom[roomId] = null;
var ix = options.rooms.indexOf(roomId);
options.rooms.splice(ix, 1);
saveOptions();
}