@kyyinfinite/zapo-message 0.0.0-stage → 1.0.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.
Files changed (191) hide show
  1. package/CHANGELOG.md +159 -0
  2. package/LICENSE +20 -0
  3. package/README.md +532 -2
  4. package/build.config.json +28 -0
  5. package/dist/airich/airich.d.ts +180 -0
  6. package/dist/airich/airich.js +548 -0
  7. package/dist/airich/context.d.ts +24 -0
  8. package/dist/airich/context.js +48 -0
  9. package/dist/airich/encode.d.ts +3 -0
  10. package/dist/airich/encode.js +16 -0
  11. package/dist/airich/index.d.ts +20 -0
  12. package/dist/airich/index.js +11 -0
  13. package/dist/airich/inline.d.ts +45 -0
  14. package/dist/airich/inline.js +152 -0
  15. package/dist/airich/nodes.d.ts +30 -0
  16. package/dist/airich/nodes.js +121 -0
  17. package/dist/airich/primitives.d.ts +144 -0
  18. package/dist/airich/primitives.js +230 -0
  19. package/dist/airich/protocol.d.ts +35 -0
  20. package/dist/airich/protocol.js +65 -0
  21. package/dist/airich/table.d.ts +22 -0
  22. package/dist/airich/table.js +35 -0
  23. package/dist/airich/tokenizer.d.ts +13 -0
  24. package/dist/airich/tokenizer.js +717 -0
  25. package/dist/airich/transport.d.ts +15 -0
  26. package/dist/airich/transport.js +56 -0
  27. package/dist/branding.d.ts +5 -0
  28. package/dist/branding.js +9 -0
  29. package/dist/btn/builders.d.ts +246 -0
  30. package/dist/btn/builders.js +710 -0
  31. package/dist/btn/media-slot.d.ts +28 -0
  32. package/dist/btn/media-slot.js +323 -0
  33. package/dist/btn/media-source.d.ts +38 -0
  34. package/dist/btn/media-source.js +116 -0
  35. package/dist/btn/sharp-loader.d.ts +5 -0
  36. package/dist/btn/sharp-loader.js +18 -0
  37. package/dist/btn/thumbnail.d.ts +21 -0
  38. package/dist/btn/thumbnail.js +124 -0
  39. package/dist/btn/validators.d.ts +40 -0
  40. package/dist/btn/validators.js +135 -0
  41. package/dist/btn.d.ts +1 -0
  42. package/dist/btn.js +1 -0
  43. package/dist/core/async-send.d.ts +20 -0
  44. package/dist/core/async-send.js +1 -0
  45. package/dist/core/base-builder.d.ts +20 -0
  46. package/dist/core/base-builder.js +67 -0
  47. package/dist/core/errors.d.ts +30 -0
  48. package/dist/core/errors.js +61 -0
  49. package/dist/download.d.ts +24 -0
  50. package/dist/download.js +123 -0
  51. package/dist/index.d.ts +38 -0
  52. package/dist/index.js +22 -0
  53. package/dist/integration/augment.d.ts +97 -0
  54. package/dist/integration/augment.js +1 -0
  55. package/dist/integration/auto.d.ts +8 -0
  56. package/dist/integration/auto.js +79 -0
  57. package/dist/integration/index.d.ts +4 -0
  58. package/dist/integration/index.js +2 -0
  59. package/dist/integration/methods.d.ts +25 -0
  60. package/dist/integration/methods.js +24 -0
  61. package/dist/interactive.d.ts +24 -0
  62. package/dist/interactive.js +121 -0
  63. package/dist/media-gate.d.ts +99 -0
  64. package/dist/media-gate.js +322 -0
  65. package/dist/message/album.d.ts +33 -0
  66. package/dist/message/album.js +107 -0
  67. package/dist/message/contact.d.ts +29 -0
  68. package/dist/message/contact.js +123 -0
  69. package/dist/message/forward.d.ts +23 -0
  70. package/dist/message/forward.js +77 -0
  71. package/dist/message/index.d.ts +26 -0
  72. package/dist/message/index.js +13 -0
  73. package/dist/message/link-preview.d.ts +28 -0
  74. package/dist/message/link-preview.js +98 -0
  75. package/dist/message/location.d.ts +24 -0
  76. package/dist/message/location.js +94 -0
  77. package/dist/message/media.d.ts +90 -0
  78. package/dist/message/media.js +123 -0
  79. package/dist/message/poll.d.ts +24 -0
  80. package/dist/message/poll.js +98 -0
  81. package/dist/message/reaction.d.ts +20 -0
  82. package/dist/message/reaction.js +71 -0
  83. package/dist/message/sticker.d.ts +29 -0
  84. package/dist/message/sticker.js +44 -0
  85. package/dist/message/target.d.ts +18 -0
  86. package/dist/message/target.js +24 -0
  87. package/dist/message/text.d.ts +17 -0
  88. package/dist/message/text.js +29 -0
  89. package/dist/message/vcard.d.ts +9 -0
  90. package/dist/message/vcard.js +23 -0
  91. package/dist/message/webp.d.ts +6 -0
  92. package/dist/message/webp.js +57 -0
  93. package/dist/payloads.d.ts +180 -0
  94. package/dist/payloads.js +1 -0
  95. package/dist/serialize.d.ts +41 -0
  96. package/dist/serialize.js +93 -0
  97. package/dist/status.d.ts +3 -0
  98. package/dist/status.js +21 -0
  99. package/dist/zapo-client.d.ts +44 -0
  100. package/dist/zapo-client.js +1 -0
  101. package/examples/airich.ts +59 -0
  102. package/examples/buttons.ts +48 -0
  103. package/examples/client-methods.ts +58 -0
  104. package/examples/hybrid.ts +133 -0
  105. package/examples/serialize.ts +61 -0
  106. package/package.json +271 -4
  107. package/scripts/check-no-comments.mjs +101 -0
  108. package/scripts/compare-golden.mjs +461 -0
  109. package/scripts/lib-config.mjs +9 -0
  110. package/scripts/smoke-auto.mjs +75 -0
  111. package/scripts/test-external-consumer.mjs +316 -0
  112. package/scripts/verify-package.mjs +78 -0
  113. package/src/airich/airich.ts +970 -0
  114. package/src/airich/context.ts +96 -0
  115. package/src/airich/encode.ts +25 -0
  116. package/src/airich/index.ts +98 -0
  117. package/src/airich/inline.ts +272 -0
  118. package/src/airich/nodes.ts +174 -0
  119. package/src/airich/primitives.ts +474 -0
  120. package/src/airich/protocol.ts +133 -0
  121. package/src/airich/table.ts +70 -0
  122. package/src/airich/tokenizer.ts +799 -0
  123. package/src/airich/transport.ts +106 -0
  124. package/src/branding.ts +23 -0
  125. package/src/btn/builders.ts +1073 -0
  126. package/src/btn/media-slot.ts +486 -0
  127. package/src/btn/media-source.ts +189 -0
  128. package/src/btn/sharp-loader.ts +23 -0
  129. package/src/btn/thumbnail.ts +182 -0
  130. package/src/btn/validators.ts +237 -0
  131. package/src/btn.ts +8 -0
  132. package/src/core/async-send.ts +28 -0
  133. package/src/core/base-builder.ts +104 -0
  134. package/src/core/errors.ts +82 -0
  135. package/src/download.ts +212 -0
  136. package/src/index.ts +229 -0
  137. package/src/integration/augment.ts +262 -0
  138. package/src/integration/auto.ts +103 -0
  139. package/src/integration/index.ts +8 -0
  140. package/src/integration/methods.ts +45 -0
  141. package/src/interactive.ts +188 -0
  142. package/src/media-gate.ts +499 -0
  143. package/src/message/album.ts +187 -0
  144. package/src/message/contact.ts +170 -0
  145. package/src/message/forward.ts +123 -0
  146. package/src/message/index.ts +83 -0
  147. package/src/message/link-preview.ts +148 -0
  148. package/src/message/location.ts +150 -0
  149. package/src/message/media.ts +323 -0
  150. package/src/message/poll.ts +158 -0
  151. package/src/message/reaction.ts +133 -0
  152. package/src/message/sticker.ts +100 -0
  153. package/src/message/target.ts +38 -0
  154. package/src/message/text.ts +61 -0
  155. package/src/message/vcard.ts +36 -0
  156. package/src/message/webp.ts +78 -0
  157. package/src/payloads.ts +204 -0
  158. package/src/serialize.ts +180 -0
  159. package/src/status.ts +29 -0
  160. package/src/zapo-client.ts +84 -0
  161. package/test/airich-inline.test.ts +200 -0
  162. package/test/airich-primitives.test.ts +226 -0
  163. package/test/airich-tokenizer.test.ts +145 -0
  164. package/test/airich.golden.test.ts +406 -0
  165. package/test/airich.test.ts +1006 -0
  166. package/test/btn-button.test.ts +416 -0
  167. package/test/btn-header.test.ts +646 -0
  168. package/test/btn-legacy.test.ts +342 -0
  169. package/test/btn-lifecycle.test.ts +295 -0
  170. package/test/btn-list.test.ts +261 -0
  171. package/test/btn-media-source.test.ts +150 -0
  172. package/test/btn-thumbnail.test.ts +160 -0
  173. package/test/builder-compat.test.ts +264 -0
  174. package/test/helpers.ts +27 -0
  175. package/test/hybrid-airich.test.ts +345 -0
  176. package/test/hybrid-client.test.ts +239 -0
  177. package/test/hybrid-errors.test.ts +557 -0
  178. package/test/hybrid-interactive.test.ts +455 -0
  179. package/test/hybrid.golden.test.ts +349 -0
  180. package/test/hybrid.test.ts +479 -0
  181. package/test/integration-auto.test.ts +159 -0
  182. package/test/integration.fakeserver.test.ts +440 -0
  183. package/test/media-gate.test.ts +215 -0
  184. package/test/message-album.test.ts +145 -0
  185. package/test/message-builders.test.ts +401 -0
  186. package/test/message-media.test.ts +303 -0
  187. package/test/message.fakeserver.test.ts +284 -0
  188. package/test/message.golden.test.ts +254 -0
  189. package/test/serializer.test.ts +448 -0
  190. package/tsconfig.json +26 -0
  191. package/tsconfig.test.json +10 -0
@@ -0,0 +1,38 @@
1
+ export interface MessageKeyLike {
2
+ remoteJid?: string;
3
+ id?: string;
4
+ fromMe?: boolean;
5
+ participant?: string;
6
+ }
7
+
8
+ export interface MessageTargetLike {
9
+ key?: MessageKeyLike;
10
+ id?: string;
11
+ remoteJid?: string;
12
+ fromMe?: boolean;
13
+ participant?: string;
14
+ message?: unknown;
15
+ rawNode?: unknown;
16
+ }
17
+
18
+ export const targetKeyOf = (target: unknown): MessageKeyLike | undefined => {
19
+ if (!target || typeof target !== "object") return undefined;
20
+ const ref = target as MessageTargetLike;
21
+ if (ref.key && typeof ref.key === "object") return ref.key;
22
+ if (typeof ref.id === "string") return ref;
23
+ return undefined;
24
+ };
25
+
26
+ export const targetChatJidOf = (target: unknown): string | undefined => {
27
+ const key = targetKeyOf(target);
28
+ const jid = key?.remoteJid;
29
+ if (typeof jid === "string" && jid) return jid;
30
+ return undefined;
31
+ };
32
+
33
+ export const targetIdOf = (target: unknown): string | undefined => {
34
+ const key = targetKeyOf(target);
35
+ const id = key?.id;
36
+ if (typeof id === "string" && id) return id;
37
+ return undefined;
38
+ };
@@ -0,0 +1,61 @@
1
+ import { BaseBuilder } from "../core/base-builder.js";
2
+ import { sendConfigured, type MessageSendOptions } from "../core/async-send.js";
3
+ import type { MessageSendResult } from "../zapo-client.js";
4
+ import { assertText } from "../btn/validators.js";
5
+ import type { TextBuildResult } from "../payloads.js";
6
+ import type { ZapoClient } from "../zapo-client.js";
7
+
8
+ export interface TextConfig {
9
+ body: string;
10
+ }
11
+
12
+ export class TextBuilder extends BaseBuilder<TextBuildResult> {
13
+ #text: string | undefined;
14
+
15
+ body(text: string): this {
16
+ this.#text = assertText(text, "text.body");
17
+ return this;
18
+ }
19
+
20
+ override build(): TextBuildResult {
21
+ if (this.#text === undefined) {
22
+ this.#text = assertText(undefined, "text.body");
23
+ }
24
+ return { conversation: this.#text };
25
+ }
26
+ }
27
+
28
+ export function text(client?: ZapoClient): TextBuilder;
29
+ export function text(
30
+ client: ZapoClient | undefined,
31
+ jid: string,
32
+ config: TextConfig,
33
+ options?: MessageSendOptions,
34
+ ): Promise<MessageSendResult>;
35
+ export function text(
36
+ client?: ZapoClient,
37
+ jid?: string,
38
+ config?: TextConfig,
39
+ options?: MessageSendOptions,
40
+ ): TextBuilder | Promise<MessageSendResult> {
41
+ if (config === undefined) return new TextBuilder(client);
42
+ return sendText(client, jid, config, options);
43
+ }
44
+
45
+ export const applyTextConfig = (
46
+ builder: TextBuilder,
47
+ config: TextConfig,
48
+ ): void => {
49
+ builder.body(config.body);
50
+ };
51
+
52
+ export const sendText = async (
53
+ client: ZapoClient | undefined,
54
+ jid: string | undefined,
55
+ config: TextConfig,
56
+ options?: MessageSendOptions,
57
+ ): Promise<MessageSendResult> => {
58
+ const builder = new TextBuilder(client);
59
+ applyTextConfig(builder, config);
60
+ return sendConfigured(builder, jid, options);
61
+ };
@@ -0,0 +1,36 @@
1
+ export interface VcardInput {
2
+ displayName: string;
3
+ phoneNumber?: string;
4
+ organization?: string;
5
+ email?: string;
6
+ url?: string;
7
+ note?: string;
8
+ }
9
+
10
+ const escapeVcardValue = (value: string): string =>
11
+ value.replace(/([\\,;])/g, "\\$1").replace(/\n/g, "\\n");
12
+
13
+ export const buildVcard = (input: VcardInput): string => {
14
+ const lines = ["BEGIN:VCARD", "VERSION:3.0"];
15
+ lines.push(`FN:${escapeVcardValue(input.displayName)}`);
16
+ lines.push(`N:;${escapeVcardValue(input.displayName)};;;`);
17
+ if (input.phoneNumber !== undefined) {
18
+ lines.push(
19
+ `TEL;type=CELL;type=VOICE:${escapeVcardValue(input.phoneNumber)}`,
20
+ );
21
+ }
22
+ if (input.organization !== undefined) {
23
+ lines.push(`ORG:${escapeVcardValue(input.organization)}`);
24
+ }
25
+ if (input.email !== undefined) {
26
+ lines.push(`EMAIL;type=INTERNET:${escapeVcardValue(input.email)}`);
27
+ }
28
+ if (input.url !== undefined) {
29
+ lines.push(`URL:${escapeVcardValue(input.url)}`);
30
+ }
31
+ if (input.note !== undefined) {
32
+ lines.push(`NOTE:${escapeVcardValue(input.note)}`);
33
+ }
34
+ lines.push("END:VCARD");
35
+ return lines.join("\n");
36
+ };
@@ -0,0 +1,78 @@
1
+ export interface WebpInfo {
2
+ width: number | undefined;
3
+ height: number | undefined;
4
+ isAnimated: boolean;
5
+ }
6
+
7
+ const RIFF = 0x52494646;
8
+ const WEBP = 0x57454250;
9
+
10
+ const readUInt32BE = (bytes: Uint8Array, offset: number): number =>
11
+ (bytes[offset]! * 0x1000000 +
12
+ (bytes[offset + 1]! << 16) +
13
+ (bytes[offset + 2]! << 8) +
14
+ bytes[offset + 3]!) >>>
15
+ 0;
16
+
17
+ const readUInt32LE = (bytes: Uint8Array, offset: number): number =>
18
+ (bytes[offset]! |
19
+ (bytes[offset + 1]! << 8) |
20
+ (bytes[offset + 2]! << 16) |
21
+ (bytes[offset + 3]! << 24)) >>>
22
+ 0;
23
+
24
+ const readUInt24LE = (bytes: Uint8Array, offset: number): number =>
25
+ (bytes[offset]! | (bytes[offset + 1]! << 8) | (bytes[offset + 2]! << 16)) >>>
26
+ 0;
27
+
28
+ const readUInt16LE = (bytes: Uint8Array, offset: number): number =>
29
+ bytes[offset]! | (bytes[offset + 1]! << 8);
30
+
31
+ const isWebpContainer = (bytes: Uint8Array): boolean =>
32
+ bytes.byteLength >= 12 &&
33
+ readUInt32BE(bytes, 0) === RIFF &&
34
+ readUInt32BE(bytes, 8) === WEBP;
35
+
36
+ export const probeWebp = (bytes: Uint8Array): WebpInfo | undefined => {
37
+ if (!isWebpContainer(bytes)) return undefined;
38
+ const info: WebpInfo = {
39
+ width: undefined,
40
+ height: undefined,
41
+ isAnimated: false,
42
+ };
43
+
44
+ let offset = 12;
45
+ while (offset + 8 <= bytes.byteLength) {
46
+ const chunkView = bytes.subarray(offset, offset + 4);
47
+ const chunk = String.fromCharCode(
48
+ chunkView[0]!,
49
+ chunkView[1]!,
50
+ chunkView[2]!,
51
+ chunkView[3]!,
52
+ );
53
+ const size = readUInt32LE(bytes, offset + 4);
54
+ const payload = offset + 8;
55
+ if (payload + size > bytes.byteLength) break;
56
+
57
+ if (chunk === "VP8X" && size >= 10) {
58
+ info.width = readUInt24LE(bytes, payload + 4) + 1;
59
+ info.height = readUInt24LE(bytes, payload + 7) + 1;
60
+ } else if (chunk === "ANIM") {
61
+ info.isAnimated = true;
62
+ } else if (chunk === "VP8 " && info.width === undefined && size >= 10) {
63
+ info.width = readUInt16LE(bytes, payload + 6) & 0x3fff;
64
+ info.height = readUInt16LE(bytes, payload + 8) & 0x3fff;
65
+ } else if (chunk === "VP8L" && info.width === undefined && size >= 5) {
66
+ const bits =
67
+ bytes[payload + 1]! |
68
+ (bytes[payload + 2]! << 8) |
69
+ (bytes[payload + 3]! << 16) |
70
+ (bytes[payload + 4]! << 24);
71
+ info.width = (bits & 0x3fff) + 1;
72
+ info.height = ((bits >>> 14) & 0x3fff) + 1;
73
+ }
74
+
75
+ offset = payload + size + (size % 2);
76
+ }
77
+ return info;
78
+ };
@@ -0,0 +1,204 @@
1
+ import type { Proto } from "zapo-js";
2
+
3
+ export type InteractiveHeaderPayload =
4
+ Proto.Message.InteractiveMessage.Header.$Properties;
5
+ export type LocationMessagePayload = Proto.Message.LocationMessage.$Properties;
6
+ export type ContextInfoPayload = Proto.ContextInfo.$Properties;
7
+
8
+ export interface NativeFlowButtonPayload {
9
+ name: string;
10
+ buttonParamsJson: string;
11
+ }
12
+
13
+ export interface NativeFlowMessagePayload {
14
+ buttons: NativeFlowButtonPayload[];
15
+ messageVersion: number;
16
+ messageParamsJson?: string;
17
+ }
18
+
19
+ export interface InteractiveCardPayload {
20
+ body: { text: string };
21
+ nativeFlowMessage: NativeFlowMessagePayload;
22
+ header?: InteractiveHeaderPayload;
23
+ footer?: { text: string };
24
+ }
25
+
26
+ export interface ButtonsMessagePayload {
27
+ contentText: string;
28
+ buttons: Proto.Message.ButtonsMessage.Button.$Properties[];
29
+ headerType: Proto.Message.ButtonsMessage.HeaderType;
30
+ text?: string;
31
+ footerText?: string;
32
+ imageMessage?: Proto.Message.ImageMessage.$Properties;
33
+ videoMessage?: Proto.Message.VideoMessage.$Properties;
34
+ documentMessage?: Proto.Message.DocumentMessage.$Properties;
35
+ locationMessage?: LocationMessagePayload | null;
36
+ }
37
+
38
+ export interface ListMessagePayload {
39
+ title: string;
40
+ description: string;
41
+ buttonText: string;
42
+ listType: Proto.Message.ListMessage.ListType;
43
+ sections: Proto.Message.ListMessage.Section.$Properties[];
44
+ footerText?: string;
45
+ }
46
+
47
+ export interface ButtonBuildResult {
48
+ interactiveMessage: InteractiveCardPayload;
49
+ }
50
+
51
+ export interface LegacyButtonBuildResult {
52
+ buttonsMessage: ButtonsMessagePayload;
53
+ }
54
+
55
+ export interface ListBuildResult {
56
+ listMessage: ListMessagePayload;
57
+ }
58
+
59
+ export type MediaSlotResolution =
60
+ | { field: "imageMessage"; message: Proto.Message.ImageMessage.$Properties }
61
+ | { field: "videoMessage"; message: Proto.Message.VideoMessage.$Properties }
62
+ | {
63
+ field: "documentMessage";
64
+ message: Proto.Message.DocumentMessage.$Properties;
65
+ }
66
+ | {
67
+ field: "audioMessage";
68
+ message: Proto.Message.AudioMessage.$Properties;
69
+ }
70
+ | {
71
+ field: "stickerMessage";
72
+ message: Proto.Message.StickerMessage.$Properties;
73
+ };
74
+
75
+ export interface CodeMetadataPayload {
76
+ codeLanguage: string;
77
+ codeBlocks: { codeContent: string; highlightType: number }[];
78
+ }
79
+
80
+ export interface TableMetadataPayload {
81
+ title: string;
82
+ rows: { items: string[]; isHeading?: boolean }[];
83
+ }
84
+
85
+ export type SubmessagePayload =
86
+ | { messageType: Proto.AIRichResponseSubMessageType; messageText: string }
87
+ | {
88
+ messageType: Proto.AIRichResponseSubMessageType;
89
+ codeMetadata: CodeMetadataPayload;
90
+ }
91
+ | {
92
+ messageType: Proto.AIRichResponseSubMessageType;
93
+ tableMetadata: TableMetadataPayload;
94
+ };
95
+
96
+ export interface RichResponseMessagePayload {
97
+ messageType: Proto.AIRichResponseMessageType;
98
+ submessages: SubmessagePayload[];
99
+ unifiedResponse: { data: Uint8Array };
100
+ contextInfo: Record<string, unknown>;
101
+ messageText?: string | undefined;
102
+ }
103
+
104
+ export interface RichResponseBuildResult {
105
+ richResponseMessage: RichResponseMessagePayload;
106
+ }
107
+
108
+ export interface BotForwardedEnvelope {
109
+ message: RichResponseBuildResult;
110
+ }
111
+
112
+ export interface WrapBotForwardedResult {
113
+ botForwardedMessage: BotForwardedEnvelope;
114
+ }
115
+
116
+ export interface MessageContextInfoPayload {
117
+ deviceListMetadataVersion: number;
118
+ messageSecret: Uint8Array;
119
+ }
120
+
121
+ export interface PlaceholderBuildResult extends WrapBotForwardedResult {
122
+ messageContextInfo: MessageContextInfoPayload;
123
+ }
124
+
125
+ export interface EditBuildResult {
126
+ botForwardedMessage: {
127
+ message: {
128
+ protocolMessage: {
129
+ key: { remoteJid: string; fromMe: true; id: string };
130
+ type: Proto.Message.ProtocolMessage.Type;
131
+ editedMessage: WrapBotForwardedResult;
132
+ };
133
+ };
134
+ };
135
+ }
136
+
137
+ export interface TextBuildResult {
138
+ conversation: string;
139
+ }
140
+
141
+ export interface ExtendedTextMessagePayload {
142
+ text: string;
143
+ matchedText: string;
144
+ previewType: Proto.Message.ExtendedTextMessage.PreviewType;
145
+ title?: string;
146
+ description?: string;
147
+ jpegThumbnail?: Uint8Array;
148
+ }
149
+
150
+ export interface LinkPreviewBuildResult {
151
+ extendedTextMessage: ExtendedTextMessagePayload;
152
+ }
153
+
154
+ export interface ContactMessagePayload {
155
+ displayName: string;
156
+ vcard: string;
157
+ }
158
+
159
+ export interface ContactBuildResult {
160
+ contactMessage: ContactMessagePayload;
161
+ }
162
+
163
+ export interface AlbumMessagePayload {
164
+ expectedImageCount: number;
165
+ expectedVideoCount: number;
166
+ }
167
+
168
+ export interface AlbumBuildResult {
169
+ albumMessage: AlbumMessagePayload;
170
+ }
171
+
172
+ export interface LocationBuildResult {
173
+ locationMessage: LocationMessagePayload;
174
+ }
175
+
176
+ export type AlbumChildPayload = { field: string; message: unknown };
177
+
178
+ export type MediaMessageKind = "image" | "video" | "audio" | "document";
179
+
180
+ export type MediaBuildResult =
181
+ | { imageMessage: Proto.Message.ImageMessage.$Properties }
182
+ | { videoMessage: Proto.Message.VideoMessage.$Properties }
183
+ | { audioMessage: Proto.Message.AudioMessage.$Properties }
184
+ | { documentMessage: Proto.Message.DocumentMessage.$Properties }
185
+ | { stickerMessage: Proto.Message.StickerMessage.$Properties };
186
+
187
+ export const mediaSlotToMessage = (
188
+ resolution: MediaSlotResolution,
189
+ ): MediaBuildResult =>
190
+ ({ [resolution.field]: resolution.message }) as MediaBuildResult;
191
+
192
+ export type BuilderMessagePayload =
193
+ | ButtonBuildResult
194
+ | LegacyButtonBuildResult
195
+ | ListBuildResult
196
+ | WrapBotForwardedResult
197
+ | PlaceholderBuildResult
198
+ | EditBuildResult
199
+ | TextBuildResult
200
+ | LinkPreviewBuildResult
201
+ | ContactBuildResult
202
+ | AlbumBuildResult
203
+ | LocationBuildResult
204
+ | MediaBuildResult;
@@ -0,0 +1,180 @@
1
+ import { getContentType, toUserJid, unwrapMessage } from "zapo-js";
2
+ import { mDownload, qDownload } from "./download.js";
3
+ import {
4
+ extractInteractiveResponse,
5
+ type ExtractedInteractiveResponse,
6
+ } from "./interactive.js";
7
+ import { applyReplyBranding } from "./branding.js";
8
+ import type { ZapoDownloadCapableClient } from "./zapo-client.js";
9
+
10
+ export interface SerializedKey {
11
+ remoteJid?: string | undefined;
12
+ participant?: string | undefined;
13
+ id?: string | undefined;
14
+ fromMe?: boolean | undefined;
15
+ isGroup?: boolean | undefined;
16
+ remoteJidAlt?: string | undefined;
17
+ participantAlt?: string | undefined;
18
+ [key: string]: unknown;
19
+ }
20
+
21
+ export interface SerializedEvent {
22
+ key: SerializedKey;
23
+ message?: Record<string, unknown>;
24
+ [key: string]: unknown;
25
+ }
26
+
27
+ export interface MSerialized extends SerializedEvent {
28
+ content: string | undefined;
29
+ text: string | undefined;
30
+ interactive: ExtractedInteractiveResponse | undefined;
31
+ q: QSerialized | undefined;
32
+ reply(text: string): Promise<unknown>;
33
+ react(emoji: string): Promise<unknown>;
34
+ download(dataType: "buffer" | "stream"): Promise<unknown>;
35
+ readonly client: ZapoDownloadCapableClient;
36
+ }
37
+
38
+ export interface QSerialized {
39
+ key: SerializedKey;
40
+ message: Record<string, unknown>;
41
+ content: string | undefined;
42
+ text: string | undefined;
43
+ interactive: ExtractedInteractiveResponse | undefined;
44
+ reply(text: string): Promise<unknown>;
45
+ react(emoji: string): Promise<unknown>;
46
+ download(dataType: "bytes" | "stream"): Promise<unknown>;
47
+ readonly client: ZapoDownloadCapableClient;
48
+ }
49
+
50
+ export const extractText = (
51
+ message: Record<string, unknown> | undefined,
52
+ content: string | undefined,
53
+ ): string | undefined => {
54
+ const contentMessage = message?.[content ?? ""] as
55
+ Record<string, unknown> | undefined;
56
+ return (
57
+ (contentMessage?.text as string | undefined) ??
58
+ (message?.conversation as string | undefined) ??
59
+ (contentMessage?.caption as string | undefined) ??
60
+ extractInteractiveText(message)
61
+ );
62
+ };
63
+
64
+ const extractInteractiveText = (
65
+ message: Record<string, unknown> | undefined,
66
+ ): string | undefined => {
67
+ if (!message || typeof message !== "object") return undefined;
68
+ const msg = unwrapMessage(message) as Record<string, unknown>;
69
+ if (!(
70
+ msg.buttonsResponseMessage ||
71
+ msg.listResponseMessage ||
72
+ msg.templateButtonReplyMessage ||
73
+ msg.interactiveResponseMessage
74
+ ))
75
+ return undefined;
76
+ return extractInteractiveResponse(msg)?.id;
77
+ };
78
+
79
+ function mReply(this: MSerialized, text: string): Promise<unknown> {
80
+ return this.client.message.send(
81
+ this.key.remoteJid as string,
82
+ text,
83
+ applyReplyBranding(text, {}),
84
+ );
85
+ }
86
+
87
+ function mReact(this: MSerialized, emoji: string): Promise<unknown> {
88
+ return this.client.message.send(this.key.remoteJid as string, {
89
+ type: "reaction",
90
+ target: this,
91
+ emoji,
92
+ });
93
+ }
94
+
95
+ function qReply(this: QSerialized, text: string): Promise<unknown> {
96
+ return this.client.message.send(
97
+ this.key.remoteJid as string,
98
+ text,
99
+ applyReplyBranding(text, {}),
100
+ );
101
+ }
102
+
103
+ function qReact(this: QSerialized, emoji: string): Promise<unknown> {
104
+ return this.client.message.send(this.key.remoteJid as string, {
105
+ type: "reaction",
106
+ target: this.key,
107
+ emoji,
108
+ });
109
+ }
110
+
111
+ export const mSerialize = (
112
+ event: SerializedEvent,
113
+ client: ZapoDownloadCapableClient,
114
+ ): MSerialized => {
115
+ const m = {
116
+ ...event,
117
+ content: getContentType(event?.message),
118
+ reply: mReply,
119
+ download: mDownload,
120
+ react: mReact,
121
+ } as MSerialized;
122
+
123
+ Object.defineProperty(m, "client", {
124
+ enumerable: false,
125
+ get() {
126
+ return client;
127
+ },
128
+ });
129
+
130
+ m.text = extractText(event?.message, m.content);
131
+
132
+ m.interactive = extractInteractiveResponse(event?.message);
133
+
134
+ m.q = qSerialize(m);
135
+
136
+ return m;
137
+ };
138
+
139
+ export const qSerialize = (m: MSerialized): QSerialized | undefined => {
140
+ const contentMessage = (m.message as Record<string, unknown> | undefined)?.[
141
+ m.content as string
142
+ ] as Record<string, unknown> | undefined;
143
+ const contextInfo = contentMessage?.contextInfo as
144
+ Record<string, unknown> | undefined;
145
+ if (!contextInfo?.quotedMessage) return undefined;
146
+
147
+ const message = contextInfo.quotedMessage as Record<string, unknown>;
148
+
149
+ const key: SerializedKey = {
150
+ remoteJid: m.key.remoteJid,
151
+ participant: contextInfo.participant as string | undefined,
152
+ id: contextInfo.stanzaId as string | undefined,
153
+ fromMe:
154
+ contextInfo.participant ===
155
+ toUserJid(m.client.auth?.getCurrentCredentials?.()?.meLid as string),
156
+ };
157
+ const content = getContentType(message);
158
+
159
+ const q = {
160
+ key,
161
+ message,
162
+ content,
163
+ reply: qReply,
164
+ react: qReact,
165
+ download: qDownload,
166
+ } as QSerialized;
167
+
168
+ q.text = extractText(message, content);
169
+
170
+ q.interactive = extractInteractiveResponse(message);
171
+
172
+ Object.defineProperty(q, "client", {
173
+ enumerable: false,
174
+ get() {
175
+ return m.client;
176
+ },
177
+ });
178
+
179
+ return q;
180
+ };
package/src/status.ts ADDED
@@ -0,0 +1,29 @@
1
+ const BOLD_SANS_BASE = 0x1d5d4;
2
+
3
+ export const toBoldSans = (text: string): string =>
4
+ [...text]
5
+ .map((ch) => {
6
+ const code = ch.codePointAt(0);
7
+ if (code === undefined) return ch;
8
+ if (code >= 65 && code <= 90)
9
+ return String.fromCodePoint(BOLD_SANS_BASE + (code - 65));
10
+ return ch;
11
+ })
12
+ .join("");
13
+
14
+ export type StatusType = "ok" | "warn" | "fail";
15
+
16
+ const STATUS_MAP: Record<StatusType, { emoji: string; label: string }> = {
17
+ ok: { emoji: "✅", label: "SUKSES" },
18
+ warn: { emoji: "⚠️", label: "GAGAL" },
19
+ fail: { emoji: "❌", label: "ERROR" },
20
+ };
21
+
22
+ export const formatStatus = (
23
+ type: StatusType,
24
+ text: string | string[],
25
+ ): string => {
26
+ const { emoji, label } = STATUS_MAP[type];
27
+ const lines = Array.isArray(text) ? text : String(text).split("\n");
28
+ return `${emoji} ${toBoldSans(label)}\n\n${lines.map((l) => "> " + l).join("\n")}`;
29
+ };
@@ -0,0 +1,84 @@
1
+ import type { Readable } from "node:stream";
2
+ import type {
3
+ WaDownloadMediaOptions,
4
+ WaMediaUploadResult,
5
+ WaMessagePublishResult,
6
+ WaSendMessageContent,
7
+ WaSendMessageOptions,
8
+ WaUploadMediaOptions,
9
+ WaUploadMediaSource,
10
+ } from "zapo-js";
11
+ import type { BuilderMessagePayload } from "./payloads.js";
12
+
13
+ export type ZapoSendOptions = { quote?: unknown } & Record<string, unknown>;
14
+
15
+ export interface ZapoReactionContent {
16
+ type: "reaction";
17
+ target: unknown;
18
+ emoji: string;
19
+ }
20
+
21
+ export interface ZapoPollContent {
22
+ type: "poll";
23
+ name: string;
24
+ options: string[];
25
+ selectableCount?: number;
26
+ allowAddOption?: boolean;
27
+ hideParticipantName?: boolean;
28
+ }
29
+
30
+ export type ZapoMessageContent =
31
+ | WaSendMessageContent
32
+ | BuilderMessagePayload
33
+ | ZapoReactionContent
34
+ | ZapoPollContent;
35
+
36
+ export interface ZapoMessageCoordinator {
37
+ send(
38
+ to: string,
39
+ content: ZapoMessageContent,
40
+ options?: WaSendMessageOptions | ZapoSendOptions,
41
+ ): Promise<WaMessagePublishResult>;
42
+ upload?(
43
+ source: WaUploadMediaSource,
44
+ options: WaUploadMediaOptions,
45
+ ): Promise<WaMediaUploadResult>;
46
+ download?(
47
+ source: unknown,
48
+ options?: WaDownloadMediaOptions,
49
+ ): Promise<Readable>;
50
+ downloadBytes?(
51
+ source: unknown,
52
+ options?: WaDownloadMediaOptions,
53
+ ): Promise<Uint8Array>;
54
+ }
55
+
56
+ export interface ZapoAuthLike {
57
+ getCurrentCredentials?():
58
+ { meJid?: string; meLid?: string } | undefined | null;
59
+ }
60
+
61
+ export interface ZapoClient {
62
+ message: ZapoMessageCoordinator;
63
+ auth?: ZapoAuthLike;
64
+ }
65
+
66
+ export type ZapoDownloadCapableClient = ZapoClient & {
67
+ message: ZapoMessageCoordinator & {
68
+ download(
69
+ source: unknown,
70
+ options?: WaDownloadMediaOptions,
71
+ ): Promise<Readable>;
72
+ downloadBytes(
73
+ source: unknown,
74
+ options?: WaDownloadMediaOptions,
75
+ ): Promise<Uint8Array>;
76
+ };
77
+ };
78
+
79
+ export type MessageSendResult = WaMessagePublishResult;
80
+
81
+ export const isZapoMessageCoordinatorShape = (
82
+ value: unknown,
83
+ ): value is ZapoClient =>
84
+ typeof (value as ZapoClient | null | undefined)?.message?.send === "function";