-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (119 loc) · 4.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
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const app = express().use(bodyParser.json());
const fs = require('fs');
const path = require('path');
const request = require('request');
const constants = require('./helpers/constants');
const messenger = require('./helpers/messenger');
const responses = require('./helpers/responses');
const actions = require('./helpers/actions');
const async = require('async');
// Serve the public directory static file
app.use(express.static(path.join(__dirname, 'public')));
// Sets server port and logs message on success
app.listen(constants.PORT, () => {
console.log('Webhook is listening on port '+constants.PORT+'.');
/*
*
* Here we set the thread up.
* Including persistent menus and the welcome screen
*
*/
messenger.setThread(responses.thread, function(res) {
// Check if stuff is fine
if (res.body.result=='success') {
console.log('Done! Ready to chat.')
} else {
console.log('Hold it, there was an error:')
console.log(res);
}
});
});
// Creates the endpoint for our webhook
app.post('/webhook', (req, res) => {
let body = req.body;
// Checks this is an event from a page subscription
if (body.object === 'page') {
// Iterates over each entry - there may be multiple if batched
body.entry.forEach(function(entry) {
// Gets the message. entry.messaging is an array, but
// will only ever contain one message, so we get index 0
const webhookEvent = entry.messaging[0];
// This is the unique PSID key we can use to message them directly
const psid = webhookEvent.sender.id;
// If there is an attachment, we translate it here
// and then tell the user what they recorded
if (webhookEvent.message) {
if (webhookEvent.message.attachments) {
if (webhookEvent.message.attachments.length!=0) {
if (webhookEvent.message.attachments[0].payload) {
if (webhookEvent.message.attachments[0].payload.url) {
actions.translate(psid, webhookEvent.message.attachments[0].payload.url);
}
}
}
}
}
// Here we handle any postbacks that may happen through Messenger
if (webhookEvent.postback) {
const { postback } = webhookEvent;
// Decision tree for postbacks (including welcome message)
// Add as many as your want here
switch (postback.payload) {
case 'WELCOME':
actions.default(psid, 'Hi there, please leave us a voice note and we\'ll do our best to translate it');
break;
}
}
// Normal messages are handled here
if (webhookEvent.message) {
const { message } = webhookEvent;
// Quick replies
if (message.quick_reply) {
const { quick_reply } = message;
// Decision tree for quick replies
switch (quick_reply.payload) {
case 'NONE':
actions.default(psid, 'Hi there, you have selected the NONE quick reply.');
break;
}
}
// Typed text messages
if (message.text) {
const { text } = message;
// Decision tree plain text messages
switch (quick_reply.payload) {
case 'NONE':
actions.default(psid, 'Hi there, you typed NONE');
break;
}
}
}
});
// Returns a '200 OK' response to all requests
res.status(200).send('EVENT_RECEIVED');
} else {
// Returns a '404 Not Found' if event is not from a page subscription
res.sendStatus(404);
}
});
// Adds support for GET requests to our webhook (fro FB)
app.get('/webhook', (req, res) => {
// Parse the query params
let mode = req.query['hub.mode'];
let token = req.query['hub.verify_token'];
let challenge = req.query['hub.challenge'];
// Checks if a token and mode is in the query string of the request
if (mode && token) {
// Checks the mode and token sent is correct
if (mode === 'subscribe' && token === constants.VERIFY_TOKEN) {
// Responds with the challenge token from the request
res.status(200).send(challenge);
} else {
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403);
}
}
});