-
Notifications
You must be signed in to change notification settings - Fork 4
/
handler.js
36 lines (30 loc) · 1.04 KB
/
handler.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
'use strict';
const axios = require('axios');
module.exports.webhook = (event, context, callback) => {
if (event.method === 'GET') {
// Facebook app verification
if (event.query['hub.verify_token'] === '<strong-token>' && event.query['hub.challenge']) {
callback(null, parseInt(event.query['hub.challenge']));
}
callback('Invalid token');
}
if (event.method === 'POST') {
event.body.entry.map((entry) => {
entry.messaging.map((messagingItem) => {
if (messagingItem.message && messagingItem.message.text) {
const accessToken = '<access-token>'; // TODO: replace this token before deployment
const url = `https://graph.facebook.com/v2.6/me/messages?access_token=${accessToken}`;
const payload = {
recipient: {
id: messagingItem.sender.id
},
message: {
text: 'Hello World!'
}
};
axios.post(url, payload).then((response) => callback(null, response));
}
});
});
}
};