mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Return messages with chats
This commit is contained in:
12
README.md
12
README.md
@@ -16,12 +16,6 @@ To run the example script, download or clone the repo and then type the followin
|
|||||||
2. ``` npm install ```
|
2. ``` npm install ```
|
||||||
3. ``` npm run example ```
|
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
|
## Install
|
||||||
Create and cd to your NPM project directory and then in terminal, write:
|
Create and cd to your NPM project directory and then in terminal, write:
|
||||||
1. stable: `npm install @adiwajshing/baileys`
|
1. stable: `npm install @adiwajshing/baileys`
|
||||||
@@ -32,6 +26,12 @@ Then import in your code using:
|
|||||||
import { WAClient } from '@adiwajshing/baileys'
|
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
|
## Connecting
|
||||||
``` ts
|
``` ts
|
||||||
const client = new WAClient()
|
const client = new WAClient()
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ export default class WAConnectionConnector extends WAConnectionValidator {
|
|||||||
let chats: Array<WAChat> = []
|
let chats: Array<WAChat> = []
|
||||||
let contacts: Array<WAContact> = []
|
let contacts: Array<WAContact> = []
|
||||||
let unreadMessages: Array<WAMessage> = []
|
let unreadMessages: Array<WAMessage> = []
|
||||||
let unreadMap: Record<string, number> = {}
|
let chatMap: Record<string, {index: number, count: number}> = {}
|
||||||
|
|
||||||
let receivedContacts = false
|
let receivedContacts = false
|
||||||
let receivedMessages = false
|
let receivedMessages = false
|
||||||
@@ -90,10 +90,14 @@ export default class WAConnectionConnector extends WAConnectionValidator {
|
|||||||
for (let k = json.length - 1; k >= 0; k--) {
|
for (let k = json.length - 1; k >= 0; k--) {
|
||||||
const message = json[k][2]
|
const message = json[k][2]
|
||||||
const jid = message.key.remoteJid.replace('@s.whatsapp.net', '@c.us')
|
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
|
// only forward if the message is from the sender
|
||||||
unreadMessages.push(message)
|
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'])
|
const json = await this.registerCallbackOneTime(['response', 'type:chat'])
|
||||||
json[2].forEach(chat => {
|
json[2].forEach(chat => {
|
||||||
chats.push(chat[1]) // chats data (log json to see what it looks like)
|
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
|
// 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()
|
if (chats.length > 0) return waitForConvos()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,6 +59,7 @@ export interface WAChat {
|
|||||||
spam: 'false' | 'true'
|
spam: 'false' | 'true'
|
||||||
jid: string
|
jid: string
|
||||||
modify_tag: string
|
modify_tag: string
|
||||||
|
messages: WAMessage[]
|
||||||
}
|
}
|
||||||
export enum WAMetric {
|
export enum WAMetric {
|
||||||
liveLocation = 3,
|
liveLocation = 3,
|
||||||
|
|||||||
Reference in New Issue
Block a user