mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
typo: retreive -> retrieve (#371)
This commit is contained in:
@@ -435,7 +435,7 @@ await conn.toggleDisappearingMessages(jid, 0)
|
||||
You can also load the entire conversation history if you want
|
||||
``` ts
|
||||
await conn.loadAllMessages ("xyz@c.us", message => console.log("Loaded message with ID: " + message.key.id))
|
||||
console.log("queried all messages") // promise resolves once all messages are retreived
|
||||
console.log("queried all messages") // promise resolves once all messages are retrieved
|
||||
```
|
||||
- To get the status of some person
|
||||
``` ts
|
||||
|
||||
@@ -30,7 +30,7 @@ export const makeConnection = () => {
|
||||
return conn
|
||||
}
|
||||
|
||||
export async function sendAndRetreiveMessage(conn: WAConnection, content, type: MessageType, options: MessageOptions = {}, recipientJid = testJid) {
|
||||
export async function sendAndRetrieveMessage(conn: WAConnection, content, type: MessageType, options: MessageOptions = {}, recipientJid = testJid) {
|
||||
const response = await conn.sendMessage(recipientJid, content, type, options)
|
||||
const {messages} = await conn.loadMessages(recipientJid, 10)
|
||||
const message = messages.find (m => m.key.id === response.key.id)
|
||||
@@ -73,4 +73,4 @@ export const assertChatDBIntegrity = (conn: WAConnection) => {
|
||||
[]
|
||||
)
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { MessageType, GroupSettingChange, delay, ChatModification, whatsappID } from '../WAConnection'
|
||||
import * as assert from 'assert'
|
||||
import { WAConnectionTest, testJid, sendAndRetreiveMessage } from './Common'
|
||||
import { WAConnectionTest, testJid, sendAndRetrieveMessage } from './Common'
|
||||
|
||||
WAConnectionTest('Groups', (conn) => {
|
||||
let gid: string
|
||||
@@ -15,12 +15,12 @@ WAConnectionTest('Groups', (conn) => {
|
||||
|
||||
console.log('created group: ' + JSON.stringify(response))
|
||||
})
|
||||
it('should retreive group invite code', async () => {
|
||||
it('should retrieve group invite code', async () => {
|
||||
const code = await conn.groupInviteCode(gid)
|
||||
assert.ok(code)
|
||||
assert.strictEqual(typeof code, 'string')
|
||||
})
|
||||
it('should retreive group metadata', async () => {
|
||||
it('should retrieve group metadata', async () => {
|
||||
const metadata = await conn.groupMetadata(gid)
|
||||
assert.strictEqual(metadata.id, gid)
|
||||
assert.strictEqual(metadata.participants.filter((obj) => obj.jid.split('@')[0] === testJid.split('@')[0]).length, 1)
|
||||
@@ -49,10 +49,10 @@ WAConnectionTest('Groups', (conn) => {
|
||||
assert.strictEqual(metadata.desc, newDesc)
|
||||
})
|
||||
it('should send a message on the group', async () => {
|
||||
await sendAndRetreiveMessage(conn, 'Hello!', MessageType.text, {}, gid)
|
||||
await sendAndRetrieveMessage(conn, 'Hello!', MessageType.text, {}, gid)
|
||||
})
|
||||
it('should delete a message on the group', async () => {
|
||||
const message = await sendAndRetreiveMessage(conn, 'Hello!', MessageType.text, {}, gid)
|
||||
const message = await sendAndRetrieveMessage(conn, 'Hello!', MessageType.text, {}, gid)
|
||||
await delay(1500)
|
||||
await conn.deleteMessage(message.key)
|
||||
})
|
||||
@@ -185,4 +185,4 @@ WAConnectionTest('Groups', (conn) => {
|
||||
await conn.modifyChat(gid, 'delete')
|
||||
await waitForEvent
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
import { MessageType, Mimetype, delay, promiseTimeout, WA_MESSAGE_STATUS_TYPE, generateMessageID, WAMessage } from '../WAConnection'
|
||||
import { promises as fs } from 'fs'
|
||||
import * as assert from 'assert'
|
||||
import { WAConnectionTest, testJid, sendAndRetreiveMessage } from './Common'
|
||||
import { WAConnectionTest, testJid, sendAndRetrieveMessage } from './Common'
|
||||
|
||||
WAConnectionTest('Messages', conn => {
|
||||
|
||||
it('should send a text message', async () => {
|
||||
const message = await sendAndRetreiveMessage(conn, 'hello fren', MessageType.text)
|
||||
const message = await sendAndRetrieveMessage(conn, 'hello fren', MessageType.text)
|
||||
assert.strictEqual(message.message.conversation || message.message.extendedTextMessage?.text, 'hello fren')
|
||||
})
|
||||
it('should send a pending message', async () => {
|
||||
const message = await sendAndRetreiveMessage(conn, 'hello fren', MessageType.text, { waitForAck: false })
|
||||
const message = await sendAndRetrieveMessage(conn, 'hello fren', MessageType.text, { waitForAck: false })
|
||||
|
||||
await new Promise(resolve => conn.on('chat-update', update => {
|
||||
if (update.jid === testJid &&
|
||||
@@ -33,7 +33,7 @@ WAConnectionTest('Messages', conn => {
|
||||
})
|
||||
it('should send a link preview', async () => {
|
||||
const text = 'hello this is from https://www.github.com/adiwajshing/Baileys'
|
||||
const message = await sendAndRetreiveMessage(conn, text, MessageType.text, { detectLinks: true })
|
||||
const message = await sendAndRetrieveMessage(conn, text, MessageType.text, { detectLinks: true })
|
||||
const received = message.message.extendedTextMessage
|
||||
|
||||
assert.strictEqual(received.text, text)
|
||||
@@ -43,7 +43,7 @@ WAConnectionTest('Messages', conn => {
|
||||
})
|
||||
it('should quote a message', async () => {
|
||||
const quoted = (await conn.loadMessages(testJid, 2)).messages[0]
|
||||
const message = await sendAndRetreiveMessage(conn, 'hello fren 2', MessageType.extendedText, { quoted })
|
||||
const message = await sendAndRetrieveMessage(conn, 'hello fren 2', MessageType.extendedText, { quoted })
|
||||
assert.strictEqual(
|
||||
message.message.extendedTextMessage.contextInfo.stanzaId,
|
||||
quoted.key.id
|
||||
@@ -61,33 +61,33 @@ WAConnectionTest('Messages', conn => {
|
||||
}
|
||||
})
|
||||
it('should send a gif', async () => {
|
||||
const message = await sendAndRetreiveMessage(conn, { url: './Media/ma_gif.mp4' }, MessageType.video, { mimetype: Mimetype.gif })
|
||||
const message = await sendAndRetrieveMessage(conn, { url: './Media/ma_gif.mp4' }, MessageType.video, { mimetype: Mimetype.gif })
|
||||
|
||||
await conn.downloadAndSaveMediaMessage(message,'./Media/received_vid')
|
||||
})
|
||||
it('should send an audio', async () => {
|
||||
const content = await fs.readFile('./Media/sonata.mp3')
|
||||
const message = await sendAndRetreiveMessage(conn, content, MessageType.audio, { mimetype: Mimetype.mp4Audio })
|
||||
const message = await sendAndRetrieveMessage(conn, content, MessageType.audio, { mimetype: Mimetype.mp4Audio })
|
||||
// check duration was okay
|
||||
assert.ok (message.message.audioMessage.seconds > 0)
|
||||
await conn.downloadAndSaveMediaMessage(message,'./Media/received_aud')
|
||||
})
|
||||
it('should send an audio as a voice note', async () => {
|
||||
const content = await fs.readFile('./Media/sonata.mp3')
|
||||
const message = await sendAndRetreiveMessage(conn, content, MessageType.audio, { mimetype: Mimetype.mp4Audio, ptt: true })
|
||||
const message = await sendAndRetrieveMessage(conn, content, MessageType.audio, { mimetype: Mimetype.mp4Audio, ptt: true })
|
||||
|
||||
assert.ok (message.message.audioMessage.seconds > 0)
|
||||
assert.strictEqual (message.message?.audioMessage?.ptt, true)
|
||||
await conn.downloadAndSaveMediaMessage(message,'./Media/received_aud')
|
||||
})
|
||||
it('should send a jpeg image', async () => {
|
||||
const message = await sendAndRetreiveMessage(conn, { url: './Media/meme.jpeg' }, MessageType.image)
|
||||
const message = await sendAndRetrieveMessage(conn, { url: './Media/meme.jpeg' }, MessageType.image)
|
||||
assert.ok(message.message.imageMessage.jpegThumbnail.length > 0)
|
||||
const msg = await conn.downloadMediaMessage(message)
|
||||
assert.deepStrictEqual(msg, await fs.readFile('./Media/meme.jpeg'))
|
||||
})
|
||||
it('should send a remote jpeg image', async () => {
|
||||
const message = await sendAndRetreiveMessage(
|
||||
const message = await sendAndRetrieveMessage(
|
||||
conn,
|
||||
{ url: 'https://www.memestemplates.com/wp-content/uploads/2020/05/tom-with-phone.jpg' },
|
||||
MessageType.image
|
||||
@@ -97,13 +97,13 @@ WAConnectionTest('Messages', conn => {
|
||||
})
|
||||
it('should send a png image', async () => {
|
||||
const content = await fs.readFile('./Media/icon.png')
|
||||
const message = await sendAndRetreiveMessage(conn, content, MessageType.image, { mimetype: 'image/png' })
|
||||
const message = await sendAndRetrieveMessage(conn, content, MessageType.image, { mimetype: 'image/png' })
|
||||
assert.ok (message.message?.imageMessage?.jpegThumbnail)
|
||||
await conn.downloadMediaMessage(message)
|
||||
})
|
||||
it('should send a sticker', async () => {
|
||||
const content = await fs.readFile('./Media/octopus.webp')
|
||||
const message = await sendAndRetreiveMessage(conn, content, MessageType.sticker)
|
||||
const message = await sendAndRetrieveMessage(conn, content, MessageType.sticker)
|
||||
|
||||
await conn.downloadMediaMessage(message)
|
||||
})
|
||||
@@ -152,13 +152,13 @@ WAConnectionTest('Messages', conn => {
|
||||
it('should send an image & quote', async () => {
|
||||
const quoted = (await conn.loadMessages(testJid, 2)).messages[0]
|
||||
const content = await fs.readFile('./Media/meme.jpeg')
|
||||
const message = await sendAndRetreiveMessage(conn, content, MessageType.image, { quoted })
|
||||
const message = await sendAndRetrieveMessage(conn, content, MessageType.image, { quoted })
|
||||
|
||||
await conn.downloadMediaMessage(message) // check for successful decoding
|
||||
assert.strictEqual(message.message.imageMessage.contextInfo.stanzaId, quoted.key.id)
|
||||
})
|
||||
it('should send a message & delete it', async () => {
|
||||
const message = await sendAndRetreiveMessage(conn, 'hello fren', MessageType.text)
|
||||
const message = await sendAndRetrieveMessage(conn, 'hello fren', MessageType.text)
|
||||
await delay (2000)
|
||||
await conn.deleteMessage (testJid, message.key)
|
||||
})
|
||||
@@ -169,14 +169,14 @@ WAConnectionTest('Messages', conn => {
|
||||
})
|
||||
it('should send media after close', async () => {
|
||||
const content = await fs.readFile('./Media/octopus.webp')
|
||||
await sendAndRetreiveMessage(conn, content, MessageType.sticker)
|
||||
await sendAndRetrieveMessage(conn, content, MessageType.sticker)
|
||||
|
||||
conn.close ()
|
||||
|
||||
await conn.connect ()
|
||||
|
||||
const content2 = await fs.readFile('./Media/cat.jpeg')
|
||||
await sendAndRetreiveMessage(conn, content2, MessageType.image)
|
||||
await sendAndRetrieveMessage(conn, content2, MessageType.image)
|
||||
})
|
||||
it('should fail to send a text message', async () => {
|
||||
const JID = '1234-1234@g.us'
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Presence, ChatModification, delay, newMessagesDB, WA_DEFAULT_EPHEMERAL,
|
||||
import { promises as fs } from 'fs'
|
||||
import * as assert from 'assert'
|
||||
import got from 'got'
|
||||
import { WAConnectionTest, testJid, sendAndRetreiveMessage } from './Common'
|
||||
import { WAConnectionTest, testJid, sendAndRetrieveMessage } from './Common'
|
||||
|
||||
WAConnectionTest('Misc', conn => {
|
||||
|
||||
@@ -160,7 +160,7 @@ WAConnectionTest('Misc', conn => {
|
||||
assert.ok(idx < idx2) // should move further down the array
|
||||
|
||||
await delay (2000)
|
||||
await sendAndRetreiveMessage(conn, 'test', MessageType.text)
|
||||
await sendAndRetrieveMessage(conn, 'test', MessageType.text)
|
||||
// should be unarchived
|
||||
const idx3 = conn.chats.all().findIndex(chat => chat.jid === testJid)
|
||||
assert.strictEqual(idx, idx3) // should be back there
|
||||
@@ -359,7 +359,7 @@ WAConnectionTest('Misc', conn => {
|
||||
|
||||
await delay(1000)
|
||||
|
||||
let msg = await sendAndRetreiveMessage(
|
||||
let msg = await sendAndRetrieveMessage(
|
||||
conn,
|
||||
'This will go poof 😱',
|
||||
MessageType.text
|
||||
@@ -378,7 +378,7 @@ WAConnectionTest('Misc', conn => {
|
||||
|
||||
await delay(1000)
|
||||
|
||||
msg = await sendAndRetreiveMessage(
|
||||
msg = await sendAndRetrieveMessage(
|
||||
conn,
|
||||
'This will not go poof 😔',
|
||||
MessageType.text
|
||||
@@ -427,4 +427,4 @@ WAConnectionTest('Misc', conn => {
|
||||
assert.strictEqual(error.status, 422)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -99,7 +99,7 @@ export class WAConnection extends Base {
|
||||
* Load the conversation with a group or person
|
||||
* @param count the number of messages to load
|
||||
* @param cursor the data for which message to offset the query by
|
||||
* @param mostRecentFirst retreive the most recent message first or retreive from the converation start
|
||||
* @param mostRecentFirst retrieve the most recent message first or retrieve from the converation start
|
||||
*/
|
||||
@Mutex (jid => jid)
|
||||
async loadMessages (
|
||||
@@ -110,7 +110,7 @@ export class WAConnection extends Base {
|
||||
) {
|
||||
jid = whatsappID(jid)
|
||||
|
||||
const retreive = (count: number, indexMessage: any) => this.fetchMessagesFromWA (jid, count, indexMessage, mostRecentFirst)
|
||||
const retrieve = (count: number, indexMessage: any) => this.fetchMessagesFromWA (jid, count, indexMessage, mostRecentFirst)
|
||||
|
||||
const chat = this.chats.get (jid)
|
||||
const hasCursor = cursor?.id && typeof cursor?.fromMe !== 'undefined'
|
||||
@@ -126,7 +126,7 @@ export class WAConnection extends Base {
|
||||
} else if (diff > 0) {
|
||||
const fMessage = chat.messages.all()[0]
|
||||
let fepoch = (fMessage && fMessage['epoch']) || 0
|
||||
const extra = await retreive (diff, messages[0]?.key || cursor)
|
||||
const extra = await retrieve (diff, messages[0]?.key || cursor)
|
||||
// add to DB
|
||||
for (let i = extra.length-1;i >= 0; i--) {
|
||||
const m = extra[i]
|
||||
@@ -139,7 +139,7 @@ export class WAConnection extends Base {
|
||||
}
|
||||
messages.unshift (...extra)
|
||||
}
|
||||
} else messages = await retreive (count, cursor)
|
||||
} else messages = await retrieve (count, cursor)
|
||||
|
||||
if (messages[0]) cursor = { id: messages[0].key.id, fromMe: messages[0].key.fromMe }
|
||||
else cursor = null
|
||||
@@ -148,9 +148,9 @@ export class WAConnection extends Base {
|
||||
}
|
||||
/**
|
||||
* Load the entire friggin conversation with a group or person
|
||||
* @param onMessage callback for every message retreived
|
||||
* @param onMessage callback for every message retrieved
|
||||
* @param chunkSize the number of messages to load in a single request
|
||||
* @param mostRecentFirst retreive the most recent message first or retreive from the converation start
|
||||
* @param mostRecentFirst retrieve the most recent message first or retrieve from the converation start
|
||||
*/
|
||||
loadAllMessages(jid: string, onMessage: (m: WAMessage) => Promise<void>|void, chunkSize = 25, mostRecentFirst = true) {
|
||||
let offsetID = null
|
||||
@@ -183,7 +183,7 @@ export class WAConnection extends Base {
|
||||
/**
|
||||
* Find a message in a given conversation
|
||||
* @param chunkSize the number of messages to load in a single request
|
||||
* @param onMessage callback for every message retreived, if return true -- the loop will break
|
||||
* @param onMessage callback for every message retrieved, if return true -- the loop will break
|
||||
*/
|
||||
async findMessage (jid: string, chunkSize: number, onMessage: (m: WAMessage) => boolean) {
|
||||
const chat = this.chats.get (whatsappID(jid))
|
||||
|
||||
Reference in New Issue
Block a user