-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathchat_utils.js
More file actions
395 lines (333 loc) · 9.71 KB
/
chat_utils.js
File metadata and controls
395 lines (333 loc) · 9.71 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
var request = require('request');
var FBTOKEN = process.env['FBTOKEN'];
function reply(sender, reply_data, test_token){
// A generic reply function
// @param: sender - recepient of this message
// @param: reply_data - a string of the text or object containing an attachment
// [@param test_token - for testing purposes.]
var token = typeof test_token !== 'undefined' ? test_token : FBTOKEN;
var reply_data = typeof reply_data === 'string' ? {text:reply_data} : reply_data;
request({
url:'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id:sender},
message:reply_data,
}
}, function(error, response, body){
// console.log('sent:',reply_data)
if(error){
console.log('ERROR SENDING MESSAGE',error);
reply(sender, 'Something went wrong sending you a Facebook message. Try again!', token);
// throw Error(error);
}else if (response.body.error){
console.log('ERROR:', response.body.error);
reply(sender, 'Something went wrong sending you a Facebook message. Try again!', token);
// throw Error(response.body.error);
}
return true;
});
}
function reply2(sender, replies, test_token){
var token = typeof test_token !== 'undefined' ? test_token : FBTOKEN;
var reply_data = replies.shift();
// do a converstion if it is not a string.
reply_data = typeof reply_data === 'string' ? {text:reply_data.substr(0,319)} : reply_data;
try{
request({
url:'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id:sender},
message:reply_data,
}
}, function(error, response, body){
// console.log('sent:',reply_data)
if(error){
console.log(reply_data)
reply2(sender, ['Wow this is embarassing! :( Something went wrong sending you a Facebook message. Try again!'], token)
console.log('ERROR SENDING MESSAGE',error);
// throw Error(error);
}else if (response.body.error){
console.log(reply_data)
reply2(sender, ['Wow this is embarassing... :/ Something went wrong sending you a Facebook message. Try again!'], token)
console.log('ERROR:', response.body.error);
// throw Error(response.body.error);
}
if(replies.length){
setTimeout(function(){
reply2(sender,replies,token);
}, 200);
}
});
}catch(e){
console.log(reply_data)
reply2(sender, ['Wow this is embarrasing :/ An error occured while trying to send you a Facebook message.'], token)
}
}
function set_state(sender, state, test_token){
// sets the state of the chat to either "seen" or "typing on"
var token = typeof test_token !== 'undefined' ? test_token : FBTOKEN;
if(state !== 'mark_seen' && state !== 'typing_on' && state !== 'typing_off'){
throw Error('Unknown thread state')
}
request({
url:'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id:sender},
sender_action:state,
}
}, function(error, response, body){
if(error){
console.log('Error setting state',error);
}else if (response.body.error){
console.log('Error setting state:', response.body.error);
}
return true;
});
}
function menu(options, test_token){
/*
Creates a menu for the chat,
options must be of the form:
[ [ type, title, payload/url ],...]
*/
var token = typeof test_token !== 'undefined' ? test_token : FBTOKEN;
if(options.length > 5){
throw Error('Cant have more than 5 options in the men')
}
var menu = {
"setting_type": "call_to_actions",
"thread_state": "existing_thread",
"call_to_actions":[]
}
for (var i = 0; i < options.length; i++) {
var button_type = options[i][0];
var button_title = options[i][1];
var button_payload = options[i][2];
var to_save = {
type: button_type,
title: button_title
}
if(button_type === "web_url"){
to_save["url"] = button_payload;
}else if(button_type === "postback"){
to_save["payload"] = button_payload;
}else{
throw Error("Button type was not web_url nor payload. Other types not supported.");
}
menu["call_to_actions"].push(to_save)
}
request({
url:'https://graph.facebook.com/v2.6/me/thread_settings',
qs: {access_token:token},
method: 'POST',
json: menu
}, function(error, response, body){
// console.log('sent:',text)
if(error){
console.log('Error creating menu',error);
// throw Error(error);
}else if (response.body.error){
console.log('Error creating menu:', response.body.error);
// throw Error(response.body.error);
}
return true;
})
}
function welcome(test_token){
// A generic reply function
// @param: sender - recepient of this message
// @param: text - a string of the text
// [@param test_token - for testing purposes.]
var token = typeof test_token !== 'undefined' ? test_token : FBTOKEN;
// Add user text when user selects "get started"
request({
url:'https://graph.facebook.com/v2.6/me/thread_settings',
qs: {access_token:token},
method: 'POST',
json: { "setting_type":"call_to_actions",
"thread_state":"new_thread",
"call_to_actions":[
{
"payload":"NEWUSER"
}
]
}
}, function(error, response, body){
// console.log('sent:',text)
if(error){
console.log('Error setting welcome message',error);
// throw Error(error);
}else if (response.body.error){
console.log('Error setting welcome message:', response.body.error);
// throw Error(response.body.error);
}
return true;
});
// add greeting.
request({
url:'https://graph.facebook.com/v2.6/me/thread_settings',
qs: {access_token:token},
method: 'POST',
json: { "setting_type":"greeting",
"greeting":{
"text":"Minerva Bot here to help you with all things McGill!"
}
}
}, function(error, response, body){
// console.log('sent:',text)
if(error){
console.log('Error setting welcome message',error);
// throw Error(error);
}else if (response.body.error){
console.log('Error setting welcome message:', response.body.error);
// throw Error(response.body.error);
}
return true;
});
}
function build_quick_reply(text, button_doubles){
/*
text: Text to dispaly
button_doubles: array of the form
[ [ text, payload ], ... ]
*/
if(button_doubles.length === 0){
throw Error('No quick reply options supplied.')
}
var quick_replies = []
for (var i = 0; i < button_doubles.length; i++) {
quick_replies.push({
content_type: "text",
title: button_doubles[i][0].substr(0,20),
payload: button_doubles[i][1]
})
}
return {
text: text,
quick_replies: quick_replies
}
}
function build_structured_response(text,button_triples){
/*
text: Text to display
button_triples: array of the form
[ [ type, title, payload ], ... ]
*/
var buttons = [];
for (var i = 0; i < button_triples.length; i++) {
buttons.push(build_buttons(button_triples[i]))
}
var to_return = {
attachment:{
type: "template",
payload: {
template_type: "button",
text: text,
"buttons":buttons
}
}
}
return to_return
}
function build_buttons(button_triple){
var button_type = button_triple[0]; // the "type": "web_url" or "postback"
// temporary object
var to_save = {type:button_type}
// console.log(button_triple)
// element share buttons are a bit different
// they do not have urls etc so we skip adding more information to them.
if(button_type === "element_share"){
return to_save;
}
// Since this is not an element share button, we extract more information.
var button_title = button_triple[1]; // "title" to display
var button_payload = button_triple[2]; // the payload, either the "url" or "payload"
to_save["title"] = button_title;
// extract what kind of button it is and work accordingly.
if(button_type === "web_url"){
to_save["url"] = button_payload;
// check if we have a webview hight option.
if( button_triple.length == 4 ){
var height_ratio = button_triple[3];
// only available options right now.
if(["compact", "full", "tall"].indexOf(height_ratio) > -1){
to_save["webview_height_ratio"] = height_ratio;
}else{
throw Error("Height Ratio Unrecognized.");
}
}
}else if(button_type === "postback"){
to_save["payload"] = button_payload;
}else{
throw Error("Button type was not web_url nor payload. Other types not supported.");
}
return to_save;
}
function build_attachment(url, type){
var to_save = {
"attachment": {
"type": type,
"payload": {
"url": url
}
}
}
return to_save;
}
function build_generic_structure(elements_data){
// elements_data is an array of the following:
/*
{
title: // title of the card
subtitle: // subtitle of the card
image_url: [optional] //an image for the card
buttons: [array of button triples.] //buttons to click
}
*/
var to_return = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": []
}
}
};
for (var i = 0; i < elements_data.length; i++) {
var element = elements_data[i];
var to_save = {
title: element.title,
subtitle: element.subtitle,
}
if(element.image_url){
to_save['image_url'] = element.image_url
}
if(element.buttons){
var buttons = [];
for (var j = 0; j < element.buttons.length; j++) {
buttons.push(build_buttons(element.buttons[j]))
}
to_save['buttons'] = buttons;
}
to_return.attachment.payload.elements.push(to_save);
}
return to_return;
}
exports.reply = reply;
exports.reply2 = reply2;
exports.welcome = welcome;
exports.menu = menu;
exports.set_state = set_state;
exports.builders = {
quick_reply: build_quick_reply,
structured_response:build_structured_response,
generic_response:build_generic_structure,
button:build_buttons,
attachment: build_attachment
}