mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Possible connect timeout bug fix + added ability to relay own messages
This commit is contained in:
@@ -34,7 +34,8 @@
|
|||||||
const [notificationType, messageType] = client.getNotificationType(m) // get what type of notification it is -- message, group add notification etc.
|
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("got notification of type: " + notificationType) // message, groupAdd, groupLeave
|
||||||
console.log("message type: " + messageType) // conversation, imageMessage, videoMessage, contactMessage etc.
|
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
|
- Called when you recieve an update on someone's presence, they went offline or online
|
||||||
``` javascript
|
``` javascript
|
||||||
client.setOnPresenceUpdate (json => console.log(json.id + " presence is " + json.type))
|
client.setOnPresenceUpdate (json => console.log(json.id + " presence is " + json.type))
|
||||||
|
|||||||
@@ -103,8 +103,7 @@ module.exports = {
|
|||||||
}) // validate the connection
|
}) // validate the connection
|
||||||
.then (() => {
|
.then (() => {
|
||||||
this.log ("waiting for chats & contacts") // wait for the message with chats
|
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 chatUpdate = (json) => {
|
||||||
const isLast = json[1].last
|
const isLast = json[1].last
|
||||||
json = json[2]
|
json = json[2]
|
||||||
@@ -132,11 +131,14 @@ module.exports = {
|
|||||||
const waitForChats = this.registerCallbackOneTime (["response", "type:chat"]).then (json => {
|
const waitForChats = this.registerCallbackOneTime (["response", "type:chat"]).then (json => {
|
||||||
chats = json[2] // chats data (log json to see what it looks like)
|
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
|
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"])
|
const waitForContacts = this.registerCallbackOneTime (["response", "type:contacts"])
|
||||||
.then (json => contacts = json[2])
|
.then (json => contacts = json[2])
|
||||||
// wait for the chats & contacts to load
|
// wait for the chats & contacts to load
|
||||||
return Promise.all ([waitForConvos, waitForChats, waitForContacts])
|
return Promise.all ([waitForChats, waitForContacts])
|
||||||
})
|
})
|
||||||
.then (() => {
|
.then (() => {
|
||||||
// now we're successfully connected
|
// now we're successfully connected
|
||||||
|
|||||||
@@ -137,14 +137,17 @@ class WhatsAppWeb {
|
|||||||
this.registerCallback ("MsgInfo", func)
|
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 {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) => {
|
this.registerCallback (["action", "add:relay", "message"], (json) => {
|
||||||
const message = json[2][0][2]
|
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)
|
callback (message)
|
||||||
|
} else if (this.logUnhandledMessages) {
|
||||||
|
this.log (`[Unhandled] message - ${JSON.stringify(message)}`)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -176,7 +179,7 @@ class WhatsAppWeb {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log (text) {
|
log (text) {
|
||||||
console.log ("[Baileys] " + text)
|
console.log (`[Baileys] ${text}"`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,25 +36,25 @@ client.connect (authInfo, 30*1000) // connect or timeout in 30 seconds
|
|||||||
if (notificationType !== "message") {
|
if (notificationType !== "message") {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (m.key.fromMe) {
|
||||||
|
console.log ("relayed my own message")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
let sender = m.key.remoteJid
|
let sender = m.key.remoteJid
|
||||||
if (m.key.participant) { // participant exists if the message is in a group
|
if (m.key.participant) { // participant exists if the message is in a group
|
||||||
sender += " ("+m.key.participant+")"
|
sender += " ("+m.key.participant+")"
|
||||||
}
|
}
|
||||||
if (messageType === WhatsAppWeb.MessageType.text) {
|
if (messageType === WhatsAppWeb.MessageType.text) {
|
||||||
|
|
||||||
const text = m.message.conversation
|
const text = m.message.conversation
|
||||||
console.log (sender + " sent: " + text)
|
console.log (sender + " sent: " + text)
|
||||||
} else if (messageType === WhatsAppWeb.MessageType.extendedText) {
|
} else if (messageType === WhatsAppWeb.MessageType.extendedText) {
|
||||||
|
|
||||||
const text = m.message.extendedTextMessage.text
|
const text = m.message.extendedTextMessage.text
|
||||||
console.log (sender + " sent: " + text + " and quoted message: " + JSON.stringify(m.message))
|
console.log (sender + " sent: " + text + " and quoted message: " + JSON.stringify(m.message))
|
||||||
} else if (messageType === WhatsAppWeb.MessageType.contact) {
|
} else if (messageType === WhatsAppWeb.MessageType.contact) {
|
||||||
|
|
||||||
const contact = m.message.contactMessage
|
const contact = m.message.contactMessage
|
||||||
console.log (sender + " sent contact (" + contact.displayName + "): " + contact.vcard)
|
console.log (sender + " sent contact (" + contact.displayName + "): " + contact.vcard)
|
||||||
} else if (messageType === WhatsAppWeb.MessageType.location || messageType === WhatsAppWeb.MessageType.liveLocation) {
|
} else if (messageType === WhatsAppWeb.MessageType.location || messageType === WhatsAppWeb.MessageType.liveLocation) {
|
||||||
|
|
||||||
const locMessage = m.message[messageType]
|
const locMessage = m.message[messageType]
|
||||||
console.log (sender + " sent location (lat: " + locMessage.degreesLatitude + ", long: " + locMessage.degreesLongitude + "), saving thumbnail...")
|
console.log (sender + " sent location (lat: " + locMessage.degreesLatitude + ", long: " + locMessage.degreesLongitude + "), saving thumbnail...")
|
||||||
client.decodeMediaMessage(m.message, "loc_thumb_in_" + m.key.id)
|
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)
|
console.log("sent message with ID '" + messageID + "' successfully: " + success)
|
||||||
})
|
})
|
||||||
}, 3*1000)
|
}, 3*1000)
|
||||||
})
|
}, true) // set to false to not relay your own sent messages
|
||||||
/* custom functionality for tracking battery */
|
/* custom functionality for tracking battery */
|
||||||
client.registerCallback (["action", null, "battery"], json => {
|
client.registerCallback (["action", null, "battery"], json => {
|
||||||
const batteryLevelStr = json[2][0][1].value
|
const batteryLevelStr = json[2][0][1].value
|
||||||
|
|||||||
Reference in New Issue
Block a user