@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,77 @@
1
+ import { getContextInfo } from "zapo-js";
2
+ import { BaseBuilder } from "../core/base-builder.js";
3
+ import { sendConfigured } from "../core/async-send.js";
4
+ import { MessageError, ValidationError } from "../core/errors.js";
5
+ import { targetKeyOf } from "./target.js";
6
+ const sourceMessageOf = (source) => {
7
+ if (!source || typeof source !== "object")
8
+ return undefined;
9
+ const ref = source;
10
+ if (ref.message && typeof ref.message === "object") {
11
+ return ref.message;
12
+ }
13
+ return undefined;
14
+ };
15
+ const sourceScoreOf = (source) => {
16
+ const message = sourceMessageOf(source);
17
+ if (!message)
18
+ return undefined;
19
+ const contextInfo = getContextInfo(message);
20
+ const score = contextInfo?.forwardingScore;
21
+ if (typeof score === "number" && Number.isFinite(score))
22
+ return score;
23
+ return undefined;
24
+ };
25
+ export class ForwardBuilder extends BaseBuilder {
26
+ #ref;
27
+ #message;
28
+ #score;
29
+ message(source) {
30
+ const message = sourceMessageOf(source);
31
+ if (!message) {
32
+ throw new ValidationError("forward.message butuh event pesan masuk atau objek dengan field message (proto IMessage)");
33
+ }
34
+ this.#ref = source;
35
+ this.#message = message;
36
+ this.#score = sourceScoreOf(source);
37
+ return this;
38
+ }
39
+ build() {
40
+ if (!this.#message) {
41
+ throw new MessageError("forward.message belum diset — berikan pesan sumber yang mau diteruskan", "NO_SOURCE");
42
+ }
43
+ return this.#message;
44
+ }
45
+ async send(jid, arg) {
46
+ const { client, options } = this._resolve(arg);
47
+ const target = typeof jid === "string" && jid ? jid : this._defaultTarget();
48
+ if (!target) {
49
+ throw new MessageError("tujuan forward tidak diketahui. panggil send(jid) atau .defaultTo(jid)", "NO_TARGET");
50
+ }
51
+ if (!this.#message) {
52
+ throw new MessageError("forward.message belum diset — berikan pesan sumber yang mau diteruskan", "NO_SOURCE");
53
+ }
54
+ const sendOptions = {
55
+ ...options,
56
+ forward: options.forward ??
57
+ (this.#score !== undefined ? { score: this.#score } : true),
58
+ };
59
+ return client.message.send(target, this.build(), sendOptions);
60
+ }
61
+ targetMessageKey() {
62
+ return targetKeyOf(this.#ref);
63
+ }
64
+ }
65
+ export function forward(client, jid, config, options) {
66
+ if (config === undefined)
67
+ return new ForwardBuilder(client);
68
+ return sendForward(client, jid, config, options);
69
+ }
70
+ export const applyForwardConfig = (builder, config) => {
71
+ builder.message(config.message);
72
+ };
73
+ export const sendForward = async (client, jid, config, options) => {
74
+ const builder = new ForwardBuilder(client);
75
+ applyForwardConfig(builder, config);
76
+ return sendConfigured(builder, jid, options);
77
+ };
@@ -0,0 +1,26 @@
1
+ export { TextBuilder, text, applyTextConfig, sendText } from "./text.js";
2
+ export type { TextConfig } from "./text.js";
3
+ export { LinkPreviewBuilder, linkPreview, applyLinkPreviewConfig, sendLinkPreview, } from "./link-preview.js";
4
+ export type { LinkPreviewKind, LinkPreviewConfig } from "./link-preview.js";
5
+ export { ReactionBuilder, reaction, applyReactionConfig, sendReaction, } from "./reaction.js";
6
+ export type { ReactionConfig } from "./reaction.js";
7
+ export { ForwardBuilder, forward, applyForwardConfig, sendForward, } from "./forward.js";
8
+ export type { ForwardConfig } from "./forward.js";
9
+ export { ContactBuilder, contact, applyContactConfig, sendContact, } from "./contact.js";
10
+ export type { ContactConfig } from "./contact.js";
11
+ export { PollBuilder, poll, applyPollConfig, sendPoll } from "./poll.js";
12
+ export type { PollConfig } from "./poll.js";
13
+ export { MediaBuilder, media, image, video, audio, document, applyMediaConfig, sendMedia, sendImage, sendVideo, sendAudio, sendDocument, } from "./media.js";
14
+ export type { MediaConfig, MediaImageConfig, MediaVideoConfig, MediaAudioConfig, MediaDocumentConfig, ImageMediaOptions, VideoMediaOptions, AudioMediaOptions, DocumentMediaOptions, ImageMessageConfig, VideoMessageConfig, AudioMessageConfig, DocumentMessageConfig, } from "./media.js";
15
+ export { StickerBuilder, sticker, applyStickerConfig, sendSticker, } from "./sticker.js";
16
+ export type { StickerConfig, StickerMediaOptions } from "./sticker.js";
17
+ export { AlbumBuilder, album, applyAlbumConfig, sendAlbum } from "./album.js";
18
+ export type { AlbumChild, AlbumConfig, AlbumMediaItem } from "./album.js";
19
+ export { LocationBuilder, location, applyLocationConfig, sendLocation, } from "./location.js";
20
+ export type { LocationConfig } from "./location.js";
21
+ export { probeWebp } from "./webp.js";
22
+ export type { WebpInfo } from "./webp.js";
23
+ export { buildVcard } from "./vcard.js";
24
+ export type { VcardInput } from "./vcard.js";
25
+ export { targetKeyOf, targetChatJidOf, targetIdOf } from "./target.js";
26
+ export type { MessageKeyLike, MessageTargetLike } from "./target.js";
@@ -0,0 +1,13 @@
1
+ export { TextBuilder, text, applyTextConfig, sendText } from "./text.js";
2
+ export { LinkPreviewBuilder, linkPreview, applyLinkPreviewConfig, sendLinkPreview, } from "./link-preview.js";
3
+ export { ReactionBuilder, reaction, applyReactionConfig, sendReaction, } from "./reaction.js";
4
+ export { ForwardBuilder, forward, applyForwardConfig, sendForward, } from "./forward.js";
5
+ export { ContactBuilder, contact, applyContactConfig, sendContact, } from "./contact.js";
6
+ export { PollBuilder, poll, applyPollConfig, sendPoll } from "./poll.js";
7
+ export { MediaBuilder, media, image, video, audio, document, applyMediaConfig, sendMedia, sendImage, sendVideo, sendAudio, sendDocument, } from "./media.js";
8
+ export { StickerBuilder, sticker, applyStickerConfig, sendSticker, } from "./sticker.js";
9
+ export { AlbumBuilder, album, applyAlbumConfig, sendAlbum } from "./album.js";
10
+ export { LocationBuilder, location, applyLocationConfig, sendLocation, } from "./location.js";
11
+ export { probeWebp } from "./webp.js";
12
+ export { buildVcard } from "./vcard.js";
13
+ export { targetKeyOf, targetChatJidOf, targetIdOf } from "./target.js";
@@ -0,0 +1,28 @@
1
+ import { BaseBuilder } from "../core/base-builder.js";
2
+ import { type MessageSendOptions } from "../core/async-send.js";
3
+ import type { MessageSendResult } from "../zapo-client.js";
4
+ import type { LinkPreviewBuildResult } from "../payloads.js";
5
+ import type { ZapoClient } from "../zapo-client.js";
6
+ export type LinkPreviewKind = "image" | "video";
7
+ export interface LinkPreviewConfig {
8
+ body: string;
9
+ url?: string;
10
+ title?: string;
11
+ description?: string;
12
+ thumbnail?: Uint8Array;
13
+ kind?: LinkPreviewKind;
14
+ }
15
+ export declare class LinkPreviewBuilder extends BaseBuilder<LinkPreviewBuildResult> {
16
+ #private;
17
+ body(text: string): this;
18
+ url(value: string): this;
19
+ title(value: string): this;
20
+ description(value: string): this;
21
+ thumbnail(bytes: Uint8Array): this;
22
+ kind(value: LinkPreviewKind): this;
23
+ build(): LinkPreviewBuildResult;
24
+ }
25
+ export declare function linkPreview(client?: ZapoClient): LinkPreviewBuilder;
26
+ export declare function linkPreview(client: ZapoClient | undefined, jid: string, config: LinkPreviewConfig, options?: MessageSendOptions): Promise<MessageSendResult>;
27
+ export declare const applyLinkPreviewConfig: (builder: LinkPreviewBuilder, config: LinkPreviewConfig) => void;
28
+ export declare const sendLinkPreview: (client: ZapoClient | undefined, jid: string | undefined, config: LinkPreviewConfig, options?: MessageSendOptions) => Promise<MessageSendResult>;
@@ -0,0 +1,98 @@
1
+ import { proto } from "zapo-js";
2
+ import { BaseBuilder } from "../core/base-builder.js";
3
+ import { sendConfigured } from "../core/async-send.js";
4
+ import { assertText, assertUrl } from "../btn/validators.js";
5
+ import { ValidationError } from "../core/errors.js";
6
+ import { asThumbnailBytes } from "../btn/thumbnail.js";
7
+ const PREVIEW_TYPE_BY_KIND = {
8
+ image: proto.Message.ExtendedTextMessage.PreviewType.IMAGE,
9
+ video: proto.Message.ExtendedTextMessage.PreviewType.VIDEO,
10
+ };
11
+ const FIRST_URL_PATTERN = /https?:\/\/[^\s<>"]+/;
12
+ export class LinkPreviewBuilder extends BaseBuilder {
13
+ #text;
14
+ #url;
15
+ #title;
16
+ #description;
17
+ #thumbnail;
18
+ #kind = "image";
19
+ body(text) {
20
+ this.#text = assertText(text, "linkPreview.body");
21
+ return this;
22
+ }
23
+ url(value) {
24
+ this.#url = assertUrl(value, "linkPreview.url");
25
+ return this;
26
+ }
27
+ title(value) {
28
+ this.#title = assertText(value, "linkPreview.title", {
29
+ max: 1024,
30
+ required: false,
31
+ });
32
+ return this;
33
+ }
34
+ description(value) {
35
+ this.#description = assertText(value, "linkPreview.description", {
36
+ max: 2048,
37
+ required: false,
38
+ });
39
+ return this;
40
+ }
41
+ thumbnail(bytes) {
42
+ this.#thumbnail = asThumbnailBytes(bytes);
43
+ return this;
44
+ }
45
+ kind(value) {
46
+ if (value !== "image" && value !== "video") {
47
+ throw new ValidationError(`linkPreview.kind harus "image" atau "video", diterima ${JSON.stringify(value)}`);
48
+ }
49
+ this.#kind = value;
50
+ return this;
51
+ }
52
+ build() {
53
+ const body = this.#text ?? assertText(undefined, "linkPreview.body");
54
+ const matched = this.#url ??
55
+ FIRST_URL_PATTERN.exec(body)?.[0] ??
56
+ assertUrl(undefined, "linkPreview.url");
57
+ if (!body.includes(matched)) {
58
+ throw new ValidationError(`linkPreview.url "${matched}" tidak muncul di body — WhatsApp memakai matchedText yang cocok dengan URL di teks`);
59
+ }
60
+ return {
61
+ extendedTextMessage: {
62
+ text: body,
63
+ matchedText: matched,
64
+ previewType: PREVIEW_TYPE_BY_KIND[this.#kind],
65
+ ...(this.#title !== undefined ? { title: this.#title } : {}),
66
+ ...(this.#description !== undefined
67
+ ? { description: this.#description }
68
+ : {}),
69
+ ...(this.#thumbnail !== undefined
70
+ ? { jpegThumbnail: this.#thumbnail }
71
+ : {}),
72
+ },
73
+ };
74
+ }
75
+ }
76
+ export function linkPreview(client, jid, config, options) {
77
+ if (config === undefined)
78
+ return new LinkPreviewBuilder(client);
79
+ return sendLinkPreview(client, jid, config, options);
80
+ }
81
+ export const applyLinkPreviewConfig = (builder, config) => {
82
+ builder.body(config.body);
83
+ if (config.url !== undefined)
84
+ builder.url(config.url);
85
+ if (config.title !== undefined)
86
+ builder.title(config.title);
87
+ if (config.description !== undefined)
88
+ builder.description(config.description);
89
+ if (config.thumbnail !== undefined)
90
+ builder.thumbnail(config.thumbnail);
91
+ if (config.kind !== undefined)
92
+ builder.kind(config.kind);
93
+ };
94
+ export const sendLinkPreview = async (client, jid, config, options) => {
95
+ const builder = new LinkPreviewBuilder(client);
96
+ applyLinkPreviewConfig(builder, config);
97
+ return sendConfigured(builder, jid, options);
98
+ };
@@ -0,0 +1,24 @@
1
+ import { BaseBuilder } from "../core/base-builder.js";
2
+ import { type MessageSendOptions } from "../core/async-send.js";
3
+ import type { MessageSendResult } from "../zapo-client.js";
4
+ import type { LocationBuildResult } from "../payloads.js";
5
+ import type { ZapoClient } from "../zapo-client.js";
6
+ export interface LocationConfig {
7
+ latitude: number;
8
+ longitude: number;
9
+ name?: string;
10
+ address?: string;
11
+ thumbnail?: Buffer | Uint8Array;
12
+ }
13
+ export declare class LocationBuilder extends BaseBuilder<LocationBuildResult> {
14
+ #private;
15
+ coordinates(latitude: number, longitude: number): this;
16
+ name(value: string): this;
17
+ address(value: string): this;
18
+ thumbnail(bytes: Buffer | Uint8Array): this;
19
+ build(): Promise<LocationBuildResult>;
20
+ }
21
+ export declare function location(client?: ZapoClient): LocationBuilder;
22
+ export declare function location(client: ZapoClient | undefined, jid: string, config: LocationConfig, options?: MessageSendOptions): Promise<MessageSendResult>;
23
+ export declare const applyLocationConfig: (builder: LocationBuilder, config: LocationConfig) => void;
24
+ export declare const sendLocation: (client: ZapoClient | undefined, jid: string | undefined, config: LocationConfig, options?: MessageSendOptions) => Promise<MessageSendResult>;
@@ -0,0 +1,94 @@
1
+ import { BaseBuilder } from "../core/base-builder.js";
2
+ import { MessageError, ValidationError } from "../core/errors.js";
3
+ import { sendConfigured } from "../core/async-send.js";
4
+ import { assertText } from "../btn/validators.js";
5
+ import { generateImageThumbnail } from "../btn/thumbnail.js";
6
+ const assertCoordinate = (value, label, { min, max }) => {
7
+ if (typeof value !== "number" ||
8
+ !Number.isFinite(value) ||
9
+ value < min ||
10
+ value > max) {
11
+ throw new ValidationError(`${label} harus number antara ${min} dan ${max}, diterima ${value}`);
12
+ }
13
+ return value;
14
+ };
15
+ export class LocationBuilder extends BaseBuilder {
16
+ #latitude;
17
+ #longitude;
18
+ #name;
19
+ #address;
20
+ #thumbnail;
21
+ coordinates(latitude, longitude) {
22
+ this.#latitude = assertCoordinate(latitude, "location.latitude", {
23
+ min: -90,
24
+ max: 90,
25
+ });
26
+ this.#longitude = assertCoordinate(longitude, "location.longitude", {
27
+ min: -180,
28
+ max: 180,
29
+ });
30
+ return this;
31
+ }
32
+ name(value) {
33
+ this.#name = assertText(value, "location.name", {
34
+ max: 1000,
35
+ required: false,
36
+ });
37
+ return this;
38
+ }
39
+ address(value) {
40
+ this.#address = assertText(value, "location.address", {
41
+ max: 1000,
42
+ required: false,
43
+ });
44
+ return this;
45
+ }
46
+ thumbnail(bytes) {
47
+ if (!Buffer.isBuffer(bytes) && !(bytes instanceof Uint8Array)) {
48
+ throw new ValidationError(`location.thumbnail butuh Buffer atau Uint8Array, diterima ${typeof bytes}`);
49
+ }
50
+ if (!bytes.byteLength) {
51
+ throw new ValidationError("location.thumbnail kosong (0 byte)");
52
+ }
53
+ this.#thumbnail = bytes;
54
+ return this;
55
+ }
56
+ async build() {
57
+ if (this.#latitude === undefined || this.#longitude === undefined) {
58
+ throw new MessageError("location belum punya koordinat — panggil coordinates(latitude, longitude)", "NO_COORDINATES");
59
+ }
60
+ const location = {
61
+ degreesLatitude: this.#latitude,
62
+ degreesLongitude: this.#longitude,
63
+ };
64
+ if (this.#name !== undefined)
65
+ location.name = this.#name;
66
+ if (this.#address !== undefined)
67
+ location.address = this.#address;
68
+ if (this.#thumbnail) {
69
+ location.jpegThumbnail = await generateImageThumbnail(this.#thumbnail, {
70
+ label: "location.thumbnail",
71
+ });
72
+ }
73
+ return { locationMessage: location };
74
+ }
75
+ }
76
+ export function location(client, jid, config, options) {
77
+ if (config === undefined)
78
+ return new LocationBuilder(client);
79
+ return sendLocation(client, jid, config, options);
80
+ }
81
+ export const applyLocationConfig = (builder, config) => {
82
+ builder.coordinates(config.latitude, config.longitude);
83
+ if (config.name !== undefined)
84
+ builder.name(config.name);
85
+ if (config.address !== undefined)
86
+ builder.address(config.address);
87
+ if (config.thumbnail !== undefined)
88
+ builder.thumbnail(config.thumbnail);
89
+ };
90
+ export const sendLocation = async (client, jid, config, options) => {
91
+ const builder = new LocationBuilder(client);
92
+ applyLocationConfig(builder, config);
93
+ return sendConfigured(builder, jid, options);
94
+ };
@@ -0,0 +1,90 @@
1
+ import { BaseBuilder } from "../core/base-builder.js";
2
+ import { type MessageSendOptions } from "../core/async-send.js";
3
+ import type { MessageSendResult } from "../zapo-client.js";
4
+ import { type MediaSlotOptions } from "../btn/media-slot.js";
5
+ import type { MediaSource } from "../btn/media-source.js";
6
+ import { type MediaBuildResult, type MediaMessageKind, type MediaSlotResolution } from "../payloads.js";
7
+ import type { ZapoClient } from "../zapo-client.js";
8
+ export type { MediaSlotOptions } from "../btn/media-slot.js";
9
+ export interface ImageMediaOptions {
10
+ mimetype?: string;
11
+ width?: number;
12
+ height?: number;
13
+ caption?: string;
14
+ thumbnail?: Buffer | Uint8Array | false;
15
+ }
16
+ export interface VideoMediaOptions {
17
+ mimetype?: string;
18
+ seconds?: number;
19
+ width?: number;
20
+ height?: number;
21
+ thumbnail?: Buffer | Uint8Array | false;
22
+ }
23
+ export interface AudioMediaOptions {
24
+ mimetype?: string;
25
+ seconds?: number;
26
+ ptt?: boolean;
27
+ waveform?: Uint8Array;
28
+ }
29
+ export interface DocumentMediaOptions {
30
+ mimetype?: string;
31
+ fileName?: string;
32
+ title?: string;
33
+ thumbnail?: Buffer | Uint8Array | false;
34
+ }
35
+ export interface MediaImageConfig extends ImageMediaOptions {
36
+ type: "image";
37
+ source: MediaSource;
38
+ }
39
+ export interface MediaVideoConfig extends VideoMediaOptions {
40
+ type: "video";
41
+ source: MediaSource;
42
+ }
43
+ export interface MediaAudioConfig extends AudioMediaOptions {
44
+ type: "audio";
45
+ source: MediaSource;
46
+ }
47
+ export interface MediaDocumentConfig extends DocumentMediaOptions {
48
+ type: "document";
49
+ source: MediaSource;
50
+ }
51
+ export type MediaConfig = MediaImageConfig | MediaVideoConfig | MediaAudioConfig | MediaDocumentConfig;
52
+ export interface ImageMessageConfig extends ImageMediaOptions {
53
+ source: MediaSource;
54
+ }
55
+ export interface VideoMessageConfig extends VideoMediaOptions {
56
+ source: MediaSource;
57
+ }
58
+ export interface AudioMessageConfig extends AudioMediaOptions {
59
+ source: MediaSource;
60
+ }
61
+ export interface DocumentMessageConfig extends DocumentMediaOptions {
62
+ source: MediaSource;
63
+ }
64
+ export declare class MediaBuilder extends BaseBuilder<MediaBuildResult> {
65
+ #private;
66
+ image(source: MediaSource, options?: MediaSlotOptions): this;
67
+ video(source: MediaSource, options?: MediaSlotOptions): this;
68
+ audio(source: MediaSource, options?: MediaSlotOptions): this;
69
+ document(source: MediaSource, options?: MediaSlotOptions): this;
70
+ caption(text: string): this;
71
+ kind(): MediaMessageKind | undefined;
72
+ slot(): Promise<MediaSlotResolution>;
73
+ build(): Promise<MediaBuildResult>;
74
+ }
75
+ export declare function media(client?: ZapoClient): MediaBuilder;
76
+ export declare function media(client: ZapoClient | undefined, jid: string, config: MediaConfig, options?: MessageSendOptions): Promise<MessageSendResult>;
77
+ export declare function image(client?: ZapoClient): MediaBuilder;
78
+ export declare function image(client: ZapoClient | undefined, jid: string, config: ImageMessageConfig, options?: MessageSendOptions): Promise<MessageSendResult>;
79
+ export declare function video(client?: ZapoClient): MediaBuilder;
80
+ export declare function video(client: ZapoClient | undefined, jid: string, config: VideoMessageConfig, options?: MessageSendOptions): Promise<MessageSendResult>;
81
+ export declare function audio(client?: ZapoClient): MediaBuilder;
82
+ export declare function audio(client: ZapoClient | undefined, jid: string, config: AudioMessageConfig, options?: MessageSendOptions): Promise<MessageSendResult>;
83
+ export declare function document(client?: ZapoClient): MediaBuilder;
84
+ export declare function document(client: ZapoClient | undefined, jid: string, config: DocumentMessageConfig, options?: MessageSendOptions): Promise<MessageSendResult>;
85
+ export declare const applyMediaConfig: (builder: MediaBuilder, config: MediaConfig) => void;
86
+ export declare const sendMedia: (client: ZapoClient | undefined, jid: string | undefined, config: MediaConfig, options?: MessageSendOptions) => Promise<MessageSendResult>;
87
+ export declare const sendImage: (client: ZapoClient | undefined, jid: string | undefined, config: ImageMessageConfig, options?: MessageSendOptions) => Promise<MessageSendResult>;
88
+ export declare const sendVideo: (client: ZapoClient | undefined, jid: string | undefined, config: VideoMessageConfig, options?: MessageSendOptions) => Promise<MessageSendResult>;
89
+ export declare const sendAudio: (client: ZapoClient | undefined, jid: string | undefined, config: AudioMessageConfig, options?: MessageSendOptions) => Promise<MessageSendResult>;
90
+ export declare const sendDocument: (client: ZapoClient | undefined, jid: string | undefined, config: DocumentMessageConfig, options?: MessageSendOptions) => Promise<MessageSendResult>;
@@ -0,0 +1,123 @@
1
+ import { BaseBuilder } from "../core/base-builder.js";
2
+ import { MessageError, ValidationError } from "../core/errors.js";
3
+ import { sendConfigured } from "../core/async-send.js";
4
+ import { resolveMediaSlot, } from "../btn/media-slot.js";
5
+ import { mediaSlotToMessage, } from "../payloads.js";
6
+ export class MediaBuilder extends BaseBuilder {
7
+ #slot;
8
+ #setKind(kind, source, options) {
9
+ if (this.#slot) {
10
+ throw new ValidationError(`media builder hanya mendukung satu jenis media — ${this.#slot.kind} sudah dipilih`);
11
+ }
12
+ this.#slot = {
13
+ kind,
14
+ source,
15
+ options: options ?? {},
16
+ label: `media:${kind}`,
17
+ };
18
+ return this;
19
+ }
20
+ image(source, options) {
21
+ return this.#setKind("image", source, options);
22
+ }
23
+ video(source, options) {
24
+ return this.#setKind("video", source, options);
25
+ }
26
+ audio(source, options) {
27
+ return this.#setKind("audio", source, options);
28
+ }
29
+ document(source, options) {
30
+ return this.#setKind("document", source, options);
31
+ }
32
+ caption(text) {
33
+ if (!this.#slot) {
34
+ throw new MessageError("panggil image()/video()/audio()/document() dulu sebelum caption()", "NO_MEDIA");
35
+ }
36
+ if (this.#slot.kind !== "image" && this.#slot.kind !== "video") {
37
+ throw new ValidationError(`caption hanya didukung untuk media image/video, bukan ${this.#slot.kind}`);
38
+ }
39
+ this.#slot.options.caption = text;
40
+ return this;
41
+ }
42
+ kind() {
43
+ return this.#slot?.kind;
44
+ }
45
+ async slot() {
46
+ return this.#resolveSlot();
47
+ }
48
+ async #resolveSlot() {
49
+ if (!this.#slot) {
50
+ throw new MessageError("media builder belum punya sumber — panggil image(), video(), audio(), atau document() dulu", "NO_MEDIA");
51
+ }
52
+ const { client } = this._resolve();
53
+ return resolveMediaSlot(client, this.#slot);
54
+ }
55
+ async build() {
56
+ return mediaSlotToMessage(await this.#resolveSlot());
57
+ }
58
+ }
59
+ export function media(client, jid, config, options) {
60
+ if (config === undefined)
61
+ return new MediaBuilder(client);
62
+ return sendMedia(client, jid, config, options);
63
+ }
64
+ export function image(client, jid, config, options) {
65
+ if (config === undefined)
66
+ return new MediaBuilder(client);
67
+ return sendImage(client, jid, config, options);
68
+ }
69
+ export function video(client, jid, config, options) {
70
+ if (config === undefined)
71
+ return new MediaBuilder(client);
72
+ return sendVideo(client, jid, config, options);
73
+ }
74
+ export function audio(client, jid, config, options) {
75
+ if (config === undefined)
76
+ return new MediaBuilder(client);
77
+ return sendAudio(client, jid, config, options);
78
+ }
79
+ export function document(client, jid, config, options) {
80
+ if (config === undefined)
81
+ return new MediaBuilder(client);
82
+ return sendDocument(client, jid, config, options);
83
+ }
84
+ export const applyMediaConfig = (builder, config) => {
85
+ const { type, source, ...options } = config;
86
+ if (type === "image")
87
+ builder.image(source, options);
88
+ else if (type === "video")
89
+ builder.video(source, options);
90
+ else if (type === "audio")
91
+ builder.audio(source, options);
92
+ else
93
+ builder.document(source, options);
94
+ };
95
+ export const sendMedia = async (client, jid, config, options) => {
96
+ const builder = new MediaBuilder(client);
97
+ applyMediaConfig(builder, config);
98
+ return sendConfigured(builder, jid, options);
99
+ };
100
+ export const sendImage = async (client, jid, config, options) => {
101
+ const { source, ...mediaOptions } = config;
102
+ const builder = new MediaBuilder(client);
103
+ builder.image(source, mediaOptions);
104
+ return sendConfigured(builder, jid, options);
105
+ };
106
+ export const sendVideo = async (client, jid, config, options) => {
107
+ const { source, ...mediaOptions } = config;
108
+ const builder = new MediaBuilder(client);
109
+ builder.video(source, mediaOptions);
110
+ return sendConfigured(builder, jid, options);
111
+ };
112
+ export const sendAudio = async (client, jid, config, options) => {
113
+ const { source, ...mediaOptions } = config;
114
+ const builder = new MediaBuilder(client);
115
+ builder.audio(source, mediaOptions);
116
+ return sendConfigured(builder, jid, options);
117
+ };
118
+ export const sendDocument = async (client, jid, config, options) => {
119
+ const { source, ...mediaOptions } = config;
120
+ const builder = new MediaBuilder(client);
121
+ builder.document(source, mediaOptions);
122
+ return sendConfigured(builder, jid, options);
123
+ };
@@ -0,0 +1,24 @@
1
+ import { BaseBuilder } from "../core/base-builder.js";
2
+ import { type MessageSendOptions } from "../core/async-send.js";
3
+ import type { MessageSendResult } from "../zapo-client.js";
4
+ import type { ZapoClient, ZapoPollContent } from "../zapo-client.js";
5
+ export interface PollConfig {
6
+ question: string;
7
+ options: readonly string[];
8
+ selectableCount?: number;
9
+ allowAddOption?: boolean;
10
+ hideParticipantName?: boolean;
11
+ }
12
+ export declare class PollBuilder extends BaseBuilder<ZapoPollContent> {
13
+ #private;
14
+ question(value: string): this;
15
+ options(values: readonly string[]): this;
16
+ selectableCount(value: number): this;
17
+ allowAddOption(flag?: boolean): this;
18
+ hideParticipantName(flag?: boolean): this;
19
+ build(): ZapoPollContent;
20
+ }
21
+ export declare function poll(client?: ZapoClient): PollBuilder;
22
+ export declare function poll(client: ZapoClient | undefined, jid: string, config: PollConfig, options?: MessageSendOptions): Promise<MessageSendResult>;
23
+ export declare const applyPollConfig: (builder: PollBuilder, config: PollConfig) => void;
24
+ export declare const sendPoll: (client: ZapoClient | undefined, jid: string | undefined, config: PollConfig, options?: MessageSendOptions) => Promise<MessageSendResult>;