-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (86 loc) · 3.38 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
'use strict';
// Imports dependencies and set up http server
const express = require('express'),
bodyParser = require('body-parser'),
app = express() // creates express http server
.use(bodyParser.json()) // support json encoded bodies
.use(bodyParser.urlencoded({ extended: true })),
{ handleMessage, handlePostback } = require('./client/messenger'),
{ handleCommand } = require('./client/slack'); // support encoded bodies
// Sets server port and logs message on success
app.listen(process.env.PORT || 1337, () =>
console.log('Webhook is listening...')
);
/****************************************
* *
* MESSENGER *
* *
****************************************/
// Accepts GET requests at the /webhook endpoint
app.get('/webhook', (req, res) => {
console.log('Trying to get...');
/** UPDATE YOUR VERIFY TOKEN **/
const VERIFY_TOKEN = process.env.VERIFY_TOKEN;
// Parse params from the webhook verification request
const mode = req.query['hub.mode'];
const token = req.query['hub.verify_token'];
const 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);
}
}
});
// Accepts POST requests at /webhook endpoint
app.post('/webhook', (req, res) => {
// Parse the request body from the POST
const { body } = req;
// Check the webhook event is from a Page subscription
if (body.object === 'page') {
// Iterate over each entry - there may be multiple if batched
body.entry.forEach(function(entry) {
// Get the webhook event. entry.messaging is an array, but
// will only ever contain one event, so we get index 0
const webhook_event = entry.messaging[0];
console.log('\n\n--> webhook_event : ', webhook_event);
// Get the sender PSID
const sender_psid = webhook_event.sender.id;
// 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);
}
});
/****************************************
* *
* SLACK *
* *
****************************************/
// Accepts POST requests at /slackhook endpoint
app.post('/slackhook', async (req, res) => {
// Parse the body from the POST request
const { body } = req;
console.log('\n\n--> slackhook body : ', body);
const message = await handleCommand(body);
// Return a '200 OK' response to all events
res.status(200).send({
response_type: 'in_channel', //controls the message's visibility
...message
});
});