-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
191 lines (172 loc) · 5.74 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
'use strict'
const express = require('express')
const bodyParser = require('body-parser')
const request = require('request')
const axios = require('axios')
const app = express()
app.set('port', (process.env.PORT || 5000))
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.get('/', function(req, res) {
res.send("Hi I am a chatbot")
})
let token = process.env.TOKEN
let greetings = ["hi", "hello", "whats up", "hey"]
let users = []
let opening_choices = ["Send a picture", "Send geographical coordinates"]
let opening_payloads = ["picture", "coordinates"]
let consent_choices = ["I consent", "I do not consent >:("]
let consent_payloads = ["yes", "no"]
app.get('/webhook/', function(req, res) {
res.send(req.query['hub.challenge'])
})
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 = messaging_events[i]
let sender = event.sender.id
//getStartedButton(sender)
if (event.message) {
if (event.message.text) {
let text = event.message.text
decideMessage(sender, text)
}
else {
sendText(sender, "Thanks for the picture!")
}
}
if (event.postback){
let text = JSON.stringify(event.postback.payload)
decideMessage(sender, text)
}
}
res.sendStatus(200)
})
function decideMessage(sender, text1){
let text = text1.toLowerCase()
if (text.includes("picture")){
sendText(sender, "Do you like this moose?")
sendMediaMessage(sender, "Here is the text")
}else if (text.includes("coordinates")){
sendText(sender, "Place your coordinates on this map and send it back")
var url = 'https://google.com/maps/place/Quebec'
sendText(sender, url)
}
else if (text.includes("moose")){
sendText(sender, "cool, me too")
}
else if (text.includes("yes")){
users.push(sender)
sendText(sender, "Cool! You are now added")
sendButtonMessage(sender, "How can I help you today?", opening_choices, opening_payloads)
}
else if (text.includes(("no"))){
sendText(sender, "Okay. Let me know if you change your mind!")
}
if (greetings.find(element => element === text)){
axios.get(`https://graph.facebook.com/${sender}?fields=first_name,last_name,profile_pic&access_token=${token}`)
.then((response) => {
if (users.find(element => element === sender)){
sendButtonMessage(sender, `Welcome back ${response.data.first_name}, how can I help you today?`,
opening_choices, opening_payloads)
}
else {
sendButtonMessage(sender, `Hello ${response.data.first_name}, to continue, please accept to be added to the research network`,
consent_choices, consent_payloads)
}
})
}
}
function sendText(sender, text) {
let messageData = {text: text}
sendRequest(sender, messageData)
}
function sendButtonMessage(sender, text, titles, payloads){
let messageData = {
"attachment":{
"type":"template",
"payload":{
"template_type":"button",
"text":text,
"buttons":[
{
"type":"postback",
"title": titles[0],
"payload":payloads[0]
},
{
"type": "postback",
"title": titles[1],
"payload" : payloads[1]
}
]
}
}
}
sendRequest(sender, messageData)
}
function sendMediaMessage(sender, text){
let messageData = {
"attachment": {
"type": "template",
"payload": {
"template_type": "media",
"elements": [
{
"media_type": "image",
"url": "https://www.facebook.com/102237832502478/photos/a.106534478739480/106534455406149/",
"buttons": [
{
"type": "postback",
"title": "I like this moose",
"payload": "moose"
}
]
}
]
}
}
}
sendRequest(sender, messageData)
}
function sendRequest(sender, messageData){
request({
url: "https://graph.facebook.com/v13.0/me/messages",
qs : {access_token: token},
method: "POST",
json: {
recipient: {id: sender},
message : messageData,
}
}, function(error, response, body) {
if (error) {
console.log("sending error")
} else if (response.body.error) {
console.log(String(response.body.error.message))
}
})
}
function getStartedButton(){
request({
url: "https://graph.facebook.com/v13.0/me/messenger_profile",
qs: {access_token: token},
method: "POST",
json: {
greeting: [
{
"locale":"default",
"text":"Hello!"
}
]
}
}, function(error, response, body) {
if (error) {
console.log("sending error")
} else if (response.body.error) {
console.log(String(response.body.error.message))
}
})
}
app.listen(app.get('port'), function() {
console.log("running: port")
})