-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfb_helper.js
638 lines (588 loc) · 12.5 KB
/
fb_helper.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
const fs = require('fs');
const https = require('https');
const https_sync = require('sync-request');
var github = require('./github_helper.js');
var util = require('./util.js')
var CONST = JSON.parse(fs.readFileSync("./secret.json", 'UTF-8'));
var fb_url = "graph.facebook.com";
var path = "/v2.6/me/messages?access_token=" + CONST.page_access_token;
/** STAGES *
* 1. NEW
* 2. GITHUB_INIT
* 3. CONVERSE
* 4. START_OVER
*/
/** CONTEXT & STATES *
* 1. REPO
* a. NAME
* b. DESCRIPTION
* c. DELETE_CONFIRMATION
* 2. ISSUE
* a. DESCRIPTION
* b. CLOSE_COMMENT
* 3. COMMENT
* a. COMMENT
* b. ISSUE_NUMBER
* 4. WAITING_FOR_GITHUB_USERNAME
* a. NEW
* b. ASKED
* c. NOT_FOUND
*/
var yes_no_quick_reply = [
{
"content_type": "text",
"title": "no",
"payload": "no"
},
{
"content_type": "text",
"title": "yes",
"payload": "yes"
}];
module.exports = {
"process_event": function(event)
{
if (!util.db.has(event.sender.id))
{
util.add_new_user(event.sender.id);
}
var user = util.db.get(event.sender.id);
switch (user.stage)
{
case "NEW":
util.send_plain_msg(event.sender.id, "Hi sir");
case "GITHUB_INIT":
case "START_OVER":
/* get Github username */
//TODO: delay this message
get_github_username(event);
break;
case "CONVERSE":
converse(event);
break;
default:
util.delete_and_startover(event.sender.id);
}
}
};
function converse(event)
{
/* Work on NLP */
var confidence = 0;
var intent;
//var action;
var action_on = "";
var msg = event.message.text;
var sender_id = event.sender.id;
var entities = event.message.nlp.entities;
var user = util.db.get(sender_id);
if (user == null)
{
console.log("converse - lost user");
util.delete_and_startover(sender_id);
return;
}
/* Testing purpose only */
switch (event.message.text)
{
case "test_get_my_repo":
github.get_my_repo();
return;
case "test_db":
console.log(JSON.stringify(util.db.keys()));
var keys = util.db.keys();
for (i = 0; i < keys.length; i++)
{
console.log("key: " + keys[i]);
}
return;
case "print_db":
console.log(JSON.stringify(util.db.get(sender_id)));
return;
case "delete_all_repos":
github.delete_all_repos(sender_id);
return;
case "startover":
util.delete_and_startover(sender_id);
return;
default:
break;
}
if (user.context == "")
{
for (entity in entities)
{
//if (confidence > entity.confidence) {
//continue;
//}
console.log("entity: " + entity);
console.log("entity-0: " + entities[entity][0].value);
switch (entity)
{
case "greetings":
//confidence = entity.confidence;
intent = entity;
break;
case "action":
intent = entities[entity][0].value;
break;
case "agenda_entry":
action_on = entities[entity][0].value;
break;
case "intent":
intent = entities[entity][0].value;
break;
case "repo":
action_on = entities[entity][0].value;
break;
case "issue":
action_on = entities[entity][0].value;
break;
case "comment":
action_on = entities[entity][0].value;
break;
}
}
console.log("intent: " + intent);
console.log("action_on: " + action_on);
switch (intent)
{
case "greetings":
send_greetings(sender_id, msg);
break;
case "create":
do_create(sender_id, action_on, msg);
break;
case "delete":
do_delete(sender_id, action_on);
break;
case "change":
do_change(sender_id, action_on);
break;
case "cancel":
do_cancel(sender_id);
break;
case "test":
do_test(sender_id);
break;
default:
util.not_understood(sender_id);
}
}
else
{
for (entity in entities)
{
if ((((entity == "intent") || (entity == "action")) &&
(entities[entity][0].value == "cancel")) ||
(entity == "cancel"))
{
do_cancel(sender_id);
return;
}
}
switch (user.context)
{
case "CREATE":
do_create(sender_id, msg.toLowerCase(), action_on);
break;
case "REPO":
create_repo(sender_id, msg);
break;
case "ISSUE":
create_issue(user, msg);
break;
case "COMMENT":
create_comment(user, msg);
break;
case "ASK_FOR_ISSUE_CLOSURE":
if (msg == "yes")
{
util.take_commit_and_ask_for_fix(user);
}
else if (msg == "no")
{
util.clear_commits(user);
}
else
{
user.context = user.state = "";
util.update_db(user.user_id, user);
converse(event);
return;
}
break;
case "ASKING_COMMITS":
solve_issue_with_commit(user, msg);
break;
default:
}
}
}
function solve_issue_with_commit(user, msg)
{
switch (user.state)
{
case "ASKED":
if (msg == "yes")
{
util.send_plain_msg(user.user_id, "which issue it solves?");
user.state = "ISSUE_NUMBER_ASKED";
util.update_db(user.user_id, user);
}
else
{
var current_repo = user.current_repo;
user.repos[current_repo].commits.splice(0, 1);
if (user.repos[current_repo].commits.length == 0)
{
user.repos[current_repo].commits = null;
user.context = user.state = "";
util.update_db(user.user_id, user);
}
else
{
util.take_commit_and_ask_for_fix(user);
}
}
break;
case "ISSUE_NUMBER_ASKED":
if (isNaN(msg))
{
util.send_plain_msg(user.user_id, "provide only the issue number");
return;
}
user.issue.number = msg;
user.state = "CLOSE_COMMENT_ASKED";
util.update_db(user.user_id, user);
util.send_plain_msg(user.user_id, "provide close description");
break;
case "CLOSE_COMMENT_ASKED":
user.issue.close_comment = msg;
user.state = "CLOSE_ISSUE";
util.update_db(user.user_id, user);
github.close_issue(user.user_id);
break;
}
}
function get_github_username(event)
{
var user;
var res = {};
var sender_id = event.sender.id;
var msg;
if (util.db.has(sender_id))
{
user = util.db.get(sender_id);
}
else
{
// FATAL!
util.delete_and_startover(sender_id);
return;
}
switch (user.state)
{
case "NEW":
msg = "please provide your Github username";
user.stage = "GITHUB_INIT";
user.context = "WAITING_FOR_GITHUB_UNAME";
user.state = "ASKED";
util.update_db(sender_id, user);
setTimeout(function()
{
util.send_plain_msg(this.id, msg);
}.bind(
{
"id": sender_id,
"msg": msg
}), 1000);
return;
case "ASKED":
case "NOT_FOUND":
var username = event.message.text;
if (github.verify_user(username))
{
util.add_username(user, username);
msg = "Thank you sir. I have successfully identified you";
user.username = username;
user.stage = "CONVERSE";
user.context = user.state = "";
util.update_db(sender_id, user);
}
else
{
msg = "Sorry. I can't find you sir. Please give proper username";
user.state = "NOT_FOUND";
util.update_db(sender_id, user);
}
}
util.send_plain_msg(sender_id, msg);
}
function do_test(id)
{
util.send_plain_msg(id, "test sequence initialized");
setTimeout(function()
{
util.run_test(id);
}, 1000);
}
/* Cancel current operation */
function do_cancel(id)
{
console.log("Cancelling current operation");
var user = util.db.get(id);
if (user == null)
{
util.delete_and_startover(id);
return;
}
user.context = user.state = "";
user.repo = {};
user.issue = {};
user.comment = {};
util.update_db(id, user);
util.send_plain_msg(id, "cancelled the current operation sir");
}
/* Create related functions */
function do_create(id, action_on, msg)
{
console.log("do_create - action_on: " + action_on);
var user = util.db.get(id);
if (user == null)
{
util.delete_and_startover(id);
return;
}
switch (action_on)
{
case "":
get_what_to_create(id);
break;
case "repo":
user.context = "REPO";
user.state = "";
util.update_db(id, user);
create_repo(id, action_on);
break;
case "issue":
user.context = "ISSUE";
user.state = "";
util.update_db(id, user);
create_issue(user, "");
break;
case "comment":
user.context = "COMMENT";
user.state = "";
util.update_db(id, user);
create_comment(user, "");
break;
case "CHANGE_CURRENT_REPO":
default:
user.context = user.state = "";
util.update_db(id, user);
util.not_understood(id);
}
}
function get_what_to_create(id)
{
var user = util.db.get(id);
if (user == null)
{
util.delete_and_startover(id);
return;
}
user.context = "CREATE";
user.state = "ASKED";
util.update_db(id, user);
util.send_plain_msg(id, "create what?");
}
function create_repo(id, msg)
{
console.log("create_repo - msg : " + msg);
var user = util.db.get(id);
if (user == null)
{
util.delete_and_startover(id);
return;
}
/* create repo sequence */
switch (user.state)
{
case "":
user.state = "NAME";
util.update_db(id, user);
util.send_plain_msg(id, "repo name please");
break;
case "NAME":
user.state = "DESCRIPTION";
user.repo = {
"name": msg
};
util.update_db(id, user);
util.send_plain_msg(id, "What description I should add?");
break;
case "DESCRIPTION":
user.state = "CREATE_CONFIRMATION";
user.repo.description = msg;
util.update_db(id, user);
var confirm_msg = "Do you want to create a new repo with name " + user.repo.name + "?";
//util.send_plain_msg(id, confirm_msg);
//setTimeout(util.send_quick_reply(id, confirm_msg, yes_no_quick_reply), 1000);
util.send_quick_reply(id, confirm_msg, yes_no_quick_reply);
break;
case "CREATE_CONFIRMATION":
if (msg == "yes")
{
github.create_repo(id);
}
else
{
do_cancel(id);
}
break;
// change current repo
case "CHANGE_CURRENT_REPO":
user.current_repo = msg;
user.context = user.state = "";
user.repos[msg] = {};
util.update_db(user.user_id, user);
util.send_plain_msg(user.user_id, "current repo updated successfully");
break;
default:
util.delete_and_startover(id);
}
}
function do_delete(sender_id, action_on)
{
var user = util.db.get(sender_id);
if (user == null) {
util.delete_and_startover(sender_id);
return;
}
switch (action_on) {
case "issue":
// Hack
user.context = "ASKING_COMMITS";
user.state = "ASKED";
util.update_db(user.user_id, user);
solve_issue_with_commit(user, "yes");
break;
}
}
function do_change(sender_id, action_on)
{
user = util.db.get(sender_id);
if (user == null)
{
util.delete_and_startover(sender_id);
return;
}
switch (action_on) {
case "repo":
user.context = "REPO";
user.state = "CHANGE_CURRENT_REPO";
util.update_db(user.user_id, user);
util.send_plain_msg(user.user_id, "tell a repo which will be set as current repo");
break;
}
}
function create_issue(user, data)
{
var msg;
switch (user.state)
{
case "":
if (user.current_repo != null)
{
user.state = "TITLE";
msg = "Issue title please";
}
else
{
user.state = "SET_REPO";
msg = "On which repo?";
}
break;
case "SET_REPO":
user.state = "";
user.current_repo = data;
util.update_db(user.user_id, user);
create_issue(user, "");
return;
case "TITLE":
user.issue.title = data;
user.state = "DESCRIPTION";
msg = "Describe it";
break;
case "DESCRIPTION":
user.issue.description = data;
util.update_db(user.user_id, user);
github.create_issue(user.user_id);
return;
}
util.update_db(user.user_id, user);
util.send_plain_msg(user.user_id, msg);
}
function create_comment(user, data)
{
var msg;
switch (user.state)
{
case "":
if (user.current_repo != null)
{
user.state = "ISSUE_NO";
msg = "issue number please";
}
else
{
user.state = "SET_REPO";
msg = "On which repo?";
}
break;
case "SET_REPO":
user.state = "";
user.current_repo = data;
util.update_db(user.user_id, user);
create_comment(user, "");
return;
case "ISSUE_NO":
if (isNaN(data))
{
msg = "please provide a valid integer represents issue number";
break;
}
user.comment.on_issue = data;
msg = "you comment please";
user.state = "COMMENT";
break;
case "COMMENT":
user.comment.comment = data;
util.update_db(user.user_id, user);
github.add_comment(user.user_id);
return;
}
util.update_db(user.user_id, user);
util.send_plain_msg(user.user_id, msg);
}
/* utility functions */
function send_greetings(id, msg)
{
util.send_plain_msg(id, "Hello Sir");
}
function send_response(event)
{
var response = {};
add_recipient(response, event.sender.id);
switch (event.message.text.toLowerCase())
{
case "hi":
case "hello":
add_msg(response, "hello sir");
break;
default:
add_msg(response, "Sorry! I'm yet to evelove for your complex language");
}
send_post_req(response);
}