forked from danielcardeenas/sulla
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send-message.js
40 lines (39 loc) Β· 1.03 KB
/
send-message.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
export function sendMessage(id, message, done) {
let chat = WAPI.getChat(id);
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
if (chat !== undefined) {
if (done !== undefined) {
chat.sendMessage(message).then(function () {
let trials = 0;
function check() {
for (let i = chat.msgs.models.length - 1; i >= 0; i--) {
let msg = chat.msgs.models[i];
if (!msg.senderObj.isMe || msg.body != message) {
continue;
}
done(WAPI._serializeMessageObj(msg));
return True;
}
trials += 1;
console.log(trials);
if (trials > 30) {
done(true);
return;
}
sleep(500).then(check);
}
check();
});
return true;
} else {
return chat
.sendMessage(message)
.then((_) => chat.lastReceivedKey._serialized);
}
} else {
if (done !== undefined) done(false);
return false;
}
}