web: add heartbeat and bounded reconnect tuning

This commit is contained in:
Peter Steinberger
2025-11-26 02:34:43 +01:00
parent e482e7768b
commit baf20af17f
19 changed files with 541 additions and 63 deletions

View File

@@ -1,7 +1,10 @@
import { randomUUID } from "node:crypto";
import type { AnyMessageContent } from "@whiskeysockets/baileys";
import { logVerbose } from "../globals.js";
import { logInfo } from "../logger.js";
import { getChildLogger } from "../logging.js";
import { toWhatsappJid } from "../utils.js";
import { loadWebMedia } from "./media.js";
import { createWaSocket, waitForWaConnection } from "./session.js";
@@ -11,9 +14,16 @@ export async function sendMessageWeb(
body: string,
options: { verbose: boolean; mediaUrl?: string },
): Promise<{ messageId: string; toJid: string }> {
const correlationId = randomUUID();
const sock = await createWaSocket(false, options.verbose);
const logger = getChildLogger({
module: "web-outbound",
correlationId,
to,
});
try {
logInfo("🔌 Connecting to WhatsApp Web…");
logger.info("connecting to whatsapp web");
await waitForWaConnection(sock);
// waitForWaConnection sets up listeners and error handling; keep the presence update safe.
const jid = toWhatsappJid(to);
@@ -34,11 +44,16 @@ export async function sendMessageWeb(
logInfo(
`📤 Sending via web session -> ${jid}${options.mediaUrl ? " (media)" : ""}`,
);
logger.info(
{ jid, hasMedia: Boolean(options.mediaUrl) },
"sending message",
);
const result = await sock.sendMessage(jid, payload);
const messageId = result?.key?.id ?? "unknown";
logInfo(
`✅ Sent via web session. Message ID: ${messageId} -> ${jid}${options.mediaUrl ? " (media)" : ""}`,
);
logger.info({ jid, messageId }, "sent message");
return { messageId, toJid: jid };
} finally {
try {