chore: update example

This commit is contained in:
Adhiraj Singh
2023-03-02 22:44:15 +05:30
parent 9d8aa67f22
commit 893fb61f05

View File

@@ -1,6 +1,6 @@
import { Boom } from '@hapi/boom'
import NodeCache from 'node-cache'
import makeWASocket, { AnyMessageContent, delay, DisconnectReason, fetchLatestBaileysVersion, makeCacheableSignalKeyStore, makeInMemoryStore, useMultiFileAuthState } from '../src'
import makeWASocket, { AnyMessageContent, delay, DisconnectReason, fetchLatestBaileysVersion, getAggregateVotesInPollMessage, makeCacheableSignalKeyStore, makeInMemoryStore, proto, useMultiFileAuthState, WAMessageContent, WAMessageKey } from '../src'
import MAIN_LOGGER from '../src/Utils/logger'
const logger = MAIN_LOGGER.child({ })
@@ -43,18 +43,8 @@ const startSock = async() => {
// ignore all broadcast messages -- to receive the same
// comment the line below out
// shouldIgnoreJid: jid => isJidBroadcast(jid),
// implement to handle retries
getMessage: async key => {
if(store) {
const msg = await store.loadMessage(key.remoteJid!, key.id!)
return msg?.message || undefined
}
// only if store is present
return {
conversation: 'hello'
}
}
// implement to handle retries & poll updates
getMessage,
})
store?.bind(sock.ev)
@@ -126,7 +116,24 @@ const startSock = async() => {
// messages updated like status delivered, message deleted etc.
if(events['messages.update']) {
console.log(events['messages.update'])
console.log(
JSON.stringify(events['messages.update'], undefined, 2)
)
for(const { key, update } of events['messages.update']) {
if(update.pollUpdates) {
const pollCreation = await getMessage(key)
if(pollCreation) {
console.log(
'got poll update, aggregation: ',
getAggregateVotesInPollMessage({
message: pollCreation,
pollUpdates: update.pollUpdates,
})
)
}
}
}
}
if(events['message-receipt.update']) {
@@ -165,6 +172,16 @@ const startSock = async() => {
)
return sock
async function getMessage(key: WAMessageKey): Promise<WAMessageContent | undefined> {
if(store) {
const msg = await store.loadMessage(key.remoteJid!, key.id!)
return msg?.message || undefined
}
// only if store is present
return proto.Message.fromObject({})
}
}
startSock()