diff --git a/README.md b/README.md index 5fa22e2..6e35b84 100644 --- a/README.md +++ b/README.md @@ -16,12 +16,6 @@ To run the example script, download or clone the repo and then type the followin 2. ``` npm install ``` 3. ``` npm run example ``` -## Unit Tests -Baileys also comes with a unit test suite. Simply cd into the Baileys directory & run `npm test`. - -You will require a phone with WhatsApp to test, and a second WhatsApp number to send messages to. -Set the phone number you can randomly send messages to in a `.env` file with `TEST_JID=1234@s.whatsapp.net` - ## Install Create and cd to your NPM project directory and then in terminal, write: 1. stable: `npm install @adiwajshing/baileys` @@ -32,6 +26,12 @@ Then import in your code using: import { WAClient } from '@adiwajshing/baileys' ``` +## Unit Tests +Baileys also comes with a unit test suite. Simply cd into the Baileys directory & run `npm test`. + +You will require a phone with WhatsApp to test, and a second WhatsApp number to send messages to. +Set the phone number you can randomly send messages to in a `.env` file with `TEST_JID=1234@s.whatsapp.net` + ## Connecting ``` ts const client = new WAClient() diff --git a/src/WAConnection/Connect.ts b/src/WAConnection/Connect.ts index c1155ad..895bfda 100644 --- a/src/WAConnection/Connect.ts +++ b/src/WAConnection/Connect.ts @@ -66,7 +66,7 @@ export default class WAConnectionConnector extends WAConnectionValidator { let chats: Array = [] let contacts: Array = [] let unreadMessages: Array = [] - let unreadMap: Record = {} + let chatMap: Record = {} let receivedContacts = false let receivedMessages = false @@ -90,10 +90,14 @@ export default class WAConnectionConnector extends WAConnectionValidator { for (let k = json.length - 1; k >= 0; k--) { const message = json[k][2] const jid = message.key.remoteJid.replace('@s.whatsapp.net', '@c.us') - if (!message.key.fromMe && unreadMap[jid] > 0) { + const index = chatMap[jid] + if (!message.key.fromMe && index && index.count > 0) { // only forward if the message is from the sender unreadMessages.push(message) - unreadMap[jid] -= 1 // reduce + index.count -= 1 // reduce + } + if (index) { + chats[index.index].messages.push (message) } } } @@ -110,8 +114,9 @@ export default class WAConnectionConnector extends WAConnectionValidator { const json = await this.registerCallbackOneTime(['response', 'type:chat']) json[2].forEach(chat => { chats.push(chat[1]) // chats data (log json to see what it looks like) + chats[chats.length-1].messages = [] // store the number of unread messages for each sender - unreadMap[chat[1].jid] = chat[1].count + chatMap[chat[1].jid] = {index: chats.length-1, count: chat[1].count} //chat[1].count }) if (chats.length > 0) return waitForConvos() } diff --git a/src/WAConnection/Constants.ts b/src/WAConnection/Constants.ts index 1f17062..ebd009c 100644 --- a/src/WAConnection/Constants.ts +++ b/src/WAConnection/Constants.ts @@ -59,6 +59,7 @@ export interface WAChat { spam: 'false' | 'true' jid: string modify_tag: string + messages: WAMessage[] } export enum WAMetric { liveLocation = 3,