Skip to content

Commit

Permalink
wbot.ts: recover message from store to retry sending
Browse files Browse the repository at this point in the history
  • Loading branch information
allgood committed Jul 6, 2024
1 parent 148dd84 commit b232bc5
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions backend/src/libs/wbot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,16 @@ import makeWASocket, {
makeCacheableSignalKeyStore,
makeInMemoryStore,
isJidBroadcast,
CacheStore
CacheStore,
WAMessageKey,
WAMessageContent,
proto
} from "@whiskeysockets/baileys";

import { Boom } from "@hapi/boom";
import MAIN_LOGGER from "@whiskeysockets/baileys/lib/Utils/logger";
import NodeCache from "node-cache";
import { Op } from "sequelize";
import Whatsapp from "../models/Whatsapp";
import { logger } from "../utils/logger";
import authState from "../helpers/authState";
Expand All @@ -23,7 +27,6 @@ import { StartWhatsAppSession } from "../services/WbotServices/StartWhatsAppSess
import DeleteBaileysService from "../services/BaileysServices/DeleteBaileysService";
import Contact from "../models/Contact";
import Ticket from "../models/Ticket";
import { Op } from "sequelize";

const loggerBaileys = MAIN_LOGGER.child({});
loggerBaileys.level = "error";
Expand Down Expand Up @@ -92,6 +95,22 @@ export const initWASocket = async (whatsapp: Whatsapp): Promise<Session> => {
logger: loggerBaileys
});

async function getMessage(
key: WAMessageKey
): Promise<WAMessageContent | undefined> {
if (store) {
const msg = await store.loadMessage(key.remoteJid!, key.id!);
logger.debug(
{ key, message: JSON.stringify(msg) },
`[wbot.ts] getMessage: result of recovering message ${key.remoteJid} ${key.id}`
);
return msg?.message || undefined;
}

// only if store isn't present
return proto.Message.fromObject({});
}

const { state, saveState } = await authState(whatsapp);

const msgRetryCounterCache = new NodeCache();
Expand All @@ -113,6 +132,7 @@ export const initWASocket = async (whatsapp: Whatsapp): Promise<Session> => {
// syncFullHistory: true,
generateHighQualityLinkPreview: true,
userDevicesCache,
getMessage,
shouldIgnoreJid: jid =>
isJidBroadcast(jid) || jid?.endsWith("@newsletter"),
transactionOpts: { maxCommitRetries: 1, delayBetweenTriesMs: 10 }
Expand Down

0 comments on commit b232bc5

Please sign in to comment.