Compatibility with older versions of Node + decodeMedia bug fix

This commit is contained in:
Adhiraj
2020-05-15 22:00:12 +05:30
parent 80adc095a3
commit ea5ca61e49
7 changed files with 29 additions and 20 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@@ -23,7 +23,7 @@ module.exports = {
* @return {Promise<[object, object]>}
*/
getStatus: function (jid) {
jid = jid ?? this.userMetaData.id
jid = jid || this.userMetaData.id
return this.query(["query","Status",jid])
},
/**
@@ -32,7 +32,7 @@ module.exports = {
* @return {Promise<[object, object]>}
*/
getProfilePicture: function (jid) {
jid = jid ?? this.userMetaData.id
jid = jid || this.userMetaData.id
return this.query(["query","ProfilePicThumb",jid])
},
/**

View File

@@ -73,12 +73,12 @@ module.exports = {
let callbacks = this.callbacks["function:" + json[0]]
var callbacks2
var callback
for (var key in json[1] ?? {}) {
for (var key in json[1] || {}) {
callbacks2 = callbacks[key + ":" + json[1][key]]
if (callbacks2) { break }
}
if (!callbacks2) {
for (var key in json[1] ?? {}) {
for (var key in json[1] || {}) {
callbacks2 = callbacks[key]
if (callbacks2) { break }
}
@@ -110,10 +110,15 @@ module.exports = {
* @return {[string, string]} [type of notification, specific type of message]
*/
getNotificationType: function (message) {
const MessageStubTypes = {
20: "addedToGroup",
32: "leftGroup",
39: "createdGroup"
}
if (message.message) {
return ["message", Object.keys(message.message)[0]]
} else if (message.messageStubType) {
return [WhatsAppWeb.MessageStubTypes[message.messageStubType] , null]
return [MessageStubTypes[message.messageStubType] , null]
} else {
return ["unknown", null]
}
@@ -143,8 +148,8 @@ module.exports = {
throw "parameters (" + parameters + ") must be a string or array"
}
const func = "function:" + parameters[0]
const key = parameters[1] ?? ""
const key2 = parameters[2] ?? ""
const key = parameters[1] || ""
const key2 = parameters[2] || ""
if (!this.callbacks[func]) {
this.callbacks[func] = {}
}
@@ -165,8 +170,8 @@ module.exports = {
throw "parameters (" + parameters + ") must be a string or array"
}
const func = "function:" + parameters[0]
const key = parameters[1] ?? ""
const key2 = parameters[2] ?? ""
const key = parameters[1] || ""
const key2 = parameters[2] || ""
if (this.callbacks[func] && this.callbacks[func][key] && this.callbacks[func][key][key2]) {
delete this.callbacks[func][key][key2]
return
@@ -211,7 +216,7 @@ module.exports = {
if (!type) {
return Promise.reject("unknown message type")
}
if (type === WhatsAppWeb.MessageType.extendedText || type === WhatsAppWeb.MessageType.text) {
if (type === "extendedTextMessage" || type === "conversation") {
return Promise.reject("cannot decode text message")
}

View File

@@ -252,7 +252,7 @@ module.exports = {
var buff = Utils.aesEncrypt(binary, this.authInfo.encKey) // encrypt it using AES and our encKey
const sign = Utils.hmacSign(buff, this.authInfo.macKey) // sign the message using HMAC and our macKey
tag = tag ?? Utils.generateMessageTag()
tag = tag || Utils.generateMessageTag()
buff = Buffer.concat([
Buffer.from(tag + ","), // generate & prefix the message tag
Buffer.from(tags), // prefix some bytes that tell whatsapp what the message is about
@@ -271,7 +271,7 @@ module.exports = {
*/
sendJSON: function (json, tag) {
const str = JSON.stringify(json)
tag = tag ?? Utils.generateMessageTag()
tag = tag || Utils.generateMessageTag()
this.send(tag + "," + str)
return tag
},

View File

@@ -73,17 +73,21 @@ module.exports = {
} else {
return this.generateKeysForAuth(json.ref)
}
case 401: // if the phone was unpaired
throw [json.status, "unpaired from phone", message]
case 429: // request to login was denied, don't know why it happens
throw [json.status, "request denied, try reconnecting", message]
case 304: // request to generate a new key for a QR code was denied
throw [json.status, "request for new key denied", message]
default:
throw [json.status, "unknown error", message]
throw [json.status, "unknown error", json]
}
})
.then (([json, q]) => {
switch (json.status) {
case 401: // if the phone was unpaired
throw [json.status, "unpaired from phone", json]
case 429: // request to login was denied, don't know why it happens
throw [json.status, "request denied, try reconnecting", json]
case 304: // request to generate a new key for a QR code was denied
throw [json.status, "request for new key denied", json]
default:
break
}
if (json[1] && json[1].challenge) { // if its a challenge request (we get it when logging in)
return this.respondToChallenge(json[1].challenge)
.then (([json, _]) => {

View File

@@ -50,7 +50,7 @@ client.connect (authInfo, 30*1000) // connect or timeout in 30 seconds
// decode, decrypt & save the media.
// The extension to the is applied automatically based on the media type
client.decodeMediaMessage(m.message, "media_in_" + m.key.id)
.then (meta => console.log(sender + " sent media, saved at: " + meta.fileName))
.then (meta => console.log(sender + " sent media, saved at: " + meta.filename))
.catch (err => console.log("error in decoding message: " + err))
}
// send a reply after 3 seconds

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB