Added within chat search + updated readme

This commit is contained in:
Adhiraj
2020-07-11 12:25:49 +05:30
parent 5f54a9443c
commit 142a92cc49
5 changed files with 40 additions and 11 deletions

View File

@@ -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.

View File

@@ -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,
]

View File

@@ -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) => {

View File

@@ -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()
/**

View File

@@ -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 (