-
Notifications
You must be signed in to change notification settings - Fork 0
/
facebook-send.js
69 lines (57 loc) · 1.45 KB
/
facebook-send.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
'use strict';
// ----------------------------------------------------------------------------
// Messenger API specific code
// See the Send API reference
// https://developers.facebook.com/docs/messenger-platform/send-api-reference
const FB_PAGE_TOKEN = "";
const fbSend = (bodyObj) => {
const body = JSON.stringify(bodyObj);
const qs = 'access_token=' + encodeURIComponent(FB_PAGE_TOKEN);
return fetch('https://graph.facebook.com/me/messages?' + qs, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body,
})
.then(rsp => rsp.json())
.then(json => {
if (json.error && json.error.message) {
throw new Error(json.error.message);
}
return json;
});
};
const fbMarkSeen = (id) => {
return fbSend({
recipient: { id },
sender_action: "mark_seen"
});
};
const fbTypingOn = (id) => {
return fbSend({
recipient: { id },
sender_action: "typing_on"
});
};
const fbTypingOff = (id) => {
return fbSend({
recipient: { id },
sender_action: "typing_off"
});
};
const fbMessage = (id, text, q_replies) => {
let quick_replies = null;
if (q_replies) {
quick_replies = q_replies.map(reply => {
return {
"content_type": "text",
"title": reply,
"payload": reply
};
});
}
return fbSend({
recipient: { id },
message: { text, quick_replies },
});
};
module.exports = { fbSend, fbMarkSeen, fbMessage,fbTypingOff, fbTypingOn };