@gakr-gakr/whatsapp 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/action-runtime-api.ts +1 -0
- package/action-runtime.runtime.ts +1 -0
- package/api.ts +67 -0
- package/auth-presence.ts +80 -0
- package/autobot.plugin.json +23 -0
- package/channel-config-api.ts +1 -0
- package/channel-plugin-api.ts +3 -0
- package/config-api.ts +4 -0
- package/constants.ts +1 -0
- package/contract-api.ts +29 -0
- package/directory-contract-api.ts +4 -0
- package/doctor-contract-api.ts +8 -0
- package/index.ts +16 -0
- package/legacy-session-surface-api.ts +6 -0
- package/legacy-state-migrations-api.ts +1 -0
- package/light-runtime-api.ts +12 -0
- package/login-qr-api.ts +1 -0
- package/login-qr-runtime.ts +23 -0
- package/outbound-payload-test-api.ts +1 -0
- package/package.json +76 -0
- package/runtime-api.ts +84 -0
- package/secret-contract-api.ts +4 -0
- package/security-contract-api.ts +4 -0
- package/setup-entry.ts +21 -0
- package/setup-plugin-api.ts +3 -0
- package/src/account-config.ts +77 -0
- package/src/account-ids.ts +17 -0
- package/src/account-types.ts +5 -0
- package/src/accounts.ts +176 -0
- package/src/action-runtime-target-auth.ts +27 -0
- package/src/action-runtime.ts +76 -0
- package/src/active-listener.ts +17 -0
- package/src/agent-tools-login.ts +113 -0
- package/src/approval-auth.ts +27 -0
- package/src/auth-store.runtime.ts +1 -0
- package/src/auth-store.ts +494 -0
- package/src/auto-reply/config.runtime.ts +16 -0
- package/src/auto-reply/constants.ts +1 -0
- package/src/auto-reply/deliver-reply.ts +332 -0
- package/src/auto-reply/loggers.ts +6 -0
- package/src/auto-reply/mentions.ts +131 -0
- package/src/auto-reply/monitor/ack-reaction.ts +99 -0
- package/src/auto-reply/monitor/audio-preflight.runtime.ts +9 -0
- package/src/auto-reply/monitor/broadcast.ts +153 -0
- package/src/auto-reply/monitor/commands.ts +19 -0
- package/src/auto-reply/monitor/echo.ts +64 -0
- package/src/auto-reply/monitor/group-activation.runtime.ts +1 -0
- package/src/auto-reply/monitor/group-activation.ts +73 -0
- package/src/auto-reply/monitor/group-gating.runtime.ts +8 -0
- package/src/auto-reply/monitor/group-gating.ts +218 -0
- package/src/auto-reply/monitor/group-members.ts +65 -0
- package/src/auto-reply/monitor/inbound-context.ts +92 -0
- package/src/auto-reply/monitor/inbound-dispatch.runtime.ts +22 -0
- package/src/auto-reply/monitor/inbound-dispatch.ts +749 -0
- package/src/auto-reply/monitor/last-route.ts +61 -0
- package/src/auto-reply/monitor/listener-log.ts +28 -0
- package/src/auto-reply/monitor/message-line.runtime.ts +38 -0
- package/src/auto-reply/monitor/message-line.ts +54 -0
- package/src/auto-reply/monitor/on-message.ts +333 -0
- package/src/auto-reply/monitor/peer.ts +17 -0
- package/src/auto-reply/monitor/process-message.ts +584 -0
- package/src/auto-reply/monitor/runtime-api.ts +36 -0
- package/src/auto-reply/monitor/status-reaction.ts +108 -0
- package/src/auto-reply/monitor-state.ts +114 -0
- package/src/auto-reply/monitor.ts +720 -0
- package/src/auto-reply/reply-resolver.runtime.ts +1 -0
- package/src/auto-reply/types.ts +48 -0
- package/src/auto-reply/util.ts +62 -0
- package/src/auto-reply.impl.ts +6 -0
- package/src/auto-reply.ts +1 -0
- package/src/channel-actions.runtime.ts +7 -0
- package/src/channel-actions.ts +85 -0
- package/src/channel-outbound.ts +87 -0
- package/src/channel-react-action.runtime.ts +10 -0
- package/src/channel-react-action.ts +247 -0
- package/src/channel.runtime.ts +117 -0
- package/src/channel.setup.ts +32 -0
- package/src/channel.ts +356 -0
- package/src/command-policy.ts +7 -0
- package/src/config-accessors.ts +22 -0
- package/src/config-schema.ts +6 -0
- package/src/config-ui-hints.ts +24 -0
- package/src/connection-controller-registry.ts +49 -0
- package/src/connection-controller.ts +680 -0
- package/src/creds-files.ts +19 -0
- package/src/creds-persistence.ts +71 -0
- package/src/directory-config.ts +40 -0
- package/src/doctor-contract.ts +11 -0
- package/src/doctor.ts +56 -0
- package/src/document-filename.ts +17 -0
- package/src/group-intro.ts +15 -0
- package/src/group-policy.ts +40 -0
- package/src/group-session-contract.ts +20 -0
- package/src/group-session-key.ts +42 -0
- package/src/heartbeat.ts +34 -0
- package/src/identity.ts +164 -0
- package/src/inbound/access-control.ts +187 -0
- package/src/inbound/dedupe.ts +132 -0
- package/src/inbound/extract.ts +484 -0
- package/src/inbound/lifecycle.ts +39 -0
- package/src/inbound/media.ts +128 -0
- package/src/inbound/monitor.ts +1042 -0
- package/src/inbound/outbound-mentions.ts +260 -0
- package/src/inbound/runtime-api.ts +7 -0
- package/src/inbound/save-media.runtime.ts +1 -0
- package/src/inbound/send-api.ts +203 -0
- package/src/inbound/send-result.ts +109 -0
- package/src/inbound/types.ts +107 -0
- package/src/inbound-policy.ts +215 -0
- package/src/inbound.ts +9 -0
- package/src/login-qr.ts +542 -0
- package/src/login.ts +83 -0
- package/src/media.ts +10 -0
- package/src/monitor-inbox.allows-messages-from-senders-allowfrom-list.test-support.ts +417 -0
- package/src/monitor-inbox.append-upsert.test-support.ts +133 -0
- package/src/monitor-inbox.blocks-messages-from-unauthorized-senders-not-allowfrom.test-support.ts +418 -0
- package/src/monitor-inbox.captures-media-path-image-messages.test-support.ts +308 -0
- package/src/monitor-inbox.streams-inbound-messages.test-support.ts +824 -0
- package/src/normalize-target.ts +148 -0
- package/src/normalize.ts +8 -0
- package/src/outbound-adapter.ts +36 -0
- package/src/outbound-base.ts +256 -0
- package/src/outbound-media-contract.ts +307 -0
- package/src/outbound-media.runtime.ts +41 -0
- package/src/outbound-send-deps.ts +1 -0
- package/src/outbound-test-support.ts +16 -0
- package/src/qa-driver.runtime.ts +189 -0
- package/src/qr-image.ts +1 -0
- package/src/qr-terminal.ts +1 -0
- package/src/quoted-message.ts +184 -0
- package/src/reaction-level.ts +24 -0
- package/src/reconnect.ts +55 -0
- package/src/resolve-outbound-target.ts +58 -0
- package/src/runtime-api.ts +59 -0
- package/src/runtime-group-policy.ts +16 -0
- package/src/runtime.ts +9 -0
- package/src/security-contract.ts +47 -0
- package/src/security-fix.ts +71 -0
- package/src/send.ts +342 -0
- package/src/session-contract.ts +43 -0
- package/src/session-errors.ts +125 -0
- package/src/session-route.ts +32 -0
- package/src/session.runtime.ts +8 -0
- package/src/session.ts +327 -0
- package/src/setup-core.ts +52 -0
- package/src/setup-finalize.ts +450 -0
- package/src/setup-surface.ts +71 -0
- package/src/setup-test-helpers.ts +217 -0
- package/src/shared.ts +291 -0
- package/src/socket-timing.ts +38 -0
- package/src/state-migrations.ts +55 -0
- package/src/status-issues.ts +185 -0
- package/src/system-prompt.ts +31 -0
- package/src/targets-runtime.ts +221 -0
- package/src/text-runtime.ts +18 -0
- package/src/vcard.ts +84 -0
- package/targets.ts +5 -0
- package/test-api.ts +2 -0
- package/tsconfig.json +16 -0
package/src/send.ts
ADDED
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
import { formatCliCommand } from "autobot/plugin-sdk/cli-runtime";
|
|
2
|
+
import type { AutoBotConfig } from "autobot/plugin-sdk/config-contracts";
|
|
3
|
+
import { generateSecureUuid } from "autobot/plugin-sdk/core";
|
|
4
|
+
import { redactIdentifier } from "autobot/plugin-sdk/logging-core";
|
|
5
|
+
import {
|
|
6
|
+
convertMarkdownTables,
|
|
7
|
+
resolveMarkdownTableMode,
|
|
8
|
+
} from "autobot/plugin-sdk/markdown-table-runtime";
|
|
9
|
+
import { requireRuntimeConfig } from "autobot/plugin-sdk/plugin-config-runtime";
|
|
10
|
+
import { normalizePollInput, type PollInput } from "autobot/plugin-sdk/poll-runtime";
|
|
11
|
+
import { createSubsystemLogger, getChildLogger } from "autobot/plugin-sdk/runtime-env";
|
|
12
|
+
import {
|
|
13
|
+
resolveDefaultWhatsAppAccountId,
|
|
14
|
+
resolveWhatsAppAccount,
|
|
15
|
+
resolveWhatsAppMediaMaxBytes,
|
|
16
|
+
} from "./accounts.js";
|
|
17
|
+
import { getRegisteredWhatsAppConnectionController } from "./connection-controller-registry.js";
|
|
18
|
+
import { resolveWhatsAppDocumentFileName } from "./document-filename.js";
|
|
19
|
+
import type { ActiveWebListener, ActiveWebSendOptions } from "./inbound/types.js";
|
|
20
|
+
import { isWhatsAppNewsletterJid } from "./normalize.js";
|
|
21
|
+
import {
|
|
22
|
+
normalizeWhatsAppPayloadText,
|
|
23
|
+
prepareWhatsAppOutboundMedia,
|
|
24
|
+
resolveWhatsAppOutboundMediaUrls,
|
|
25
|
+
} from "./outbound-media-contract.js";
|
|
26
|
+
import { loadOutboundMediaFromUrl } from "./outbound-media.runtime.js";
|
|
27
|
+
import { markdownToWhatsApp, toWhatsappJid } from "./text-runtime.js";
|
|
28
|
+
|
|
29
|
+
const outboundLog = createSubsystemLogger("gateway/channels/whatsapp").child("outbound");
|
|
30
|
+
|
|
31
|
+
function supportsForcedDocumentDelivery(kind: "image" | "audio" | "video" | "document"): boolean {
|
|
32
|
+
return kind === "image" || kind === "video";
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function resolveOutboundWhatsAppAccountId(params: {
|
|
36
|
+
cfg: AutoBotConfig;
|
|
37
|
+
accountId?: string;
|
|
38
|
+
}): string | undefined {
|
|
39
|
+
const explicitAccountId = params.accountId?.trim();
|
|
40
|
+
if (explicitAccountId) {
|
|
41
|
+
return explicitAccountId;
|
|
42
|
+
}
|
|
43
|
+
return resolveDefaultWhatsAppAccountId(params.cfg);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function requireOutboundActiveWebListener(params: { cfg: AutoBotConfig; accountId?: string }): {
|
|
47
|
+
accountId: string;
|
|
48
|
+
listener: ActiveWebListener;
|
|
49
|
+
} {
|
|
50
|
+
const accountId = resolveOutboundWhatsAppAccountId(params);
|
|
51
|
+
const resolvedAccountId = accountId ?? resolveDefaultWhatsAppAccountId(params.cfg);
|
|
52
|
+
const listener =
|
|
53
|
+
getRegisteredWhatsAppConnectionController(resolvedAccountId)?.getActiveListener() ?? null;
|
|
54
|
+
if (!listener) {
|
|
55
|
+
throw new Error(
|
|
56
|
+
`No active WhatsApp Web listener (account: ${resolvedAccountId}). Start the gateway, then link WhatsApp with: ${formatCliCommand(`autobot channels login --channel whatsapp --account ${resolvedAccountId}`)}.`,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
return { accountId: resolvedAccountId, listener };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export async function sendMessageWhatsApp(
|
|
63
|
+
to: string,
|
|
64
|
+
body: string,
|
|
65
|
+
options: {
|
|
66
|
+
verbose: boolean;
|
|
67
|
+
cfg: AutoBotConfig;
|
|
68
|
+
mediaUrl?: string;
|
|
69
|
+
mediaUrls?: readonly string[];
|
|
70
|
+
mediaAccess?: {
|
|
71
|
+
localRoots?: readonly string[];
|
|
72
|
+
readFile?: (filePath: string) => Promise<Buffer>;
|
|
73
|
+
};
|
|
74
|
+
mediaLocalRoots?: readonly string[];
|
|
75
|
+
mediaReadFile?: (filePath: string) => Promise<Buffer>;
|
|
76
|
+
mediaPayload?: {
|
|
77
|
+
buffer: Buffer;
|
|
78
|
+
contentType?: string;
|
|
79
|
+
kind?: "image" | "audio" | "video" | "document";
|
|
80
|
+
fileName?: string;
|
|
81
|
+
};
|
|
82
|
+
gifPlayback?: boolean;
|
|
83
|
+
audioAsVoice?: boolean;
|
|
84
|
+
forceDocument?: boolean;
|
|
85
|
+
accountId?: string;
|
|
86
|
+
quotedMessageKey?: {
|
|
87
|
+
id: string;
|
|
88
|
+
remoteJid: string;
|
|
89
|
+
fromMe: boolean;
|
|
90
|
+
participant?: string;
|
|
91
|
+
messageText?: string;
|
|
92
|
+
};
|
|
93
|
+
preserveLeadingWhitespace?: boolean;
|
|
94
|
+
},
|
|
95
|
+
): Promise<{ messageId: string; toJid: string }> {
|
|
96
|
+
let text = options.preserveLeadingWhitespace ? body : normalizeWhatsAppPayloadText(body);
|
|
97
|
+
const jid = toWhatsappJid(to);
|
|
98
|
+
const mediaUrls = resolveWhatsAppOutboundMediaUrls(options);
|
|
99
|
+
const mediaPayload = options.mediaPayload;
|
|
100
|
+
const primaryMediaUrl = mediaUrls[0] ?? mediaPayload?.fileName;
|
|
101
|
+
const hasMedia = Boolean(mediaPayload || primaryMediaUrl);
|
|
102
|
+
if (!text && !hasMedia) {
|
|
103
|
+
return { messageId: "", toJid: jid };
|
|
104
|
+
}
|
|
105
|
+
const correlationId = generateSecureUuid();
|
|
106
|
+
const startedAt = Date.now();
|
|
107
|
+
const cfg = requireRuntimeConfig(options.cfg, "WhatsApp send");
|
|
108
|
+
const { listener: active, accountId: resolvedAccountId } = requireOutboundActiveWebListener({
|
|
109
|
+
cfg,
|
|
110
|
+
accountId: options.accountId,
|
|
111
|
+
});
|
|
112
|
+
const account = resolveWhatsAppAccount({
|
|
113
|
+
cfg,
|
|
114
|
+
accountId: resolvedAccountId ?? options.accountId,
|
|
115
|
+
});
|
|
116
|
+
const tableMode = resolveMarkdownTableMode({
|
|
117
|
+
cfg,
|
|
118
|
+
channel: "whatsapp",
|
|
119
|
+
accountId: resolvedAccountId ?? options.accountId,
|
|
120
|
+
});
|
|
121
|
+
text = convertMarkdownTables(text ?? "", tableMode);
|
|
122
|
+
text = markdownToWhatsApp(text);
|
|
123
|
+
const redactedTo = redactIdentifier(to);
|
|
124
|
+
const logger = getChildLogger({
|
|
125
|
+
module: "web-outbound",
|
|
126
|
+
correlationId,
|
|
127
|
+
to: redactedTo,
|
|
128
|
+
});
|
|
129
|
+
try {
|
|
130
|
+
const redactedJid = redactIdentifier(jid);
|
|
131
|
+
let mediaBuffer: Buffer | undefined;
|
|
132
|
+
let mediaType: string | undefined;
|
|
133
|
+
let documentFileName: string | undefined;
|
|
134
|
+
let visibleTextAfterVoice: string | undefined;
|
|
135
|
+
let forceDocumentDelivery = false;
|
|
136
|
+
if (mediaPayload) {
|
|
137
|
+
const media = await prepareWhatsAppOutboundMedia(mediaPayload, primaryMediaUrl);
|
|
138
|
+
const caption = text || undefined;
|
|
139
|
+
mediaBuffer = media.buffer;
|
|
140
|
+
mediaType = media.mimetype;
|
|
141
|
+
forceDocumentDelivery = Boolean(
|
|
142
|
+
options.forceDocument && supportsForcedDocumentDelivery(media.kind),
|
|
143
|
+
);
|
|
144
|
+
if (media.kind === "audio" && caption) {
|
|
145
|
+
visibleTextAfterVoice = caption;
|
|
146
|
+
text = "";
|
|
147
|
+
} else if (media.kind === "document") {
|
|
148
|
+
text = caption ?? "";
|
|
149
|
+
documentFileName = media.fileName;
|
|
150
|
+
} else {
|
|
151
|
+
text = caption ?? "";
|
|
152
|
+
}
|
|
153
|
+
if (forceDocumentDelivery) {
|
|
154
|
+
documentFileName ??= resolveWhatsAppDocumentFileName({
|
|
155
|
+
fileName: media.fileName,
|
|
156
|
+
mimetype: media.mimetype,
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
} else if (primaryMediaUrl) {
|
|
160
|
+
const media = await prepareWhatsAppOutboundMedia(
|
|
161
|
+
await loadOutboundMediaFromUrl(primaryMediaUrl, {
|
|
162
|
+
maxBytes: resolveWhatsAppMediaMaxBytes(account),
|
|
163
|
+
optimizeImages: options.forceDocument ? false : undefined,
|
|
164
|
+
mediaAccess: options.mediaAccess,
|
|
165
|
+
mediaLocalRoots: options.mediaLocalRoots,
|
|
166
|
+
mediaReadFile: options.mediaReadFile,
|
|
167
|
+
}),
|
|
168
|
+
primaryMediaUrl,
|
|
169
|
+
);
|
|
170
|
+
const caption = text || undefined;
|
|
171
|
+
mediaBuffer = media.buffer;
|
|
172
|
+
mediaType = media.mimetype;
|
|
173
|
+
forceDocumentDelivery = Boolean(
|
|
174
|
+
options.forceDocument && supportsForcedDocumentDelivery(media.kind),
|
|
175
|
+
);
|
|
176
|
+
if (media.kind === "audio" && caption) {
|
|
177
|
+
visibleTextAfterVoice = caption;
|
|
178
|
+
text = "";
|
|
179
|
+
} else if (media.kind === "document") {
|
|
180
|
+
text = caption ?? "";
|
|
181
|
+
documentFileName = media.fileName;
|
|
182
|
+
} else {
|
|
183
|
+
text = caption ?? "";
|
|
184
|
+
}
|
|
185
|
+
if (forceDocumentDelivery) {
|
|
186
|
+
documentFileName ??= resolveWhatsAppDocumentFileName({
|
|
187
|
+
fileName: media.fileName,
|
|
188
|
+
mimetype: media.mimetype,
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
outboundLog.info(`Sending message -> ${redactedJid}${hasMedia ? " (media)" : ""}`);
|
|
193
|
+
logger.info({ jid: redactedJid, hasMedia }, "sending message");
|
|
194
|
+
if (!isWhatsAppNewsletterJid(jid)) {
|
|
195
|
+
await active.sendComposingTo(to);
|
|
196
|
+
}
|
|
197
|
+
const hasExplicitAccountId = Boolean(options.accountId?.trim());
|
|
198
|
+
const accountId = hasExplicitAccountId ? resolvedAccountId : undefined;
|
|
199
|
+
const sendOptions: ActiveWebSendOptions | undefined =
|
|
200
|
+
options.gifPlayback ||
|
|
201
|
+
forceDocumentDelivery ||
|
|
202
|
+
accountId ||
|
|
203
|
+
documentFileName ||
|
|
204
|
+
options.quotedMessageKey
|
|
205
|
+
? {
|
|
206
|
+
...(options.gifPlayback ? { gifPlayback: true } : {}),
|
|
207
|
+
...(forceDocumentDelivery ? { asDocument: true } : {}),
|
|
208
|
+
...(documentFileName ? { fileName: documentFileName } : {}),
|
|
209
|
+
...(options.quotedMessageKey ? { quotedMessageKey: options.quotedMessageKey } : {}),
|
|
210
|
+
accountId,
|
|
211
|
+
}
|
|
212
|
+
: undefined;
|
|
213
|
+
const result = sendOptions
|
|
214
|
+
? await active.sendMessage(to, text, mediaBuffer, mediaType, sendOptions)
|
|
215
|
+
: await active.sendMessage(to, text, mediaBuffer, mediaType);
|
|
216
|
+
if (visibleTextAfterVoice) {
|
|
217
|
+
if (sendOptions) {
|
|
218
|
+
await active.sendMessage(to, visibleTextAfterVoice, undefined, undefined, sendOptions);
|
|
219
|
+
} else {
|
|
220
|
+
await active.sendMessage(to, visibleTextAfterVoice, undefined, undefined);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
const messageId = (result as { messageId?: string })?.messageId ?? "unknown";
|
|
224
|
+
const durationMs = Date.now() - startedAt;
|
|
225
|
+
outboundLog.info(
|
|
226
|
+
`Sent message ${messageId} -> ${redactedJid}${hasMedia ? " (media)" : ""} (${durationMs}ms)`,
|
|
227
|
+
);
|
|
228
|
+
logger.info({ jid: redactedJid, messageId }, "sent message");
|
|
229
|
+
return { messageId, toJid: jid };
|
|
230
|
+
} catch (err) {
|
|
231
|
+
logger.error({ err: String(err), to: redactedTo, hasMedia }, "failed to send via web session");
|
|
232
|
+
throw err;
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export async function sendTypingWhatsApp(
|
|
237
|
+
to: string,
|
|
238
|
+
options: {
|
|
239
|
+
cfg: AutoBotConfig;
|
|
240
|
+
accountId?: string;
|
|
241
|
+
},
|
|
242
|
+
): Promise<void> {
|
|
243
|
+
const cfg = requireRuntimeConfig(options.cfg, "WhatsApp typing send");
|
|
244
|
+
const { listener: active } = requireOutboundActiveWebListener({
|
|
245
|
+
cfg,
|
|
246
|
+
accountId: options.accountId,
|
|
247
|
+
});
|
|
248
|
+
if (!isWhatsAppNewsletterJid(toWhatsappJid(to))) {
|
|
249
|
+
await active.sendComposingTo(to);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
export async function sendReactionWhatsApp(
|
|
254
|
+
chatJid: string,
|
|
255
|
+
messageId: string,
|
|
256
|
+
emoji: string,
|
|
257
|
+
options: {
|
|
258
|
+
verbose: boolean;
|
|
259
|
+
fromMe?: boolean;
|
|
260
|
+
participant?: string;
|
|
261
|
+
accountId?: string;
|
|
262
|
+
cfg: AutoBotConfig;
|
|
263
|
+
},
|
|
264
|
+
): Promise<void> {
|
|
265
|
+
const correlationId = generateSecureUuid();
|
|
266
|
+
const cfg = requireRuntimeConfig(options.cfg, "WhatsApp reaction");
|
|
267
|
+
const { listener: active } = requireOutboundActiveWebListener({
|
|
268
|
+
cfg,
|
|
269
|
+
accountId: options.accountId,
|
|
270
|
+
});
|
|
271
|
+
const redactedChatJid = redactIdentifier(chatJid);
|
|
272
|
+
const logger = getChildLogger({
|
|
273
|
+
module: "web-outbound",
|
|
274
|
+
correlationId,
|
|
275
|
+
chatJid: redactedChatJid,
|
|
276
|
+
messageId,
|
|
277
|
+
});
|
|
278
|
+
try {
|
|
279
|
+
const jid = toWhatsappJid(chatJid);
|
|
280
|
+
const redactedJid = redactIdentifier(jid);
|
|
281
|
+
outboundLog.info(`Sending reaction "${emoji}" -> message ${messageId}`);
|
|
282
|
+
logger.info({ chatJid: redactedJid, messageId, emoji }, "sending reaction");
|
|
283
|
+
await active.sendReaction(
|
|
284
|
+
chatJid,
|
|
285
|
+
messageId,
|
|
286
|
+
emoji,
|
|
287
|
+
options.fromMe ?? false,
|
|
288
|
+
options.participant,
|
|
289
|
+
);
|
|
290
|
+
outboundLog.info(`Sent reaction "${emoji}" -> message ${messageId}`);
|
|
291
|
+
logger.info({ chatJid: redactedJid, messageId, emoji }, "sent reaction");
|
|
292
|
+
} catch (err) {
|
|
293
|
+
logger.error(
|
|
294
|
+
{ err: String(err), chatJid: redactedChatJid, messageId, emoji },
|
|
295
|
+
"failed to send reaction via web session",
|
|
296
|
+
);
|
|
297
|
+
throw err;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
export async function sendPollWhatsApp(
|
|
302
|
+
to: string,
|
|
303
|
+
poll: PollInput,
|
|
304
|
+
options: { verbose: boolean; accountId?: string; cfg: AutoBotConfig },
|
|
305
|
+
): Promise<{ messageId: string; toJid: string }> {
|
|
306
|
+
const correlationId = generateSecureUuid();
|
|
307
|
+
const startedAt = Date.now();
|
|
308
|
+
const cfg = requireRuntimeConfig(options.cfg, "WhatsApp poll");
|
|
309
|
+
const { listener: active } = requireOutboundActiveWebListener({
|
|
310
|
+
cfg,
|
|
311
|
+
accountId: options.accountId,
|
|
312
|
+
});
|
|
313
|
+
const redactedTo = redactIdentifier(to);
|
|
314
|
+
const logger = getChildLogger({
|
|
315
|
+
module: "web-outbound",
|
|
316
|
+
correlationId,
|
|
317
|
+
to: redactedTo,
|
|
318
|
+
});
|
|
319
|
+
try {
|
|
320
|
+
const jid = toWhatsappJid(to);
|
|
321
|
+
const redactedJid = redactIdentifier(jid);
|
|
322
|
+
const normalized = normalizePollInput(poll, { maxOptions: 12 });
|
|
323
|
+
outboundLog.info(`Sending poll -> ${redactedJid}`);
|
|
324
|
+
logger.info(
|
|
325
|
+
{
|
|
326
|
+
jid: redactedJid,
|
|
327
|
+
optionCount: normalized.options.length,
|
|
328
|
+
maxSelections: normalized.maxSelections,
|
|
329
|
+
},
|
|
330
|
+
"sending poll",
|
|
331
|
+
);
|
|
332
|
+
const result = await active.sendPoll(to, normalized);
|
|
333
|
+
const messageId = (result as { messageId?: string })?.messageId ?? "unknown";
|
|
334
|
+
const durationMs = Date.now() - startedAt;
|
|
335
|
+
outboundLog.info(`Sent poll ${messageId} -> ${redactedJid} (${durationMs}ms)`);
|
|
336
|
+
logger.info({ jid: redactedJid, messageId }, "sent poll");
|
|
337
|
+
return { messageId, toJid: jid };
|
|
338
|
+
} catch (err) {
|
|
339
|
+
logger.error({ err: String(err), to: redactedTo }, "failed to send poll via web session");
|
|
340
|
+
throw err;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { normalizeLowercaseStringOrEmpty } from "autobot/plugin-sdk/string-coerce-runtime";
|
|
2
|
+
|
|
3
|
+
function extractLegacyWhatsAppGroupId(key: string): string | null {
|
|
4
|
+
const trimmed = key.trim();
|
|
5
|
+
if (!trimmed) {
|
|
6
|
+
return null;
|
|
7
|
+
}
|
|
8
|
+
const lower = normalizeLowercaseStringOrEmpty(trimmed);
|
|
9
|
+
if (trimmed.startsWith("group:")) {
|
|
10
|
+
const id = trimmed.slice("group:".length).trim();
|
|
11
|
+
return normalizeLowercaseStringOrEmpty(id).includes("@g.us") ? id : null;
|
|
12
|
+
}
|
|
13
|
+
if (!lower.includes("@g.us")) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
if (!trimmed.includes(":")) {
|
|
17
|
+
return trimmed;
|
|
18
|
+
}
|
|
19
|
+
if (lower.startsWith("whatsapp:") && !trimmed.includes(":group:")) {
|
|
20
|
+
const remainder = trimmed.slice("whatsapp:".length).trim();
|
|
21
|
+
const cleaned = remainder.replace(/^group:/i, "").trim();
|
|
22
|
+
return cleaned || null;
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function isLegacyGroupSessionKey(key: string): boolean {
|
|
28
|
+
return extractLegacyWhatsAppGroupId(key) !== null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function deriveLegacySessionChatType(key: string): "group" | undefined {
|
|
32
|
+
return isLegacyGroupSessionKey(key) ? "group" : undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function canonicalizeLegacySessionKey(params: {
|
|
36
|
+
key: string;
|
|
37
|
+
agentId: string;
|
|
38
|
+
}): string | null {
|
|
39
|
+
const legacyGroupId = extractLegacyWhatsAppGroupId(params.key);
|
|
40
|
+
return legacyGroupId
|
|
41
|
+
? `agent:${normalizeLowercaseStringOrEmpty(params.agentId)}:whatsapp:group:${normalizeLowercaseStringOrEmpty(legacyGroupId)}`
|
|
42
|
+
: null;
|
|
43
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
function safeStringify(value: unknown, limit = 800): string {
|
|
2
|
+
try {
|
|
3
|
+
const seen = new WeakSet();
|
|
4
|
+
const raw = JSON.stringify(
|
|
5
|
+
value,
|
|
6
|
+
(_key, v) => {
|
|
7
|
+
if (typeof v === "bigint") {
|
|
8
|
+
return v.toString();
|
|
9
|
+
}
|
|
10
|
+
if (typeof v === "function") {
|
|
11
|
+
const maybeName = (v as { name?: unknown }).name;
|
|
12
|
+
const name =
|
|
13
|
+
typeof maybeName === "string" && maybeName.length > 0 ? maybeName : "anonymous";
|
|
14
|
+
return `[Function ${name}]`;
|
|
15
|
+
}
|
|
16
|
+
if (typeof v === "object" && v) {
|
|
17
|
+
if (seen.has(v)) {
|
|
18
|
+
return "[Circular]";
|
|
19
|
+
}
|
|
20
|
+
seen.add(v);
|
|
21
|
+
}
|
|
22
|
+
return v;
|
|
23
|
+
},
|
|
24
|
+
2,
|
|
25
|
+
);
|
|
26
|
+
if (!raw) {
|
|
27
|
+
return String(value);
|
|
28
|
+
}
|
|
29
|
+
return raw.length > limit ? `${raw.slice(0, limit)}…` : raw;
|
|
30
|
+
} catch {
|
|
31
|
+
return String(value);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function extractBoomDetails(err: unknown): {
|
|
36
|
+
statusCode?: number;
|
|
37
|
+
error?: string;
|
|
38
|
+
message?: string;
|
|
39
|
+
} | null {
|
|
40
|
+
if (!err || typeof err !== "object") {
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const output = (err as { output?: unknown })?.output as
|
|
44
|
+
| { statusCode?: unknown; payload?: unknown }
|
|
45
|
+
| undefined;
|
|
46
|
+
if (!output || typeof output !== "object") {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
const payload = (output as { payload?: unknown }).payload as
|
|
50
|
+
| { error?: unknown; message?: unknown; statusCode?: unknown }
|
|
51
|
+
| undefined;
|
|
52
|
+
const statusCode =
|
|
53
|
+
typeof (output as { statusCode?: unknown }).statusCode === "number"
|
|
54
|
+
? ((output as { statusCode?: unknown }).statusCode as number)
|
|
55
|
+
: typeof payload?.statusCode === "number"
|
|
56
|
+
? payload.statusCode
|
|
57
|
+
: undefined;
|
|
58
|
+
const error = typeof payload?.error === "string" ? payload.error : undefined;
|
|
59
|
+
const message = typeof payload?.message === "string" ? payload.message : undefined;
|
|
60
|
+
if (!statusCode && !error && !message) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
return { statusCode, error, message };
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function getStatusCode(err: unknown) {
|
|
67
|
+
return (
|
|
68
|
+
(err as { output?: { statusCode?: number } })?.output?.statusCode ??
|
|
69
|
+
(err as { status?: number })?.status ??
|
|
70
|
+
(err as { error?: { output?: { statusCode?: number } } })?.error?.output?.statusCode
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function formatError(err: unknown): string {
|
|
75
|
+
if (err instanceof Error) {
|
|
76
|
+
return err.message;
|
|
77
|
+
}
|
|
78
|
+
if (typeof err === "string") {
|
|
79
|
+
return err;
|
|
80
|
+
}
|
|
81
|
+
if (!err || typeof err !== "object") {
|
|
82
|
+
return String(err);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const boom =
|
|
86
|
+
extractBoomDetails(err) ??
|
|
87
|
+
extractBoomDetails((err as { error?: unknown })?.error) ??
|
|
88
|
+
extractBoomDetails((err as { lastDisconnect?: { error?: unknown } })?.lastDisconnect?.error);
|
|
89
|
+
|
|
90
|
+
const status = boom?.statusCode ?? getStatusCode(err);
|
|
91
|
+
const code = (err as { code?: unknown })?.code;
|
|
92
|
+
const codeText = typeof code === "string" || typeof code === "number" ? String(code) : undefined;
|
|
93
|
+
|
|
94
|
+
const messageCandidates = [
|
|
95
|
+
boom?.message,
|
|
96
|
+
typeof (err as { message?: unknown })?.message === "string"
|
|
97
|
+
? ((err as { message?: unknown }).message as string)
|
|
98
|
+
: undefined,
|
|
99
|
+
typeof (err as { error?: { message?: unknown } })?.error?.message === "string"
|
|
100
|
+
? ((err as { error?: { message?: unknown } }).error?.message as string)
|
|
101
|
+
: undefined,
|
|
102
|
+
];
|
|
103
|
+
const message = messageCandidates.find((value): value is string =>
|
|
104
|
+
Boolean(value && value.trim().length > 0),
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
const pieces: string[] = [];
|
|
108
|
+
if (typeof status === "number") {
|
|
109
|
+
pieces.push(`status=${status}`);
|
|
110
|
+
}
|
|
111
|
+
if (boom?.error) {
|
|
112
|
+
pieces.push(boom.error);
|
|
113
|
+
}
|
|
114
|
+
if (message) {
|
|
115
|
+
pieces.push(message);
|
|
116
|
+
}
|
|
117
|
+
if (codeText) {
|
|
118
|
+
pieces.push(`code=${codeText}`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (pieces.length > 0) {
|
|
122
|
+
return pieces.join(" ");
|
|
123
|
+
}
|
|
124
|
+
return safeStringify(err);
|
|
125
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildChannelOutboundSessionRoute,
|
|
3
|
+
type ChannelOutboundSessionRouteParams,
|
|
4
|
+
} from "autobot/plugin-sdk/core";
|
|
5
|
+
import {
|
|
6
|
+
isWhatsAppGroupJid,
|
|
7
|
+
isWhatsAppNewsletterJid,
|
|
8
|
+
normalizeWhatsAppTarget,
|
|
9
|
+
} from "./normalize.js";
|
|
10
|
+
|
|
11
|
+
export function resolveWhatsAppOutboundSessionRoute(params: ChannelOutboundSessionRouteParams) {
|
|
12
|
+
const normalized = normalizeWhatsAppTarget(params.target);
|
|
13
|
+
if (!normalized) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
const isGroup = isWhatsAppGroupJid(normalized);
|
|
17
|
+
const isNewsletter = isWhatsAppNewsletterJid(normalized);
|
|
18
|
+
const chatType = isGroup ? "group" : isNewsletter ? "channel" : "direct";
|
|
19
|
+
return buildChannelOutboundSessionRoute({
|
|
20
|
+
cfg: params.cfg,
|
|
21
|
+
agentId: params.agentId,
|
|
22
|
+
channel: "whatsapp",
|
|
23
|
+
accountId: params.accountId,
|
|
24
|
+
peer: {
|
|
25
|
+
kind: chatType,
|
|
26
|
+
id: normalized,
|
|
27
|
+
},
|
|
28
|
+
chatType,
|
|
29
|
+
from: normalized,
|
|
30
|
+
to: normalized,
|
|
31
|
+
});
|
|
32
|
+
}
|