-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
347 lines (310 loc) · 11.9 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
'use strict'
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const app = express()
app.set('port', (process.env.PORT || 5000))
// Process application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({extended: false}))
// Process application/json
app.use(bodyParser.json())
// Index route
app.get('/', function (req, res) {
res.send('Hello world, I am a chat bot')
})
// for Facebook verification
app.get('/webhook/', function (req, res) {
if (req.query['hub.verify_token'] === 'my_voice_is_my_password_verify_me') {
res.send(req.query['hub.challenge'])
}
res.send('Error, wrong token')
})
app.post('/webhook/', function (req, res) {
let messaging_events = req.body.entry[0].messaging
for (let i = 0; i < messaging_events.length; i++) {
let event = req.body.entry[0].messaging[i]
let sender = event.sender.id
if (event.message && event.message.text) {
let text = event.message.text
decideMessage(sender, text);
continue
}
if (event.postback) {
let text = JSON.stringify(event.postback)
decideMessage(sender, text);
continue
}
}
res.sendStatus(200)
})
const token = "EAADPuY7AusgBAL5wNKMrokW32BgDCVESDwtXp0u4ytJ0gmxHB9LISBwGW2y6XFEi3oBgNFb0BqMbV89d80DUqfZBV4Ln4ttZBsKOg1alqDqaNA1Wlx4ZAFQOZBAj7iquLpmTcEEcDs6FriESRU0WTQ99TAECWB4UNWArcqGIhAZDZD"
var popularTypesOfItems = {
invitations: {
header: "Invitations",
subtitle: "Friends and Family Welcome!",
image_url: "http://scene7.targetimg1.com/is/image/Target/15406466?wid=1024&hei=1024&qlt=70&fmt=pjpeg",
url: "http://www.target.com/c/baby-shower-party-supplies/-/N-4soei%7Cd_item_type_all%3Ainvitation%20packs?Sort=Featured&clearCategId=4soei&Nao=0",
button_title: "See Items Like"
},
favorBags: {
header: "Favor Bags",
subtitle: "Grab-and-go sweet treats",
image_url: "http://scene7.targetimg1.com/is/image/Target/50701956?wid=450&hei=450&fmt=pjpeg",
url: "http://www.target.com/c/baby-shower-party-supplies/-/N-4soei%7Cd_item_type_all%3AFavor%20Bag?Sort=Featured&clearCategId=4soei&Nao=0",
button_title: "See Items Like"
}
}
var specificItems = {
sunscreens: {
sunscreen1: {
header: "Alba Botanica",
subtitle: "Keeping your skin youthful",
image_url: "http://scene7.targetimg1.com/is/image/Target/16872833?wid=1024&hei=1024&qlt=70&fmt=pjpeg",
url: "http://www.target.com/p/alba-botanica-emollient-sunscreen-active-kids-clear-spray-spf-50-6-oz/-/A-16872833",
button_title_1: "See Item",
button_title_2: "Add to Cart"
},
sunscreen2: {
header: "Neutrogena Beach Defense",
subtitle: "Protecting your skin on the go!",
image_url: "http://scene7.targetimg1.com/is/image/Target/50787513?wid=450&hei=450&fmt=pjpeg",
url: "http://www.target.com/p/neutrogena-oh-joy-beach-defense-spray-sunscreen-broad-spectrum-spf-70-6-5-oz/-/A-50787513",
button_title_1: "See Item",
button_title_2: "Add to Cart"
},
sunscreen3: {
header: "Coppertone Kids",
subtitle: "Sun protection made easy!",
image_url: "http://scene7.targetimg1.com/is/image/Target/50584700?wid=1024&hei=1024&qlt=70&fmt=pjpeg",
url: "http://www.target.com/p/coppertone-kids-sunscreen-continuous-spray-spf-50/-/A-50584700",
button_title_1: "See Item",
button_title_2: "Add to Cart"
}
},
infant_boy_clothing: {
clothing1: {
header: "Organically-Made Pajamas",
subtitle: "Keeping your baby comfy throughout the night",
image_url: "http://target.scene7.com/is/image/Target/51186963?wid=450&hei=450&fmt=pjpeg",
url: "http://www.target.com/p/lamaze-baby-waffle-footed-sleeper-red/-/A-51261899",
button_title_1: "See Item",
button_title_2: "Add to Cart"
},
clothing2: {
header: "Baby Boys' 4 Piece: Cat & Jack",
subtitle: "Providing style and comfort",
image_url: "http://target.scene7.com/is/image/Target/50897885?wid=450&hei=450&fmt=pjpeg",
url: "http://www.target.com/p/baby-boys-4-piece-tiger-set-baby-cat-jack-turquoise-white/-/A-51112241",
button_title_1: "See Item",
button_title_2: "Add to Cart"
},
clothing3: {
header: "Burt's Bees Infant 2 Pack",
subtitle: "Ready to rock and roll!",
image_url: "http://target.scene7.com/is/image/Target/50363846?wid=1024&hei=1024&qlt=70&fmt=pjpeg",
url: "http://www.target.com/p/burt-s-bees-baby-infant-boys-2-pack-bodysuit-camo-striped/-/A-50363847",
button_title_1: "See Item",
button_title_2: "Add to Cart"
}
}
}
function decideMessage(sender, text1) {
let text = text1.toLowerCase();
if (text.includes("baby shower") && text.includes("throwing")) {
sendTextMessage(sender, "Congrats! Do you need anything for your baby shower?");
send2TypesOfItems(sender, popularTypesOfItems.invitations, popularTypesOfItems.favorBags)
} else if (text.includes("add to cart")) {
sendTextMessage(sender, "Added to shopping cart!")
} else if (text.includes("no")) {
sendButtonMessage(sender, "I see it's summertime. Here are some things you might need.", "Sunscreen", "Flip Flops", "Sunglasses")
} else if (text.includes("sunscreen")) {
sendTextMessage(sender, "Here are some great deals on sunscreen!");
send3SpecificItems(sender, specificItems.sunscreens.sunscreen1, specificItems.sunscreens.sunscreen2, specificItems.sunscreens.sunscreen3)
} else if (text.includes("gift")) {
sendButtonMessage(sender, "What is the occasion?", "Baby Shower", "Birthday", "Graduation");
} else if (text.includes("baby shower")) {
sendButtonMessage(sender, "What's your price range?", "$0 - $15", "$15 - $50", "$50 - $100")
} else if (text.includes("$")) {
sendButtonMessage(sender, "Here are some customer favorites!", "Clothes", "Stroller", "Toys")
} else if (text.includes("clothes")) {
sendTextMessage(sender, "Boy or girl?")
} else if (text.includes("boy")) {
send3SpecificItems(sender, specificItems.infant_boy_clothing.clothing1, specificItems.infant_boy_clothing.clothing2, specificItems.infant_boy_clothing.clothing3)
} else {
sendTextMessage(sender, "Can you ask again?");
}
}
function sendButtonMessage(sender, text, button1, button2, button3) {
let messageData = {
"attachment":{
"type":"template",
"payload":{
"template_type":"button",
"text":text,
"buttons":[
{
"type":"postback",
"title":button1,
"payload":button1.toLowerCase()
},
{
"type":"postback",
"title":button2,
"payload":button2.toLowerCase()
},
{
"type":"postback",
"title":button3,
"payload":button3.toLowerCase()
}
]
}
}
}
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id:sender},
message: messageData,
}
}, function(error, response, body) {
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})
}
function sendTextMessage(sender, text) {
let messageData = { text:text }
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id:sender},
message: messageData,
}
}, function(error, response, body) {
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})
}
function send2TypesOfItems(sender, obj1, obj2) {
let messageData = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": [{
"title": obj1.header,
"subtitle": obj1.subtitle,
"image_url": obj1.image_url,
"buttons": [{
"type": "web_url",
"url": obj1.url,
"title": obj1.button_title
}],
}, {
"title": obj2.header,
"subtitle": obj2.subtitle,
"image_url": obj2.image_url,
"buttons": [{
"type": "web_url",
"url": obj2.url,
"title": obj2.button_title
}],
}]
}
}
}
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id:sender},
message: messageData,
}
}, function(error, response, body) {
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})
}
function send3SpecificItems(sender, obj1, obj2, obj3) {
let messageData = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": [{
"title": obj1.header,
"subtitle": obj1.subtitle,
"image_url": obj1.image_url,
"buttons": [{
"type": "web_url",
"url": obj1.url,
"title": obj1.button_title_1
}, {
"type": "postback",
"title": obj1.button_title_2,
"payload": obj1.button_title_2.toLowerCase(),
}],
}, {
"title": obj2.header,
"subtitle": obj2.subtitle,
"image_url": obj2.image_url,
"buttons": [{
"type": "web_url",
"url": obj2.url,
"title": obj2.button_title_1,
}, {
"type": "postback",
"title": obj2.button_title_2,
"payload": obj2.button_title_2.toLowerCase(),
}],
}, {
"title": obj3.header,
"subtitle": obj3.subtitle,
"image_url": obj3.image_url,
"buttons": [{
"type": "web_url",
"url": obj3.url,
"title": obj3.button_title_1,
}, {
"type": "postback",
"title": obj3.button_title_2,
"payload": obj3.button_title_2.toLowerCase(),
}],
}]
}
}
}
request({
url: 'https://graph.facebook.com/v2.6/me/messages',
qs: {access_token:token},
method: 'POST',
json: {
recipient: {id:sender},
message: messageData,
}
}, function(error, response, body) {
if (error) {
console.log('Error sending messages: ', error)
} else if (response.body.error) {
console.log('Error: ', response.body.error)
}
})
}
// Spin up the server
app.listen(app.get('port'), function() {
console.log('running on port', app.get('port'))
})