@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,170 @@
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 { MessageError, ValidationError } from "../core/errors.js";
6
+ import { buildVcard } from "./vcard.js";
7
+ import type { ContactBuildResult } from "../payloads.js";
8
+ import type { ZapoClient } from "../zapo-client.js";
9
+
10
+ export interface ContactConfig {
11
+ name?: string;
12
+ phone?: string;
13
+ organization?: string;
14
+ email?: string;
15
+ url?: string;
16
+ note?: string;
17
+ vcard?: string;
18
+ }
19
+
20
+ const DISPLAY_NAME_MAX = 200;
21
+ const VCARD_MAX = 4096;
22
+
23
+ const fnOfVcard = (vcard: string): string | undefined => {
24
+ for (const line of vcard.split("\n")) {
25
+ if (line.startsWith("FN:")) return line.slice(3).trim() || undefined;
26
+ }
27
+ return undefined;
28
+ };
29
+
30
+ export class ContactBuilder extends BaseBuilder<ContactBuildResult> {
31
+ #displayName: string | undefined;
32
+ #vcard: string | undefined;
33
+ #phone: string | undefined;
34
+ #organization: string | undefined;
35
+ #email: string | undefined;
36
+ #url: string | undefined;
37
+ #note: string | undefined;
38
+
39
+ name(displayName: string): this {
40
+ this.#displayName = assertText(displayName, "contact.name", {
41
+ max: DISPLAY_NAME_MAX,
42
+ });
43
+ return this;
44
+ }
45
+
46
+ vcard(value: string): this {
47
+ const raw = assertText(value, "contact.vcard", {
48
+ max: VCARD_MAX,
49
+ required: false,
50
+ });
51
+ if (raw !== undefined && !raw.startsWith("BEGIN:VCARD")) {
52
+ throw new ValidationError(
53
+ "contact.vcard harus diawali BEGIN:VCARD dan diakhiri END:VCARD",
54
+ );
55
+ }
56
+ this.#vcard = raw;
57
+ return this;
58
+ }
59
+
60
+ phone(value: string): this {
61
+ this.#phone = assertText(value, "contact.phone", { max: 64 });
62
+ return this;
63
+ }
64
+
65
+ organization(value: string): this {
66
+ this.#organization = assertText(value, "contact.organization", {
67
+ max: 256,
68
+ required: false,
69
+ });
70
+ return this;
71
+ }
72
+
73
+ email(value: string): this {
74
+ this.#email = assertText(value, "contact.email", {
75
+ max: 256,
76
+ required: false,
77
+ });
78
+ return this;
79
+ }
80
+
81
+ url(value: string): this {
82
+ this.#url = assertText(value, "contact.url", {
83
+ max: 512,
84
+ required: false,
85
+ });
86
+ return this;
87
+ }
88
+
89
+ note(value: string): this {
90
+ this.#note = assertText(value, "contact.note", {
91
+ max: 1024,
92
+ required: false,
93
+ });
94
+ return this;
95
+ }
96
+
97
+ override build(): ContactBuildResult {
98
+ if (this.#vcard !== undefined) {
99
+ const displayName =
100
+ this.#displayName ?? fnOfVcard(this.#vcard) ?? "Kontak";
101
+ return { contactMessage: { displayName, vcard: this.#vcard } };
102
+ }
103
+ if (this.#displayName === undefined) {
104
+ throw new MessageError(
105
+ 'contact.name belum diset — panggil name("Nama Orang")',
106
+ "NO_NAME",
107
+ );
108
+ }
109
+ const vcard = buildVcard({
110
+ displayName: this.#displayName,
111
+ ...(this.#phone !== undefined ? { phoneNumber: this.#phone } : {}),
112
+ ...(this.#organization !== undefined
113
+ ? { organization: this.#organization }
114
+ : {}),
115
+ ...(this.#email !== undefined ? { email: this.#email } : {}),
116
+ ...(this.#url !== undefined ? { url: this.#url } : {}),
117
+ ...(this.#note !== undefined ? { note: this.#note } : {}),
118
+ });
119
+ if (vcard.length > VCARD_MAX) {
120
+ throw new ValidationError(
121
+ `contact vcard melebihi batas ${VCARD_MAX} karakter (${vcard.length})`,
122
+ );
123
+ }
124
+ return {
125
+ contactMessage: { displayName: this.#displayName, vcard },
126
+ };
127
+ }
128
+ }
129
+
130
+ export function contact(client?: ZapoClient): ContactBuilder;
131
+ export function contact(
132
+ client: ZapoClient | undefined,
133
+ jid: string,
134
+ config: ContactConfig,
135
+ options?: MessageSendOptions,
136
+ ): Promise<MessageSendResult>;
137
+ export function contact(
138
+ client?: ZapoClient,
139
+ jid?: string,
140
+ config?: ContactConfig,
141
+ options?: MessageSendOptions,
142
+ ): ContactBuilder | Promise<MessageSendResult> {
143
+ if (config === undefined) return new ContactBuilder(client);
144
+ return sendContact(client, jid, config, options);
145
+ }
146
+
147
+ export const applyContactConfig = (
148
+ builder: ContactBuilder,
149
+ config: ContactConfig,
150
+ ): void => {
151
+ if (config.name !== undefined) builder.name(config.name);
152
+ if (config.phone !== undefined) builder.phone(config.phone);
153
+ if (config.organization !== undefined)
154
+ builder.organization(config.organization);
155
+ if (config.email !== undefined) builder.email(config.email);
156
+ if (config.url !== undefined) builder.url(config.url);
157
+ if (config.note !== undefined) builder.note(config.note);
158
+ if (config.vcard !== undefined) builder.vcard(config.vcard);
159
+ };
160
+
161
+ export const sendContact = async (
162
+ client: ZapoClient | undefined,
163
+ jid: string | undefined,
164
+ config: ContactConfig,
165
+ options?: MessageSendOptions,
166
+ ): Promise<MessageSendResult> => {
167
+ const builder = new ContactBuilder(client);
168
+ applyContactConfig(builder, config);
169
+ return sendConfigured(builder, jid, options);
170
+ };
@@ -0,0 +1,123 @@
1
+ import type { Proto } from "zapo-js";
2
+ import { getContextInfo } from "zapo-js";
3
+ import { BaseBuilder, type SendResolutionArg } from "../core/base-builder.js";
4
+ import { sendConfigured, type MessageSendOptions } from "../core/async-send.js";
5
+ import type { MessageSendResult } from "../zapo-client.js";
6
+ import { MessageError, ValidationError } from "../core/errors.js";
7
+ import type { ZapoClient, ZapoSendOptions } from "../zapo-client.js";
8
+ import { targetKeyOf } from "./target.js";
9
+
10
+ export interface ForwardConfig {
11
+ message: unknown;
12
+ }
13
+
14
+ const sourceMessageOf = (source: unknown): Proto.IMessage | undefined => {
15
+ if (!source || typeof source !== "object") return undefined;
16
+ const ref = source as { message?: unknown };
17
+ if (ref.message && typeof ref.message === "object") {
18
+ return ref.message as Proto.IMessage;
19
+ }
20
+ return undefined;
21
+ };
22
+
23
+ const sourceScoreOf = (source: unknown): number | undefined => {
24
+ const message = sourceMessageOf(source);
25
+ if (!message) return undefined;
26
+ const contextInfo = getContextInfo(message);
27
+ const score = contextInfo?.forwardingScore;
28
+ if (typeof score === "number" && Number.isFinite(score)) return score;
29
+ return undefined;
30
+ };
31
+
32
+ export class ForwardBuilder extends BaseBuilder<Proto.IMessage> {
33
+ #ref: unknown;
34
+ #message: Proto.IMessage | undefined;
35
+ #score: number | undefined;
36
+
37
+ message(source: unknown): this {
38
+ const message = sourceMessageOf(source);
39
+ if (!message) {
40
+ throw new ValidationError(
41
+ "forward.message butuh event pesan masuk atau objek dengan field message (proto IMessage)",
42
+ );
43
+ }
44
+ this.#ref = source;
45
+ this.#message = message;
46
+ this.#score = sourceScoreOf(source);
47
+ return this;
48
+ }
49
+
50
+ override build(): Proto.IMessage {
51
+ if (!this.#message) {
52
+ throw new MessageError(
53
+ "forward.message belum diset — berikan pesan sumber yang mau diteruskan",
54
+ "NO_SOURCE",
55
+ );
56
+ }
57
+ return this.#message;
58
+ }
59
+
60
+ override async send(jid?: string, arg?: SendResolutionArg): Promise<unknown> {
61
+ const { client, options } = this._resolve(arg);
62
+ const target = typeof jid === "string" && jid ? jid : this._defaultTarget();
63
+ if (!target) {
64
+ throw new MessageError(
65
+ "tujuan forward tidak diketahui. panggil send(jid) atau .defaultTo(jid)",
66
+ "NO_TARGET",
67
+ );
68
+ }
69
+ if (!this.#message) {
70
+ throw new MessageError(
71
+ "forward.message belum diset — berikan pesan sumber yang mau diteruskan",
72
+ "NO_SOURCE",
73
+ );
74
+ }
75
+ const sendOptions: ZapoSendOptions = {
76
+ ...options,
77
+ forward:
78
+ options.forward ??
79
+ (this.#score !== undefined ? { score: this.#score } : true),
80
+ };
81
+ return client.message.send(target, this.build(), sendOptions);
82
+ }
83
+
84
+ targetMessageKey():
85
+ { remoteJid?: string; id?: string; fromMe?: boolean } | undefined {
86
+ return targetKeyOf(this.#ref);
87
+ }
88
+ }
89
+
90
+ export function forward(client?: ZapoClient): ForwardBuilder;
91
+ export function forward(
92
+ client: ZapoClient | undefined,
93
+ jid: string,
94
+ config: ForwardConfig,
95
+ options?: MessageSendOptions,
96
+ ): Promise<MessageSendResult>;
97
+ export function forward(
98
+ client?: ZapoClient,
99
+ jid?: string,
100
+ config?: ForwardConfig,
101
+ options?: MessageSendOptions,
102
+ ): ForwardBuilder | Promise<MessageSendResult> {
103
+ if (config === undefined) return new ForwardBuilder(client);
104
+ return sendForward(client, jid, config, options);
105
+ }
106
+
107
+ export const applyForwardConfig = (
108
+ builder: ForwardBuilder,
109
+ config: ForwardConfig,
110
+ ): void => {
111
+ builder.message(config.message);
112
+ };
113
+
114
+ export const sendForward = async (
115
+ client: ZapoClient | undefined,
116
+ jid: string | undefined,
117
+ config: ForwardConfig,
118
+ options?: MessageSendOptions,
119
+ ): Promise<MessageSendResult> => {
120
+ const builder = new ForwardBuilder(client);
121
+ applyForwardConfig(builder, config);
122
+ return sendConfigured(builder, jid, options);
123
+ };
@@ -0,0 +1,83 @@
1
+ export { TextBuilder, text, applyTextConfig, sendText } from "./text.js";
2
+ export type { TextConfig } from "./text.js";
3
+ export {
4
+ LinkPreviewBuilder,
5
+ linkPreview,
6
+ applyLinkPreviewConfig,
7
+ sendLinkPreview,
8
+ } from "./link-preview.js";
9
+ export type { LinkPreviewKind, LinkPreviewConfig } from "./link-preview.js";
10
+ export {
11
+ ReactionBuilder,
12
+ reaction,
13
+ applyReactionConfig,
14
+ sendReaction,
15
+ } from "./reaction.js";
16
+ export type { ReactionConfig } from "./reaction.js";
17
+ export {
18
+ ForwardBuilder,
19
+ forward,
20
+ applyForwardConfig,
21
+ sendForward,
22
+ } from "./forward.js";
23
+ export type { ForwardConfig } from "./forward.js";
24
+ export {
25
+ ContactBuilder,
26
+ contact,
27
+ applyContactConfig,
28
+ sendContact,
29
+ } from "./contact.js";
30
+ export type { ContactConfig } from "./contact.js";
31
+ export { PollBuilder, poll, applyPollConfig, sendPoll } from "./poll.js";
32
+ export type { PollConfig } from "./poll.js";
33
+ export {
34
+ MediaBuilder,
35
+ media,
36
+ image,
37
+ video,
38
+ audio,
39
+ document,
40
+ applyMediaConfig,
41
+ sendMedia,
42
+ sendImage,
43
+ sendVideo,
44
+ sendAudio,
45
+ sendDocument,
46
+ } from "./media.js";
47
+ export type {
48
+ MediaConfig,
49
+ MediaImageConfig,
50
+ MediaVideoConfig,
51
+ MediaAudioConfig,
52
+ MediaDocumentConfig,
53
+ ImageMediaOptions,
54
+ VideoMediaOptions,
55
+ AudioMediaOptions,
56
+ DocumentMediaOptions,
57
+ ImageMessageConfig,
58
+ VideoMessageConfig,
59
+ AudioMessageConfig,
60
+ DocumentMessageConfig,
61
+ } from "./media.js";
62
+ export {
63
+ StickerBuilder,
64
+ sticker,
65
+ applyStickerConfig,
66
+ sendSticker,
67
+ } from "./sticker.js";
68
+ export type { StickerConfig, StickerMediaOptions } from "./sticker.js";
69
+ export { AlbumBuilder, album, applyAlbumConfig, sendAlbum } from "./album.js";
70
+ export type { AlbumChild, AlbumConfig, AlbumMediaItem } from "./album.js";
71
+ export {
72
+ LocationBuilder,
73
+ location,
74
+ applyLocationConfig,
75
+ sendLocation,
76
+ } from "./location.js";
77
+ export type { LocationConfig } from "./location.js";
78
+ export { probeWebp } from "./webp.js";
79
+ export type { WebpInfo } from "./webp.js";
80
+ export { buildVcard } from "./vcard.js";
81
+ export type { VcardInput } from "./vcard.js";
82
+ export { targetKeyOf, targetChatJidOf, targetIdOf } from "./target.js";
83
+ export type { MessageKeyLike, MessageTargetLike } from "./target.js";
@@ -0,0 +1,148 @@
1
+ import { proto } from "zapo-js";
2
+ import type { Proto } from "zapo-js";
3
+ import { BaseBuilder } from "../core/base-builder.js";
4
+ import { sendConfigured, type MessageSendOptions } from "../core/async-send.js";
5
+ import type { MessageSendResult } from "../zapo-client.js";
6
+ import { assertText, assertUrl } from "../btn/validators.js";
7
+ import { ValidationError } from "../core/errors.js";
8
+ import { asThumbnailBytes } from "../btn/thumbnail.js";
9
+ import type { LinkPreviewBuildResult } from "../payloads.js";
10
+ import type { ZapoClient } from "../zapo-client.js";
11
+
12
+ export type LinkPreviewKind = "image" | "video";
13
+
14
+ export interface LinkPreviewConfig {
15
+ body: string;
16
+ url?: string;
17
+ title?: string;
18
+ description?: string;
19
+ thumbnail?: Uint8Array;
20
+ kind?: LinkPreviewKind;
21
+ }
22
+
23
+ const PREVIEW_TYPE_BY_KIND: Record<
24
+ LinkPreviewKind,
25
+ Proto.Message.ExtendedTextMessage.PreviewType
26
+ > = {
27
+ image: proto.Message.ExtendedTextMessage.PreviewType.IMAGE,
28
+ video: proto.Message.ExtendedTextMessage.PreviewType.VIDEO,
29
+ };
30
+
31
+ const FIRST_URL_PATTERN = /https?:\/\/[^\s<>"]+/;
32
+
33
+ export class LinkPreviewBuilder extends BaseBuilder<LinkPreviewBuildResult> {
34
+ #text: string | undefined;
35
+ #url: string | undefined;
36
+ #title: string | undefined;
37
+ #description: string | undefined;
38
+ #thumbnail: Uint8Array | undefined;
39
+ #kind: LinkPreviewKind = "image";
40
+
41
+ body(text: string): this {
42
+ this.#text = assertText(text, "linkPreview.body");
43
+ return this;
44
+ }
45
+
46
+ url(value: string): this {
47
+ this.#url = assertUrl(value, "linkPreview.url");
48
+ return this;
49
+ }
50
+
51
+ title(value: string): this {
52
+ this.#title = assertText(value, "linkPreview.title", {
53
+ max: 1024,
54
+ required: false,
55
+ });
56
+ return this;
57
+ }
58
+
59
+ description(value: string): this {
60
+ this.#description = assertText(value, "linkPreview.description", {
61
+ max: 2048,
62
+ required: false,
63
+ });
64
+ return this;
65
+ }
66
+
67
+ thumbnail(bytes: Uint8Array): this {
68
+ this.#thumbnail = asThumbnailBytes(bytes);
69
+ return this;
70
+ }
71
+
72
+ kind(value: LinkPreviewKind): this {
73
+ if (value !== "image" && value !== "video") {
74
+ throw new ValidationError(
75
+ `linkPreview.kind harus "image" atau "video", diterima ${JSON.stringify(value)}`,
76
+ );
77
+ }
78
+ this.#kind = value;
79
+ return this;
80
+ }
81
+
82
+ override build(): LinkPreviewBuildResult {
83
+ const body = this.#text ?? assertText(undefined, "linkPreview.body");
84
+ const matched =
85
+ this.#url ??
86
+ (FIRST_URL_PATTERN.exec(body)?.[0] as string | undefined) ??
87
+ assertUrl(undefined, "linkPreview.url");
88
+ if (!body.includes(matched)) {
89
+ throw new ValidationError(
90
+ `linkPreview.url "${matched}" tidak muncul di body — WhatsApp memakai matchedText yang cocok dengan URL di teks`,
91
+ );
92
+ }
93
+ return {
94
+ extendedTextMessage: {
95
+ text: body,
96
+ matchedText: matched,
97
+ previewType: PREVIEW_TYPE_BY_KIND[this.#kind],
98
+ ...(this.#title !== undefined ? { title: this.#title } : {}),
99
+ ...(this.#description !== undefined
100
+ ? { description: this.#description }
101
+ : {}),
102
+ ...(this.#thumbnail !== undefined
103
+ ? { jpegThumbnail: this.#thumbnail }
104
+ : {}),
105
+ },
106
+ };
107
+ }
108
+ }
109
+
110
+ export function linkPreview(client?: ZapoClient): LinkPreviewBuilder;
111
+ export function linkPreview(
112
+ client: ZapoClient | undefined,
113
+ jid: string,
114
+ config: LinkPreviewConfig,
115
+ options?: MessageSendOptions,
116
+ ): Promise<MessageSendResult>;
117
+ export function linkPreview(
118
+ client?: ZapoClient,
119
+ jid?: string,
120
+ config?: LinkPreviewConfig,
121
+ options?: MessageSendOptions,
122
+ ): LinkPreviewBuilder | Promise<MessageSendResult> {
123
+ if (config === undefined) return new LinkPreviewBuilder(client);
124
+ return sendLinkPreview(client, jid, config, options);
125
+ }
126
+
127
+ export const applyLinkPreviewConfig = (
128
+ builder: LinkPreviewBuilder,
129
+ config: LinkPreviewConfig,
130
+ ): void => {
131
+ builder.body(config.body);
132
+ if (config.url !== undefined) builder.url(config.url);
133
+ if (config.title !== undefined) builder.title(config.title);
134
+ if (config.description !== undefined) builder.description(config.description);
135
+ if (config.thumbnail !== undefined) builder.thumbnail(config.thumbnail);
136
+ if (config.kind !== undefined) builder.kind(config.kind);
137
+ };
138
+
139
+ export const sendLinkPreview = async (
140
+ client: ZapoClient | undefined,
141
+ jid: string | undefined,
142
+ config: LinkPreviewConfig,
143
+ options?: MessageSendOptions,
144
+ ): Promise<MessageSendResult> => {
145
+ const builder = new LinkPreviewBuilder(client);
146
+ applyLinkPreviewConfig(builder, config);
147
+ return sendConfigured(builder, jid, options);
148
+ };
@@ -0,0 +1,150 @@
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 { assertText } from "../btn/validators.js";
6
+ import { generateImageThumbnail } from "../btn/thumbnail.js";
7
+ import type {
8
+ LocationBuildResult,
9
+ LocationMessagePayload,
10
+ } from "../payloads.js";
11
+ import type { ZapoClient } from "../zapo-client.js";
12
+
13
+ interface CoordinateRange {
14
+ min: number;
15
+ max: number;
16
+ }
17
+
18
+ const assertCoordinate = (
19
+ value: unknown,
20
+ label: string,
21
+ { min, max }: CoordinateRange,
22
+ ): number => {
23
+ if (
24
+ typeof value !== "number" ||
25
+ !Number.isFinite(value) ||
26
+ value < min ||
27
+ value > max
28
+ ) {
29
+ throw new ValidationError(
30
+ `${label} harus number antara ${min} dan ${max}, diterima ${value}`,
31
+ );
32
+ }
33
+ return value;
34
+ };
35
+
36
+ export interface LocationConfig {
37
+ latitude: number;
38
+ longitude: number;
39
+ name?: string;
40
+ address?: string;
41
+ thumbnail?: Buffer | Uint8Array;
42
+ }
43
+
44
+ export class LocationBuilder extends BaseBuilder<LocationBuildResult> {
45
+ #latitude: number | undefined;
46
+ #longitude: number | undefined;
47
+ #name: string | undefined;
48
+ #address: string | undefined;
49
+ #thumbnail: Uint8Array | undefined;
50
+
51
+ coordinates(latitude: number, longitude: number): this {
52
+ this.#latitude = assertCoordinate(latitude, "location.latitude", {
53
+ min: -90,
54
+ max: 90,
55
+ });
56
+ this.#longitude = assertCoordinate(longitude, "location.longitude", {
57
+ min: -180,
58
+ max: 180,
59
+ });
60
+ return this;
61
+ }
62
+
63
+ name(value: string): this {
64
+ this.#name = assertText(value, "location.name", {
65
+ max: 1000,
66
+ required: false,
67
+ });
68
+ return this;
69
+ }
70
+
71
+ address(value: string): this {
72
+ this.#address = assertText(value, "location.address", {
73
+ max: 1000,
74
+ required: false,
75
+ });
76
+ return this;
77
+ }
78
+
79
+ thumbnail(bytes: Buffer | Uint8Array): this {
80
+ if (!Buffer.isBuffer(bytes) && !(bytes instanceof Uint8Array)) {
81
+ throw new ValidationError(
82
+ `location.thumbnail butuh Buffer atau Uint8Array, diterima ${typeof bytes}`,
83
+ );
84
+ }
85
+ if (!bytes.byteLength) {
86
+ throw new ValidationError("location.thumbnail kosong (0 byte)");
87
+ }
88
+ this.#thumbnail = bytes;
89
+ return this;
90
+ }
91
+
92
+ override async build(): Promise<LocationBuildResult> {
93
+ if (this.#latitude === undefined || this.#longitude === undefined) {
94
+ throw new MessageError(
95
+ "location belum punya koordinat — panggil coordinates(latitude, longitude)",
96
+ "NO_COORDINATES",
97
+ );
98
+ }
99
+ const location: LocationMessagePayload = {
100
+ degreesLatitude: this.#latitude,
101
+ degreesLongitude: this.#longitude,
102
+ };
103
+ if (this.#name !== undefined) location.name = this.#name;
104
+ if (this.#address !== undefined) location.address = this.#address;
105
+ if (this.#thumbnail) {
106
+ location.jpegThumbnail = await generateImageThumbnail(this.#thumbnail, {
107
+ label: "location.thumbnail",
108
+ });
109
+ }
110
+ return { locationMessage: location };
111
+ }
112
+ }
113
+
114
+ export function location(client?: ZapoClient): LocationBuilder;
115
+ export function location(
116
+ client: ZapoClient | undefined,
117
+ jid: string,
118
+ config: LocationConfig,
119
+ options?: MessageSendOptions,
120
+ ): Promise<MessageSendResult>;
121
+ export function location(
122
+ client?: ZapoClient,
123
+ jid?: string,
124
+ config?: LocationConfig,
125
+ options?: MessageSendOptions,
126
+ ): LocationBuilder | Promise<MessageSendResult> {
127
+ if (config === undefined) return new LocationBuilder(client);
128
+ return sendLocation(client, jid, config, options);
129
+ }
130
+
131
+ export const applyLocationConfig = (
132
+ builder: LocationBuilder,
133
+ config: LocationConfig,
134
+ ): void => {
135
+ builder.coordinates(config.latitude, config.longitude);
136
+ if (config.name !== undefined) builder.name(config.name);
137
+ if (config.address !== undefined) builder.address(config.address);
138
+ if (config.thumbnail !== undefined) builder.thumbnail(config.thumbnail);
139
+ };
140
+
141
+ export const sendLocation = async (
142
+ client: ZapoClient | undefined,
143
+ jid: string | undefined,
144
+ config: LocationConfig,
145
+ options?: MessageSendOptions,
146
+ ): Promise<MessageSendResult> => {
147
+ const builder = new LocationBuilder(client);
148
+ applyLocationConfig(builder, config);
149
+ return sendConfigured(builder, jid, options);
150
+ };