-
Notifications
You must be signed in to change notification settings - Fork 1
/
whatsapp.js
109 lines (93 loc) · 2.94 KB
/
whatsapp.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const sulla = require('sulla-hotfix');
const request = require('request');
const config = require('./config');
const socket = require('socket.io-client')(config.websocket_host);
function triggerServer(requestMessage, client) {
if (config.test_mode) {
return client.sendText(requestMessage.number, config.reply_test_message);
} else if (requestMessage.message.match(config.required_code)) {
return request.post({
url: config.callback_url,
body: JSON.stringify(requestMessage),
headers: {
"Authorization": config.authorization_token,
"Content-Type": "application/json"
}
}, function (error, response, body) {
console.log({error, body, response})
if (error)
return false;
if (response.statusCode === 200) {
let snapshot = JSON.parse(body);
if (snapshot.reply)
client.sendText(requestMessage.number, snapshot.message);
}
});
}
}
function sanitizeMessage(message) {
return config.sanitized_code(message);
}
sulla.create().then(client => start(client));
async function start(client) {
/**
* Auto reply messages
*
*
* @return void
*/
client.onMessage(message => {
if ('string' === typeof message.body)
const requestMessage = {
number: message.from,
message: sanitizeMessage(message.body)
};
triggerServer(requestMessage, client);
});
/**
* Crawling unreplied messages
*
* if there are messages that are not answered, they will be replied automatically by the system
* @return void
*/
await client.getUnreadMessages(true, true, true).then(async chats => {
let requestMessages = [];
await chats.forEach(function (chat, i) {
chat.messages.forEach(function (message, i) {
if ('string' === typeof message.body && message.body.match(config.required_code)) {
const requestMessage = {
number: message.from._serialized,
message: sanitizeMessage(message.body)
};
requestMessages.push(requestMessage);
}
});
});
let requestMessagesLength = requestMessages.length;
let sent = 0;
if (requestMessagesLength > 0) {
setInterval(() => {
triggerServer(requestMessages[sent], client);
if (sent < requestMessagesLength-1)
sent++;
if (sent === requestMessagesLength-1) {
requestMessages = [];
sent = 0;
}
}, config.crawling_timeout);
}
});
/**
* If there is a message that must be sent to a number
*
* @return void
*/
socket.on("send_message", function (requestMessage) {
if (config.blast_mode) {
if (requestMessage.number && requestMessage.message) {
const { number, message } = requestMessage;
client.sendText(number, message);
}
}
});
}