diff --git a/src/Tests/Tests.Groups.ts b/src/Tests/Tests.Groups.ts index 36e16bc..4a9b3d5 100644 --- a/src/Tests/Tests.Groups.ts +++ b/src/Tests/Tests.Groups.ts @@ -51,6 +51,11 @@ WAConnectionTest('Groups', (conn) => { it('should send a message on the group', async () => { await sendAndRetreiveMessage(conn, 'Hello!', MessageType.text, {}, gid) }) + it('should delete a message on the group', async () => { + const message = await sendAndRetreiveMessage(conn, 'Hello!', MessageType.text, {}, gid) + await delay(1500) + await conn.deleteMessage(message.key) + }) it('should quote a message on the group', async () => { const {messages} = await conn.loadMessages (gid, 100) const quotableMessage = messages.find (m => m.message) diff --git a/src/WAConnection/4.Events.ts b/src/WAConnection/4.Events.ts index 7a95534..d98829a 100644 --- a/src/WAConnection/4.Events.ts +++ b/src/WAConnection/4.Events.ts @@ -291,7 +291,7 @@ export class WAConnection extends Base { // profile picture updates this.on('CB:Cmd,type:picture', async json => { const jid = whatsappID(json[1].jid) - const imgUrl = await this.getProfilePicture(jid) + const imgUrl = await this.getProfilePicture(jid).catch(() => '') const contact = this.contacts[jid] if (contact) contact.imgUrl = imgUrl diff --git a/src/WAConnection/7.MessagesExtra.ts b/src/WAConnection/7.MessagesExtra.ts index 993039b..6f28bed 100644 --- a/src/WAConnection/7.MessagesExtra.ts +++ b/src/WAConnection/7.MessagesExtra.ts @@ -290,14 +290,17 @@ export class WAConnection extends Base { * @param id the person or group where you're trying to delete the message * @param messageKey key of the message you want to delete */ - async deleteMessage (id: string, messageKey: WAMessageKey) { + async deleteMessage (k: string | WAMessageKey, messageKey?: WAMessageKey) { + if (typeof k === 'object') { + messageKey = k + } const json: WAMessageContent = { protocolMessage: { key: messageKey, type: WAMessageProto.ProtocolMessage.ProtocolMessageType.REVOKE } } - const waMessage = this.prepareMessageFromContent (id, json, {}) + const waMessage = this.prepareMessageFromContent (messageKey.remoteJid, json, {}) await this.relayWAMessage (waMessage) return waMessage } diff --git a/src/WAConnection/Utils.ts b/src/WAConnection/Utils.ts index 32d4e4a..4ef714f 100644 --- a/src/WAConnection/Utils.ts +++ b/src/WAConnection/Utils.ts @@ -10,6 +10,7 @@ import { Agent } from 'https' import Decoder from '../Binary/Decoder' import { MessageType, HKDFInfoKeys, MessageOptions, WAChat, WAMessageContent, BaileysError, WAMessageProto, TimedOutError, CancelledError, WAGenericMediaMessage, WAMessage, WAMessageKey } from './Constants' import KeyedDB from '@adiwajshing/keyed-db' +import { Response } from 'node-fetch' const platformMap = { 'aix': 'AIX', @@ -274,7 +275,7 @@ export async function generateThumbnail(buffer: Buffer, mediaType: MessageType, * Decode a media message (video, image, document, audio) & return decrypted buffer * @param message the media message you want to decode */ -export async function decodeMediaMessageBuffer(message: WAMessageContent, fetchRequest: (host: string, method: string) => any) { +export async function decodeMediaMessageBuffer(message: WAMessageContent, fetchRequest: (host: string, method: string) => Promise) { /* One can infer media type from the key in the message it is usually written as [mediaType]Message. Eg. imageMessage, audioMessage etc. @@ -287,7 +288,7 @@ export async function decodeMediaMessageBuffer(message: WAMessageContent, fetchR throw new BaileysError('cannot decode text message', message) } if (type === MessageType.location || type === MessageType.liveLocation) { - return new Buffer(message[type].jpegThumbnail) + return Buffer.from(message[type].jpegThumbnail) } let messageContent: WAGenericMediaMessage if (message.productMessage) { @@ -300,11 +301,10 @@ export async function decodeMediaMessageBuffer(message: WAMessageContent, fetchR // download the message const fetched = await fetchRequest(messageContent.url, 'GET') - const buffer = await fetched.buffer() - - if (buffer.length <= 10) { - throw new BaileysError ('Empty buffer returned. File has possibly been deleted from WA servers. Run `client.updateMediaMessage()` to refresh the url', {status: 404}) + if (fetched.status >= 400) { + throw new BaileysError ('Invalid code (' + fetched.status + ') returned. File has possibly been deleted from WA servers. Run `client.updateMediaMessage()` to refresh the url', {status: fetched.status}) } + const buffer = await fetched.buffer() const decryptedMedia = (type: MessageType) => { // get the keys to decrypt the message