From eb7387b6fb3a16e5a713379c6397f9659df2fb58 Mon Sep 17 00:00:00 2001 From: Adhiraj Date: Sat, 9 May 2020 19:01:22 +0530 Subject: [PATCH] Fixed promise bug --- README.md | 2 +- WhatsAppWeb.Query.js | 2 +- WhatsAppWeb.Recv.js | 8 +------- WhatsAppWeb.Send.js | 5 +++-- example/example.js | 11 +++++++++-- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index f38ae38..32bc978 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ Baileys is super easy to use: * To check if a given ID is on WhatsApp ``` javascript client.isOnWhatsApp ("xyz@c.us") - .then ((exists, id) => console.log(id + (exists ? " exists " : " does not exist") + "on WhatsApp")) + .then (([exists, id]) => console.log(id + (exists ? " exists " : " does not exist") + "on WhatsApp")) ``` * To query chat history on a group or with someone ``` javascript diff --git a/WhatsAppWeb.Query.js b/WhatsAppWeb.Query.js index 0192163..9c5b993 100644 --- a/WhatsAppWeb.Query.js +++ b/WhatsAppWeb.Query.js @@ -4,7 +4,7 @@ module.exports = function(WhatsAppWeb) { // check if given number is registered on WhatsApp WhatsAppWeb.prototype.isOnWhatsApp = function (jid) { - return this.query(["query", "exist", jid]) + return this.query(["query", "exist", jid]).then (([m, q]) => [m.status === 200, q[2]]) } // check the presence status of a given jid WhatsAppWeb.prototype.requestPresenceUpdate = function (jid) { diff --git a/WhatsAppWeb.Recv.js b/WhatsAppWeb.Recv.js index 5cdd16c..0a4fc7d 100644 --- a/WhatsAppWeb.Recv.js +++ b/WhatsAppWeb.Recv.js @@ -186,13 +186,7 @@ module.exports = function(WhatsAppWeb) { // if this message is responding to a query if (this.callbacks[messageTag]) { const q = this.callbacks[messageTag] - if (q.queryJSON[1] === "exist") { - q.callback(json.status == 200, q.queryJSON[2]) - } else if (q.queryJSON[1] === "mediaConn") { - q.callback(json.media_conn) - } else { - q.callback(json) - } + q.callback([json, q.queryJSON]) delete this.callbacks[messageTag] } } else { diff --git a/WhatsAppWeb.Send.js b/WhatsAppWeb.Send.js index 132ab5e..923c61e 100644 --- a/WhatsAppWeb.Send.js +++ b/WhatsAppWeb.Send.js @@ -95,7 +95,8 @@ module.exports = function(WhatsAppWeb) { return Utils.generateThumbnail(buffer, mediaType, info) .then (() => this.query(["query", "mediaConn"])) // send a query JSON to obtain the url & auth token to upload our media - .then (json => { + .then (([json,_]) => { + json = json.media_conn const auth = json.auth // the auth token let hostname = "https://" + json.hosts[0].hostname // first hostname available hostname += mediaPathMap[mediaType] + "/" + fileEncSha256B64 // append path @@ -151,7 +152,7 @@ module.exports = function(WhatsAppWeb) { const json = [ "action", - {epoch: this.msgCount.toString(), type: "relay" }, + {epoch: this.msgCount.toString(), type: "relay"}, [ ["message", null, messageJSON] ] ] return this.query(json, [16, 64]) diff --git a/example/example.js b/example/example.js index bbfe5cf..083da7c 100644 --- a/example/example.js +++ b/example/example.js @@ -54,16 +54,23 @@ client.handlers.onUnreadMessage = (m) => { setTimeout(() => client.sendReadReceipt(m.key.remoteJid, m.key.id), 2*1000) // send a read reciept for the message in 2 seconds setTimeout(() => client.updatePresence(m.key.remoteJid, WhatsAppWeb.Presence.composing), 2.5*1000) // let them know you're typing in 2.5 seconds setTimeout(() => { + let promise if (Math.random() > 0.5) { // choose at random - client.sendTextMessage(m.key.remoteJid, "hello!", m) // send a "hello!" & quote the message recieved + promise = client.sendTextMessage(m.key.remoteJid, "hello!", m) // send a "hello!" & quote the message recieved } else { const buffer = fs.readFileSync("./ma_gif.mp4") // load the gif const info = { gif: true, // the video is a gif caption: "hello!" // the caption } - client.sendMediaMessage (m.key.remoteJid, buffer, WhatsAppWeb.MessageType.video, info) // send this gif! + promise = client.sendMediaMessage (m.key.remoteJid, buffer, WhatsAppWeb.MessageType.video, info) // send this gif! } + promise.then (([m, q]) => { + const success = m.status === 200 + const messageID = q[2][0][2].key.id + console.log("sent message with ID '" + messageID + "' successfully: " + success) + }) + }, 4*1000) // send after 4 seconds }