Fixed promise bug

This commit is contained in:
Adhiraj
2020-05-09 19:01:22 +05:30
parent 91cfbd1412
commit eb7387b6fb
5 changed files with 15 additions and 13 deletions

View File

@@ -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

View File

@@ -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) {

View File

@@ -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 {

View File

@@ -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])

View File

@@ -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
}