This repository has been archived by the owner on Sep 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfbbot.js
95 lines (72 loc) · 1.91 KB
/
fbbot.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
const request = require('request');
class fbbot {
constructor() {
this.msgApi = 'https://graph.facebook.com/v2.6/me/messages';
this.settingApi = 'https://graph.facebook.com/v2.6/me/thread_settings';
this.config = require('config');
}
/*
* return 'TEXT', 'POSTBACK', 'OPTIN', 'DELIVERY'
*/
typeofMsg(msg) {
}
sendMsg(id, json, type = 'TEXT') {
var msgData = {
recipient: {
id: id
},
message: json
}
this.callSendAPI(msgData, type);
}
/*SETTING*/
createGetStartedBtn(defaultPayload) {
if(!defaultPayload) {
var defaultPayload = 'GET_STARTED_BUTTON';
}
var setting = true;
var json = {
"setting_type": "call_to_actions",
"thread_state": "new_thread",
"call_to_actions": [{
"payload": defaultPayload
}]
}
this.callSendAPI(json, 'setting', true);
}
deleteGetStartedBtn() {
var setting = true;
var json = {
"setting_type": "call_to_actions",
"thread_state": "new_thread"
}
this.callSendAPI(json, 'setting', true, 'DELETE');
}
/* SEND */
callSendAPI(messageData, type = 'generic', setting = false, method = 'POST') {
var reqUrl = this.msgApi;
if (setting) reqUrl = this.settingApi;
request({
uri: reqUrl,
qs: {
access_token: this.config.pageAccessToken
},
method: method,
json: messageData
}, (error, response, body) => {
if (!error && response.statusCode == 200) {
var reportStr = "Successfully sent %s message";
if (setting) reportStr += " with id %s to recipient %s";
else {
var recipientId = body.recipient_id;
var messageId = body.message_id;
}
console.log(reportStr, type, messageId, recipientId);
} else {
console.error("Unable to send message.");
console.error(error);
}
});
}
}
exports.fbbot = fbbot;