diff --git a/index.d.ts b/index.d.ts index 7b54fdd..ea8be4b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -60,6 +60,9 @@ declare namespace WAWebJS { /** Get contact instance by ID */ getContactById(contactId: string): Promise + /** Get message by ID */ + getMessageById(messageId: string): Promise + /** Get all current contact instances */ getContacts(): Promise diff --git a/src/Client.js b/src/Client.js index 97a2a89..c673cef 100644 --- a/src/Client.js +++ b/src/Client.js @@ -898,6 +898,24 @@ class Client extends EventEmitter { return ContactFactory.create(this, contact); } + + async getMessageById(messageId) { + const msg = await this.pupPage.evaluate(async messageId => { + let msg = window.Store.Msg.get(messageId); + if(msg) return window.WWebJS.getMessageModel(msg); + + const params = messageId.split('_'); + if(params.length !== 3) throw new Error('Invalid serialized message id specified'); + + let messagesObject = await window.Store.Msg.getMessagesById([messageId]); + if (messagesObject && messagesObject.messages.length) msg = messagesObject.messages[0]; + + if(msg) return window.WWebJS.getMessageModel(msg); + }, messageId); + + if(msg) return new Message(this, msg); + return null; + } /** * Returns an object with information about the invite code's group