-
Notifications
You must be signed in to change notification settings - Fork 3
/
respond.js
67 lines (62 loc) · 1.95 KB
/
respond.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
/*
/respond is an action triggered by /memorize
takes in final output, quick replies and attachment and sends them to FB Messenger as a response
/respond acts as the last link in the sequence; it triggers no subsequent actions
Default Parameters
messenger_page_token
*/
function main(params) {
const request = require('request-promise')
if (params.attachment && params.quick_replies){
var json = {
"recipient":{
"id": params.userid
},
"message":{
"attachment": params.attachment,
"quick_replies": params.quick_replies
}
}
}
else if (params.attachment){
var json = {
"recipient":{
"id": params.userid
},
"message":{
"attachment": params.attachment
}
}
}
else if (params.quick_replies){
var json = {
"recipient":{
"id": params.userid
},
"message":{
"text": params.text,
"quick_replies": params.quick_replies
}
}
}
else {
var json = {
"recipient":{
"id": params.userid
},
"message":{
"text": params.text
}
}
}
console.log("formed JSON")
const reqPromise = request({ method: 'POST',
//add page access token here
url: 'https://graph.facebook.com/v2.6/me/messages?access_token=' + params.messenger_page_token,
json: json
})
console.log("sending request")
return reqPromise
.then ( res => ({ result: 'message successfully sent'} ))
.catch ( err => ({ error: 'There was an error sending message:' + JSON.stringify(err)}))
}