@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
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
import type { proto } from "baileys";
|
|
2
|
+
import { extractMessageContent, getContentType, normalizeMessageContent } from "baileys";
|
|
3
|
+
import { formatLocationText, type NormalizedLocation } from "autobot/plugin-sdk/channel-inbound";
|
|
4
|
+
import { logVerbose } from "autobot/plugin-sdk/runtime-env";
|
|
5
|
+
import { resolveComparableIdentity, type WhatsAppReplyContext } from "../identity.js";
|
|
6
|
+
import { jidToE164 } from "../text-runtime.js";
|
|
7
|
+
import { parseVcard } from "../vcard.js";
|
|
8
|
+
import type { WhatsAppStructuredContactContext } from "./types.js";
|
|
9
|
+
|
|
10
|
+
const MESSAGE_WRAPPER_KEYS = [
|
|
11
|
+
"botInvokeMessage",
|
|
12
|
+
"ephemeralMessage",
|
|
13
|
+
"viewOnceMessage",
|
|
14
|
+
"viewOnceMessageV2",
|
|
15
|
+
"viewOnceMessageV2Extension",
|
|
16
|
+
"documentWithCaptionMessage",
|
|
17
|
+
"groupMentionedMessage",
|
|
18
|
+
] as const;
|
|
19
|
+
|
|
20
|
+
const MESSAGE_CONTENT_KEYS = [
|
|
21
|
+
"conversation",
|
|
22
|
+
"extendedTextMessage",
|
|
23
|
+
"imageMessage",
|
|
24
|
+
"videoMessage",
|
|
25
|
+
"audioMessage",
|
|
26
|
+
"documentMessage",
|
|
27
|
+
"stickerMessage",
|
|
28
|
+
"locationMessage",
|
|
29
|
+
"liveLocationMessage",
|
|
30
|
+
"contactMessage",
|
|
31
|
+
"contactsArrayMessage",
|
|
32
|
+
"buttonsResponseMessage",
|
|
33
|
+
"listResponseMessage",
|
|
34
|
+
"templateButtonReplyMessage",
|
|
35
|
+
"interactiveResponseMessage",
|
|
36
|
+
"buttonsMessage",
|
|
37
|
+
"listMessage",
|
|
38
|
+
] as const;
|
|
39
|
+
|
|
40
|
+
function fallbackNormalizeMessageContent(
|
|
41
|
+
message: proto.IMessage | undefined,
|
|
42
|
+
): proto.IMessage | undefined {
|
|
43
|
+
let current = message as unknown;
|
|
44
|
+
while (current && typeof current === "object") {
|
|
45
|
+
let unwrapped = false;
|
|
46
|
+
for (const key of MESSAGE_WRAPPER_KEYS) {
|
|
47
|
+
const candidate = (current as Record<string, unknown>)[key];
|
|
48
|
+
if (
|
|
49
|
+
candidate &&
|
|
50
|
+
typeof candidate === "object" &&
|
|
51
|
+
"message" in (candidate as Record<string, unknown>) &&
|
|
52
|
+
(candidate as { message?: unknown }).message
|
|
53
|
+
) {
|
|
54
|
+
current = (candidate as { message: unknown }).message;
|
|
55
|
+
unwrapped = true;
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
if (!unwrapped) {
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return current as proto.IMessage | undefined;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function normalizeMessage(message: proto.IMessage | undefined): proto.IMessage | undefined {
|
|
67
|
+
if (typeof normalizeMessageContent === "function") {
|
|
68
|
+
return normalizeMessageContent(message);
|
|
69
|
+
}
|
|
70
|
+
return fallbackNormalizeMessageContent(message);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function fallbackGetContentType(
|
|
74
|
+
message: proto.IMessage | undefined,
|
|
75
|
+
): keyof proto.IMessage | undefined {
|
|
76
|
+
const normalized = fallbackNormalizeMessageContent(message);
|
|
77
|
+
if (!normalized || typeof normalized !== "object") {
|
|
78
|
+
return undefined;
|
|
79
|
+
}
|
|
80
|
+
for (const key of MESSAGE_CONTENT_KEYS) {
|
|
81
|
+
if ((normalized as Record<string, unknown>)[key] != null) {
|
|
82
|
+
return key as keyof proto.IMessage;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function getMessageContentType(
|
|
89
|
+
message: proto.IMessage | undefined,
|
|
90
|
+
): keyof proto.IMessage | undefined {
|
|
91
|
+
if (typeof getContentType === "function") {
|
|
92
|
+
return getContentType(message);
|
|
93
|
+
}
|
|
94
|
+
return fallbackGetContentType(message);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function extractMessage(message: proto.IMessage | undefined): proto.IMessage | undefined {
|
|
98
|
+
if (typeof extractMessageContent === "function") {
|
|
99
|
+
return extractMessageContent(message);
|
|
100
|
+
}
|
|
101
|
+
const normalized = fallbackNormalizeMessageContent(message);
|
|
102
|
+
const contentType = fallbackGetContentType(normalized);
|
|
103
|
+
if (!normalized || !contentType || contentType === "conversation") {
|
|
104
|
+
return normalized;
|
|
105
|
+
}
|
|
106
|
+
const candidate = (normalized as Record<string, unknown>)[contentType];
|
|
107
|
+
return candidate && typeof candidate === "object" ? (candidate as proto.IMessage) : normalized;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function getFutureProofInnerMessage(message: proto.IMessage): proto.IMessage | undefined {
|
|
111
|
+
const contentType = getMessageContentType(message);
|
|
112
|
+
const candidate = contentType ? (message as Record<string, unknown>)[contentType] : undefined;
|
|
113
|
+
if (
|
|
114
|
+
candidate &&
|
|
115
|
+
typeof candidate === "object" &&
|
|
116
|
+
"message" in candidate &&
|
|
117
|
+
(candidate as { message?: unknown }).message &&
|
|
118
|
+
typeof (candidate as { message: unknown }).message === "object"
|
|
119
|
+
) {
|
|
120
|
+
const inner = normalizeMessage((candidate as { message: proto.IMessage }).message);
|
|
121
|
+
if (inner) {
|
|
122
|
+
const innerType = getMessageContentType(inner);
|
|
123
|
+
if (innerType && innerType !== contentType) {
|
|
124
|
+
return inner;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function buildMessageChain(message: proto.IMessage | undefined): proto.IMessage[] {
|
|
132
|
+
const chain: proto.IMessage[] = [];
|
|
133
|
+
let current = normalizeMessage(message);
|
|
134
|
+
while (current && chain.length < 4) {
|
|
135
|
+
chain.push(current);
|
|
136
|
+
current = getFutureProofInnerMessage(current);
|
|
137
|
+
}
|
|
138
|
+
return chain;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function unwrapMessage(message: proto.IMessage | undefined): proto.IMessage | undefined {
|
|
142
|
+
const chain = buildMessageChain(message);
|
|
143
|
+
return chain.at(-1);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function extractContextInfoFromMessage(message: proto.IMessage): proto.IContextInfo | undefined {
|
|
147
|
+
const contentType = getMessageContentType(message);
|
|
148
|
+
const candidate = contentType ? (message as Record<string, unknown>)[contentType] : undefined;
|
|
149
|
+
const contextInfo =
|
|
150
|
+
candidate && typeof candidate === "object" && "contextInfo" in candidate
|
|
151
|
+
? (candidate as { contextInfo?: proto.IContextInfo }).contextInfo
|
|
152
|
+
: undefined;
|
|
153
|
+
if (contextInfo) {
|
|
154
|
+
return contextInfo;
|
|
155
|
+
}
|
|
156
|
+
const fallback =
|
|
157
|
+
message.extendedTextMessage?.contextInfo ??
|
|
158
|
+
message.imageMessage?.contextInfo ??
|
|
159
|
+
message.videoMessage?.contextInfo ??
|
|
160
|
+
message.documentMessage?.contextInfo ??
|
|
161
|
+
message.audioMessage?.contextInfo ??
|
|
162
|
+
message.stickerMessage?.contextInfo ??
|
|
163
|
+
message.buttonsResponseMessage?.contextInfo ??
|
|
164
|
+
message.listResponseMessage?.contextInfo ??
|
|
165
|
+
message.templateButtonReplyMessage?.contextInfo ??
|
|
166
|
+
message.interactiveResponseMessage?.contextInfo ??
|
|
167
|
+
message.buttonsMessage?.contextInfo ??
|
|
168
|
+
message.listMessage?.contextInfo;
|
|
169
|
+
if (fallback) {
|
|
170
|
+
return fallback;
|
|
171
|
+
}
|
|
172
|
+
for (const value of Object.values(message)) {
|
|
173
|
+
if (!value || typeof value !== "object") {
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if ("contextInfo" in value) {
|
|
177
|
+
const candidateContext = (value as { contextInfo?: proto.IContextInfo }).contextInfo;
|
|
178
|
+
if (candidateContext) {
|
|
179
|
+
return candidateContext;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
// FutureProofMessage wrapper: dig into .message to find contextInfo
|
|
183
|
+
if ("message" in value) {
|
|
184
|
+
const inner = (value as { message?: proto.IMessage }).message;
|
|
185
|
+
if (inner) {
|
|
186
|
+
const innerCtx = extractContextInfo(inner);
|
|
187
|
+
if (innerCtx) {
|
|
188
|
+
return innerCtx;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
return undefined;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export function extractContextInfo(
|
|
197
|
+
message: proto.IMessage | undefined,
|
|
198
|
+
): proto.IContextInfo | undefined {
|
|
199
|
+
for (const candidate of buildMessageChain(message)) {
|
|
200
|
+
const contextInfo = extractContextInfoFromMessage(candidate);
|
|
201
|
+
if (contextInfo) {
|
|
202
|
+
return contextInfo;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return undefined;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export function extractMentionedJids(rawMessage: proto.IMessage | undefined): string[] | undefined {
|
|
209
|
+
const message = unwrapMessage(rawMessage);
|
|
210
|
+
if (!message) {
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const candidates: Array<string[] | null | undefined> = [
|
|
215
|
+
message.extendedTextMessage?.contextInfo?.mentionedJid,
|
|
216
|
+
message.imageMessage?.contextInfo?.mentionedJid,
|
|
217
|
+
message.videoMessage?.contextInfo?.mentionedJid,
|
|
218
|
+
message.documentMessage?.contextInfo?.mentionedJid,
|
|
219
|
+
message.audioMessage?.contextInfo?.mentionedJid,
|
|
220
|
+
message.stickerMessage?.contextInfo?.mentionedJid,
|
|
221
|
+
message.buttonsResponseMessage?.contextInfo?.mentionedJid,
|
|
222
|
+
message.listResponseMessage?.contextInfo?.mentionedJid,
|
|
223
|
+
];
|
|
224
|
+
|
|
225
|
+
const flattened = candidates.flatMap((arr) => arr ?? []).filter(Boolean);
|
|
226
|
+
if (flattened.length === 0) {
|
|
227
|
+
return undefined;
|
|
228
|
+
}
|
|
229
|
+
return Array.from(new Set(flattened));
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export function extractText(rawMessage: proto.IMessage | undefined): string | undefined {
|
|
233
|
+
const message = unwrapMessage(rawMessage);
|
|
234
|
+
if (!message) {
|
|
235
|
+
return undefined;
|
|
236
|
+
}
|
|
237
|
+
const extracted = extractMessage(message);
|
|
238
|
+
const candidates = [message, extracted && extracted !== message ? extracted : undefined];
|
|
239
|
+
for (const candidate of candidates) {
|
|
240
|
+
if (!candidate) {
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
if (typeof candidate.conversation === "string" && candidate.conversation.trim()) {
|
|
244
|
+
return candidate.conversation.trim();
|
|
245
|
+
}
|
|
246
|
+
const extended = candidate.extendedTextMessage?.text;
|
|
247
|
+
if (extended?.trim()) {
|
|
248
|
+
return extended.trim();
|
|
249
|
+
}
|
|
250
|
+
const caption =
|
|
251
|
+
candidate.imageMessage?.caption ??
|
|
252
|
+
candidate.videoMessage?.caption ??
|
|
253
|
+
candidate.documentMessage?.caption;
|
|
254
|
+
if (caption?.trim()) {
|
|
255
|
+
return caption.trim();
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
const contactPlaceholder =
|
|
259
|
+
extractContactPlaceholder(message) ??
|
|
260
|
+
(extracted && extracted !== message
|
|
261
|
+
? extractContactPlaceholder(extracted as proto.IMessage | undefined)
|
|
262
|
+
: undefined);
|
|
263
|
+
if (contactPlaceholder) {
|
|
264
|
+
return contactPlaceholder;
|
|
265
|
+
}
|
|
266
|
+
return undefined;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function extractMediaPlaceholder(
|
|
270
|
+
rawMessage: proto.IMessage | undefined,
|
|
271
|
+
): string | undefined {
|
|
272
|
+
const message = unwrapMessage(rawMessage);
|
|
273
|
+
if (!message) {
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
if (message.imageMessage) {
|
|
277
|
+
return "<media:image>";
|
|
278
|
+
}
|
|
279
|
+
if (message.videoMessage) {
|
|
280
|
+
return "<media:video>";
|
|
281
|
+
}
|
|
282
|
+
if (message.audioMessage) {
|
|
283
|
+
return "<media:audio>";
|
|
284
|
+
}
|
|
285
|
+
if (message.documentMessage) {
|
|
286
|
+
return "<media:document>";
|
|
287
|
+
}
|
|
288
|
+
if (message.stickerMessage) {
|
|
289
|
+
return "<media:sticker>";
|
|
290
|
+
}
|
|
291
|
+
return undefined;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
function extractContactPlaceholder(rawMessage: proto.IMessage | undefined): string | undefined {
|
|
295
|
+
const contactContext = extractContactContext(rawMessage);
|
|
296
|
+
if (!contactContext) {
|
|
297
|
+
return undefined;
|
|
298
|
+
}
|
|
299
|
+
if (contactContext.kind === "contact") {
|
|
300
|
+
return "<contact>";
|
|
301
|
+
}
|
|
302
|
+
const suffix = contactContext.total === 1 ? "contact" : "contacts";
|
|
303
|
+
return `<contacts: ${contactContext.total} ${suffix}>`;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
export function extractContactContext(
|
|
307
|
+
rawMessage: proto.IMessage | undefined,
|
|
308
|
+
): WhatsAppStructuredContactContext | undefined {
|
|
309
|
+
const message = unwrapMessage(rawMessage);
|
|
310
|
+
if (!message) {
|
|
311
|
+
return undefined;
|
|
312
|
+
}
|
|
313
|
+
const contact = message.contactMessage ?? undefined;
|
|
314
|
+
if (contact) {
|
|
315
|
+
const { name, phones } = describeContact({
|
|
316
|
+
displayName: contact.displayName,
|
|
317
|
+
vcard: contact.vcard,
|
|
318
|
+
});
|
|
319
|
+
return {
|
|
320
|
+
kind: "contact",
|
|
321
|
+
total: 1,
|
|
322
|
+
contacts: [{ name, phones }],
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
const contactsArray = message.contactsArrayMessage?.contacts ?? undefined;
|
|
326
|
+
if (!contactsArray || contactsArray.length === 0) {
|
|
327
|
+
return undefined;
|
|
328
|
+
}
|
|
329
|
+
return {
|
|
330
|
+
kind: "contacts",
|
|
331
|
+
total: contactsArray.length,
|
|
332
|
+
contacts: contactsArray.map((entry) =>
|
|
333
|
+
describeContact({ displayName: entry.displayName, vcard: entry.vcard }),
|
|
334
|
+
),
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function describeContact(input: { displayName?: string | null; vcard?: string | null }): {
|
|
339
|
+
name?: string;
|
|
340
|
+
phones: string[];
|
|
341
|
+
} {
|
|
342
|
+
const displayName = (input.displayName ?? "").trim();
|
|
343
|
+
const parsed = parseVcard(input.vcard ?? undefined);
|
|
344
|
+
const name = displayName || parsed.name;
|
|
345
|
+
return { name, phones: parsed.phones };
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export function extractLocationData(
|
|
349
|
+
rawMessage: proto.IMessage | undefined,
|
|
350
|
+
): NormalizedLocation | null {
|
|
351
|
+
const message = unwrapMessage(rawMessage);
|
|
352
|
+
if (!message) {
|
|
353
|
+
return null;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const live = message.liveLocationMessage ?? undefined;
|
|
357
|
+
if (live) {
|
|
358
|
+
const latitudeRaw = live.degreesLatitude;
|
|
359
|
+
const longitudeRaw = live.degreesLongitude;
|
|
360
|
+
if (latitudeRaw != null && longitudeRaw != null) {
|
|
361
|
+
const latitude = latitudeRaw;
|
|
362
|
+
const longitude = longitudeRaw;
|
|
363
|
+
if (Number.isFinite(latitude) && Number.isFinite(longitude)) {
|
|
364
|
+
return {
|
|
365
|
+
latitude,
|
|
366
|
+
longitude,
|
|
367
|
+
accuracy: live.accuracyInMeters ?? undefined,
|
|
368
|
+
caption: live.caption ?? undefined,
|
|
369
|
+
source: "live",
|
|
370
|
+
isLive: true,
|
|
371
|
+
};
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const location = message.locationMessage ?? undefined;
|
|
377
|
+
if (location) {
|
|
378
|
+
const latitudeRaw = location.degreesLatitude;
|
|
379
|
+
const longitudeRaw = location.degreesLongitude;
|
|
380
|
+
if (latitudeRaw != null && longitudeRaw != null) {
|
|
381
|
+
const latitude = latitudeRaw;
|
|
382
|
+
const longitude = longitudeRaw;
|
|
383
|
+
if (Number.isFinite(latitude) && Number.isFinite(longitude)) {
|
|
384
|
+
const isLive = Boolean(location.isLive);
|
|
385
|
+
return {
|
|
386
|
+
latitude,
|
|
387
|
+
longitude,
|
|
388
|
+
accuracy: location.accuracyInMeters ?? undefined,
|
|
389
|
+
name: location.name ?? undefined,
|
|
390
|
+
address: location.address ?? undefined,
|
|
391
|
+
caption: location.comment ?? undefined,
|
|
392
|
+
source: isLive ? "live" : location.name || location.address ? "place" : "pin",
|
|
393
|
+
isLive,
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
return null;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export function describeReplyContext(
|
|
403
|
+
rawMessage: proto.IMessage | undefined,
|
|
404
|
+
): WhatsAppReplyContext | null {
|
|
405
|
+
const message = unwrapMessage(rawMessage);
|
|
406
|
+
if (!message) {
|
|
407
|
+
return null;
|
|
408
|
+
}
|
|
409
|
+
const contextInfo = extractContextInfo(message);
|
|
410
|
+
const quoted = normalizeMessage(contextInfo?.quotedMessage as proto.IMessage | undefined);
|
|
411
|
+
if (!quoted) {
|
|
412
|
+
return null;
|
|
413
|
+
}
|
|
414
|
+
const location = extractLocationData(quoted);
|
|
415
|
+
const locationText = location ? formatLocationText(location) : undefined;
|
|
416
|
+
const text = extractText(quoted);
|
|
417
|
+
let body: string | undefined = [text, locationText].filter(Boolean).join("\n").trim();
|
|
418
|
+
if (!body) {
|
|
419
|
+
body = extractMediaPlaceholder(quoted);
|
|
420
|
+
}
|
|
421
|
+
if (!body) {
|
|
422
|
+
const quotedType = quoted ? getMessageContentType(quoted) : undefined;
|
|
423
|
+
logVerbose(
|
|
424
|
+
`Quoted message missing extractable body${quotedType ? ` (type ${quotedType})` : ""}`,
|
|
425
|
+
);
|
|
426
|
+
return null;
|
|
427
|
+
}
|
|
428
|
+
const senderJid = contextInfo?.participant ?? undefined;
|
|
429
|
+
const sender = resolveComparableIdentity({
|
|
430
|
+
jid: senderJid,
|
|
431
|
+
label: senderJid ? (jidToE164(senderJid) ?? senderJid) : "unknown sender",
|
|
432
|
+
});
|
|
433
|
+
return {
|
|
434
|
+
id: contextInfo?.stanzaId || undefined,
|
|
435
|
+
body,
|
|
436
|
+
sender,
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
function hasInteractiveResponseContent(message: proto.IMessage | undefined): boolean {
|
|
441
|
+
if (!message) {
|
|
442
|
+
return false;
|
|
443
|
+
}
|
|
444
|
+
// Button/list/template/interactive selections that the existing four
|
|
445
|
+
// extractors do not cover. Treat any presence of these keys as user
|
|
446
|
+
// content — Baileys never delivers these as receipts or protocol
|
|
447
|
+
// envelopes, only as explicit user choices.
|
|
448
|
+
return Boolean(
|
|
449
|
+
message.buttonsResponseMessage ||
|
|
450
|
+
message.listResponseMessage ||
|
|
451
|
+
message.templateButtonReplyMessage ||
|
|
452
|
+
message.interactiveResponseMessage,
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Fast check that a Baileys message carries user-visible inbound content
|
|
458
|
+
* (text, media, contact, location, button/list selection). Returns false for
|
|
459
|
+
* protocol/receipt/typing notifications that arrive on the same
|
|
460
|
+
* `messages.upsert` stream as real messages but should not trigger pairing
|
|
461
|
+
* access-control side effects.
|
|
462
|
+
*/
|
|
463
|
+
export function hasInboundUserContent(rawMessage: proto.IMessage | undefined): boolean {
|
|
464
|
+
if (!rawMessage) {
|
|
465
|
+
return false;
|
|
466
|
+
}
|
|
467
|
+
if (extractText(rawMessage)) {
|
|
468
|
+
return true;
|
|
469
|
+
}
|
|
470
|
+
if (extractMediaPlaceholder(rawMessage)) {
|
|
471
|
+
return true;
|
|
472
|
+
}
|
|
473
|
+
if (extractLocationData(rawMessage)) {
|
|
474
|
+
return true;
|
|
475
|
+
}
|
|
476
|
+
// Walk wrappers (ephemeral, viewOnce, etc.) — interactive responses
|
|
477
|
+
// can arrive nested.
|
|
478
|
+
for (const candidate of buildMessageChain(rawMessage)) {
|
|
479
|
+
if (hasInteractiveResponseContent(candidate)) {
|
|
480
|
+
return true;
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
return false;
|
|
484
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
type Listener = (...args: unknown[]) => void;
|
|
2
|
+
|
|
3
|
+
type OffCapableEmitter = {
|
|
4
|
+
on: (event: string, listener: Listener) => void;
|
|
5
|
+
off?: (event: string, listener: Listener) => void;
|
|
6
|
+
removeListener?: (event: string, listener: Listener) => void;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
type ClosableSocket = {
|
|
10
|
+
end?: (error: Error | undefined) => void;
|
|
11
|
+
ws?: {
|
|
12
|
+
close?: () => void;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function attachEmitterListener(
|
|
17
|
+
emitter: OffCapableEmitter,
|
|
18
|
+
event: string,
|
|
19
|
+
listener: Listener,
|
|
20
|
+
): () => void {
|
|
21
|
+
emitter.on(event, listener);
|
|
22
|
+
return () => {
|
|
23
|
+
if (typeof emitter.off === "function") {
|
|
24
|
+
emitter.off(event, listener);
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (typeof emitter.removeListener === "function") {
|
|
28
|
+
emitter.removeListener(event, listener);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function closeInboundMonitorSocket(sock: ClosableSocket): void {
|
|
34
|
+
if (typeof sock.end === "function") {
|
|
35
|
+
sock.end(new Error("AutoBot WhatsApp listener close"));
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
sock.ws?.close?.();
|
|
39
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import type { proto, WAMessage } from "baileys";
|
|
2
|
+
import { saveMediaStream, type SavedMedia } from "autobot/plugin-sdk/media-store";
|
|
3
|
+
import { logVerbose } from "autobot/plugin-sdk/runtime-env";
|
|
4
|
+
import type { createWaSocket } from "../session.js";
|
|
5
|
+
import { extractContextInfo } from "./extract.js";
|
|
6
|
+
import { downloadMediaMessage, normalizeMessageContent } from "./runtime-api.js";
|
|
7
|
+
|
|
8
|
+
export class WhatsAppInboundMediaLimitExceededError extends Error {
|
|
9
|
+
constructor(maxBytes: number) {
|
|
10
|
+
super(`Media exceeds ${Math.round(maxBytes / (1024 * 1024))}MB limit`);
|
|
11
|
+
this.name = "WhatsAppInboundMediaLimitExceededError";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function unwrapMessage(message: proto.IMessage | undefined): proto.IMessage | undefined {
|
|
16
|
+
const normalized = normalizeMessageContent(message);
|
|
17
|
+
return normalized;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Resolve the MIME type for an inbound media message.
|
|
22
|
+
* Falls back to WhatsApp's standard formats when Baileys omits the MIME.
|
|
23
|
+
*/
|
|
24
|
+
function resolveMediaMimetype(message: proto.IMessage): string | undefined {
|
|
25
|
+
const explicit =
|
|
26
|
+
message.imageMessage?.mimetype ??
|
|
27
|
+
message.videoMessage?.mimetype ??
|
|
28
|
+
message.documentMessage?.mimetype ??
|
|
29
|
+
message.audioMessage?.mimetype ??
|
|
30
|
+
message.stickerMessage?.mimetype ??
|
|
31
|
+
undefined;
|
|
32
|
+
if (explicit) {
|
|
33
|
+
return explicit;
|
|
34
|
+
}
|
|
35
|
+
// WhatsApp voice messages (PTT) and audio use OGG Opus by default
|
|
36
|
+
if (message.audioMessage) {
|
|
37
|
+
return "audio/ogg; codecs=opus";
|
|
38
|
+
}
|
|
39
|
+
if (message.imageMessage) {
|
|
40
|
+
return "image/jpeg";
|
|
41
|
+
}
|
|
42
|
+
if (message.videoMessage) {
|
|
43
|
+
return "video/mp4";
|
|
44
|
+
}
|
|
45
|
+
if (message.stickerMessage) {
|
|
46
|
+
return "image/webp";
|
|
47
|
+
}
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export async function downloadInboundMedia(
|
|
52
|
+
msg: proto.IWebMessageInfo,
|
|
53
|
+
sock: Awaited<ReturnType<typeof createWaSocket>>,
|
|
54
|
+
maxBytes = 50 * 1024 * 1024,
|
|
55
|
+
): Promise<{ saved: SavedMedia; mimetype?: string; fileName?: string } | undefined> {
|
|
56
|
+
const message = unwrapMessage(msg.message as proto.IMessage | undefined);
|
|
57
|
+
if (!message) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
const mimetype = resolveMediaMimetype(message);
|
|
61
|
+
const fileName = message.documentMessage?.fileName ?? undefined;
|
|
62
|
+
if (
|
|
63
|
+
!message.imageMessage &&
|
|
64
|
+
!message.videoMessage &&
|
|
65
|
+
!message.documentMessage &&
|
|
66
|
+
!message.audioMessage &&
|
|
67
|
+
!message.stickerMessage
|
|
68
|
+
) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
try {
|
|
72
|
+
const stream = await downloadMediaMessage(
|
|
73
|
+
msg as WAMessage,
|
|
74
|
+
"stream",
|
|
75
|
+
{},
|
|
76
|
+
{
|
|
77
|
+
reuploadRequest: sock.updateMediaMessage,
|
|
78
|
+
logger: sock.logger,
|
|
79
|
+
},
|
|
80
|
+
);
|
|
81
|
+
const saved = await saveMediaStream(
|
|
82
|
+
stream as AsyncIterable<unknown>,
|
|
83
|
+
mimetype,
|
|
84
|
+
"inbound",
|
|
85
|
+
maxBytes,
|
|
86
|
+
fileName,
|
|
87
|
+
).catch((err) => {
|
|
88
|
+
if (err instanceof Error && /Media exceeds/i.test(err.message)) {
|
|
89
|
+
throw new WhatsAppInboundMediaLimitExceededError(maxBytes);
|
|
90
|
+
}
|
|
91
|
+
throw err;
|
|
92
|
+
});
|
|
93
|
+
return { saved, mimetype, fileName };
|
|
94
|
+
} catch (err) {
|
|
95
|
+
if (err instanceof WhatsAppInboundMediaLimitExceededError) {
|
|
96
|
+
throw err;
|
|
97
|
+
}
|
|
98
|
+
logVerbose(`downloadMediaMessage failed: ${String(err)}`);
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export async function downloadQuotedInboundMedia(
|
|
104
|
+
msg: proto.IWebMessageInfo,
|
|
105
|
+
sock: Awaited<ReturnType<typeof createWaSocket>>,
|
|
106
|
+
maxBytes = 50 * 1024 * 1024,
|
|
107
|
+
): Promise<{ saved: SavedMedia; mimetype?: string; fileName?: string } | undefined> {
|
|
108
|
+
const message = unwrapMessage(msg.message as proto.IMessage | undefined);
|
|
109
|
+
const contextInfo = extractContextInfo(message);
|
|
110
|
+
if (!contextInfo?.quotedMessage) {
|
|
111
|
+
return undefined;
|
|
112
|
+
}
|
|
113
|
+
const quotedMessage = contextInfo.quotedMessage;
|
|
114
|
+
return downloadInboundMedia(
|
|
115
|
+
{
|
|
116
|
+
key: {
|
|
117
|
+
id: contextInfo?.stanzaId || undefined,
|
|
118
|
+
remoteJid: contextInfo.remoteJid ?? msg.key?.remoteJid ?? undefined,
|
|
119
|
+
participant: contextInfo?.participant ?? undefined,
|
|
120
|
+
fromMe: false,
|
|
121
|
+
},
|
|
122
|
+
message: quotedMessage,
|
|
123
|
+
messageTimestamp: msg.messageTimestamp,
|
|
124
|
+
},
|
|
125
|
+
sock,
|
|
126
|
+
maxBytes,
|
|
127
|
+
);
|
|
128
|
+
}
|