web: add heartbeat and bounded reconnect tuning
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user