diff --git a/.DS_Store b/.DS_Store index 85f6455..99e765a 100644 Binary files a/.DS_Store and b/.DS_Store differ 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