-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
604 lines (529 loc) · 18 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
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
"use strict";
const PAGE_ACCESS_TOKEN =
"EAACTY2uvuOkBAFBcPFvJriCcPDyyTNgBXQLsSLU08e9ZAYQf5uRBogoUVPzpI2KZB8C4krfvu541IPGMihLjIr7klOGnoFHHWDuYSPZChKnIAgQAIZC4JCfUxAZBNcqhn8psy1BihTKMHgPcPqVPAZCZBcKRFANC50MrdPZCUdcEvQZDZD";
const APIAI_TOKEN = "9093b127fa0f4769ae2b4453b794c5cf";
const WEATHER_API_KEY = "098696f24429f40a428d56fde3d4a6e7";
const FB_VALIDATION_TOKEN = "Team19";
const express = require("express");
const bodyParser = require("body-parser");
const request = require("request");
const apiai = require("apiai");
const moment = require("moment-timezone");
const Shopify = require("shopify-api-node");
const clarifai = require('clarifai');
const clarifaiapp = new clarifai.App({
apiKey: 'b8776e47515c4c16a5ca8f6dff722b0e'
});
const SHOPIFY_SHOP_NAME = "dev-circle-toronto-hackathon";
const SHOPIFY_API_KEY = "dc032c19e4460b1e9df1b31b86417fae";
const SHOPIFY_API_PASSWORD = "52db31500c4d546381f9aedacbe707bc";
const HOST_URL =
"https://dc032c19e4460b1e9df1b31b86417fae:52db31500c4d546381f9aedacbe707bc@dev-circle-toronto-hackathon.myshopify.com/";
const shopify = new Shopify({
shopName: SHOPIFY_SHOP_NAME,
apiKey: SHOPIFY_API_KEY,
password: SHOPIFY_API_PASSWORD
});
const app = express();
app.set("port", process.env.PORT || 5000);
let currentTime = moment();
let estTimeStamp = moment.tz(currentTime, "America/Toronto").format();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const server = app.listen(process.env.PORT || 5000, () => {
console.log(
"Express server listening on port %d in %s mode",
server.address().port,
app.settings.env + "-" + estTimeStamp
);
});
const apiaiApp = apiai(APIAI_TOKEN);
app.get("/", (req, res) => {
console.log("Time Stamp :" + estTimeStamp);
res.send("Home Page");
});
/* For Facebook Validation */
app.get("/webhook", function(req, res) {
if (
req.query["hub.mode"] === "subscribe" &&
req.query["hub.verify_token"] === FB_VALIDATION_TOKEN
) {
console.log("[app.get] 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("/webhook", function(req, res) {
// You must send back a status 200 to let the Messenger Platform know that you've
// received the callback. Do that right away because the countdown doesn't stop when
// you're paused on a breakpoint! Otherwise, the request might time out.
res.sendStatus(200);
var data = req.body;
// Make sure this is a page subscription
if (data.object == "page") {
// entries may be batched so iterate over each one
data.entry.forEach(function(pageEntry) {
var pageID = pageEntry.id;
var timeOfEvent = pageEntry.time;
// iterate over each messaging event
pageEntry.messaging.forEach(function(messagingEvent) {
let propertyNames = [];
for (var prop in messagingEvent) {
propertyNames.push(prop);
}
console.log(
"[app.post] Webhook received a messagingEvent with properties:\n ",
+propertyNames.join()
);
if (messagingEvent.message) {
// someone sent a message
receivedMessage(messagingEvent);
} else if (messagingEvent.delivery) {
// messenger platform sent a delivery confirmation
receivedDeliveryConfirmation(messagingEvent);
} else if (messagingEvent.postback) {
// user replied by tapping one of our postback buttons
receivedPostback(messagingEvent);
} else {
console.log(
"[app.post] Webhook is not prepared to handle this message."
);
}
});
});
}
});
/*
* Postback Event
*
* This event is called when a postback is tapped on a Structured Message.
* https://developers.facebook.com/docs/messenger-platform/webhook-reference/postback-received
*
*/
function receivedPostback(event) {
var senderID = event.sender.id;
var recipientID = event.recipient.id;
var timeOfPostback = event.timestamp;
// The 'payload' param is a developer-defined field which is set in a postback
// button for Structured Messages.
var payload = event.postback.payload;
console.log(
"[receivedPostback] from user (%d) on page (%d) with payload ('%s') " +
"at (%d)",
senderID,
recipientID,
payload,
timeOfPostback
);
sendButtonMessages(senderID, payload);
}
function receivedDeliveryConfirmation(event) {
var senderID = event.sender.id; // the user who sent the message
var recipientID = event.recipient.id; // the page they sent it from
var delivery = event.delivery;
var messageIDs = delivery.mids;
var watermark = delivery.watermark;
var sequenceNumber = delivery.seq;
if (messageIDs) {
messageIDs.forEach(function(messageID) {
console.log(
"[receivedDeliveryConfirmation] Message with ID %s was delivered",
messageID
);
});
}
console.log(
"[receivedDeliveryConfirmation] All messages before timestamp %d were delivered.",
watermark
);
}
/*call to calrifi api */
function sendToClarify(fbImgURL){
clarifaiapp.models.predict('e0be3b9d6a454f0493ac3a30784001ff', fbImgURL).then(
function(response) {
console.log("Clarify response - \n "+response);
},
function(err) {
console.error(err);
}
);
}
/* Received message from FB-> send it to api.ai to get action -> GET query from API.ai for the text */
function receivedMessage(event) {
console.log(JSON.stringify(event));
let sender = event.sender.id;
let text = event.message.text;
let receivedMessage = event.message;
if (receivedMessage.attachments && receivedMessage.attachments[0].payload.url) {
let attachedImgURL = receivedMessage.attachments[0].payload.url;
console.log("Received image message : %s" + attachedImgURL);
sendToClarify(attachedImgURL);
} else {
let apiaiSession = apiaiApp.textRequest(text, { sessionId: "tabby_cat" });
apiaiSession.on("response", response => {
console.log(JSON.stringify(response));
let aiTextAction = response.result.action;
let aiTextResponse = response.result.fulfillment.speech;
console.log(
"\n****************************processing event****************************\n"
);
console.log("aiTextAction-->" + aiTextAction);
switch (aiTextAction) {
case "SHOW_BIOGRAPHY":
console.log(
"\n\nswitch to prepareSendBio Time Stamp :" + estTimeStamp + "\n"
);
prepareSendBio(sender);
break;
case "search":
if (
response.result.parameters["userSearchText"] ||
response.result.parameters["recommandType"]
) {
//&limit=1
let searchText = response.result.parameters["userSearchText"]
? response.result.parameters["userSearchText"]
: "Leggings tank yoga 50-75 Apparel active";
console.log("searchText->" + searchText);
let searchShopifyURL =
HOST_URL +
"admin/products.json?title=" +
searchText +
"&limit=10";
console.log(searchShopifyURL);
request.get(searchShopifyURL, (err, response, body) => {
if (!err && response.statusCode == 200) {
let product_json = JSON.parse(body);
console.log("shopify result-->" + product_json.products.length);
if (product_json.products.length < 1) {
let errorMessage =
"I failed to look up the your search item in our store.do you want to search something else?";
prepareSendTextMessage(sender, errorMessage);
} else {
sendProductsOptionsAsButtonTemplates(
sender,
product_json.products,
searchText
);
// sendButtonMessages(sender, requestForHelpOnFeature);
}
} else {
let errorMessage = "I failed to look up the your search.";
prepareSendTextMessage(sender, errorMessage);
}
});
} else {
let errorMessage = "please narrow down your search.";
prepareSendTextMessage(sender, errorMessage);
//ask users something to search
}
break;
default:
console.log(
"\n\nswitch to prepareSendTextMessage Time Stamp :" +
estTimeStamp +
"\n"
);
prepareSendTextMessage(sender, aiTextResponse);
}
});
apiaiSession.on("error", error => {
console.log(error);
});
apiaiSession.end();
}
}
function sendProductsOptionsAsButtonTemplates(recipientId, products,searchTag) {
console.log(
"[sendHelpOptionsAsButtonTemplates] Sending the help options menu"
);
// var products = shopify.product.list({ limit: requestPayload.limit });
// products.then(function(listOfProducs) {
var sectionButton = function(title, action, options) {
var payload = options | {};
payload = Object.assign(options, { action: action });
return {
type: "postback",
title: title,
payload: JSON.stringify(payload)
};
};
var templateElements = [];
products.forEach(function(product) {
var url = HOST_URL + "products/" + product.handle;
// console.log("Product url -\n" + url);
templateElements.push({
title: product.title,
subtitle: product.tags,
image_url: product.image.src,
buttons: [
{
type: "web_url",
url: url,
title: "Read description",
// webview_height_ratio: "compact",
// messenger_extensions: "true"
},
sectionButton("Check avaliable Sizes and colors", "QR_GET_PRODUCT_OPTIONS", {
id: product.id
}),
sectionButton("Check Price", "QR_GET_PRODUCT_PRICE", {
id: product.id
})
]
});
});
var messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: templateElements
}
}
}
};
sendMessagetoFB(messageData);
// });
}
function sendButtonMessages(recipientId, requestForHelpOnFeature) {
var templateElements = [];
var requestPayload = JSON.parse(requestForHelpOnFeature);
var sectionButton = function(title, action, options) {
var payload = options | {};
payload = Object.assign(options, { action: action });
return {
type: "postback",
title: title,
payload: JSON.stringify(payload)
};
};
var textButton = function(title, action, options) {
var payload = options | {};
payload = Object.assign(options, { action: action });
return {
content_type: "text",
title: title,
payload: JSON.stringify(payload)
};
};
let payloadAction = requestPayload.action
? requestPayload.action
: "QR_GET_PRODUCT_LIST";
console.log("requestPayload.action" + payloadAction);
switch (payloadAction) {
case "QR_GET_PRODUCT_PRICE":
var sh_product = shopify.product.get(requestPayload.id);
sh_product.then(function(product) {
var options = "";
var variants = "";
product.variants.forEach(function(variant) {
console.log("checking price ->" + variant.price);
variants = variants + variant.title + ": " + variant.price + "\n";
});
// product.variants.map(function(variant) {
// variants =
// variants + variant.title + ": " + variant.price + "\n";
// });
var messageData = {
recipient: {
id: recipientId
},
message: {
text: variants.substring(0, 640)
// url:
// quick_replies: [
// textButton("Get 3 products", "QR_GET_PRODUCT_LIST", { limit: 3 })
// ]
}
};
sendMessagetoFB(messageData);
});
break;
case "QR_GET_PRODUCT_OPTIONS":
var sh_product = shopify.product.get(requestPayload.id);
sh_product.then(function(product) {
var options = "";
product.options.map(function(option) {
options =
options + option.name + ": " + option.values.join(",") + "\n";
});
var templateElements = [];
templateElements.push({
title: options,
subtitle: "Sizing/Colors",
buttons: [
{
type: "web_url",
url: "https://candyboxx.com/pages/sizing",
title: "Measure size"
}
]
});
var messageData = {
recipient: {
id: recipientId
},
message: {
attachment: {
type: "template",
payload: {
template_type: "generic",
elements: templateElements
}
}
}
};
sendMessagetoFB(messageData);
});
break;
}
}
function sendMessagetoFB(messageData) {
console.log("Send Message method :-" + messageData);
request(
{
url: "https://graph.facebook.com/v2.6/me/messages",
qs: { access_token: PAGE_ACCESS_TOKEN },
method: "POST",
json: messageData
},
(error, response) => {
if (error) {
console.log("Error sending message: ", error);
} else if (response.body.error) {
console.log("Error: in send message ", response.body.error);
}
}
);
}
function prepareSendTextMessage(sender, aiText) {
let messageData = { recipient: { id: sender }, message: { text: aiText } };
sendMessagetoFB(messageData);
}
/* Webhook for API.ai to get response from the 3rd party API */
app.post("/ai", (req, res) => {
var templateElements = [];
switch (req.body.result.action) {
case "shipping":
console.log("\n\n*** Shipping *** Time Stamp :" + estTimeStamp + "\n");
let address = req.body.result.parameters["geo-country"];
let shippingUrl = HOST_URL + "admin/shipping_zones.json";
console.log("\nShopify url :" + shippingUrl);
let shipRate = 0;
request.get(shippingUrl, (err, response, body) => {
console.log(response + JSON.parse(body));
if (!err && response.statusCode == 200) {
let shipping_zones = JSON.parse(body);
console.log("\nShopify shipping info: " + shipping_zones);
if (address.toUpperCase() === "CANADA") {
shipRate = "20";
} else {
shipRate = "45";
}
// let shipRates = shipping_zones[0].weight_based_shipping_rates;
let msg = "The shipping rate to " + address + ":" + shipRate;
return res.json({
speech: msg,
displayText: msg,
source: "shipping"
});
} else {
let errorMessage = "I failed to look up the shipping.";
return res
.status(400)
.json({ status: { code: 400, errorType: errorMessage } });
}
});
break;
case "search":
console.log("\ncase - search");
let msg = "Converted Text to JSON";
return res.json({
speech: msg,
displayText: msg,
source: "search"
});
break;
case "weather":
console.log("\n\n*** weather *** Time Stamp :" + estTimeStamp + "\n");
let city = req.body.result.parameters["geo-city"];
let openWeatherMapUrl =
"http://api.openweathermap.org/data/2.5/weather?APPID=" +
WEATHER_API_KEY +
"&q=" +
city;
request.get(openWeatherMapUrl, (err, response, body) => {
if (!err && response.statusCode == 200) {
let json = JSON.parse(body);
// console.log("openweathermap response --> \n" + JSON.stringify(json));
let tempF = ~~(json.main.temp * 9 / 5 - 459.67);
let tempC = ~~(json.main.temp - 273.15);
let msg =
"The current condition in " +
json.name +
" is " +
json.weather[0].description +
" and the temperature is " +
tempF +
" ℉ (" +
tempC +
" ℃).";
return res.json({ speech: msg, displayText: msg, source: "weather" });
} else {
let errorMessage = "I failed to look up the city name.";
return res
.status(400)
.json({ status: { code: 400, errorType: errorMessage } });
}
});
break;
case "currency":
console.log("\n\n*** Currency *** Time Stamp :" + estTimeStamp + "\n");
let fromCurrency = req.body.result.parameters["from-currency"];
let toCurrency = req.body.result.parameters["to-currency"];
// let unitCurrency = req.body.result.parameters.unit-currency['amount'];
let fixerCurrencyUrl =
"https://api.fixer.io/latest?base=" +
fromCurrency +
"&symbols=" +
toCurrency;
request.get(fixerCurrencyUrl, (err, response, body) => {
if (!err && response.statusCode == 200) {
let currecnyJson = JSON.parse(body);
console.log(
"\nfixerCurrencyUrl response --> \n" + JSON.stringify(currecnyJson)
);
let conversionRates = currecnyJson.rates[toCurrency];
console.log(
"\n conversionRates --> " + JSON.stringify(conversionRates)
);
let msg =
"The current currency conversion rate from " +
currecnyJson.base +
" to " +
toCurrency +
" : " +
conversionRates;
return res.json({
speech: msg,
displayText: msg,
source: "currency"
});
} else {
let errorMessage = "I failed to look up the currency.";
return res
.status(400)
.json({ status: { code: 400, errorType: errorMessage } });
}
});
default:
// code to be executed if n is different from first 2 cases.
}
});