botify-wa 0.2.0 → 0.6.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.
@@ -0,0 +1,131 @@
1
+ /**
2
+ * messageTypes
3
+ * ------------
4
+ * Maps a raw WA message (`rawMessage.message`) to a friendly Botify type
5
+ * string, plus the inner content object for that type.
6
+ *
7
+ * Uses the vendored core's own `getContentType` / `normalizeMessageContent`
8
+ * so ephemeral messages, view-once wrappers (v1/v2/v3), edited-message
9
+ * wrappers, and "document with caption" wrappers are all unwrapped
10
+ * automatically — the returned type/content always describes the *real*
11
+ * message, not the envelope around it.
12
+ *
13
+ * Covers every message type a bot realistically needs to branch on. Types
14
+ * that exist in the protocol but aren't meaningful for bot logic (delivery
15
+ * receipts, history sync payloads, low-level sender-key distribution, etc.)
16
+ * fall through to "unsupported" rather than getting their own case.
17
+ */
18
+ import {
19
+ getContentType,
20
+ normalizeMessageContent,
21
+ } from "../../vendor/core/lib/index.js";
22
+
23
+ /** Raw WAProto content key → friendly Botify `ctx.type`. */
24
+ const TYPE_MAP = {
25
+ conversation: "text",
26
+ extendedTextMessage: "text",
27
+
28
+ imageMessage: "image",
29
+ videoMessage: "video",
30
+ ptvMessage: "video", // "video note" — still a video under the hood
31
+ audioMessage: "audio", // ctx.isPtt distinguishes voice notes
32
+ stickerMessage: "sticker",
33
+ documentMessage: "document",
34
+
35
+ contactMessage: "contact",
36
+ contactsArrayMessage: "contacts",
37
+ locationMessage: "location",
38
+ liveLocationMessage: "liveLocation",
39
+
40
+ pollCreationMessage: "poll",
41
+ pollCreationMessageV2: "poll",
42
+ pollCreationMessageV3: "poll",
43
+ pollCreationMessageV4: "poll",
44
+ pollCreationMessageV5: "poll",
45
+ pollCreationMessageV6: "poll",
46
+ pollUpdateMessage: "pollUpdate",
47
+
48
+ reactionMessage: "reaction",
49
+
50
+ buttonsMessage: "buttons",
51
+ buttonsResponseMessage: "buttonsResponse",
52
+ listMessage: "list",
53
+ listResponseMessage: "listResponse",
54
+ templateMessage: "template",
55
+ templateButtonReplyMessage: "templateButtonReply",
56
+ interactiveMessage: "interactive",
57
+ interactiveResponseMessage: "interactiveResponse",
58
+
59
+ groupInviteMessage: "groupInvite",
60
+ productMessage: "product",
61
+ orderMessage: "order",
62
+ eventMessage: "event",
63
+
64
+ protocolMessage: "protocol", // edit / delete(revoke) / disappearing-mode change
65
+ call: "call",
66
+ };
67
+
68
+ /** Content keys that represent an actual media attachment. */
69
+ const MEDIA_TYPES = new Set(["image", "video", "audio", "sticker", "document"]);
70
+
71
+ /**
72
+ * @param {object | null | undefined} rawMessageContent `rawMessage.message`
73
+ * @returns {{ type: string, key: string | null, content: object | null }}
74
+ */
75
+ export function detectMessageType(rawMessageContent) {
76
+ const normalized = normalizeMessageContent(rawMessageContent);
77
+ if (!normalized) return { type: "unknown", key: null, content: null };
78
+
79
+ const key = getContentType(normalized);
80
+ if (!key) return { type: "unknown", key: null, content: null };
81
+
82
+ const content = normalized[key];
83
+ const type = TYPE_MAP[key] ?? "unsupported";
84
+
85
+ return { type, key, content: content ?? {} };
86
+ }
87
+
88
+ /** Whether a Botify `type` string represents a downloadable media attachment. */
89
+ export function isMediaType(type) {
90
+ return MEDIA_TYPES.has(type);
91
+ }
92
+
93
+ /**
94
+ * Best-effort plain-text extraction — used for `ctx.text` / quoted text.
95
+ * Falls back through captions so `!command` still works if someone sends
96
+ * an image/video/document with the command in the caption.
97
+ */
98
+ export function extractPlainText(rawMessageContent) {
99
+ const normalized = normalizeMessageContent(rawMessageContent);
100
+ if (!normalized) return "";
101
+ return (
102
+ normalized.conversation ??
103
+ normalized.extendedTextMessage?.text ??
104
+ normalized.imageMessage?.caption ??
105
+ normalized.videoMessage?.caption ??
106
+ normalized.documentMessage?.caption ??
107
+ normalized.buttonsResponseMessage?.selectedDisplayText ??
108
+ normalized.listResponseMessage?.title ??
109
+ normalized.templateButtonReplyMessage?.selectedDisplayText ??
110
+ extractNativeFlowId(normalized.interactiveResponseMessage) ??
111
+ ""
112
+ );
113
+ }
114
+
115
+ /**
116
+ * Native-flow buttons/list menus (`interactiveMessage`, sent via
117
+ * `ctx.sendButtons()` / `ctx.sendListMenu()`) don't reply with a
118
+ * `selectedDisplayText` — WhatsApp echoes back an `interactiveResponseMessage`
119
+ * whose `nativeFlowResponseMessage.paramsJson` is a JSON string containing
120
+ * the tapped button/row's `id`. This pulls that `id` out (or `null` if the
121
+ * message isn't a native-flow response, or the JSON is malformed).
122
+ */
123
+ export function extractNativeFlowId(interactiveResponseMessage) {
124
+ const paramsJson = interactiveResponseMessage?.nativeFlowResponseMessage?.paramsJson;
125
+ if (!paramsJson) return null;
126
+ try {
127
+ return JSON.parse(paramsJson)?.id ?? null;
128
+ } catch {
129
+ return null;
130
+ }
131
+ }
@@ -322,6 +322,16 @@ export class Dugong {
322
322
  degreesLongitude:
323
323
  location.degressLongitude || location.degreesLongitude || 0,
324
324
  name: location.name || "",
325
+ address: location.address || "",
326
+ // Custom thumbnail overrides WA's auto-generated map snapshot.
327
+ ...(jpegThumbnail
328
+ ? {
329
+ jpegThumbnail:
330
+ typeof jpegThumbnail === "string"
331
+ ? Buffer.from(jpegThumbnail, "base64")
332
+ : jpegThumbnail,
333
+ }
334
+ : {}),
325
335
  },
326
336
  };
327
337
  mediaAttached =
@@ -765,4 +775,4 @@ export class Dugong {
765
775
  return msg;
766
776
  }
767
777
  }
768
- //# sourceMappingURL=dugong.js.map
778
+ //# sourceMappingURL=dugong.js.map
Binary file