-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacebookBot.js
70 lines (61 loc) · 1.92 KB
/
facebookBot.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
let request = require('request-promise');
let config = require('./config');
module.exports = {
/*
* 驗證 Facebook message webhook
*/
verify: function(req, res, next) {
// 驗證這個 url 是否正確
if (req.query['hub.verify_token'] === '阿不就好棒棒') {
return res.send(req.query['hub.challenge']);
} else {
return res.send('Error, wrong validation token');
}
},
/*
* Facebook message
*/
callback: function(req, res, next) {
// 1. 處理傳進來的訊息
let messaging_events = req.body.entry[0].messaging; // 訊息的內容
let sender; // 送出訊息的人
let recipient; // 收到訊息的人
// 2. 處理發訊息,收訊息,訊息的內容
for(let i = 0; i < messaging_events.length; i++) {
let event = req.body.entry[0].messaging[i];
sender = event.sender.id;
recipient = event.recipient.id;
if (event.message && event.message.text) {
let text = event.message.text;
}
}
// 3. 要送出去的訊息格式
let messageData = {
text: 'JavaScript 好棒棒'
};
// 4. 訊息選項
let options = {
method: 'POST',
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: {
access_token: config.facebook.accessToken
},
body: {
message: messageData,
recipient: {
id: sender
}
},
json: true
};
// 5. 送出訊息
request(options)
.then(function (body) {
return res.status(200).send();
})
.catch(function (err) {
console.log(err);
return res.status(400).send();
});
}
};