-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
106 lines (80 loc) · 2.97 KB
/
index.js
File metadata and controls
106 lines (80 loc) · 2.97 KB
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
/**
* Copyright 2017-present, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*
* Messenger Platform Quick Start Tutorial
*
* This is the completed code for the Messenger Platform quick start tutorial
*
* https://developers.facebook.com/docs/messenger-platform/getting-started/quick-start/
*
* To run this code, you must do the following:
*
* 1. Deploy this code to a server running Node.js
* 2. Run `npm install`
* 3. Update the VERIFY_TOKEN
* 4. Add your PAGE_ACCESS_TOKEN to your environment vars
*
*/
require('dotenv').config()
'use strict';
// Imports dependencies and set up http server
const
express = require('express'),
body_parser = require('body-parser'),
app = express().use(body_parser.json()); // creates express http server
const {handleMessage, handlePostback} = require('./handler/messages')
// Accepts POST requests at /webhook endpoint
app.post('/webhook', (req, res) => {
// Parse the request body from the POST
let body = req.body;
// Check the webhook event is from a Page subscription
if (body.object === 'page') {
body.entry.forEach(function(entry) {
// Gets the body of the webhook event
let webhook_event = entry.messaging[0];
console.log(webhook_event);
// Get the sender PSID
let sender_psid = webhook_event.sender.id;
console.log('Sender ID: ' + sender_psid);
// Check if the event is a message or postback and
// pass the event to the appropriate handler function
if (webhook_event.message) {
handleMessage(sender_psid, webhook_event.message);
} else if (webhook_event.postback) {
handlePostback(sender_psid, webhook_event.postback);
}
});
// Return a '200 OK' response to all events
res.status(200).send('EVENT_RECEIVED');
} else {
// Return a '404 Not Found' if event is not from a page subscription
res.sendStatus(404);
}
});
// Accepts GET requests at the /webhook endpoint
app.get('/webhook', (req, res) => {
/** UPDATE YOUR VERIFY TOKEN **/
const VERIFY_TOKEN = process.env.VERIFY_TOKEN;
// Parse params from the webhook verification request
let mode = req.query['hub.mode'];
let token = req.query['hub.verify_token'];
let challenge = req.query['hub.challenge'];
// Check if a token and mode were sent
if (mode && token) {
// Check the mode and token sent are correct
if (mode === 'subscribe' && token === VERIFY_TOKEN) {
// Respond with 200 OK and challenge token from the request
console.log('WEBHOOK_VERIFIED');
res.status(200).send(challenge);
} else {
// Responds with '403 Forbidden' if verify tokens do not match
res.sendStatus(403);
}
}
});
// Sets server port and logs message on success
var PORT = process.env.PORT || 1337
app.listen(PORT, () => console.log(`webhook is listening on port ${PORT}`));