-
Notifications
You must be signed in to change notification settings - Fork 257
/
Copy pathreply.js
74 lines (67 loc) · 2.2 KB
/
reply.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
'use strict';
const rp = require('minimal-request-promise'),
breakText = require('../breaktext');
module.exports = function fbReply(recipient, message, fbAccessToken) {
var sendSingle = function sendSingle (message) {
if (typeof message === 'object' && typeof message.claudiaPause === 'number') {
return new Promise(resolve => setTimeout(resolve, parseInt(message.claudiaPause, 10)));
}
const messageBody = {
recipient: {
id: recipient
}
};
// Append special parameters to body of outbound POST
if (message.hasOwnProperty('messaging_type')) {
messageBody.messaging_type = message.messaging_type;
delete message.messaging_type;
}
if (message.hasOwnProperty('message_tag')) {
messageBody.tag = message.message_tag;
delete message.message_tag;
}
if (message.hasOwnProperty('notification_type')) {
messageBody.notification_type = message.notification_type;
delete message.notification_type;
}
if (message.hasOwnProperty('sender_action')) {
messageBody.sender_action = message.sender_action;
} else {
messageBody.message = message;
}
const options = {
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(messageBody)
};
return rp.post(`https://graph.facebook.com/v2.6/me/messages?access_token=${fbAccessToken}`, options);
},
sendAll = function () {
if (!messages.length) {
return Promise.resolve();
} else {
return sendSingle(messages.shift()).then(sendAll);
}
},
messages = [];
function breakTextAndReturnFormatted(message) {
return breakText(message, 640).map(m => ({ text: m }));
}
if (typeof message === 'string') {
messages = breakTextAndReturnFormatted(message);
} else if (Array.isArray(message)) {
message.forEach(msg => {
if (typeof msg === 'string') {
messages = messages.concat(breakTextAndReturnFormatted(msg));
} else {
messages.push(msg);
}
});
} else if (!message) {
return Promise.resolve();
} else {
messages = [message];
}
return sendAll();
};