From 5bb9bbd1efd50db3e66a476db26523eec76174c6 Mon Sep 17 00:00:00 2001 From: Adhiraj Date: Sat, 6 Jun 2020 13:00:38 +0530 Subject: [PATCH] Possible connect timeout bug fix + added ability to relay own messages --- .DS_Store | Bin 14340 -> 10244 bytes README.md | 3 ++- WhatsAppWeb.Session.js | 8 +++++--- WhatsAppWeb.js | 11 +++++++---- example/example.js | 10 +++++----- 5 files changed, 19 insertions(+), 13 deletions(-) diff --git a/.DS_Store b/.DS_Store index 85f6455e82c4ce493fb3c4503d023d5affdb2fc9..99e765a7a296952390c5a09a3a76dd28d6c64ba4 100644 GIT binary patch delta 179 zcmZoEXbF&DU|?W$DortDU{C-uIe-{M3-C-V6q~3gIoZI3MH0wokON{ypg03VJVPc! z9zz;KK7-z7LB?-vlNA`(Zf56T;b4@Tq^dBvO@eRpCly`B$;s-{lQ*f|-|Q`LgmH4W zh}YzNQOV5#qPw|(8jc7ugA4`Q#SJ7}LG}U7`p!I=-^7p=XbnTdWJePvP7RO}AlG1W MhRMXuCqY~=8Gcu`V@kGZ`a`zU=0L|zW57zyrAUfQVN{}}G-_j8HT?eIMAhN|3UL{EPCiMDf$D_TYG4ZIp&Z*5FqH;eV!RgT8g{!xCI zuUG@I24W4w8i+OUf2aX|dvPTW=sZqi4a6FVHL#@t+J`i5=Cgx*L+8qLAmJ9C@41Cn z7s}Y8tGe_cpB>~II#*C&6co9J8Uq)@&~VhJ*&O-oAm7lrhKr%$Vhku70}sVe)PYN@ z=3>n1JWgW`#2OgYz-d&`_qL=C@4FyHlp0clFCFMZk9~#q=eB%rk^R6v28MomTkK&^ zUH**LbK$M4kNVw#U;mG4x83S{natP5*q*)nUfZ8YOiWB1nz&zCaUWGY%UkW2t=?Te zEm_`5bx^+Uy6vs9RlV7=A1$YjUveGKYB_dKfm?MOP43-lId%6@*O3!J# zP7@R;MG_;P!H8Ee17=WW8WKY>ehWy#H=@Lvh}76XF=A6h3??5ucXq^WG+iPs^kAwq z2YleX3&fIQ23%(q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9Y*<>@U?$b<|53 z`vP{(AGb_GKcU#JSN&kM@AZJW4limQyG_O-I&wj7qVKkfzi>}`0bT|@01KrXc;?QX zwH*5u>{ELv(Q9EeqKO)|C+;CIF4oQkS%SB6Relan<&vUxL9U?gGTv-^7nbra7SRVK zEaUd@e!(Ai#WUyK?lN5&$h2!a)epx)N4WH=D`8GW9Z${z+Em8WYQWi%*W4k@0cy%p z{?9VdjJ7D<4f|rspYr=+yC4H%ue-GN$6hB95-I%yyC3|KGMX!;y^G|5p)K`>!7YRKX!~@IqfX_Wxu5 J|CRRt{|)lj4M_k1 diff --git a/README.md b/README.md index 662abdb..18056ea 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,8 @@ const [notificationType, messageType] = client.getNotificationType(m) // get what type of notification it is -- message, group add notification etc. console.log("got notification of type: " + notificationType) // message, groupAdd, groupLeave console.log("message type: " + messageType) // conversation, imageMessage, videoMessage, contactMessage etc. - }) + }, false) // set to `true` if you want to receive outgoing messages that may be sent from your phone + ``` - Called when you recieve an update on someone's presence, they went offline or online ``` javascript client.setOnPresenceUpdate (json => console.log(json.id + " presence is " + json.type)) diff --git a/WhatsAppWeb.Session.js b/WhatsAppWeb.Session.js index 234da06..b101806 100644 --- a/WhatsAppWeb.Session.js +++ b/WhatsAppWeb.Session.js @@ -103,8 +103,7 @@ module.exports = { }) // validate the connection .then (() => { this.log ("waiting for chats & contacts") // wait for the message with chats - - const waitForConvos = new Promise ((resolve, _) => { + const waitForConvos = () => new Promise ((resolve, _) => { const chatUpdate = (json) => { const isLast = json[1].last json = json[2] @@ -132,11 +131,14 @@ module.exports = { const waitForChats = this.registerCallbackOneTime (["response", "type:chat"]).then (json => { chats = json[2] // chats data (log json to see what it looks like) chats.forEach (chat => unreadMap [chat[1].jid] = chat[1].count) // store the number of unread messages for each sender + if (chats.length > 0) { + return waitForConvos () + } }) const waitForContacts = this.registerCallbackOneTime (["response", "type:contacts"]) .then (json => contacts = json[2]) // wait for the chats & contacts to load - return Promise.all ([waitForConvos, waitForChats, waitForContacts]) + return Promise.all ([waitForChats, waitForContacts]) }) .then (() => { // now we're successfully connected diff --git a/WhatsAppWeb.js b/WhatsAppWeb.js index 76233c0..f1bec0b 100644 --- a/WhatsAppWeb.js +++ b/WhatsAppWeb.js @@ -137,14 +137,17 @@ class WhatsAppWeb { this.registerCallback ("MsgInfo", func) } /** - * Set the callback for new/unread messages, if someone sends a message, this callback will be fired + * Set the callback for new/unread messages; if someone sends you a message, this callback will be fired * @param {function(WhatsAppMessage)} callback + * @param {boolean} callbackOnMyMessages - should the callback be fired on a message you sent */ - setOnUnreadMessage (callback) { + setOnUnreadMessage (callback, callbackOnMyMessages=false) { this.registerCallback (["action", "add:relay", "message"], (json) => { const message = json[2][0][2] - if (!message.key.fromMe) { // if this message was sent to us, notify + if (!message.key.fromMe || callbackOnMyMessages) { // if this message was sent to us, notify callback (message) + } else if (this.logUnhandledMessages) { + this.log (`[Unhandled] message - ${JSON.stringify(message)}`) } }) } @@ -176,7 +179,7 @@ class WhatsAppWeb { } log (text) { - console.log ("[Baileys] " + text) + console.log (`[Baileys] ${text}"`) } } diff --git a/example/example.js b/example/example.js index 67c07cd..9f60d54 100644 --- a/example/example.js +++ b/example/example.js @@ -36,25 +36,25 @@ client.connect (authInfo, 30*1000) // connect or timeout in 30 seconds if (notificationType !== "message") { return } + if (m.key.fromMe) { + console.log ("relayed my own message") + return + } let sender = m.key.remoteJid if (m.key.participant) { // participant exists if the message is in a group sender += " ("+m.key.participant+")" } if (messageType === WhatsAppWeb.MessageType.text) { - const text = m.message.conversation console.log (sender + " sent: " + text) } else if (messageType === WhatsAppWeb.MessageType.extendedText) { - const text = m.message.extendedTextMessage.text console.log (sender + " sent: " + text + " and quoted message: " + JSON.stringify(m.message)) } else if (messageType === WhatsAppWeb.MessageType.contact) { - const contact = m.message.contactMessage console.log (sender + " sent contact (" + contact.displayName + "): " + contact.vcard) } else if (messageType === WhatsAppWeb.MessageType.location || messageType === WhatsAppWeb.MessageType.liveLocation) { - const locMessage = m.message[messageType] console.log (sender + " sent location (lat: " + locMessage.degreesLatitude + ", long: " + locMessage.degreesLongitude + "), saving thumbnail...") client.decodeMediaMessage(m.message, "loc_thumb_in_" + m.key.id) @@ -94,7 +94,7 @@ client.connect (authInfo, 30*1000) // connect or timeout in 30 seconds console.log("sent message with ID '" + messageID + "' successfully: " + success) }) }, 3*1000) - }) + }, true) // set to false to not relay your own sent messages /* custom functionality for tracking battery */ client.registerCallback (["action", null, "battery"], json => { const batteryLevelStr = json[2][0][1].value