-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
293 lines (252 loc) · 10.2 KB
/
index.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
var express = require('express');
var app = express();
var config = require('./config.json');
var bodyParser = require('body-parser');
var request = require('request');
var apiai = require('apiai');
var mongoose = require('mongoose');
var message = require('./messaging');
var controller = require('./controller.js');
app.set('port', (process.env.PORT || 5000));
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
var ai = apiai('fb2c9b42a72f491783ff189925dd909f');
var profile = {
id: 0,
name: '',
user: '',
phone: 0
}
// Home Page
app.get('/', function(req, res) {
res.sendFile(__dirname + '/views/pages/landing-page/landing.html');
});
app.get('/guide-signup', function (req, res) {
res.sendFile(__dirname + '/views/pages/guide-signup.html');
});
app.get('/get-all-guides', function (req, res) {
controller.Guide.find(function(err, guides){
if (!err) {
res.json(guides);
} else {
res.send('No guides found');
}
});
});
// Let's facebook verify our app
app.get('/webhook', function(req, res) {
if (req.query['hub.mode'] === 'subscribe' &&
req.query['hub.verify_token'] === process.env.webhook_token) {
console.log("Validating webhook");
res.status(200).send(req.query['hub.challenge']);
} else {
console.error("Failed validation. Make sure the validation tokens match.");
res.sendStatus(403);
}
});
app.post('/aihook', function (req, res) {
var result = req.body.result;
switch (result.action) {
case 'findTourGuide':
console.log('findTourGuide');
console.log(result);
if (!result.actionIncomplete) {
controller.Guide.find({city: result.parameters.city}, function (err, users) {
console.log(users);
result.fulfillment.speech = 'Your tour guide is ' + users[0].name + '.\n'+
'Get in touch with them by texting ' + users[0].phone;
send(users[0].guideId, profile.name + ' asked for a travel guide in ' + users[0].city + ', so we gave them your number. Their number is '+ result.parameters['phone-number'] + '. Expect to hear from them!');
res.json(result.fulfillment);
});
} else {
res.json({});
}
break;
case 'addTourGuide':
console.log('addTourGuide');
console.log(result)
if (!result.actionIncomplete) {
console.log('Adding new guide: ' + profile.user);
var newGuide = new controller.Guide({
city: result.parameters.city,
name: profile.name,
phone: result.parameters['phone-number'],
guideId: profile.id
});
newGuide.save(function (err, guide) {
console.log(guide);
if (!err) {
result.fulfillment.speech = 'You are now a travel guide in ' + result.parameters.city + '!'
res.json(result.fulfillment);
}
});
} else {
res.json({});
}
break;
case 'addActivity':
console.log('addActivity');
var guide = controller.Guide.find({guideId: profile.id}, function (err, guide) {
console.log(guide);
console.log(result.parameters);
if ((guide.length) > 0) {
if (!result.actionIncomplete) {
var newActivity = new controller.Activity({
guideId: profile.id,
name: result.parameters.name,
loc: result.parameters.address,
dat: result.parameters['date-time'].date_time.refString,
city: guide[0].city
});
newActivity.save(function (err, activity) {
console.log(activity);
if (!err) {
result.fulfillment.speech = "Added Activity";
res.json(result.fulfillment);
} else {
result.fulfillment.speech = "Saving activity failed";
res.json(result.fulfillment);
}
});
} else {
res.json();
}
} else {
result.fulfillment.speech = "You aren\'t a travel guide, so you can\'t add events"
res.json(result.fulfillment);
}
})
break;
case 'findActivities':
console.log('findActivities');
console.log(result);
if (!result.actionIncomplete) {
controller.Activity.find({city: result.parameters.city}, 'name address date', function (err, activities) {
console.log(activities);
if (activities.length > 0) {
result.fulfillment.speech = 'Guides recommend the following activities in ' + result.parameters.city + '\n\n';
activities.forEach(function (act, i, arr) {
console.log(act._id);
console.log(i);
result.fulfillment.speech += act.name + '\n' + 'Location: ' + act.loc + '\n' + 'Time: ' + act.dat + '\n\n';
});
res.json(result.fulfillment);
} else {
result.fulfillment.speech = 'No activities found in ' + result.parameters.city;
res.json(result.fulfillment)
}
});
} else {
res.json({});
}
break;
default:
console.log('no handler foudn');
}
});
// Main message processing
app.post('/webhook', function(req, res) {
var data = req.body;
// Make sure this is a page subscription
if (data.object == 'page') {
// Iterate over each entry
// There may be multiple if batched
data.entry.forEach(function(pageEntry) {
var pageID = pageEntry.id;
var timeOfEvent = pageEntry.time;
// Iterate over each messaging event
pageEntry.messaging.forEach(function(messagingEvent) {
if (messagingEvent.optin) {
// User Authenticated to our bot
console.log("Recieved Auth: " + JSON.stringify(messagingEvent));
} else if (messagingEvent.message) {
// Recieved a message
//console.log("Recieved Message: " + JSON.stringify(messagingEvent));
profile.id = messagingEvent.sender.id;
getProfile(profile.id, function (p) {
console.log(p);
profile.name = p.first_name;
});
message = messagingEvent.message.text;
atts = messagingEvent.attachments;
var ai_req = ai.textRequest(message);
console.log(ai_req);
ai_req.on('response', function(response) {
console.log(response);
respmsg = response.result.fulfillment.speech;
if (respmsg.indexOf('$name') > -1) {
respmsg = respmsg.replace('$name', profile.name);
}
send(profile.id, respmsg)
});
ai_req.on('error', function(error) {
console.log(error + "Ai error");
});
ai_req.end();
} else if (messagingEvent.delivery) {
// Message sent successfully
console.log("Recieved Delivery: " + JSON.stringify(messagingEvent));
} else if (messagingEvent.postback) {
// User clicked a button on a formatted message
console.log("Recieved Postback: " + JSON.stringify(messagingEvent));
} else {
// some error
console.log("Webhook received unknown messagingEvent: ", messagingEvent);
}
});
});
// Assume all went well.
//
// You must send back a 200, within 20 seconds, to let us know you've
// successfully received the callback. Otherwise, the request will time out.
res.sendStatus(200);
}
});
function getProfile(id, callback) {
var ret;
request.get({
uri: 'https://graph.facebook.com/v2.6/' + id,
qs: {
fields: 'first_name, last_name, locale, timezone, gender',
access_token: process.env.page_token,
}
}, function (err, resp, profile) {
callback(JSON.parse(profile));
});
}
function send(recipientId, messageText) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text: messageText
}
};
console.log('Calling send api');
callSendAPI(messageData);
}
function callSendAPI(messageData) {
request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: { access_token: process.env.page_token },
method: 'POST',
json: messageData
}, function (error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
console.log("Successfully sent generic message with id %s to recipient %s",
messageId, recipientId);
} else {
console.error("Unable to send message.");
//console.error(response);
console.error(error);
}
});
}
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});