From 142a92cc498f4c15d4447e1221e8b4b100edd40a Mon Sep 17 00:00:00 2001 From: Adhiraj Date: Sat, 11 Jul 2020 12:25:49 +0530 Subject: [PATCH] Added within chat search + updated readme --- README.md | 28 +++++++++++++++++++++- src/WAClient/Messages.ts | 8 ++++--- src/WAClient/Tests.ts | 9 ++++--- src/WAConnection/Base.ts | 4 +--- src/WAConnection/BrowserMessageDecoding.ts | 2 +- 5 files changed, 40 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index b6e82e6..5768dc5 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,30 @@ client.connectSlim(null, 20*1000) // use loaded credentials & timeout in 20s ``` See the browser credentials type [here](/src/WAConnection/Constants.ts). +## QR Overriding + +If you want to do some custom processing with the QR code used to authenticate, you can override the following method: +``` ts +client.onReadyForPhoneAuthentication = ([ref, publicKey, clientID]) => { + const str = ref + ',' + publicKey + ',' + clientID // the QR string + // Now, use 'str' to display in QR UI or send somewhere +} +const user = await client.connect () +``` + +If you need to regenerate the QR, you can also do so using: +``` ts +let generateQR: async () => void // call generateQR on some timeout or error +client.onReadyForPhoneAuthentication = ([ref, publicKey, clientID]) => { + generateQR = async () => { + ref = await client.generateQRCode () // returns a new ref code to use for QR generation + const str = ref + ',' + publicKey + ',' + clientID // the QR string + // re-print str as QR or update in UI or send somewhere + //QR.generate(str, { small: true }) + } +} +const user = await client.connect () +``` ## Handling Events Implement the following callbacks in your code: @@ -298,8 +322,10 @@ setTimeout (() => { ``` - To search through messages ``` ts - const response = await client.searchMessages ('so cool', 25, 0) // get 25 messages of the first page of results + const response = await client.searchMessages ('so cool', null, 25, 1) // search in all chats console.log (`got ${response.messages.length} messages in search`) + + const response2 = await client.searchMessages ('so cool', '1234@c.us', 25, 1) // search in given chat ``` Of course, replace ``` xyz ``` with an actual ID. Append ``` @s.whatsapp.net ``` for individuals & ``` @g.us ``` for groups. diff --git a/src/WAClient/Messages.ts b/src/WAClient/Messages.ts index e1011ae..bd0f733 100644 --- a/src/WAClient/Messages.ts +++ b/src/WAClient/Messages.ts @@ -70,10 +70,11 @@ export default class WhatsAppWebMessages extends WhatsAppWebBase { /** * Search WhatsApp messages with a given text string * @param txt the search string + * @param inJid the ID of the chat to search in, set to null to search all chats * @param count number of results to return - * @param page page number of results + * @param page page number of results (starts from 1) */ - async searchMessages(txt: string, count: number, page: number) { + async searchMessages(txt: string, inJid: string | null, count: number, page: number) { const json = [ 'query', { @@ -81,7 +82,8 @@ export default class WhatsAppWebMessages extends WhatsAppWebBase { type: 'search', search: txt, count: count.toString(), - page: page.toString() + page: page.toString(), + jid: inJid }, null, ] diff --git a/src/WAClient/Tests.ts b/src/WAClient/Tests.ts index 2c6b8c9..771cd19 100644 --- a/src/WAClient/Tests.ts +++ b/src/WAClient/Tests.ts @@ -131,9 +131,12 @@ WAClientTest('Misc', (client) => { await client.modifyChat (testJid, ChatModification.unmute, {stamp: mutedate}) }) it('should return search results', async () => { - const response = await client.searchMessages('Adh', 25, 0) - assert.ok (response.messages) - assert.ok (response.messages.length >= 0) + const jids = [null, testJid] + for (let i in jids) { + const response = await client.searchMessages('Hello', jids[i], 25, 1) + assert.ok (response.messages) + assert.ok (response.messages.length >= 0) + } }) }) WAClientTest('Groups', (client) => { diff --git a/src/WAConnection/Base.ts b/src/WAConnection/Base.ts index 7bd0b73..e51de6d 100644 --- a/src/WAConnection/Base.ts +++ b/src/WAConnection/Base.ts @@ -50,9 +50,7 @@ export default class WAConnectionBase { protected callbacks = {} protected encoder = new Encoder() protected decoder = new Decoder() - /** - * What to do when you need the phone to authenticate the connection (generate QR code by default) - */ + /** What to do when you need the phone to authenticate the connection (generate QR code by default) */ onReadyForPhoneAuthentication = generateQRCode unexpectedDisconnect = (err) => this.close() /** diff --git a/src/WAConnection/BrowserMessageDecoding.ts b/src/WAConnection/BrowserMessageDecoding.ts index 20b58de..2a0a953 100644 --- a/src/WAConnection/BrowserMessageDecoding.ts +++ b/src/WAConnection/BrowserMessageDecoding.ts @@ -22,7 +22,7 @@ const decrypt = buffer => { } json.messages.forEach ((str, i) => { - const buffer = Buffer.from (str, 'hex') + const buffer = Buffer.from (str, 'base64') try { const [tag, json, binaryTags] = decrypt (buffer) console.log (