@kyyinfinite/zapo-message 0.0.0-stage → 1.0.1

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 +1401 -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 +36 -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,323 @@
1
+ import { BaseBuilder } from "../core/base-builder.js";
2
+ import { MessageError, ValidationError } from "../core/errors.js";
3
+ import { sendConfigured, type MessageSendOptions } from "../core/async-send.js";
4
+ import type { MessageSendResult } from "../zapo-client.js";
5
+ import {
6
+ resolveMediaSlot,
7
+ type MediaSlot,
8
+ type MediaSlotOptions,
9
+ } from "../btn/media-slot.js";
10
+ import type { MediaSource } from "../btn/media-source.js";
11
+ import {
12
+ mediaSlotToMessage,
13
+ type MediaBuildResult,
14
+ type MediaMessageKind,
15
+ type MediaSlotResolution,
16
+ } from "../payloads.js";
17
+ import type { ZapoClient } from "../zapo-client.js";
18
+
19
+ export type { MediaSlotOptions } from "../btn/media-slot.js";
20
+
21
+ export interface ImageMediaOptions {
22
+ mimetype?: string;
23
+ width?: number;
24
+ height?: number;
25
+ caption?: string;
26
+ thumbnail?: Buffer | Uint8Array | false;
27
+ }
28
+
29
+ export interface VideoMediaOptions {
30
+ mimetype?: string;
31
+ seconds?: number;
32
+ width?: number;
33
+ height?: number;
34
+ thumbnail?: Buffer | Uint8Array | false;
35
+ }
36
+
37
+ export interface AudioMediaOptions {
38
+ mimetype?: string;
39
+ seconds?: number;
40
+ ptt?: boolean;
41
+ waveform?: Uint8Array;
42
+ }
43
+
44
+ export interface DocumentMediaOptions {
45
+ mimetype?: string;
46
+ fileName?: string;
47
+ title?: string;
48
+ thumbnail?: Buffer | Uint8Array | false;
49
+ }
50
+
51
+ export interface MediaImageConfig extends ImageMediaOptions {
52
+ type: "image";
53
+ source: MediaSource;
54
+ }
55
+
56
+ export interface MediaVideoConfig extends VideoMediaOptions {
57
+ type: "video";
58
+ source: MediaSource;
59
+ }
60
+
61
+ export interface MediaAudioConfig extends AudioMediaOptions {
62
+ type: "audio";
63
+ source: MediaSource;
64
+ }
65
+
66
+ export interface MediaDocumentConfig extends DocumentMediaOptions {
67
+ type: "document";
68
+ source: MediaSource;
69
+ }
70
+
71
+ export type MediaConfig =
72
+ MediaImageConfig | MediaVideoConfig | MediaAudioConfig | MediaDocumentConfig;
73
+
74
+ export interface ImageMessageConfig extends ImageMediaOptions {
75
+ source: MediaSource;
76
+ }
77
+
78
+ export interface VideoMessageConfig extends VideoMediaOptions {
79
+ source: MediaSource;
80
+ }
81
+
82
+ export interface AudioMessageConfig extends AudioMediaOptions {
83
+ source: MediaSource;
84
+ }
85
+
86
+ export interface DocumentMessageConfig extends DocumentMediaOptions {
87
+ source: MediaSource;
88
+ }
89
+
90
+ type MediaBuilderSlot = MediaSlot & { kind: MediaMessageKind };
91
+
92
+ export class MediaBuilder extends BaseBuilder<MediaBuildResult> {
93
+ #slot: MediaBuilderSlot | undefined;
94
+
95
+ #setKind(
96
+ kind: MediaMessageKind,
97
+ source: MediaSource,
98
+ options: MediaSlotOptions | undefined,
99
+ ): this {
100
+ if (this.#slot) {
101
+ throw new ValidationError(
102
+ `media builder hanya mendukung satu jenis media — ${this.#slot.kind} sudah dipilih`,
103
+ );
104
+ }
105
+ this.#slot = {
106
+ kind,
107
+ source,
108
+ options: options ?? {},
109
+ label: `media:${kind}`,
110
+ };
111
+ return this;
112
+ }
113
+
114
+ image(source: MediaSource, options?: MediaSlotOptions): this {
115
+ return this.#setKind("image", source, options);
116
+ }
117
+
118
+ video(source: MediaSource, options?: MediaSlotOptions): this {
119
+ return this.#setKind("video", source, options);
120
+ }
121
+
122
+ audio(source: MediaSource, options?: MediaSlotOptions): this {
123
+ return this.#setKind("audio", source, options);
124
+ }
125
+
126
+ document(source: MediaSource, options?: MediaSlotOptions): this {
127
+ return this.#setKind("document", source, options);
128
+ }
129
+
130
+ caption(text: string): this {
131
+ if (!this.#slot) {
132
+ throw new MessageError(
133
+ "panggil image()/video()/audio()/document() dulu sebelum caption()",
134
+ "NO_MEDIA",
135
+ );
136
+ }
137
+ if (this.#slot.kind !== "image" && this.#slot.kind !== "video") {
138
+ throw new ValidationError(
139
+ `caption hanya didukung untuk media image/video, bukan ${this.#slot.kind}`,
140
+ );
141
+ }
142
+ this.#slot.options.caption = text;
143
+ return this;
144
+ }
145
+
146
+ kind(): MediaMessageKind | undefined {
147
+ return this.#slot?.kind;
148
+ }
149
+
150
+ async slot(): Promise<MediaSlotResolution> {
151
+ return this.#resolveSlot();
152
+ }
153
+
154
+ async #resolveSlot(): Promise<MediaSlotResolution> {
155
+ if (!this.#slot) {
156
+ throw new MessageError(
157
+ "media builder belum punya sumber — panggil image(), video(), audio(), atau document() dulu",
158
+ "NO_MEDIA",
159
+ );
160
+ }
161
+ const { client } = this._resolve();
162
+ return resolveMediaSlot(client, this.#slot);
163
+ }
164
+
165
+ override async build(): Promise<MediaBuildResult> {
166
+ return mediaSlotToMessage(await this.#resolveSlot());
167
+ }
168
+ }
169
+
170
+ export function media(client?: ZapoClient): MediaBuilder;
171
+ export function media(
172
+ client: ZapoClient | undefined,
173
+ jid: string,
174
+ config: MediaConfig,
175
+ options?: MessageSendOptions,
176
+ ): Promise<MessageSendResult>;
177
+ export function media(
178
+ client?: ZapoClient,
179
+ jid?: string,
180
+ config?: MediaConfig,
181
+ options?: MessageSendOptions,
182
+ ): MediaBuilder | Promise<MessageSendResult> {
183
+ if (config === undefined) return new MediaBuilder(client);
184
+ return sendMedia(client, jid, config, options);
185
+ }
186
+
187
+ export function image(client?: ZapoClient): MediaBuilder;
188
+ export function image(
189
+ client: ZapoClient | undefined,
190
+ jid: string,
191
+ config: ImageMessageConfig,
192
+ options?: MessageSendOptions,
193
+ ): Promise<MessageSendResult>;
194
+ export function image(
195
+ client?: ZapoClient,
196
+ jid?: string,
197
+ config?: ImageMessageConfig,
198
+ options?: MessageSendOptions,
199
+ ): MediaBuilder | Promise<MessageSendResult> {
200
+ if (config === undefined) return new MediaBuilder(client);
201
+ return sendImage(client, jid, config, options);
202
+ }
203
+
204
+ export function video(client?: ZapoClient): MediaBuilder;
205
+ export function video(
206
+ client: ZapoClient | undefined,
207
+ jid: string,
208
+ config: VideoMessageConfig,
209
+ options?: MessageSendOptions,
210
+ ): Promise<MessageSendResult>;
211
+ export function video(
212
+ client?: ZapoClient,
213
+ jid?: string,
214
+ config?: VideoMessageConfig,
215
+ options?: MessageSendOptions,
216
+ ): MediaBuilder | Promise<MessageSendResult> {
217
+ if (config === undefined) return new MediaBuilder(client);
218
+ return sendVideo(client, jid, config, options);
219
+ }
220
+
221
+ export function audio(client?: ZapoClient): MediaBuilder;
222
+ export function audio(
223
+ client: ZapoClient | undefined,
224
+ jid: string,
225
+ config: AudioMessageConfig,
226
+ options?: MessageSendOptions,
227
+ ): Promise<MessageSendResult>;
228
+ export function audio(
229
+ client?: ZapoClient,
230
+ jid?: string,
231
+ config?: AudioMessageConfig,
232
+ options?: MessageSendOptions,
233
+ ): MediaBuilder | Promise<MessageSendResult> {
234
+ if (config === undefined) return new MediaBuilder(client);
235
+ return sendAudio(client, jid, config, options);
236
+ }
237
+
238
+ export function document(client?: ZapoClient): MediaBuilder;
239
+ export function document(
240
+ client: ZapoClient | undefined,
241
+ jid: string,
242
+ config: DocumentMessageConfig,
243
+ options?: MessageSendOptions,
244
+ ): Promise<MessageSendResult>;
245
+ export function document(
246
+ client?: ZapoClient,
247
+ jid?: string,
248
+ config?: DocumentMessageConfig,
249
+ options?: MessageSendOptions,
250
+ ): MediaBuilder | Promise<MessageSendResult> {
251
+ if (config === undefined) return new MediaBuilder(client);
252
+ return sendDocument(client, jid, config, options);
253
+ }
254
+
255
+ export const applyMediaConfig = (
256
+ builder: MediaBuilder,
257
+ config: MediaConfig,
258
+ ): void => {
259
+ const { type, source, ...options } = config;
260
+ if (type === "image") builder.image(source, options);
261
+ else if (type === "video") builder.video(source, options);
262
+ else if (type === "audio") builder.audio(source, options);
263
+ else builder.document(source, options);
264
+ };
265
+
266
+ export const sendMedia = async (
267
+ client: ZapoClient | undefined,
268
+ jid: string | undefined,
269
+ config: MediaConfig,
270
+ options?: MessageSendOptions,
271
+ ): Promise<MessageSendResult> => {
272
+ const builder = new MediaBuilder(client);
273
+ applyMediaConfig(builder, config);
274
+ return sendConfigured(builder, jid, options);
275
+ };
276
+
277
+ export const sendImage = async (
278
+ client: ZapoClient | undefined,
279
+ jid: string | undefined,
280
+ config: ImageMessageConfig,
281
+ options?: MessageSendOptions,
282
+ ): Promise<MessageSendResult> => {
283
+ const { source, ...mediaOptions } = config;
284
+ const builder = new MediaBuilder(client);
285
+ builder.image(source, mediaOptions);
286
+ return sendConfigured(builder, jid, options);
287
+ };
288
+
289
+ export const sendVideo = async (
290
+ client: ZapoClient | undefined,
291
+ jid: string | undefined,
292
+ config: VideoMessageConfig,
293
+ options?: MessageSendOptions,
294
+ ): Promise<MessageSendResult> => {
295
+ const { source, ...mediaOptions } = config;
296
+ const builder = new MediaBuilder(client);
297
+ builder.video(source, mediaOptions);
298
+ return sendConfigured(builder, jid, options);
299
+ };
300
+
301
+ export const sendAudio = async (
302
+ client: ZapoClient | undefined,
303
+ jid: string | undefined,
304
+ config: AudioMessageConfig,
305
+ options?: MessageSendOptions,
306
+ ): Promise<MessageSendResult> => {
307
+ const { source, ...mediaOptions } = config;
308
+ const builder = new MediaBuilder(client);
309
+ builder.audio(source, mediaOptions);
310
+ return sendConfigured(builder, jid, options);
311
+ };
312
+
313
+ export const sendDocument = async (
314
+ client: ZapoClient | undefined,
315
+ jid: string | undefined,
316
+ config: DocumentMessageConfig,
317
+ options?: MessageSendOptions,
318
+ ): Promise<MessageSendResult> => {
319
+ const { source, ...mediaOptions } = config;
320
+ const builder = new MediaBuilder(client);
321
+ builder.document(source, mediaOptions);
322
+ return sendConfigured(builder, jid, options);
323
+ };
@@ -0,0 +1,158 @@
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 { ValidationError, MessageError } from "../core/errors.js";
5
+ import type { ZapoClient, ZapoPollContent } from "../zapo-client.js";
6
+
7
+ const QUESTION_MAX = 1024;
8
+ const OPTION_NAME_MAX = 100;
9
+ const POLL_OPTIONS_MIN = 1;
10
+ const POLL_OPTIONS_MAX = 12;
11
+ const SELECTABLE_MAX = 12;
12
+
13
+ const assertQuestion = (value: unknown): string => {
14
+ if (typeof value !== "string" || !value.trim()) {
15
+ throw new ValidationError("poll.question wajib diisi (string non-kosong)");
16
+ }
17
+ if (value.length > QUESTION_MAX) {
18
+ throw new ValidationError(
19
+ `poll.question melebihi batas ${QUESTION_MAX} karakter (${value.length})`,
20
+ );
21
+ }
22
+ return value;
23
+ };
24
+
25
+ export interface PollConfig {
26
+ question: string;
27
+ options: readonly string[];
28
+ selectableCount?: number;
29
+ allowAddOption?: boolean;
30
+ hideParticipantName?: boolean;
31
+ }
32
+
33
+ export class PollBuilder extends BaseBuilder<ZapoPollContent> {
34
+ #question: string | undefined;
35
+ #options: string[] = [];
36
+ #selectableCount: number | undefined;
37
+ #allowAddOption = false;
38
+ #hideParticipantName = false;
39
+
40
+ question(value: string): this {
41
+ this.#question = assertQuestion(value);
42
+ return this;
43
+ }
44
+
45
+ options(values: readonly string[]): this {
46
+ if (!Array.isArray(values)) {
47
+ throw new ValidationError(
48
+ `poll.options butuh array string, diterima ${typeof values}`,
49
+ );
50
+ }
51
+ this.#options = values.map((value) => {
52
+ if (typeof value !== "string" || !value.trim()) {
53
+ throw new ValidationError(
54
+ "poll.options semua opsi harus string non-kosong",
55
+ );
56
+ }
57
+ if (value.length > OPTION_NAME_MAX) {
58
+ throw new ValidationError(
59
+ `poll.options opsi melebihi batas ${OPTION_NAME_MAX} karakter (${value.length})`,
60
+ );
61
+ }
62
+ return value;
63
+ });
64
+ return this;
65
+ }
66
+
67
+ selectableCount(value: number): this {
68
+ if (!Number.isInteger(value) || value < 1 || value > SELECTABLE_MAX) {
69
+ throw new ValidationError(
70
+ `poll.selectableCount harus integer 1-${SELECTABLE_MAX}, diterima ${JSON.stringify(value)}`,
71
+ );
72
+ }
73
+ this.#selectableCount = value;
74
+ return this;
75
+ }
76
+
77
+ allowAddOption(flag = true): this {
78
+ this.#allowAddOption = flag;
79
+ return this;
80
+ }
81
+
82
+ hideParticipantName(flag = true): this {
83
+ this.#hideParticipantName = flag;
84
+ return this;
85
+ }
86
+
87
+ override build(): ZapoPollContent {
88
+ if (this.#question === undefined) {
89
+ throw new MessageError(
90
+ 'poll.question belum diset — panggil question("Pilih satu")',
91
+ "NO_QUESTION",
92
+ );
93
+ }
94
+ if (
95
+ this.#options.length < POLL_OPTIONS_MIN ||
96
+ this.#options.length > POLL_OPTIONS_MAX
97
+ ) {
98
+ throw new ValidationError(
99
+ `poll.options butuh ${POLL_OPTIONS_MIN}-${POLL_OPTIONS_MAX} opsi, diterima ${this.#options.length}`,
100
+ );
101
+ }
102
+ const selectable = this.#selectableCount ?? 1;
103
+ if (selectable > this.#options.length) {
104
+ throw new ValidationError(
105
+ `poll.selectableCount (${selectable}) melebihi jumlah opsi (${this.#options.length})`,
106
+ );
107
+ }
108
+ return {
109
+ type: "poll",
110
+ name: this.#question,
111
+ options: [...this.#options],
112
+ selectableCount: selectable,
113
+ ...(this.#allowAddOption ? { allowAddOption: true } : {}),
114
+ ...(this.#hideParticipantName ? { hideParticipantName: true } : {}),
115
+ };
116
+ }
117
+ }
118
+
119
+ export function poll(client?: ZapoClient): PollBuilder;
120
+ export function poll(
121
+ client: ZapoClient | undefined,
122
+ jid: string,
123
+ config: PollConfig,
124
+ options?: MessageSendOptions,
125
+ ): Promise<MessageSendResult>;
126
+ export function poll(
127
+ client?: ZapoClient,
128
+ jid?: string,
129
+ config?: PollConfig,
130
+ options?: MessageSendOptions,
131
+ ): PollBuilder | Promise<MessageSendResult> {
132
+ if (config === undefined) return new PollBuilder(client);
133
+ return sendPoll(client, jid, config, options);
134
+ }
135
+
136
+ export const applyPollConfig = (
137
+ builder: PollBuilder,
138
+ config: PollConfig,
139
+ ): void => {
140
+ builder.question(config.question).options(config.options);
141
+ if (config.selectableCount !== undefined)
142
+ builder.selectableCount(config.selectableCount);
143
+ if (config.allowAddOption !== undefined)
144
+ builder.allowAddOption(config.allowAddOption);
145
+ if (config.hideParticipantName !== undefined)
146
+ builder.hideParticipantName(config.hideParticipantName);
147
+ };
148
+
149
+ export const sendPoll = async (
150
+ client: ZapoClient | undefined,
151
+ jid: string | undefined,
152
+ config: PollConfig,
153
+ options?: MessageSendOptions,
154
+ ): Promise<MessageSendResult> => {
155
+ const builder = new PollBuilder(client);
156
+ applyPollConfig(builder, config);
157
+ return sendConfigured(builder, jid, options);
158
+ };
@@ -0,0 +1,133 @@
1
+ import { BaseBuilder, type SendResolutionArg } 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 { ValidationError, MessageError } from "../core/errors.js";
5
+ import type { ZapoReactionContent, ZapoClient } from "../zapo-client.js";
6
+ import { targetChatJidOf, targetIdOf } from "./target.js";
7
+
8
+ export interface ReactionConfig {
9
+ target: unknown;
10
+ emoji: string;
11
+ }
12
+
13
+ const EMOJI_MAX = 64;
14
+
15
+ const assertEmoji = (value: unknown): string => {
16
+ if (typeof value !== "string") {
17
+ throw new ValidationError(
18
+ `reaction.emoji harus string, diterima ${typeof value}`,
19
+ );
20
+ }
21
+ if (value.length > EMOJI_MAX) {
22
+ throw new ValidationError(
23
+ `reaction.emoji melebihi batas ${EMOJI_MAX} karakter (${value.length})`,
24
+ );
25
+ }
26
+ return value;
27
+ };
28
+
29
+ export class ReactionBuilder extends BaseBuilder<ZapoReactionContent> {
30
+ #target: unknown;
31
+ #emoji: string | undefined;
32
+
33
+ target(ref: unknown): this {
34
+ if (!targetIdOf(ref)) {
35
+ throw new ValidationError(
36
+ "reaction.target butuh event pesan masuk atau message key dengan id",
37
+ );
38
+ }
39
+ this.#target = ref;
40
+ return this;
41
+ }
42
+
43
+ emoji(value: string): this {
44
+ this.#emoji = assertEmoji(value);
45
+ return this;
46
+ }
47
+
48
+ override build(): ZapoReactionContent {
49
+ if (this.#emoji === undefined) {
50
+ throw new MessageError(
51
+ 'reaction.emoji belum diset — panggil emoji("🔥") (string kosong untuk menghapus reaksi)',
52
+ "NO_EMOJI",
53
+ );
54
+ }
55
+ if (!targetIdOf(this.#target)) {
56
+ throw new ValidationError(
57
+ "reaction.target butuh event pesan masuk atau message key dengan id",
58
+ );
59
+ }
60
+ return { type: "reaction", target: this.#target, emoji: this.#emoji };
61
+ }
62
+
63
+ override async send(jid?: string, arg?: SendResolutionArg): Promise<unknown> {
64
+ const { client, options } = this._resolve(arg);
65
+ const target =
66
+ typeof jid === "string" && jid
67
+ ? jid
68
+ : (targetChatJidOf(this.#target) ?? this._defaultTarget());
69
+ if (!target) {
70
+ throw new MessageError(
71
+ "tujuan reaksi tidak diketahui — panggil send(jid), defaultTo(jid), atau berikan target pesan yang punya remoteJid",
72
+ "NO_TARGET",
73
+ );
74
+ }
75
+ const content = this.build();
76
+ const send = client.message.send.bind(client.message);
77
+ return Object.keys(options).length
78
+ ? send(target, content, options)
79
+ : send(target, content);
80
+ }
81
+ }
82
+
83
+ const isReactionConfig = (value: unknown): value is ReactionConfig =>
84
+ !!value && typeof value === "object" && "target" in value && "emoji" in value;
85
+
86
+ export function reaction(client?: ZapoClient): ReactionBuilder;
87
+ export function reaction(
88
+ client: ZapoClient | undefined,
89
+ config: ReactionConfig,
90
+ options?: MessageSendOptions,
91
+ ): Promise<MessageSendResult>;
92
+ export function reaction(
93
+ client: ZapoClient | undefined,
94
+ jid: string,
95
+ config: ReactionConfig,
96
+ options?: MessageSendOptions,
97
+ ): Promise<MessageSendResult>;
98
+ export function reaction(
99
+ client?: ZapoClient,
100
+ first?: string | ReactionConfig,
101
+ second?: ReactionConfig | MessageSendOptions,
102
+ third?: MessageSendOptions,
103
+ ): ReactionBuilder | Promise<MessageSendResult> {
104
+ if (typeof first === "string") {
105
+ if (!isReactionConfig(second)) return new ReactionBuilder(client);
106
+ return sendReaction(client, first, second, third);
107
+ }
108
+ if (!isReactionConfig(first)) return new ReactionBuilder(client);
109
+ return sendReaction(
110
+ client,
111
+ undefined,
112
+ first,
113
+ second as MessageSendOptions | undefined,
114
+ );
115
+ }
116
+
117
+ export const applyReactionConfig = (
118
+ builder: ReactionBuilder,
119
+ config: ReactionConfig,
120
+ ): void => {
121
+ builder.target(config.target).emoji(config.emoji);
122
+ };
123
+
124
+ export const sendReaction = async (
125
+ client: ZapoClient | undefined,
126
+ jid: string | undefined,
127
+ config: ReactionConfig,
128
+ options?: MessageSendOptions,
129
+ ): Promise<MessageSendResult> => {
130
+ const builder = new ReactionBuilder(client);
131
+ applyReactionConfig(builder, config);
132
+ return sendConfigured(builder, jid, options);
133
+ };
@@ -0,0 +1,100 @@
1
+ import { BaseBuilder } from "../core/base-builder.js";
2
+ import { MessageError } from "../core/errors.js";
3
+ import { sendConfigured, type MessageSendOptions } from "../core/async-send.js";
4
+ import type { MessageSendResult } from "../zapo-client.js";
5
+ import {
6
+ resolveMediaSlot,
7
+ type MediaSlot,
8
+ type MediaSlotOptions,
9
+ } from "../btn/media-slot.js";
10
+ import type { MediaSource } from "../btn/media-source.js";
11
+ import {
12
+ mediaSlotToMessage,
13
+ type MediaBuildResult,
14
+ type MediaSlotResolution,
15
+ } from "../payloads.js";
16
+ import type { ZapoClient } from "../zapo-client.js";
17
+
18
+ export type { MediaSlotOptions } from "../btn/media-slot.js";
19
+
20
+ export interface StickerMediaOptions {
21
+ mimetype?: string;
22
+ isAnimated?: boolean;
23
+ pngThumbnail?: Uint8Array | false;
24
+ width?: number;
25
+ height?: number;
26
+ stickerSentTs?: number;
27
+ }
28
+
29
+ export interface StickerConfig extends StickerMediaOptions {
30
+ source: MediaSource;
31
+ }
32
+
33
+ export class StickerBuilder extends BaseBuilder<MediaBuildResult> {
34
+ #slot: MediaSlot | undefined;
35
+
36
+ source(source: MediaSource, options?: MediaSlotOptions): this {
37
+ this.#slot = {
38
+ kind: "sticker",
39
+ source,
40
+ options: options ?? {},
41
+ label: "sticker",
42
+ };
43
+ return this;
44
+ }
45
+
46
+ async slot(): Promise<MediaSlotResolution> {
47
+ return this.#resolveSlot();
48
+ }
49
+
50
+ async #resolveSlot(): Promise<MediaSlotResolution> {
51
+ if (!this.#slot) {
52
+ throw new MessageError(
53
+ "sticker builder belum punya sumber — panggil source(bytesAtauPath)",
54
+ "NO_SOURCE",
55
+ );
56
+ }
57
+ const { client } = this._resolve();
58
+ return resolveMediaSlot(client, this.#slot);
59
+ }
60
+
61
+ override async build(): Promise<MediaBuildResult> {
62
+ return mediaSlotToMessage(await this.#resolveSlot());
63
+ }
64
+ }
65
+
66
+ export function sticker(client?: ZapoClient): StickerBuilder;
67
+ export function sticker(
68
+ client: ZapoClient | undefined,
69
+ jid: string,
70
+ config: StickerConfig,
71
+ options?: MessageSendOptions,
72
+ ): Promise<MessageSendResult>;
73
+ export function sticker(
74
+ client?: ZapoClient,
75
+ jid?: string,
76
+ config?: StickerConfig,
77
+ options?: MessageSendOptions,
78
+ ): StickerBuilder | Promise<MessageSendResult> {
79
+ if (config === undefined) return new StickerBuilder(client);
80
+ return sendSticker(client, jid, config, options);
81
+ }
82
+
83
+ export const applyStickerConfig = (
84
+ builder: StickerBuilder,
85
+ config: StickerConfig,
86
+ ): void => {
87
+ const { source, ...options } = config;
88
+ builder.source(source, options);
89
+ };
90
+
91
+ export const sendSticker = async (
92
+ client: ZapoClient | undefined,
93
+ jid: string | undefined,
94
+ config: StickerConfig,
95
+ options?: MessageSendOptions,
96
+ ): Promise<MessageSendResult> => {
97
+ const builder = new StickerBuilder(client);
98
+ applyStickerConfig(builder, config);
99
+ return sendConfigured(builder, jid, options);
100
+ };