-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
144 lines (132 loc) · 4.27 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
var express = require('express')
var bodyParser = require('body-parser')
var request = require('request')
var app = express()
var token = "enter token here"
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'] === token) {
res.send(req.query['hub.challenge'])
}
res.send('Error, wrong token')
})
app.post('/webhook/', function(req, res) {
messaging_events = req.body.entry[0].messaging
for (i = 0; i < messaging_events.length; i++) {
event = req.body.entry[0].messaging[i]
sender = event.sender.id
if (event.message && event.message.text) {
text = event.message.text
if (text === 'hi') {
sendGenericMessage(sender)
continue
}
sendTextMessage(sender, "parrot: " + text.substring(0, 200))
}
if (event.postback) {
text = JSON.stringify(event.postback)
sendTextMessage(sender, "Postback received: " + text.substring(0, 200), token)
continue
}
}
res.sendStatus(200)
})
function sendTextMessage(sender, text) {
messageData = {
text: text
}
sendRequest(sender, messageData)
}
function sendGenericMessage(sender) {
messageData = {
"attachment": {
"type": "template",
"payload": {
"template_type": "generic",
"elements": [{
"title": "Ai Chat Bot Communities",
"subtitle": "Communities to Follow",
"image_url": "http://1u88jj3r4db2x4txp44yqfj1.wpengine.netdna-cdn.com/...",
"buttons": [{
"type": "web_url",
"url": "https://www.facebook.com/groups/aic...",
"title": "FB Chatbot Group"
}, {
"type": "web_url",
"url": "https://www.reddit.com/r/Chat_Bots/",
"title": "Chatbots on Reddit"
}, {
"type": "web_url",
"url": "https://twitter.com/aichatbots",
"title": "Chatbots on Twitter"
}],
}, {
"title": "Chatbots FAQ",
"subtitle": "Aking the Deep Questions",
"image_url": "https://tctechcrunch2011.files.wordpress.com/...",
"buttons": [{
"type": "postback",
"title": "What's the benefit?",
"payload": "Chatbots make content interactive instead of static",
}, {
"type": "postback",
"title": "What can Chatbots do",
"payload": "One day Chatbots will control the Internet of Things! You will be able to control your homes temperature with a text",
}, {
"type": "postback",
"title": "The Future",
"payload": "Chatbots are fun! One day your BFF might be a Chatbot",
}],
}, {
"title": "Learning More",
"subtitle": "Aking the Deep Questions",
"image_url": "http://www.brandknewmag.com/wp-cont...",
"buttons": [{
"type": "postback",
"title": "AIML",
"payload": "Checkout Artificial Intelligence Mark Up Language. Its easier than you think!",
}, {
"type": "postback",
"title": "Machine Learning",
"payload": "Use python to teach your maching in 16D space in 15min",
}, {
"type": "postback",
"title": "Communities",
"payload": "Online communities & Meetups are the best way to stay ahead of the curve!",
}],
}]
}
}
}
sendRequest(sender, messageData)
}
function sendRequest(sender, messageData) {
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'))
})