-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (82 loc) · 2.58 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
require("now-logs")(process.env.FACEBOOK_VERIFY_TOKEN);
const express = require("express");
const bodyParser = require("body-parser");
const message = require("./lib/message");
// Set up Express
let app = express();
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true
})
);
// Webhook validation
app.get("/webhook", function(req, res) {
if (
req.query["hub.mode"] === "subscribe" &&
req.query["hub.verify_token"] === process.env.FACEBOOK_VERIFY_TOKEN
) {
console.log("Validating webhook");
res.status(200).send(req.query["hub.challenge"]);
} else {
console.error("Failed validation. Make sure the validation tokens match.");
res.sendStatus(403);
}
});
// Display the web page
app.get("/", function(req, res) {
res.writeHead(200, { "Content-Type": "text/html" });
res.write("hi");
res.end();
});
// Message processing
app.post("/webhook", function(req, res) {
var data = req.body;
// Make sure this is a page subscription
if (data.object === "page") {
// Iterate over each entry - there may be multiple if batched
data.entry.forEach(function(entry) {
var pageID = entry.id;
var timeOfEvent = entry.time;
// Iterate over each messaging event
entry.messaging.forEach(function(event) {
if (event.message) {
message.process(event);
} else if (event.postback) {
receivedPostback(event);
} else {
console.log("Webhook received unknown event: ", event);
}
});
});
// Assume all went well.
//
// You must send back a 200, within 20 seconds, to let us know
// you've successfully received the callback. Otherwise, the request
// will time out and we will keep trying to resend.
res.sendStatus(200);
}
});
function onboard() {}
function receivedPostback(event) {
var senderID = event.sender.id;
var recipientID = event.recipient.id;
var timeOfPostback = event.timestamp;
// The 'payload' param is a developer-defined field which is set in a postback
// button for Structured Messages.
var payload = event.postback.payload;
console.log(
"Received postback for user %d and page %d with payload '%s' " + "at %d",
senderID,
recipientID,
payload,
timeOfPostback
);
// When a postback is called, we'll send a message back to the sender to
// let them know it was successful
message.send(senderID, "Postback called");
}
// Set Express to listen out for HTTP requests
var server = app.listen(process.env.PORT || 3000, function() {
console.log("Listening on port %s", server.address().port);
});