From 893fb61f05f89fc95ba84485d4f93cdc875dc16c Mon Sep 17 00:00:00 2001 From: Adhiraj Singh Date: Thu, 2 Mar 2023 22:44:15 +0530 Subject: [PATCH] chore: update example --- Example/example.ts | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/Example/example.ts b/Example/example.ts index dcb6eff..eb6fd15 100644 --- a/Example/example.ts +++ b/Example/example.ts @@ -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 { + 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() \ No newline at end of file