-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
78 lines (52 loc) · 1.93 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
var express = require('express');
var bodyParser = require('body-parser');
var request = require('request');
var app = express();
// You MUST change these values, consult the Messenger Platform Getting Started guide
var verify_token = 'superdupersecrettokenyay';
var token = "43dff234lKedYBvMKfiKbonmbwaYUuGmBtmKtoJ8b3YxgPKOJpeNNLLIWxpJAqYSyorgQFQclU59IkYBXzaFASIFYOUREADTHISYOUAREAWESOMEOh4xh36szaxgysl2u7gP1xNexpLiLkOhiE2wZZdqlm9GgyouQAwv2ZPoUoBnJMrpqlqoMdvgoPMbfImbNkxGsISPhbsTzs4ps3d14";
app.use(bodyParser.json());
app.get('/', function (req, res) {
res.send('Hello World! This is the bot\'s root endpoint!');
});
app.get('/webhook/', function (req, res) {
if (req.query['hub.verify_token'] === verify_token) {
res.send(req.query['hub.challenge']);
}
res.send('Error, wrong validation token');
});
app.post('/webhook/', function (req, res) {
var messaging_events = req.body.entry[0].messaging;
for (var i = 0; i < messaging_events.length; i++) {
var event = req.body.entry[0].messaging[i];
var sender = event.sender.id;
if (event.message && event.message.text) {
var text = event.message.text;
sendTextMessage(sender, "Echo: " + text.substring(0, 200));
}
}
res.sendStatus(200);
});
app.listen(1337, function () {
console.log('Facebook Messenger echoing bot started on port 1337!');
});
function sendTextMessage(sender, text) {
var 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) {
if (error) {
console.log('Error sending message: ', error);
} else if (response.body.error) {
console.log('Error: ', response.body.error);
}
});
}