@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.
- package/CHANGELOG.md +159 -0
- package/LICENSE +20 -0
- package/README.md +532 -2
- package/build.config.json +28 -0
- package/dist/airich/airich.d.ts +180 -0
- package/dist/airich/airich.js +548 -0
- package/dist/airich/context.d.ts +24 -0
- package/dist/airich/context.js +48 -0
- package/dist/airich/encode.d.ts +3 -0
- package/dist/airich/encode.js +16 -0
- package/dist/airich/index.d.ts +20 -0
- package/dist/airich/index.js +11 -0
- package/dist/airich/inline.d.ts +45 -0
- package/dist/airich/inline.js +152 -0
- package/dist/airich/nodes.d.ts +30 -0
- package/dist/airich/nodes.js +121 -0
- package/dist/airich/primitives.d.ts +144 -0
- package/dist/airich/primitives.js +230 -0
- package/dist/airich/protocol.d.ts +35 -0
- package/dist/airich/protocol.js +65 -0
- package/dist/airich/table.d.ts +22 -0
- package/dist/airich/table.js +35 -0
- package/dist/airich/tokenizer.d.ts +13 -0
- package/dist/airich/tokenizer.js +717 -0
- package/dist/airich/transport.d.ts +15 -0
- package/dist/airich/transport.js +56 -0
- package/dist/branding.d.ts +5 -0
- package/dist/branding.js +9 -0
- package/dist/btn/builders.d.ts +246 -0
- package/dist/btn/builders.js +710 -0
- package/dist/btn/media-slot.d.ts +28 -0
- package/dist/btn/media-slot.js +323 -0
- package/dist/btn/media-source.d.ts +38 -0
- package/dist/btn/media-source.js +116 -0
- package/dist/btn/sharp-loader.d.ts +5 -0
- package/dist/btn/sharp-loader.js +18 -0
- package/dist/btn/thumbnail.d.ts +21 -0
- package/dist/btn/thumbnail.js +124 -0
- package/dist/btn/validators.d.ts +40 -0
- package/dist/btn/validators.js +135 -0
- package/dist/btn.d.ts +1 -0
- package/dist/btn.js +1 -0
- package/dist/core/async-send.d.ts +20 -0
- package/dist/core/async-send.js +1 -0
- package/dist/core/base-builder.d.ts +20 -0
- package/dist/core/base-builder.js +67 -0
- package/dist/core/errors.d.ts +30 -0
- package/dist/core/errors.js +61 -0
- package/dist/download.d.ts +24 -0
- package/dist/download.js +123 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.js +22 -0
- package/dist/integration/augment.d.ts +97 -0
- package/dist/integration/augment.js +1 -0
- package/dist/integration/auto.d.ts +8 -0
- package/dist/integration/auto.js +79 -0
- package/dist/integration/index.d.ts +4 -0
- package/dist/integration/index.js +2 -0
- package/dist/integration/methods.d.ts +25 -0
- package/dist/integration/methods.js +24 -0
- package/dist/interactive.d.ts +24 -0
- package/dist/interactive.js +121 -0
- package/dist/media-gate.d.ts +99 -0
- package/dist/media-gate.js +322 -0
- package/dist/message/album.d.ts +33 -0
- package/dist/message/album.js +107 -0
- package/dist/message/contact.d.ts +29 -0
- package/dist/message/contact.js +123 -0
- package/dist/message/forward.d.ts +23 -0
- package/dist/message/forward.js +77 -0
- package/dist/message/index.d.ts +26 -0
- package/dist/message/index.js +13 -0
- package/dist/message/link-preview.d.ts +28 -0
- package/dist/message/link-preview.js +98 -0
- package/dist/message/location.d.ts +24 -0
- package/dist/message/location.js +94 -0
- package/dist/message/media.d.ts +90 -0
- package/dist/message/media.js +123 -0
- package/dist/message/poll.d.ts +24 -0
- package/dist/message/poll.js +98 -0
- package/dist/message/reaction.d.ts +20 -0
- package/dist/message/reaction.js +71 -0
- package/dist/message/sticker.d.ts +29 -0
- package/dist/message/sticker.js +44 -0
- package/dist/message/target.d.ts +18 -0
- package/dist/message/target.js +24 -0
- package/dist/message/text.d.ts +17 -0
- package/dist/message/text.js +29 -0
- package/dist/message/vcard.d.ts +9 -0
- package/dist/message/vcard.js +23 -0
- package/dist/message/webp.d.ts +6 -0
- package/dist/message/webp.js +57 -0
- package/dist/payloads.d.ts +180 -0
- package/dist/payloads.js +1 -0
- package/dist/serialize.d.ts +41 -0
- package/dist/serialize.js +93 -0
- package/dist/status.d.ts +3 -0
- package/dist/status.js +21 -0
- package/dist/zapo-client.d.ts +44 -0
- package/dist/zapo-client.js +1 -0
- package/examples/airich.ts +59 -0
- package/examples/buttons.ts +48 -0
- package/examples/client-methods.ts +58 -0
- package/examples/hybrid.ts +133 -0
- package/examples/serialize.ts +61 -0
- package/package.json +271 -4
- package/scripts/check-no-comments.mjs +101 -0
- package/scripts/compare-golden.mjs +461 -0
- package/scripts/lib-config.mjs +9 -0
- package/scripts/smoke-auto.mjs +75 -0
- package/scripts/test-external-consumer.mjs +316 -0
- package/scripts/verify-package.mjs +78 -0
- package/src/airich/airich.ts +970 -0
- package/src/airich/context.ts +96 -0
- package/src/airich/encode.ts +25 -0
- package/src/airich/index.ts +98 -0
- package/src/airich/inline.ts +272 -0
- package/src/airich/nodes.ts +174 -0
- package/src/airich/primitives.ts +474 -0
- package/src/airich/protocol.ts +133 -0
- package/src/airich/table.ts +70 -0
- package/src/airich/tokenizer.ts +799 -0
- package/src/airich/transport.ts +106 -0
- package/src/branding.ts +23 -0
- package/src/btn/builders.ts +1073 -0
- package/src/btn/media-slot.ts +486 -0
- package/src/btn/media-source.ts +189 -0
- package/src/btn/sharp-loader.ts +23 -0
- package/src/btn/thumbnail.ts +182 -0
- package/src/btn/validators.ts +237 -0
- package/src/btn.ts +8 -0
- package/src/core/async-send.ts +28 -0
- package/src/core/base-builder.ts +104 -0
- package/src/core/errors.ts +82 -0
- package/src/download.ts +212 -0
- package/src/index.ts +229 -0
- package/src/integration/augment.ts +262 -0
- package/src/integration/auto.ts +103 -0
- package/src/integration/index.ts +8 -0
- package/src/integration/methods.ts +45 -0
- package/src/interactive.ts +188 -0
- package/src/media-gate.ts +499 -0
- package/src/message/album.ts +187 -0
- package/src/message/contact.ts +170 -0
- package/src/message/forward.ts +123 -0
- package/src/message/index.ts +83 -0
- package/src/message/link-preview.ts +148 -0
- package/src/message/location.ts +150 -0
- package/src/message/media.ts +323 -0
- package/src/message/poll.ts +158 -0
- package/src/message/reaction.ts +133 -0
- package/src/message/sticker.ts +100 -0
- package/src/message/target.ts +38 -0
- package/src/message/text.ts +61 -0
- package/src/message/vcard.ts +36 -0
- package/src/message/webp.ts +78 -0
- package/src/payloads.ts +204 -0
- package/src/serialize.ts +180 -0
- package/src/status.ts +29 -0
- package/src/zapo-client.ts +84 -0
- package/test/airich-inline.test.ts +200 -0
- package/test/airich-primitives.test.ts +226 -0
- package/test/airich-tokenizer.test.ts +145 -0
- package/test/airich.golden.test.ts +406 -0
- package/test/airich.test.ts +1006 -0
- package/test/btn-button.test.ts +416 -0
- package/test/btn-header.test.ts +646 -0
- package/test/btn-legacy.test.ts +342 -0
- package/test/btn-lifecycle.test.ts +295 -0
- package/test/btn-list.test.ts +261 -0
- package/test/btn-media-source.test.ts +150 -0
- package/test/btn-thumbnail.test.ts +160 -0
- package/test/builder-compat.test.ts +264 -0
- package/test/helpers.ts +27 -0
- package/test/hybrid-airich.test.ts +345 -0
- package/test/hybrid-client.test.ts +239 -0
- package/test/hybrid-errors.test.ts +557 -0
- package/test/hybrid-interactive.test.ts +455 -0
- package/test/hybrid.golden.test.ts +349 -0
- package/test/hybrid.test.ts +479 -0
- package/test/integration-auto.test.ts +159 -0
- package/test/integration.fakeserver.test.ts +440 -0
- package/test/media-gate.test.ts +215 -0
- package/test/message-album.test.ts +145 -0
- package/test/message-builders.test.ts +401 -0
- package/test/message-media.test.ts +303 -0
- package/test/message.fakeserver.test.ts +284 -0
- package/test/message.golden.test.ts +254 -0
- package/test/serializer.test.ts +448 -0
- package/tsconfig.json +26 -0
- package/tsconfig.test.json +10 -0
|
@@ -0,0 +1,710 @@
|
|
|
1
|
+
import { isGroupJid, proto } from "zapo-js";
|
|
2
|
+
import { BaseBuilder } from "../core/base-builder.js";
|
|
3
|
+
import { sendConfigured } from "../core/async-send.js";
|
|
4
|
+
import { MessageError, ValidationError, DuplicateIdError, } from "../core/errors.js";
|
|
5
|
+
import { resolveMediaSlot, } from "./media-slot.js";
|
|
6
|
+
import { generateImageThumbnail, validateJpegThumbnail, asThumbnailBytes, } from "./thumbnail.js";
|
|
7
|
+
import { assertText, assertId, assertUniqueId, assertUrl, assertPhoneNumber, assertPositiveInt, validateNativeSections, stringifyParams, DISPLAY_TEXT_MAX, SELECT_TITLE_MAX, COPY_CODE_MAX, LEGACY_BUTTON_LIMIT, } from "./validators.js";
|
|
8
|
+
const HeaderType = proto.Message.ButtonsMessage.HeaderType;
|
|
9
|
+
const ButtonType = proto.Message.ButtonsMessage.Button.Type;
|
|
10
|
+
const ListType = proto.Message.ListMessage.ListType;
|
|
11
|
+
const assertCoordinate = (value, label, { min, max }) => {
|
|
12
|
+
if (typeof value !== "number" ||
|
|
13
|
+
!Number.isFinite(value) ||
|
|
14
|
+
value < min ||
|
|
15
|
+
value > max) {
|
|
16
|
+
throw new ValidationError(`${label} harus number antara ${min} dan ${max}, diterima ${value}`);
|
|
17
|
+
}
|
|
18
|
+
return value;
|
|
19
|
+
};
|
|
20
|
+
const LEGACY_HEADER_TYPE_BY_KIND = {
|
|
21
|
+
image: HeaderType.IMAGE,
|
|
22
|
+
video: HeaderType.VIDEO,
|
|
23
|
+
document: HeaderType.DOCUMENT,
|
|
24
|
+
};
|
|
25
|
+
class BaseMessage extends BaseBuilder {
|
|
26
|
+
_body;
|
|
27
|
+
_footer;
|
|
28
|
+
constructor(client) {
|
|
29
|
+
super(client);
|
|
30
|
+
this._body = "";
|
|
31
|
+
this._footer = undefined;
|
|
32
|
+
}
|
|
33
|
+
body(text) {
|
|
34
|
+
if (typeof text !== "string")
|
|
35
|
+
throw new ValidationError(`body harus string, diterima ${typeof text}`);
|
|
36
|
+
this._body = text;
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
footer(text) {
|
|
40
|
+
if (text !== undefined && typeof text !== "string") {
|
|
41
|
+
throw new ValidationError(`footer harus string, diterima ${typeof text}`);
|
|
42
|
+
}
|
|
43
|
+
this._footer = text;
|
|
44
|
+
return this;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
class HeaderMessage extends BaseMessage {
|
|
48
|
+
_title;
|
|
49
|
+
_subtitle;
|
|
50
|
+
_location;
|
|
51
|
+
_thumb;
|
|
52
|
+
_headerThumbnail;
|
|
53
|
+
_mediaSlot;
|
|
54
|
+
constructor(client) {
|
|
55
|
+
super(client);
|
|
56
|
+
this._title = undefined;
|
|
57
|
+
this._subtitle = undefined;
|
|
58
|
+
this._location = null;
|
|
59
|
+
this._thumb = null;
|
|
60
|
+
this._headerThumbnail = null;
|
|
61
|
+
this._mediaSlot = null;
|
|
62
|
+
}
|
|
63
|
+
location({ latitude, longitude, name, address, thumbnail, } = {}) {
|
|
64
|
+
this.#assertNoMediaSlot(".location()");
|
|
65
|
+
if (this._headerThumbnail) {
|
|
66
|
+
throw new ValidationError(".headerThumbnail() tidak bisa dipakai bareng .location() — thumbnail location pakai .location({ thumbnail })");
|
|
67
|
+
}
|
|
68
|
+
const lat = assertCoordinate(latitude, "latitude", { min: -90, max: 90 });
|
|
69
|
+
const lng = assertCoordinate(longitude, "longitude", {
|
|
70
|
+
min: -180,
|
|
71
|
+
max: 180,
|
|
72
|
+
});
|
|
73
|
+
assertText(name, "name location", { required: false });
|
|
74
|
+
assertText(address, "address location", { required: false });
|
|
75
|
+
this._location = {
|
|
76
|
+
latitude: lat,
|
|
77
|
+
longitude: lng,
|
|
78
|
+
...(name !== undefined ? { name } : {}),
|
|
79
|
+
...(address !== undefined ? { address } : {}),
|
|
80
|
+
};
|
|
81
|
+
if (thumbnail)
|
|
82
|
+
this.thumb(thumbnail);
|
|
83
|
+
return this;
|
|
84
|
+
}
|
|
85
|
+
thumb(buffer) {
|
|
86
|
+
if (this._headerThumbnail) {
|
|
87
|
+
throw new ValidationError(".thumb() tidak bisa dipakai bareng .headerThumbnail() — .thumb() khusus thumbnail location");
|
|
88
|
+
}
|
|
89
|
+
this.#assertNoMediaSlot(".thumb()");
|
|
90
|
+
this._thumb = buffer ? asThumbnailBytes(buffer) : null;
|
|
91
|
+
return this;
|
|
92
|
+
}
|
|
93
|
+
headerThumbnail(bytes) {
|
|
94
|
+
if (this._location || this._thumb) {
|
|
95
|
+
throw new ValidationError(".headerThumbnail() tidak bisa dipakai bareng .location()/.thumb() — itu thumbnail location, bukan thumbnail header interaktif");
|
|
96
|
+
}
|
|
97
|
+
this.#assertNoMediaSlot(".headerThumbnail()");
|
|
98
|
+
this._headerThumbnail = validateJpegThumbnail(bytes, {
|
|
99
|
+
label: ".headerThumbnail()",
|
|
100
|
+
});
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
image(source, options = {}) {
|
|
104
|
+
return this.#setMediaSlot("image", source, options);
|
|
105
|
+
}
|
|
106
|
+
video(source, options = {}) {
|
|
107
|
+
return this.#setMediaSlot("video", source, options);
|
|
108
|
+
}
|
|
109
|
+
document(source, options = {}) {
|
|
110
|
+
return this.#setMediaSlot("document", source, options);
|
|
111
|
+
}
|
|
112
|
+
#assertNoMediaSlot(caller) {
|
|
113
|
+
if (this._mediaSlot) {
|
|
114
|
+
throw new ValidationError(`${caller} tidak bisa dipakai bareng header media (${this._mediaSlot.kind}) — satu header hanya satu jenis konten`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
#setMediaSlot(kind, source, options) {
|
|
118
|
+
if (options === null ||
|
|
119
|
+
typeof options !== "object" ||
|
|
120
|
+
Array.isArray(options)) {
|
|
121
|
+
throw new ValidationError(`options ${kind}() harus object, diterima ${options === null ? "null" : typeof options}`);
|
|
122
|
+
}
|
|
123
|
+
if (this._location || this._thumb) {
|
|
124
|
+
throw new ValidationError(`.${kind}() tidak bisa dipakai bareng .location()/.thumb() — satu header hanya satu jenis konten`);
|
|
125
|
+
}
|
|
126
|
+
if (this._headerThumbnail) {
|
|
127
|
+
throw new ValidationError(`.${kind}() tidak bisa dipakai bareng .headerThumbnail() — thumbnail gambar header pakai .${kind}(source, { thumbnail })`);
|
|
128
|
+
}
|
|
129
|
+
this._mediaSlot = {
|
|
130
|
+
kind,
|
|
131
|
+
source,
|
|
132
|
+
options: { ...options },
|
|
133
|
+
label: `header ${kind}`,
|
|
134
|
+
};
|
|
135
|
+
return this;
|
|
136
|
+
}
|
|
137
|
+
async _resolveLocation() {
|
|
138
|
+
if (!this._location) {
|
|
139
|
+
if (this._thumb) {
|
|
140
|
+
throw new ValidationError(".thumb() butuh .location() — thumbnail ini dipasang di locationMessage, bukan header interaktif");
|
|
141
|
+
}
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
const { latitude, longitude, name, address } = this._location;
|
|
145
|
+
const location = {
|
|
146
|
+
degreesLatitude: latitude,
|
|
147
|
+
degreesLongitude: longitude,
|
|
148
|
+
};
|
|
149
|
+
if (name !== undefined)
|
|
150
|
+
location.name = name;
|
|
151
|
+
if (address !== undefined)
|
|
152
|
+
location.address = address;
|
|
153
|
+
if (this._thumb?.length) {
|
|
154
|
+
location.jpegThumbnail = await generateImageThumbnail(this._thumb, {
|
|
155
|
+
label: "location .thumb()",
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
return location;
|
|
159
|
+
}
|
|
160
|
+
async _resolveSlotMedia() {
|
|
161
|
+
const slot = this._mediaSlot;
|
|
162
|
+
if (!slot)
|
|
163
|
+
return null;
|
|
164
|
+
if (slot.result)
|
|
165
|
+
return slot.result;
|
|
166
|
+
if (!slot.promise) {
|
|
167
|
+
const client = this.client;
|
|
168
|
+
if (!client)
|
|
169
|
+
throw new MessageError("client belum diset — media header butuh client.message.upload", "NO_CLIENT");
|
|
170
|
+
slot.promise = resolveMediaSlot(client, slot).then((result) => {
|
|
171
|
+
slot.result = result;
|
|
172
|
+
slot.promise = undefined;
|
|
173
|
+
return result;
|
|
174
|
+
}, (error) => {
|
|
175
|
+
slot.promise = undefined;
|
|
176
|
+
throw error;
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
return slot.promise;
|
|
180
|
+
}
|
|
181
|
+
async _resolveInteractiveHeader() {
|
|
182
|
+
const title = this._title;
|
|
183
|
+
const subtitle = this._subtitle;
|
|
184
|
+
const hasTitle = title !== undefined;
|
|
185
|
+
const hasSubtitle = subtitle !== undefined;
|
|
186
|
+
if (this._mediaSlot) {
|
|
187
|
+
const media = await this._resolveSlotMedia();
|
|
188
|
+
if (!media)
|
|
189
|
+
throw new ValidationError("header media tidak bisa diselesaikan");
|
|
190
|
+
const header = { hasMediaAttachment: true };
|
|
191
|
+
if (media.field === "imageMessage")
|
|
192
|
+
header.imageMessage = media.message;
|
|
193
|
+
else if (media.field === "videoMessage")
|
|
194
|
+
header.videoMessage = media.message;
|
|
195
|
+
else
|
|
196
|
+
header.documentMessage = media.message;
|
|
197
|
+
if (hasTitle)
|
|
198
|
+
header.title = title;
|
|
199
|
+
if (hasSubtitle)
|
|
200
|
+
header.subtitle = subtitle;
|
|
201
|
+
return header;
|
|
202
|
+
}
|
|
203
|
+
if (this._location) {
|
|
204
|
+
const location = await this._resolveLocation();
|
|
205
|
+
const header = {
|
|
206
|
+
hasMediaAttachment: true,
|
|
207
|
+
locationMessage: location,
|
|
208
|
+
};
|
|
209
|
+
if (hasTitle)
|
|
210
|
+
header.title = title;
|
|
211
|
+
if (hasSubtitle)
|
|
212
|
+
header.subtitle = subtitle;
|
|
213
|
+
return header;
|
|
214
|
+
}
|
|
215
|
+
if (this._headerThumbnail) {
|
|
216
|
+
const header = {
|
|
217
|
+
hasMediaAttachment: false,
|
|
218
|
+
jpegThumbnail: this._headerThumbnail,
|
|
219
|
+
};
|
|
220
|
+
if (hasTitle)
|
|
221
|
+
header.title = title;
|
|
222
|
+
if (hasSubtitle)
|
|
223
|
+
header.subtitle = subtitle;
|
|
224
|
+
return header;
|
|
225
|
+
}
|
|
226
|
+
if (this._thumb) {
|
|
227
|
+
throw new ValidationError(".thumb() butuh .location() — thumbnail ini dipasang di locationMessage, bukan header interaktif");
|
|
228
|
+
}
|
|
229
|
+
if (hasTitle || hasSubtitle) {
|
|
230
|
+
const header = { hasMediaAttachment: false };
|
|
231
|
+
if (hasTitle)
|
|
232
|
+
header.title = title;
|
|
233
|
+
if (hasSubtitle)
|
|
234
|
+
header.subtitle = subtitle;
|
|
235
|
+
return header;
|
|
236
|
+
}
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
export class Button extends HeaderMessage {
|
|
241
|
+
_buttons;
|
|
242
|
+
_params;
|
|
243
|
+
_offer;
|
|
244
|
+
_offerExpiration;
|
|
245
|
+
_ids;
|
|
246
|
+
constructor(client) {
|
|
247
|
+
super(client);
|
|
248
|
+
this._buttons = [];
|
|
249
|
+
this._params = {};
|
|
250
|
+
this._offer = null;
|
|
251
|
+
this._offerExpiration = null;
|
|
252
|
+
this._ids = new Set();
|
|
253
|
+
}
|
|
254
|
+
title(text) {
|
|
255
|
+
assertText(text, "title", { max: SELECT_TITLE_MAX, required: false });
|
|
256
|
+
this._title = text;
|
|
257
|
+
return this;
|
|
258
|
+
}
|
|
259
|
+
subtitle(text) {
|
|
260
|
+
assertText(text, "subtitle", { max: SELECT_TITLE_MAX, required: false });
|
|
261
|
+
this._subtitle = text;
|
|
262
|
+
return this;
|
|
263
|
+
}
|
|
264
|
+
#push(name, params) {
|
|
265
|
+
this._buttons.push({
|
|
266
|
+
name,
|
|
267
|
+
buttonParamsJson: stringifyParams(params, `buttonParamsJson (${name})`),
|
|
268
|
+
});
|
|
269
|
+
return this;
|
|
270
|
+
}
|
|
271
|
+
quick_reply(display_text, id) {
|
|
272
|
+
assertText(display_text, "display_text quick_reply", {
|
|
273
|
+
max: DISPLAY_TEXT_MAX,
|
|
274
|
+
});
|
|
275
|
+
assertId(id, "id quick_reply");
|
|
276
|
+
assertUniqueId(this._ids, id);
|
|
277
|
+
return this.#push("quick_reply", { display_text, id });
|
|
278
|
+
}
|
|
279
|
+
cta_url(display_text, url) {
|
|
280
|
+
assertText(display_text, "display_text cta_url", { max: DISPLAY_TEXT_MAX });
|
|
281
|
+
assertUrl(url);
|
|
282
|
+
return this.#push("cta_url", { display_text, url });
|
|
283
|
+
}
|
|
284
|
+
cta_copy(display_text, copy_code) {
|
|
285
|
+
assertText(display_text, "display_text cta_copy", {
|
|
286
|
+
max: DISPLAY_TEXT_MAX,
|
|
287
|
+
});
|
|
288
|
+
assertText(copy_code, "copy_code cta_copy", { max: COPY_CODE_MAX });
|
|
289
|
+
return this.#push("cta_copy", { display_text, copy_code });
|
|
290
|
+
}
|
|
291
|
+
cta_call(display_text, phone_number) {
|
|
292
|
+
assertText(display_text, "display_text cta_call", {
|
|
293
|
+
max: DISPLAY_TEXT_MAX,
|
|
294
|
+
});
|
|
295
|
+
assertPhoneNumber(phone_number);
|
|
296
|
+
return this.#push("cta_call", { display_text, phone_number });
|
|
297
|
+
}
|
|
298
|
+
send_location(display_text) {
|
|
299
|
+
const params = display_text === undefined
|
|
300
|
+
? {}
|
|
301
|
+
: {
|
|
302
|
+
display_text: assertText(display_text, "display_text send_location", { max: DISPLAY_TEXT_MAX }),
|
|
303
|
+
};
|
|
304
|
+
return this.#push("send_location", params);
|
|
305
|
+
}
|
|
306
|
+
single_select(title, sections) {
|
|
307
|
+
assertText(title, "title single_select", { max: SELECT_TITLE_MAX });
|
|
308
|
+
validateNativeSections(sections);
|
|
309
|
+
return this.#push("single_select", { title, sections });
|
|
310
|
+
}
|
|
311
|
+
bottom_sheet({ limit = 1, listTitle, buttonTitle, } = {}) {
|
|
312
|
+
assertPositiveInt(limit, "limit bottom_sheet", { min: 1 });
|
|
313
|
+
assertText(listTitle, "listTitle bottom_sheet", { required: false });
|
|
314
|
+
assertText(buttonTitle, "buttonTitle bottom_sheet", { required: false });
|
|
315
|
+
this._params.bottom_sheet = {
|
|
316
|
+
in_thread_buttons_limit: limit,
|
|
317
|
+
...(listTitle !== undefined ? { list_title: listTitle } : {}),
|
|
318
|
+
...(buttonTitle !== undefined ? { button_title: buttonTitle } : {}),
|
|
319
|
+
};
|
|
320
|
+
return this;
|
|
321
|
+
}
|
|
322
|
+
limited_time_offer({ text, url, copyCode, expiresIn = 24 * 60 * 60, } = {}) {
|
|
323
|
+
assertText(text, "text limited_time_offer", { required: false });
|
|
324
|
+
if (url !== undefined)
|
|
325
|
+
assertUrl(url, "url limited_time_offer");
|
|
326
|
+
assertText(copyCode, "copy_code limited_time_offer", {
|
|
327
|
+
max: COPY_CODE_MAX,
|
|
328
|
+
required: false,
|
|
329
|
+
});
|
|
330
|
+
assertPositiveInt(expiresIn, "expiresIn limited_time_offer", { min: 1 });
|
|
331
|
+
this._offer = {
|
|
332
|
+
...(text !== undefined ? { text } : {}),
|
|
333
|
+
...(url !== undefined ? { url } : {}),
|
|
334
|
+
...(copyCode !== undefined ? { copy_code: copyCode } : {}),
|
|
335
|
+
expiresIn,
|
|
336
|
+
};
|
|
337
|
+
this._offerExpiration = null;
|
|
338
|
+
return this;
|
|
339
|
+
}
|
|
340
|
+
async toCard() {
|
|
341
|
+
if (!this._buttons.length)
|
|
342
|
+
throw new ValidationError("minimal satu button");
|
|
343
|
+
const header = await this._resolveInteractiveHeader();
|
|
344
|
+
const footer = this._footer;
|
|
345
|
+
const card = {
|
|
346
|
+
body: { text: this._body },
|
|
347
|
+
nativeFlowMessage: this.#buildNativeFlow(),
|
|
348
|
+
};
|
|
349
|
+
if (header)
|
|
350
|
+
card.header = header;
|
|
351
|
+
if (footer !== undefined)
|
|
352
|
+
card.footer = { text: footer };
|
|
353
|
+
return card;
|
|
354
|
+
}
|
|
355
|
+
async build() {
|
|
356
|
+
return { interactiveMessage: await this.toCard() };
|
|
357
|
+
}
|
|
358
|
+
#buildNativeFlow() {
|
|
359
|
+
const params = { ...this._params };
|
|
360
|
+
if (this._offer) {
|
|
361
|
+
if (this._offerExpiration === null) {
|
|
362
|
+
this._offerExpiration =
|
|
363
|
+
Math.floor(Date.now() / 1000) + Math.floor(this._offer.expiresIn);
|
|
364
|
+
}
|
|
365
|
+
const { expiresIn: expires, ...offer } = this._offer;
|
|
366
|
+
params.limited_time_offer = {
|
|
367
|
+
...offer,
|
|
368
|
+
expiration_time: this._offerExpiration,
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
const nativeFlowMessage = {
|
|
372
|
+
buttons: [...this._buttons],
|
|
373
|
+
messageVersion: 1,
|
|
374
|
+
};
|
|
375
|
+
if (Object.keys(params).length) {
|
|
376
|
+
nativeFlowMessage.messageParamsJson = stringifyParams(params, "messageParamsJson");
|
|
377
|
+
}
|
|
378
|
+
return nativeFlowMessage;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
export class LegacyButton extends HeaderMessage {
|
|
382
|
+
_buttons;
|
|
383
|
+
_ids;
|
|
384
|
+
constructor(client) {
|
|
385
|
+
super(client);
|
|
386
|
+
this._buttons = [];
|
|
387
|
+
this._ids = new Set();
|
|
388
|
+
}
|
|
389
|
+
title(text) {
|
|
390
|
+
assertText(text, "title legacy", {
|
|
391
|
+
max: DISPLAY_TEXT_MAX,
|
|
392
|
+
required: false,
|
|
393
|
+
});
|
|
394
|
+
this._title = text;
|
|
395
|
+
return this;
|
|
396
|
+
}
|
|
397
|
+
headerThumbnail(_bytes) {
|
|
398
|
+
throw new ValidationError("LegacyButton tidak mendukung .headerThumbnail() — ButtonsMessage tidak punya field jpegThumbnail header; pakai .thumb() untuk location atau .image()/.video()/.document() untuk attachment");
|
|
399
|
+
}
|
|
400
|
+
button(id, text) {
|
|
401
|
+
if (this._buttons.length >= LEGACY_BUTTON_LIMIT) {
|
|
402
|
+
throw new ValidationError(`legacy buttons maksimal ${LEGACY_BUTTON_LIMIT}, tombol ke-${this._buttons.length + 1} ditolak`);
|
|
403
|
+
}
|
|
404
|
+
assertId(id, "buttonId legacy");
|
|
405
|
+
assertUniqueId(this._ids, id);
|
|
406
|
+
assertText(text, "displayText legacy button", { max: DISPLAY_TEXT_MAX });
|
|
407
|
+
this._buttons.push({
|
|
408
|
+
buttonId: id,
|
|
409
|
+
buttonText: { displayText: text },
|
|
410
|
+
type: ButtonType.RESPONSE,
|
|
411
|
+
});
|
|
412
|
+
return this;
|
|
413
|
+
}
|
|
414
|
+
async build() {
|
|
415
|
+
if (!this._buttons.length)
|
|
416
|
+
throw new ValidationError("minimal satu button");
|
|
417
|
+
const title = this._title;
|
|
418
|
+
const footer = this._footer;
|
|
419
|
+
const buttonsMessage = {
|
|
420
|
+
contentText: this._body,
|
|
421
|
+
buttons: [...this._buttons],
|
|
422
|
+
headerType: HeaderType.EMPTY,
|
|
423
|
+
};
|
|
424
|
+
if (this._mediaSlot) {
|
|
425
|
+
const media = await this._resolveSlotMedia();
|
|
426
|
+
if (!media)
|
|
427
|
+
throw new ValidationError("header media tidak bisa diselesaikan");
|
|
428
|
+
const kind = this._mediaSlot.kind;
|
|
429
|
+
buttonsMessage.headerType = LEGACY_HEADER_TYPE_BY_KIND[kind];
|
|
430
|
+
if (media.field === "imageMessage")
|
|
431
|
+
buttonsMessage.imageMessage = media.message;
|
|
432
|
+
else if (media.field === "videoMessage")
|
|
433
|
+
buttonsMessage.videoMessage = media.message;
|
|
434
|
+
else
|
|
435
|
+
buttonsMessage.documentMessage = media.message;
|
|
436
|
+
if (title !== undefined)
|
|
437
|
+
buttonsMessage.text = title;
|
|
438
|
+
}
|
|
439
|
+
else if (this._location) {
|
|
440
|
+
buttonsMessage.headerType = HeaderType.LOCATION;
|
|
441
|
+
buttonsMessage.locationMessage = await this._resolveLocation();
|
|
442
|
+
}
|
|
443
|
+
else if (title !== undefined) {
|
|
444
|
+
buttonsMessage.headerType = HeaderType.TEXT;
|
|
445
|
+
buttonsMessage.text = title;
|
|
446
|
+
}
|
|
447
|
+
else {
|
|
448
|
+
buttonsMessage.headerType = HeaderType.EMPTY;
|
|
449
|
+
}
|
|
450
|
+
if (footer !== undefined)
|
|
451
|
+
buttonsMessage.footerText = footer;
|
|
452
|
+
return { buttonsMessage };
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
export class ListMessage extends BaseMessage {
|
|
456
|
+
_title;
|
|
457
|
+
_buttonText;
|
|
458
|
+
_sections;
|
|
459
|
+
_fallback;
|
|
460
|
+
_inGroupOverride;
|
|
461
|
+
_rowIds;
|
|
462
|
+
constructor(client) {
|
|
463
|
+
super(client);
|
|
464
|
+
this._title = "";
|
|
465
|
+
this._buttonText = "Buka";
|
|
466
|
+
this._sections = [];
|
|
467
|
+
this._fallback = "auto";
|
|
468
|
+
this._inGroupOverride = null;
|
|
469
|
+
this._rowIds = new Set();
|
|
470
|
+
}
|
|
471
|
+
title(text) {
|
|
472
|
+
assertText(text, "title list", { max: DISPLAY_TEXT_MAX, required: false });
|
|
473
|
+
this._title = text ?? "";
|
|
474
|
+
return this;
|
|
475
|
+
}
|
|
476
|
+
buttonText(text) {
|
|
477
|
+
assertText(text, "buttonText list", { max: DISPLAY_TEXT_MAX });
|
|
478
|
+
this._buttonText = text;
|
|
479
|
+
return this;
|
|
480
|
+
}
|
|
481
|
+
section(title, rows) {
|
|
482
|
+
assertText(title, "title section", {
|
|
483
|
+
max: SELECT_TITLE_MAX,
|
|
484
|
+
required: false,
|
|
485
|
+
});
|
|
486
|
+
if (!Array.isArray(rows) || rows.length === 0) {
|
|
487
|
+
throw new ValidationError("section butuh array rows berisi minimal satu row");
|
|
488
|
+
}
|
|
489
|
+
rows.forEach((row, index) => {
|
|
490
|
+
if (!row || typeof row !== "object" || Array.isArray(row)) {
|
|
491
|
+
throw new ValidationError(`rows[${index}] harus object row`);
|
|
492
|
+
}
|
|
493
|
+
const record = row;
|
|
494
|
+
const rowId = assertId(record.id, `rows[${index}].id`);
|
|
495
|
+
assertText(record.title, `rows[${index}].title`);
|
|
496
|
+
assertText(record.description, `rows[${index}].description`, {
|
|
497
|
+
max: 200,
|
|
498
|
+
required: false,
|
|
499
|
+
});
|
|
500
|
+
if (this._rowIds.has(rowId))
|
|
501
|
+
throw new DuplicateIdError(rowId);
|
|
502
|
+
this._rowIds.add(rowId);
|
|
503
|
+
});
|
|
504
|
+
this._sections.push({
|
|
505
|
+
...(title !== undefined ? { title } : {}),
|
|
506
|
+
rows: rows.map(({ id, title: rowTitle, description }) => ({
|
|
507
|
+
rowId: id,
|
|
508
|
+
title: rowTitle,
|
|
509
|
+
...(description !== undefined ? { description } : {}),
|
|
510
|
+
})),
|
|
511
|
+
});
|
|
512
|
+
return this;
|
|
513
|
+
}
|
|
514
|
+
fallback(mode) {
|
|
515
|
+
if (mode !== "auto" && mode !== "native" && mode !== "never") {
|
|
516
|
+
throw new ValidationError(`fallback harus 'auto' | 'native' | 'never', diterima "${mode}"`);
|
|
517
|
+
}
|
|
518
|
+
this._fallback = mode;
|
|
519
|
+
return this;
|
|
520
|
+
}
|
|
521
|
+
inGroup(flag) {
|
|
522
|
+
if (typeof flag !== "boolean") {
|
|
523
|
+
throw new ValidationError(`inGroup() butuh boolean, diterima ${typeof flag}`);
|
|
524
|
+
}
|
|
525
|
+
this._inGroupOverride = flag;
|
|
526
|
+
return this;
|
|
527
|
+
}
|
|
528
|
+
toNativeFlow() {
|
|
529
|
+
if (!this._sections.length)
|
|
530
|
+
throw new ValidationError("minimal satu section");
|
|
531
|
+
const footer = this._footer;
|
|
532
|
+
const buttonParamsJson = stringifyParams({
|
|
533
|
+
title: this._buttonText,
|
|
534
|
+
sections: this._sections.map(({ title, rows }) => ({
|
|
535
|
+
...(title !== undefined ? { title } : {}),
|
|
536
|
+
rows: (rows ?? []).map(({ rowId, title, description }) => ({
|
|
537
|
+
id: rowId,
|
|
538
|
+
title,
|
|
539
|
+
...(description !== undefined ? { description } : {}),
|
|
540
|
+
})),
|
|
541
|
+
})),
|
|
542
|
+
}, "buttonParamsJson (single_select)");
|
|
543
|
+
return {
|
|
544
|
+
interactiveMessage: {
|
|
545
|
+
body: { text: this._body },
|
|
546
|
+
...(footer !== undefined ? { footer: { text: footer } } : {}),
|
|
547
|
+
nativeFlowMessage: {
|
|
548
|
+
messageVersion: 1,
|
|
549
|
+
buttons: [{ name: "single_select", buttonParamsJson }],
|
|
550
|
+
},
|
|
551
|
+
},
|
|
552
|
+
};
|
|
553
|
+
}
|
|
554
|
+
build() {
|
|
555
|
+
if (!this._sections.length)
|
|
556
|
+
throw new ValidationError("minimal satu section");
|
|
557
|
+
const footer = this._footer;
|
|
558
|
+
const listMessage = {
|
|
559
|
+
title: this._title,
|
|
560
|
+
description: this._body,
|
|
561
|
+
buttonText: this._buttonText,
|
|
562
|
+
listType: ListType.SINGLE_SELECT,
|
|
563
|
+
sections: this._sections.map((section) => ({
|
|
564
|
+
...section,
|
|
565
|
+
rows: [...(section.rows ?? [])],
|
|
566
|
+
})),
|
|
567
|
+
};
|
|
568
|
+
if (footer !== undefined)
|
|
569
|
+
listMessage.footerText = footer;
|
|
570
|
+
return { listMessage };
|
|
571
|
+
}
|
|
572
|
+
async send(jid, arg) {
|
|
573
|
+
const { client, options } = this._resolve(arg);
|
|
574
|
+
const target = typeof jid === "string" && jid ? jid : this._defaultTarget();
|
|
575
|
+
if (!target) {
|
|
576
|
+
throw new MessageError("tujuan listMessage tidak diketahui. panggil send(jid) atau .defaultTo(jid)", "NO_TARGET");
|
|
577
|
+
}
|
|
578
|
+
const isGroup = this._inGroupOverride ?? isGroupJid(target);
|
|
579
|
+
const useNativeFlow = this._fallback === "native" || (this._fallback === "auto" && isGroup);
|
|
580
|
+
const content = useNativeFlow ? this.toNativeFlow() : this.build();
|
|
581
|
+
const send = client.message.send.bind(client.message);
|
|
582
|
+
return Object.keys(options).length
|
|
583
|
+
? send(target, content, options)
|
|
584
|
+
: send(target, content);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
export function button(client, jid, config, options) {
|
|
588
|
+
if (config === undefined)
|
|
589
|
+
return new Button(client);
|
|
590
|
+
return sendButton(client, jid, config, options);
|
|
591
|
+
}
|
|
592
|
+
export function legacyButton(client, jid, config, options) {
|
|
593
|
+
if (config === undefined)
|
|
594
|
+
return new LegacyButton(client);
|
|
595
|
+
return sendLegacyButton(client, jid, config, options);
|
|
596
|
+
}
|
|
597
|
+
export function listMessage(client, jid, config, options) {
|
|
598
|
+
if (config === undefined)
|
|
599
|
+
return new ListMessage(client);
|
|
600
|
+
return sendListMessage(client, jid, config, options);
|
|
601
|
+
}
|
|
602
|
+
const applyInteractiveHeader = (builder, header) => {
|
|
603
|
+
if (header.type === "image") {
|
|
604
|
+
const { type: _kind, source, ...options } = header;
|
|
605
|
+
builder.image(source, options);
|
|
606
|
+
}
|
|
607
|
+
else if (header.type === "video") {
|
|
608
|
+
const { type: _kind, source, ...options } = header;
|
|
609
|
+
builder.video(source, options);
|
|
610
|
+
}
|
|
611
|
+
else if (header.type === "document") {
|
|
612
|
+
const { type: _kind, source, ...options } = header;
|
|
613
|
+
builder.document(source, options);
|
|
614
|
+
}
|
|
615
|
+
else if (header.type === "location") {
|
|
616
|
+
builder.location({
|
|
617
|
+
latitude: header.latitude,
|
|
618
|
+
longitude: header.longitude,
|
|
619
|
+
...(header.name !== undefined ? { name: header.name } : {}),
|
|
620
|
+
...(header.address !== undefined ? { address: header.address } : {}),
|
|
621
|
+
...(header.thumbnail !== undefined
|
|
622
|
+
? { thumbnail: header.thumbnail }
|
|
623
|
+
: {}),
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
else {
|
|
627
|
+
builder.headerThumbnail(header.jpegThumbnail);
|
|
628
|
+
}
|
|
629
|
+
};
|
|
630
|
+
const applyButtonItem = (builder, item) => {
|
|
631
|
+
if (item.type === "quick_reply")
|
|
632
|
+
builder.quick_reply(item.text, item.id);
|
|
633
|
+
else if (item.type === "cta_url")
|
|
634
|
+
builder.cta_url(item.text, item.url);
|
|
635
|
+
else if (item.type === "cta_copy")
|
|
636
|
+
builder.cta_copy(item.text, item.copyCode);
|
|
637
|
+
else if (item.type === "cta_call")
|
|
638
|
+
builder.cta_call(item.text, item.phoneNumber);
|
|
639
|
+
else if (item.type === "send_location")
|
|
640
|
+
builder.send_location(item.text);
|
|
641
|
+
else
|
|
642
|
+
builder.single_select(item.title, [...item.sections]);
|
|
643
|
+
};
|
|
644
|
+
export const applyButtonConfig = (builder, config) => {
|
|
645
|
+
if (config.title !== undefined)
|
|
646
|
+
builder.title(config.title);
|
|
647
|
+
if (config.subtitle !== undefined)
|
|
648
|
+
builder.subtitle(config.subtitle);
|
|
649
|
+
builder.body(config.body);
|
|
650
|
+
if (config.footer !== undefined)
|
|
651
|
+
builder.footer(config.footer);
|
|
652
|
+
if (config.header !== undefined)
|
|
653
|
+
applyInteractiveHeader(builder, config.header);
|
|
654
|
+
if (config.buttons !== undefined) {
|
|
655
|
+
for (const item of config.buttons)
|
|
656
|
+
applyButtonItem(builder, item);
|
|
657
|
+
}
|
|
658
|
+
if (config.bottomSheet !== undefined)
|
|
659
|
+
builder.bottom_sheet(config.bottomSheet);
|
|
660
|
+
if (config.limitedTimeOffer !== undefined) {
|
|
661
|
+
const { text, url, copyCode, expiresIn } = config.limitedTimeOffer;
|
|
662
|
+
builder.limited_time_offer({
|
|
663
|
+
...(text !== undefined ? { text } : {}),
|
|
664
|
+
...(url !== undefined ? { url } : {}),
|
|
665
|
+
...(copyCode !== undefined ? { copyCode } : {}),
|
|
666
|
+
...(expiresIn !== undefined ? { expiresIn } : {}),
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
};
|
|
670
|
+
export const sendButton = async (client, jid, config, options) => {
|
|
671
|
+
const builder = new Button(client);
|
|
672
|
+
applyButtonConfig(builder, config);
|
|
673
|
+
return sendConfigured(builder, jid, options);
|
|
674
|
+
};
|
|
675
|
+
export const applyLegacyButtonConfig = (builder, config) => {
|
|
676
|
+
if (config.title !== undefined)
|
|
677
|
+
builder.title(config.title);
|
|
678
|
+
builder.body(config.body);
|
|
679
|
+
if (config.footer !== undefined)
|
|
680
|
+
builder.footer(config.footer);
|
|
681
|
+
if (config.header !== undefined)
|
|
682
|
+
applyInteractiveHeader(builder, config.header);
|
|
683
|
+
for (const item of config.buttons)
|
|
684
|
+
builder.button(item.id, item.text);
|
|
685
|
+
};
|
|
686
|
+
export const sendLegacyButton = async (client, jid, config, options) => {
|
|
687
|
+
const builder = new LegacyButton(client);
|
|
688
|
+
applyLegacyButtonConfig(builder, config);
|
|
689
|
+
return sendConfigured(builder, jid, options);
|
|
690
|
+
};
|
|
691
|
+
export const applyListMessageConfig = (builder, config) => {
|
|
692
|
+
if (config.title !== undefined)
|
|
693
|
+
builder.title(config.title);
|
|
694
|
+
builder.body(config.body);
|
|
695
|
+
if (config.footer !== undefined)
|
|
696
|
+
builder.footer(config.footer);
|
|
697
|
+
builder.buttonText(config.buttonText);
|
|
698
|
+
for (const section of config.sections) {
|
|
699
|
+
builder.section(section.title, [...section.rows]);
|
|
700
|
+
}
|
|
701
|
+
if (config.fallback !== undefined)
|
|
702
|
+
builder.fallback(config.fallback);
|
|
703
|
+
if (config.inGroup !== undefined)
|
|
704
|
+
builder.inGroup(config.inGroup);
|
|
705
|
+
};
|
|
706
|
+
export const sendListMessage = async (client, jid, config, options) => {
|
|
707
|
+
const builder = new ListMessage(client);
|
|
708
|
+
applyListMessageConfig(builder, config);
|
|
709
|
+
return sendConfigured(builder, jid, options);
|
|
710
|
+
};
|