Download media from ephemeral messages

This commit is contained in:
Adhiraj Singh
2020-11-19 00:26:17 +05:30
parent c69d3832de
commit ba8b8c693a
2 changed files with 10 additions and 6 deletions

View File

@@ -201,12 +201,12 @@ on (event: 'user-status-update', listener: (update: {jid: string, status?: strin
on (event: 'chat-new', listener: (chat: WAChat) => void): this
/** when contacts are sent by WA */
on (event: 'contacts-received', listener: () => void): this
/** when chats are sent by WA */
on (event: 'chats-received', listener: (update: {hasNewChats: boolean}) => void): this
/** when chats are sent by WA, and when all messages are received from WhatsApp */
on (event: 'chats-received', listener: (update: {hasNewChats?: boolean, hasReceivedLastMessage?: boolean}) => void): this
/** when multiple chats are updated (new message, updated message, deleted, pinned, etc) */
on (event: 'chats-update', listener: (chats: (Partial<WAChat> & { jid: string })[]) => void): this
on (event: 'chats-update', listener: (chats: WAChatUpdate[]) => void): this
/** when a chat is updated (new message, updated message, deleted, pinned, presence updated etc) */
on (event: 'chat-update', listener: (chat: Partial<WAChat> & { jid: string }) => void): this
on (event: 'chat-update', listener: (chat: Partial<WAChat> & { hasNewMessage: boolean }) => void): this
/** when a message's status is updated (deleted, delivered, read, sent etc.) */
on (event: 'message-status-update', listener: (message: WAMessageStatusUpdate) => void): this
/** when participants are added to a group */

View File

@@ -245,14 +245,18 @@ export class WAConnection extends Base {
*/
@Mutex (message => message?.key?.id)
async downloadMediaMessage (message: WAMessage) {
let mContent = message.message?.ephemeralMessage?.message || message.message
if (!mContent) throw new BaileysError('No message present', { status: 400 })
try {
const buff = await decodeMediaMessageBuffer (message.message, this.fetchRequest)
const buff = await decodeMediaMessageBuffer (mContent, this.fetchRequest)
return buff
} catch (error) {
if (error instanceof BaileysError && error.status === 404) { // media needs to be updated
this.logger.info (`updating media of message: ${message.key.id}`)
await this.updateMediaMessage (message)
const buff = await decodeMediaMessageBuffer (message.message, this.fetchRequest)
mContent = message.message?.ephemeralMessage?.message || message.message
const buff = await decodeMediaMessageBuffer (mContent, this.fetchRequest)
return buff
}
throw error