-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanswer-tree.js
134 lines (122 loc) · 4.37 KB
/
answer-tree.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
'use strict';
var user = require('./user.model');
var tree = require('./answer-tree.json');
var Jobs = require('./indeed-client.js');
var Events = require('./eventbrite-client.js');
module.exports = class AnswerTree {
constructor(bot){
this.bot = bot;
}
tryHandleReply(clientUserId, message){
return user.getUser(clientUserId)
.then(uz => {
if (!uz){
user.addUser(clientUserId, 'root', 0);
return tree.branches['root'][0].reply;
} else {
if (uz.stateBranch === 'help.jobOffers' || uz.stateBranch === 'help.networking') {
this.updateQuery(uz, clientUserId, message);
let replyType = tree.branches[uz.stateBranch][uz.branchStep + 1].type;
if (replyType === 'end') {
this.executeQuery(uz.stateBranch, uz.query, clientUserId)
.then(() => {
this.startOver(clientUserId);
});
user.enterBranch(clientUserId, 'root');
}
}
if (uz.stateBranch === 'help.other' && tree.branches[uz.stateBranch][uz.branchStep + 1].type === 'end'){
this.startOver(clientUserId);
}
if (tree.branches[uz.stateBranch][uz.branchStep + 1]){
user.nextBranchStep(clientUserId);
return tree.branches[uz.stateBranch][uz.branchStep + 1].reply;
} else {
// We got ourselves into an unplanned step - start over.
user.enterBranch(clientUserId, 'root');
return tree.branches['root'][0].reply;
}
}
})
.catch(err => {
console.error(err.stack);
});
}
/**
* A postback means we're entering a new branch of the tree
* @param {string} clientUserId
* @param {string} payload
* @returns {*|Array}
*/
handlePostback(clientUserId, payload){
user.enterBranch(clientUserId, payload);
return tree.branches[payload][0].reply;
}
/**
* The user sent a message that is a reply to a question on part of their query (location, industry etc...)
* We update the corresponding query element in the database
* @param {object} userObject - User object from database
* @param {string} clientUserId - Id of user for chat client
* @param {string} replyText - Content of user's reply
*/
updateQuery(userObject, clientUserId, replyText){
let messageType = tree.branches[userObject.stateBranch][userObject.branchStep].type;
if (messageType === 'location' || 'industry' || 'role'){
// Update object to keep it consistent with DB
if (!userObject.query){
userObject.query = {};
}
userObject.query[messageType] = replyText;
user.updateQuery(clientUserId, messageType, replyText);
}
}
/**
* Once we got all the info we need to find the data the user is looking for,
* we actually get the data
* @param {string} stateBranch - Which branch the user is on (i.e. what kind of query)
* @param {object} query - Object containing the correct element (a subset of industry, role, location)
* @param {string} clientUserId
*/
executeQuery(stateBranch, query, clientUserId){
var prom;
if (stateBranch === 'help.jobOffers'){
prom = Jobs.getJobs(query.role, query.location)
.then(out => {
if (out){
Object.assign(out, {channel: clientUserId, text: ''});
this.bot.say(out);
} else {
let reply = tree.branches['noResult'][0].reply;
Object.assign(reply, {channel: clientUserId});
this.bot.say(reply);
}
});
}
else if (stateBranch === 'help.networking'){
prom = Events.getEvents(query.industry, query.location)
.then(out => {
if (out) {
Object.assign(out, {channel: clientUserId, text: ''});
this.bot.say(out);
} else {
let reply = tree.branches['noResult'][0].reply;
Object.assign(reply, {channel: clientUserId});
this.bot.say(reply);
}
});
}
return prom
}
/**
* Start again: ask the user if they need something else
* @param {string} clientUserId
*/
startOver(clientUserId){
user.enterBranch(clientUserId, 'root');
setTimeout(() => {
var replyObj = {channel: clientUserId};
Object.assign(replyObj, tree.branches['startOver'][0].reply);
this.bot.say(replyObj);
}, 3000);
}
};