Return messages with chats

This commit is contained in:
Adhiraj Singh
2020-07-08 14:10:48 +05:30
parent 9907f54bcd
commit 1c05c75cc8
3 changed files with 16 additions and 10 deletions

View File

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

View File

@@ -66,7 +66,7 @@ export default class WAConnectionConnector extends WAConnectionValidator {
let chats: Array<WAChat> = []
let contacts: Array<WAContact> = []
let unreadMessages: Array<WAMessage> = []
let unreadMap: Record<string, number> = {}
let chatMap: Record<string, {index: number, count: number}> = {}
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()
}

View File

@@ -59,6 +59,7 @@ export interface WAChat {
spam: 'false' | 'true'
jid: string
modify_tag: string
messages: WAMessage[]
}
export enum WAMetric {
liveLocation = 3,