@wats/graph 0.3.14 → 0.3.16
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/dist/endpoints/messages/builders-basic.d.ts +2 -1
- package/dist/endpoints/messages/builders-basic.js +50 -10
- package/dist/endpoints/messages/builders-interactive.js +6 -2
- package/dist/endpoints/messages/builders-template.js +23 -2
- package/dist/endpoints/messages/callables.js +33 -1
- package/dist/endpoints/messages/index.d.ts +1 -1
- package/dist/endpoints/messages/index.js +2 -1
- package/dist/endpoints/messages/types.d.ts +50 -30
- package/dist/endpoints/messages/validation.d.ts +11 -0
- package/dist/endpoints/messages/validation.js +48 -0
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { GraphMessagesAudioPayload, GraphMessagesContactsPayload, GraphMessagesDocumentPayload, GraphMessagesImagePayload, GraphMessagesLocationPayload, GraphMessagesReactionPayload, GraphMessagesRemoveReactionInput, GraphMessagesSendAudioInput, GraphMessagesSendContactsInput, GraphMessagesSendDocumentInput, GraphMessagesSendImageInput, GraphMessagesSendLocationInput, GraphMessagesSendReactionInput, GraphMessagesSendStickerInput, GraphMessagesSendTextInput, GraphMessagesSendVideoInput, GraphMessagesStickerPayload, GraphMessagesTextPayload, GraphMessagesVideoPayload } from "./types.js";
|
|
1
|
+
import type { GraphMessagesAudioPayload, GraphMessagesContactsPayload, GraphMessagesDocumentPayload, GraphMessagesImagePayload, GraphMessagesLocationPayload, GraphMessagesReactionPayload, GraphMessagesRemoveReactionInput, GraphMessagesPinPayload, GraphMessagesSendAudioInput, GraphMessagesSendContactsInput, GraphMessagesSendDocumentInput, GraphMessagesSendImageInput, GraphMessagesSendLocationInput, GraphMessagesSendPinInput, GraphMessagesSendReactionInput, GraphMessagesSendStickerInput, GraphMessagesSendTextInput, GraphMessagesSendVideoInput, GraphMessagesStickerPayload, GraphMessagesTextPayload, GraphMessagesVideoPayload } from "./types.js";
|
|
2
2
|
export declare function buildSendTextPayload(input: GraphMessagesSendTextInput): GraphMessagesTextPayload;
|
|
3
3
|
export declare function buildSendImagePayload(input: GraphMessagesSendImageInput): GraphMessagesImagePayload;
|
|
4
4
|
export declare function buildSendVideoPayload(input: GraphMessagesSendVideoInput): GraphMessagesVideoPayload;
|
|
@@ -9,3 +9,4 @@ export declare function buildSendLocationPayload(input: GraphMessagesSendLocatio
|
|
|
9
9
|
export declare function buildSendContactsPayload(input: GraphMessagesSendContactsInput): GraphMessagesContactsPayload;
|
|
10
10
|
export declare function buildSendReactionPayload(input: GraphMessagesSendReactionInput): GraphMessagesReactionPayload;
|
|
11
11
|
export declare function buildRemoveReactionPayload(input: GraphMessagesRemoveReactionInput): GraphMessagesReactionPayload;
|
|
12
|
+
export declare function buildSendPinPayload(input: GraphMessagesSendPinInput): GraphMessagesPinPayload;
|
|
@@ -7,15 +7,18 @@ import {
|
|
|
7
7
|
GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH,
|
|
8
8
|
GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH,
|
|
9
9
|
GRAPH_MESSAGES_MAX_CONTACTS,
|
|
10
|
+
applyRecipientType,
|
|
10
11
|
assertBoundedArray,
|
|
12
|
+
assertMessageRecipient,
|
|
11
13
|
assertNonEmptyControlFreeString,
|
|
12
14
|
assertNoUnsupportedMediaFields,
|
|
15
|
+
assertValidGroupId,
|
|
13
16
|
assertValidMediaLink,
|
|
14
17
|
assertValidNumberInRange,
|
|
15
18
|
asRecordInput,
|
|
16
|
-
assertValidRecipient,
|
|
17
19
|
assertValidReplyToMessageId,
|
|
18
20
|
assertValidText,
|
|
21
|
+
rejectGroupRecipient,
|
|
19
22
|
isPlainOptionsObject,
|
|
20
23
|
mapValidatedArray,
|
|
21
24
|
maybeText,
|
|
@@ -25,7 +28,7 @@ export function buildSendTextPayload(input) {
|
|
|
25
28
|
if (!isPlainOptionsObject(input)) {
|
|
26
29
|
throw new GraphRequestValidationError("Invalid sendText input: expected an options object.");
|
|
27
30
|
}
|
|
28
|
-
const to =
|
|
31
|
+
const { to, recipientType } = assertMessageRecipient(input, "sendText");
|
|
29
32
|
const text = assertValidText(input.text);
|
|
30
33
|
if (input.previewUrl !== undefined && typeof input.previewUrl !== "boolean") {
|
|
31
34
|
throw new GraphRequestValidationError("Invalid sendText input: previewUrl must be a boolean when provided.");
|
|
@@ -41,6 +44,7 @@ export function buildSendTextPayload(input) {
|
|
|
41
44
|
if (input.previewUrl !== undefined) {
|
|
42
45
|
payload.text.preview_url = input.previewUrl;
|
|
43
46
|
}
|
|
47
|
+
applyRecipientType(payload, recipientType);
|
|
44
48
|
if (input.replyToMessageId !== undefined) {
|
|
45
49
|
payload.context = {
|
|
46
50
|
message_id: assertValidReplyToMessageId(input.replyToMessageId)
|
|
@@ -54,7 +58,7 @@ function buildSendMediaPayload(input, mediaType, helperName, options) {
|
|
|
54
58
|
}
|
|
55
59
|
const record = input;
|
|
56
60
|
assertNoUnsupportedMediaFields(record, helperName, options);
|
|
57
|
-
const to =
|
|
61
|
+
const { to, recipientType } = assertMessageRecipient(record, helperName);
|
|
58
62
|
const hasMediaId = record.mediaId !== undefined;
|
|
59
63
|
const hasLink = record.link !== undefined;
|
|
60
64
|
if (hasMediaId === hasLink) {
|
|
@@ -78,6 +82,7 @@ function buildSendMediaPayload(input, mediaType, helperName, options) {
|
|
|
78
82
|
type: mediaType,
|
|
79
83
|
[mediaType]: media
|
|
80
84
|
};
|
|
85
|
+
applyRecipientType(payload, recipientType);
|
|
81
86
|
if (record.replyToMessageId !== undefined) {
|
|
82
87
|
payload.context = {
|
|
83
88
|
message_id: assertValidReplyToMessageId(record.replyToMessageId)
|
|
@@ -126,9 +131,11 @@ export function buildSendStickerPayload(input) {
|
|
|
126
131
|
}
|
|
127
132
|
export function buildSendLocationPayload(input) {
|
|
128
133
|
const record = asRecordInput(input, "sendLocation");
|
|
134
|
+
rejectGroupRecipient(record, "sendLocation", "location messages");
|
|
135
|
+
const { to, recipientType } = assertMessageRecipient(record, "sendLocation");
|
|
129
136
|
const payload = {
|
|
130
137
|
messaging_product: "whatsapp",
|
|
131
|
-
to
|
|
138
|
+
to,
|
|
132
139
|
type: "location",
|
|
133
140
|
location: {
|
|
134
141
|
latitude: assertValidNumberInRange(record.latitude, "latitude", -90, 90, "sendLocation"),
|
|
@@ -141,6 +148,7 @@ export function buildSendLocationPayload(input) {
|
|
|
141
148
|
payload.location.name = name;
|
|
142
149
|
if (address !== undefined)
|
|
143
150
|
payload.location.address = address;
|
|
151
|
+
applyRecipientType(payload, recipientType);
|
|
144
152
|
return withReplyContext(payload, record);
|
|
145
153
|
}
|
|
146
154
|
function normalizeContactName(value, helperName) {
|
|
@@ -242,31 +250,63 @@ function normalizeContact(value, helperName) {
|
|
|
242
250
|
}
|
|
243
251
|
export function buildSendContactsPayload(input) {
|
|
244
252
|
const record = asRecordInput(input, "sendContacts");
|
|
253
|
+
rejectGroupRecipient(record, "sendContacts", "contacts messages");
|
|
254
|
+
const { to, recipientType } = assertMessageRecipient(record, "sendContacts");
|
|
245
255
|
const contacts = mapValidatedArray(assertBoundedArray(record.contacts, "contacts", 1, GRAPH_MESSAGES_MAX_CONTACTS, "sendContacts"), (c) => normalizeContact(c, "sendContacts"));
|
|
246
|
-
return withReplyContext({ messaging_product: "whatsapp", to
|
|
256
|
+
return withReplyContext(applyRecipientType({ messaging_product: "whatsapp", to, type: "contacts", contacts }, recipientType), record);
|
|
247
257
|
}
|
|
248
258
|
export function buildSendReactionPayload(input) {
|
|
249
259
|
const record = asRecordInput(input, "sendReaction");
|
|
260
|
+
rejectGroupRecipient(record, "sendReaction", "reaction messages");
|
|
261
|
+
const { to, recipientType } = assertMessageRecipient(record, "sendReaction");
|
|
250
262
|
const emoji = assertNonEmptyControlFreeString(record.emoji, "emoji", 32, "sendReaction");
|
|
251
|
-
return {
|
|
263
|
+
return applyRecipientType({
|
|
252
264
|
messaging_product: "whatsapp",
|
|
253
|
-
to
|
|
265
|
+
to,
|
|
254
266
|
type: "reaction",
|
|
255
267
|
reaction: {
|
|
256
268
|
message_id: assertNonEmptyControlFreeString(record.messageId, "messageId", GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH, "sendReaction"),
|
|
257
269
|
emoji
|
|
258
270
|
}
|
|
259
|
-
};
|
|
271
|
+
}, recipientType);
|
|
260
272
|
}
|
|
261
273
|
export function buildRemoveReactionPayload(input) {
|
|
262
274
|
const record = asRecordInput(input, "removeReaction");
|
|
263
|
-
|
|
275
|
+
rejectGroupRecipient(record, "removeReaction", "reaction messages");
|
|
276
|
+
const { to, recipientType } = assertMessageRecipient(record, "removeReaction");
|
|
277
|
+
return applyRecipientType({
|
|
264
278
|
messaging_product: "whatsapp",
|
|
265
|
-
to
|
|
279
|
+
to,
|
|
266
280
|
type: "reaction",
|
|
267
281
|
reaction: {
|
|
268
282
|
message_id: assertNonEmptyControlFreeString(record.messageId, "messageId", GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH, "removeReaction"),
|
|
269
283
|
emoji: ""
|
|
270
284
|
}
|
|
285
|
+
}, recipientType);
|
|
286
|
+
}
|
|
287
|
+
export function buildSendPinPayload(input) {
|
|
288
|
+
const record = asRecordInput(input, "sendPin");
|
|
289
|
+
if (record.recipientType !== undefined && record.recipientType !== "group") {
|
|
290
|
+
throw new GraphRequestValidationError("Invalid sendPin input: recipientType must be group when provided.");
|
|
291
|
+
}
|
|
292
|
+
const to = assertValidGroupId(record.to, "sendPin");
|
|
293
|
+
const pinType = record.pinType;
|
|
294
|
+
if (pinType !== "pin" && pinType !== "unpin") {
|
|
295
|
+
throw new GraphRequestValidationError("Invalid sendPin input: pinType must be pin or unpin.");
|
|
296
|
+
}
|
|
297
|
+
const expirationDays = record.expirationDays;
|
|
298
|
+
if (typeof expirationDays !== "number" || !Number.isInteger(expirationDays) || expirationDays < 1 || expirationDays > 30) {
|
|
299
|
+
throw new GraphRequestValidationError("Invalid sendPin input: expirationDays must be an integer between 1 and 30.");
|
|
300
|
+
}
|
|
301
|
+
return {
|
|
302
|
+
messaging_product: "whatsapp",
|
|
303
|
+
recipient_type: "group",
|
|
304
|
+
to,
|
|
305
|
+
type: "pin",
|
|
306
|
+
pin: {
|
|
307
|
+
type: pinType,
|
|
308
|
+
message_id: assertNonEmptyControlFreeString(record.messageId, "messageId", GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH, "sendPin"),
|
|
309
|
+
expiration_days: expirationDays
|
|
310
|
+
}
|
|
271
311
|
};
|
|
272
312
|
}
|
|
@@ -13,12 +13,14 @@ import {
|
|
|
13
13
|
GRAPH_MESSAGES_ROW_TITLE_MAX_LENGTH,
|
|
14
14
|
GRAPH_MESSAGES_SECTION_TITLE_MAX_LENGTH,
|
|
15
15
|
GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH,
|
|
16
|
+
applyRecipientType,
|
|
16
17
|
assertBoundedArray,
|
|
18
|
+
assertMessageRecipient,
|
|
17
19
|
assertNonEmptyControlFreeString,
|
|
18
20
|
assertOnlyKnownKeys,
|
|
19
21
|
assertValidMediaLink,
|
|
20
|
-
assertValidRecipient,
|
|
21
22
|
asRecordInput,
|
|
23
|
+
rejectGroupRecipient,
|
|
22
24
|
isPlainOptionsObject,
|
|
23
25
|
mapValidatedArray,
|
|
24
26
|
maybeText,
|
|
@@ -26,7 +28,9 @@ import {
|
|
|
26
28
|
} from "./validation.js";
|
|
27
29
|
function interactiveBase(input, helperName, interactive) {
|
|
28
30
|
const record = asRecordInput(input, helperName);
|
|
29
|
-
|
|
31
|
+
rejectGroupRecipient(record, helperName, "interactive messages");
|
|
32
|
+
const { to, recipientType } = assertMessageRecipient(record, helperName);
|
|
33
|
+
return withReplyContext(applyRecipientType({ messaging_product: "whatsapp", to, type: "interactive", interactive }, recipientType), record);
|
|
30
34
|
}
|
|
31
35
|
function addHeaderFooter(target, record, helperName) {
|
|
32
36
|
const headerText = maybeText(record.headerText, "headerText", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, helperName);
|
|
@@ -2,17 +2,32 @@ import { GraphRequestValidationError } from "../../errors.js";
|
|
|
2
2
|
import {
|
|
3
3
|
GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH,
|
|
4
4
|
GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH,
|
|
5
|
+
applyRecipientType,
|
|
5
6
|
assertBoundedArray,
|
|
7
|
+
assertMessageRecipient,
|
|
6
8
|
assertNonEmptyControlFreeString,
|
|
7
9
|
assertOnlyKnownKeys,
|
|
10
|
+
assertRecipientType,
|
|
8
11
|
assertValidRecipient,
|
|
9
12
|
asRecordInput,
|
|
13
|
+
rejectGroupRecipient,
|
|
10
14
|
inspectTemplateValue,
|
|
11
15
|
isPlainOptionsObject,
|
|
12
16
|
withReplyContext,
|
|
13
17
|
mapValidatedArray,
|
|
14
18
|
sanitizeTemplateParameter
|
|
15
19
|
} from "./validation.js";
|
|
20
|
+
function assertGroupTemplateCategory(value) {
|
|
21
|
+
if (value === undefined) {
|
|
22
|
+
throw new GraphRequestValidationError("Invalid sendTemplate input: templateCategory is required for group recipients.");
|
|
23
|
+
}
|
|
24
|
+
if (value === "AUTHENTICATION") {
|
|
25
|
+
throw new GraphRequestValidationError("Invalid sendTemplate input: auth templates are not supported for group recipients.");
|
|
26
|
+
}
|
|
27
|
+
if (value !== "UTILITY" && value !== "MARKETING") {
|
|
28
|
+
throw new GraphRequestValidationError("Invalid sendTemplate input: templateCategory must be UTILITY or MARKETING for group recipients.");
|
|
29
|
+
}
|
|
30
|
+
}
|
|
16
31
|
function normalizeTemplateComponent(value, helperName) {
|
|
17
32
|
const cloned = sanitizeTemplateParameter(value, helperName, "component", new WeakSet);
|
|
18
33
|
if (!isPlainOptionsObject(cloned))
|
|
@@ -35,10 +50,14 @@ function normalizeTemplateComponent(value, helperName) {
|
|
|
35
50
|
}
|
|
36
51
|
export function buildSendTemplatePayload(input) {
|
|
37
52
|
const record = asRecordInput(input, "sendTemplate");
|
|
53
|
+
const recipient = assertMessageRecipient(record, "sendTemplate");
|
|
54
|
+
if (recipient.recipientType === "group") {
|
|
55
|
+
assertGroupTemplateCategory(record.templateCategory);
|
|
56
|
+
}
|
|
38
57
|
const template = { name: assertNonEmptyControlFreeString(record.name, "name", GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH, "sendTemplate"), language: { code: assertNonEmptyControlFreeString(record.languageCode, "languageCode", GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, "sendTemplate") } };
|
|
39
58
|
if (record.components !== undefined)
|
|
40
59
|
template.components = mapValidatedArray(assertBoundedArray(record.components, "components", 0, 100, "sendTemplate"), (c) => normalizeTemplateComponent(c, "sendTemplate"));
|
|
41
|
-
return withReplyContext({ messaging_product: "whatsapp", to:
|
|
60
|
+
return withReplyContext(applyRecipientType({ messaging_product: "whatsapp", to: recipient.to, type: "template", template }, recipient.recipientType), record);
|
|
42
61
|
}
|
|
43
62
|
function assertMarketingRecipient(value) {
|
|
44
63
|
if (value === undefined)
|
|
@@ -83,11 +102,13 @@ function copySendMarketingTemplateInput(input) {
|
|
|
83
102
|
if (descriptor.value !== undefined)
|
|
84
103
|
out[key] = descriptor.value;
|
|
85
104
|
}
|
|
86
|
-
assertOnlyKnownKeys(out, ["to", "recipient", "name", "languageCode", "components", "productPolicy", "messageActivitySharing"], "sendMarketingTemplate");
|
|
105
|
+
assertOnlyKnownKeys(out, ["to", "recipient", "recipientType", "name", "languageCode", "components", "productPolicy", "messageActivitySharing"], "sendMarketingTemplate");
|
|
87
106
|
return out;
|
|
88
107
|
}
|
|
89
108
|
export function buildSendMarketingTemplatePayload(input) {
|
|
90
109
|
const record = copySendMarketingTemplateInput(input);
|
|
110
|
+
rejectGroupRecipient(record, "sendMarketingTemplate", "marketing templates");
|
|
111
|
+
assertRecipientType(record.recipientType, "sendMarketingTemplate");
|
|
91
112
|
const to = record.to === undefined ? undefined : assertValidRecipient(record.to, "sendMarketingTemplate");
|
|
92
113
|
const recipient = assertMarketingRecipient(record.recipient);
|
|
93
114
|
if (to === undefined && recipient === undefined) {
|
|
@@ -14,6 +14,10 @@ export class GraphMessagesEndpoint {
|
|
|
14
14
|
import { defineEndpoint } from "../../endpoint.js";
|
|
15
15
|
import { GraphRequestValidationError } from "../../errors.js";
|
|
16
16
|
import { buildSendMarketingTemplatePayload } from "./builders-template.js";
|
|
17
|
+
import {
|
|
18
|
+
assertNonEmptyControlFreeString,
|
|
19
|
+
assertValidGroupId
|
|
20
|
+
} from "./validation.js";
|
|
17
21
|
const sendMarketingTemplateEndpoint = defineEndpoint({
|
|
18
22
|
method: "POST",
|
|
19
23
|
pathTemplate: "/{phoneNumberId}/marketing_messages",
|
|
@@ -53,5 +57,33 @@ export const sendMessage = defineEndpoint({
|
|
|
53
57
|
method: "POST",
|
|
54
58
|
pathTemplate: "/{phoneNumberId}/messages",
|
|
55
59
|
params: { phoneNumberId: { in: "path", required: true } },
|
|
56
|
-
bodyContentType: "application/json"
|
|
60
|
+
bodyContentType: "application/json",
|
|
61
|
+
buildBody: (body) => {
|
|
62
|
+
if (typeof body !== "object" || body === null || Array.isArray(body)) {
|
|
63
|
+
throw new GraphRequestValidationError("Invalid sendMessage input: body must be an object.");
|
|
64
|
+
}
|
|
65
|
+
const record = body;
|
|
66
|
+
if (record.recipient_type === "group") {
|
|
67
|
+
if (record.type !== "text" && record.type !== "image" && record.type !== "video" && record.type !== "audio" && record.type !== "document" && record.type !== "sticker" && record.type !== "template" && record.type !== "pin") {
|
|
68
|
+
throw new GraphRequestValidationError("Invalid sendMessage input: group recipients support text, media, template, and pin message bodies only.");
|
|
69
|
+
}
|
|
70
|
+
assertValidGroupId(record.to, "sendMessage");
|
|
71
|
+
if (record.type === "pin") {
|
|
72
|
+
const pin = record.pin;
|
|
73
|
+
if (typeof pin !== "object" || pin === null || Array.isArray(pin)) {
|
|
74
|
+
throw new GraphRequestValidationError("Invalid sendMessage input: group pin body must be an object.");
|
|
75
|
+
}
|
|
76
|
+
const pinRecord = pin;
|
|
77
|
+
if (pinRecord.type !== "pin" && pinRecord.type !== "unpin") {
|
|
78
|
+
throw new GraphRequestValidationError("Invalid sendMessage input: group pin.type must be pin or unpin.");
|
|
79
|
+
}
|
|
80
|
+
assertNonEmptyControlFreeString(pinRecord.message_id, "pin.message_id", 256, "sendMessage");
|
|
81
|
+
const expirationDays = pinRecord.expiration_days;
|
|
82
|
+
if (typeof expirationDays !== "number" || !Number.isInteger(expirationDays) || expirationDays < 1 || expirationDays > 30) {
|
|
83
|
+
throw new GraphRequestValidationError("Invalid sendMessage input: group pin.expiration_days must be an integer between 1 and 30.");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return body;
|
|
88
|
+
}
|
|
57
89
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * from "./types.js";
|
|
2
2
|
export { GRAPH_MESSAGES_TEXT_BODY_MAX_LENGTH, GRAPH_MESSAGES_RECIPIENT_MAX_DIGITS, GRAPH_MESSAGES_REPLY_TO_MESSAGE_ID_MAX_LENGTH, GRAPH_MESSAGES_MEDIA_ID_MAX_LENGTH, GRAPH_MESSAGES_MEDIA_LINK_MAX_LENGTH, GRAPH_MESSAGES_MEDIA_CAPTION_MAX_LENGTH, GRAPH_MESSAGES_DOCUMENT_FILENAME_MAX_LENGTH, GRAPH_MESSAGES_GENERAL_TEXT_MAX_LENGTH, GRAPH_MESSAGES_SHORT_LABEL_MAX_LENGTH, GRAPH_MESSAGES_BUTTON_TITLE_MAX_LENGTH, GRAPH_MESSAGES_BUTTON_ID_MAX_LENGTH, GRAPH_MESSAGES_SECTION_TITLE_MAX_LENGTH, GRAPH_MESSAGES_ROW_TITLE_MAX_LENGTH, GRAPH_MESSAGES_ROW_DESCRIPTION_MAX_LENGTH, GRAPH_MESSAGES_MAX_REPLY_BUTTONS, GRAPH_MESSAGES_MAX_LIST_SECTIONS, GRAPH_MESSAGES_MAX_LIST_ROWS, GRAPH_MESSAGES_MAX_CONTACTS, GRAPH_MESSAGES_MAX_PRODUCT_ITEMS } from "./validation.js";
|
|
3
|
-
export { buildSendTextPayload, buildSendImagePayload, buildSendVideoPayload, buildSendAudioPayload, buildSendDocumentPayload, buildSendStickerPayload, buildSendLocationPayload, buildSendContactsPayload, buildSendReactionPayload, buildRemoveReactionPayload } from "./builders-basic.js";
|
|
3
|
+
export { buildSendTextPayload, buildSendImagePayload, buildSendVideoPayload, buildSendAudioPayload, buildSendDocumentPayload, buildSendStickerPayload, buildSendLocationPayload, buildSendContactsPayload, buildSendReactionPayload, buildRemoveReactionPayload, buildSendPinPayload } from "./builders-basic.js";
|
|
4
4
|
export { buildSendButtonsPayload, buildSendListPayload, buildSendCtaUrlPayload, buildSendCallPermissionRequestPayload, buildSendProductPayload, buildSendProductsPayload, buildSendCatalogPayload, buildRequestLocationPayload, buildMarkMessageAsReadPayload, buildTypingIndicatorPayload } from "./builders-interactive.js";
|
|
5
5
|
export { buildSendTemplatePayload, buildSendMarketingTemplatePayload } from "./builders-template.js";
|
|
6
6
|
export { buildSendMessagePayload, sendMarketingTemplate, sendMessage, GraphMessagesEndpoint } from "./callables.js";
|
|
@@ -4,14 +4,17 @@ export interface GraphMessagesSendMessageInput {
|
|
|
4
4
|
text: string;
|
|
5
5
|
previewUrl?: boolean;
|
|
6
6
|
}
|
|
7
|
-
export
|
|
7
|
+
export type GraphMessagesRecipientType = "individual" | "group";
|
|
8
|
+
export interface GraphMessagesRecipientInput {
|
|
8
9
|
readonly to: string;
|
|
10
|
+
readonly recipientType?: GraphMessagesRecipientType;
|
|
11
|
+
}
|
|
12
|
+
export interface GraphMessagesSendTextInput extends GraphMessagesRecipientInput {
|
|
9
13
|
readonly text: string;
|
|
10
14
|
readonly previewUrl?: boolean;
|
|
11
15
|
readonly replyToMessageId?: string;
|
|
12
16
|
}
|
|
13
|
-
export interface GraphMessagesSendMediaInput {
|
|
14
|
-
readonly to: string;
|
|
17
|
+
export interface GraphMessagesSendMediaInput extends GraphMessagesRecipientInput {
|
|
15
18
|
readonly mediaId?: string;
|
|
16
19
|
readonly link?: string;
|
|
17
20
|
readonly replyToMessageId?: string;
|
|
@@ -29,8 +32,7 @@ export interface GraphMessagesSendAudioInput extends GraphMessagesSendMediaInput
|
|
|
29
32
|
readonly voice?: boolean;
|
|
30
33
|
}
|
|
31
34
|
export type GraphMessagesSendStickerInput = GraphMessagesSendMediaInput;
|
|
32
|
-
export interface GraphMessagesSendLocationInput {
|
|
33
|
-
readonly to: string;
|
|
35
|
+
export interface GraphMessagesSendLocationInput extends GraphMessagesRecipientInput {
|
|
34
36
|
readonly latitude: number;
|
|
35
37
|
readonly longitude: number;
|
|
36
38
|
readonly name?: string;
|
|
@@ -81,22 +83,18 @@ export interface GraphMessagesContactInput {
|
|
|
81
83
|
readonly org?: GraphMessagesContactOrgInput;
|
|
82
84
|
readonly birthday?: string;
|
|
83
85
|
}
|
|
84
|
-
export interface GraphMessagesSendContactsInput {
|
|
85
|
-
readonly to: string;
|
|
86
|
+
export interface GraphMessagesSendContactsInput extends GraphMessagesRecipientInput {
|
|
86
87
|
readonly contacts: readonly GraphMessagesContactInput[];
|
|
87
88
|
readonly replyToMessageId?: string;
|
|
88
89
|
}
|
|
89
|
-
export interface GraphMessagesSendReactionInput {
|
|
90
|
-
readonly to: string;
|
|
90
|
+
export interface GraphMessagesSendReactionInput extends GraphMessagesRecipientInput {
|
|
91
91
|
readonly messageId: string;
|
|
92
92
|
readonly emoji: string;
|
|
93
93
|
}
|
|
94
|
-
export interface GraphMessagesRemoveReactionInput {
|
|
95
|
-
readonly to: string;
|
|
94
|
+
export interface GraphMessagesRemoveReactionInput extends GraphMessagesRecipientInput {
|
|
96
95
|
readonly messageId: string;
|
|
97
96
|
}
|
|
98
|
-
export interface GraphMessagesSendButtonsInput {
|
|
99
|
-
readonly to: string;
|
|
97
|
+
export interface GraphMessagesSendButtonsInput extends GraphMessagesRecipientInput {
|
|
100
98
|
readonly bodyText: string;
|
|
101
99
|
readonly buttons: readonly {
|
|
102
100
|
readonly id: string;
|
|
@@ -106,8 +104,7 @@ export interface GraphMessagesSendButtonsInput {
|
|
|
106
104
|
readonly footerText?: string;
|
|
107
105
|
readonly replyToMessageId?: string;
|
|
108
106
|
}
|
|
109
|
-
export interface GraphMessagesSendListInput {
|
|
110
|
-
readonly to: string;
|
|
107
|
+
export interface GraphMessagesSendListInput extends GraphMessagesRecipientInput {
|
|
111
108
|
readonly bodyText: string;
|
|
112
109
|
readonly buttonText: string;
|
|
113
110
|
readonly sections: readonly {
|
|
@@ -122,30 +119,26 @@ export interface GraphMessagesSendListInput {
|
|
|
122
119
|
readonly footerText?: string;
|
|
123
120
|
readonly replyToMessageId?: string;
|
|
124
121
|
}
|
|
125
|
-
export interface GraphMessagesSendCtaUrlInput {
|
|
126
|
-
readonly to: string;
|
|
122
|
+
export interface GraphMessagesSendCtaUrlInput extends GraphMessagesRecipientInput {
|
|
127
123
|
readonly bodyText: string;
|
|
128
124
|
readonly displayText: string;
|
|
129
125
|
readonly url: string;
|
|
130
126
|
readonly footerText?: string;
|
|
131
127
|
readonly replyToMessageId?: string;
|
|
132
128
|
}
|
|
133
|
-
export interface GraphMessagesSendCallPermissionRequestInput {
|
|
134
|
-
readonly to: string;
|
|
129
|
+
export interface GraphMessagesSendCallPermissionRequestInput extends GraphMessagesRecipientInput {
|
|
135
130
|
readonly bodyText: string;
|
|
136
131
|
readonly footerText?: string;
|
|
137
132
|
readonly replyToMessageId?: string;
|
|
138
133
|
}
|
|
139
|
-
export interface GraphMessagesSendProductInput {
|
|
140
|
-
readonly to: string;
|
|
134
|
+
export interface GraphMessagesSendProductInput extends GraphMessagesRecipientInput {
|
|
141
135
|
readonly catalogId: string;
|
|
142
136
|
readonly productRetailerId: string;
|
|
143
137
|
readonly bodyText?: string;
|
|
144
138
|
readonly footerText?: string;
|
|
145
139
|
readonly replyToMessageId?: string;
|
|
146
140
|
}
|
|
147
|
-
export interface GraphMessagesSendProductsInput {
|
|
148
|
-
readonly to: string;
|
|
141
|
+
export interface GraphMessagesSendProductsInput extends GraphMessagesRecipientInput {
|
|
149
142
|
readonly catalogId: string;
|
|
150
143
|
readonly headerText: string;
|
|
151
144
|
readonly bodyText: string;
|
|
@@ -158,18 +151,21 @@ export interface GraphMessagesSendProductsInput {
|
|
|
158
151
|
readonly footerText?: string;
|
|
159
152
|
readonly replyToMessageId?: string;
|
|
160
153
|
}
|
|
161
|
-
export interface GraphMessagesSendCatalogInput {
|
|
162
|
-
readonly to: string;
|
|
154
|
+
export interface GraphMessagesSendCatalogInput extends GraphMessagesRecipientInput {
|
|
163
155
|
readonly bodyText: string;
|
|
164
156
|
readonly footerText?: string;
|
|
165
157
|
readonly thumbnailProductRetailerId?: string;
|
|
166
158
|
readonly replyToMessageId?: string;
|
|
167
159
|
}
|
|
168
|
-
export interface GraphMessagesRequestLocationInput {
|
|
169
|
-
readonly to: string;
|
|
160
|
+
export interface GraphMessagesRequestLocationInput extends GraphMessagesRecipientInput {
|
|
170
161
|
readonly bodyText: string;
|
|
171
162
|
readonly replyToMessageId?: string;
|
|
172
163
|
}
|
|
164
|
+
export interface GraphMessagesSendPinInput extends GraphMessagesRecipientInput {
|
|
165
|
+
readonly pinType: "pin" | "unpin";
|
|
166
|
+
readonly messageId: string;
|
|
167
|
+
readonly expirationDays: number;
|
|
168
|
+
}
|
|
173
169
|
export interface GraphMessagesMarkMessageAsReadInput {
|
|
174
170
|
readonly messageId: string;
|
|
175
171
|
}
|
|
@@ -180,10 +176,11 @@ export interface GraphMessagesTemplateComponentInput {
|
|
|
180
176
|
readonly subType?: string;
|
|
181
177
|
readonly index?: string;
|
|
182
178
|
}
|
|
183
|
-
export
|
|
184
|
-
|
|
179
|
+
export type GraphMessagesTemplateCategory = "MARKETING" | "UTILITY" | "AUTHENTICATION";
|
|
180
|
+
export interface GraphMessagesSendTemplateInput extends GraphMessagesRecipientInput {
|
|
185
181
|
readonly name: string;
|
|
186
182
|
readonly languageCode: string;
|
|
183
|
+
readonly templateCategory?: GraphMessagesTemplateCategory;
|
|
187
184
|
readonly components?: readonly GraphMessagesTemplateComponentInput[];
|
|
188
185
|
readonly replyToMessageId?: string;
|
|
189
186
|
}
|
|
@@ -191,6 +188,7 @@ export type GraphMessagesMarketingProductPolicy = "CLOUD_API_FALLBACK" | "STRICT
|
|
|
191
188
|
export type GraphMessagesMarketingMessageStatus = "accepted" | "held_for_quality_assessment" | "paused" | string;
|
|
192
189
|
export interface GraphMessagesSendMarketingTemplateInput {
|
|
193
190
|
readonly to?: string;
|
|
191
|
+
readonly recipientType?: GraphMessagesRecipientType;
|
|
194
192
|
readonly recipient?: string;
|
|
195
193
|
readonly name: string;
|
|
196
194
|
readonly languageCode: string;
|
|
@@ -234,6 +232,7 @@ export interface GraphMessagesMarketingTemplatePayload {
|
|
|
234
232
|
}
|
|
235
233
|
export interface GraphMessagesTextPayload {
|
|
236
234
|
messaging_product: "whatsapp";
|
|
235
|
+
recipient_type?: GraphMessagesRecipientType;
|
|
237
236
|
to: string;
|
|
238
237
|
type: "text";
|
|
239
238
|
text: {
|
|
@@ -256,6 +255,7 @@ interface GraphMessagesAudioReferencePayload extends GraphMessagesMediaReference
|
|
|
256
255
|
}
|
|
257
256
|
export type GraphMessagesImagePayload = {
|
|
258
257
|
messaging_product: "whatsapp";
|
|
258
|
+
recipient_type?: GraphMessagesRecipientType;
|
|
259
259
|
to: string;
|
|
260
260
|
type: "image";
|
|
261
261
|
image: GraphMessagesMediaReferencePayload;
|
|
@@ -265,6 +265,7 @@ export type GraphMessagesImagePayload = {
|
|
|
265
265
|
};
|
|
266
266
|
export type GraphMessagesVideoPayload = {
|
|
267
267
|
messaging_product: "whatsapp";
|
|
268
|
+
recipient_type?: GraphMessagesRecipientType;
|
|
268
269
|
to: string;
|
|
269
270
|
type: "video";
|
|
270
271
|
video: GraphMessagesMediaReferencePayload;
|
|
@@ -274,6 +275,7 @@ export type GraphMessagesVideoPayload = {
|
|
|
274
275
|
};
|
|
275
276
|
export type GraphMessagesAudioPayload = {
|
|
276
277
|
messaging_product: "whatsapp";
|
|
278
|
+
recipient_type?: GraphMessagesRecipientType;
|
|
277
279
|
to: string;
|
|
278
280
|
type: "audio";
|
|
279
281
|
audio: GraphMessagesAudioReferencePayload;
|
|
@@ -283,6 +285,7 @@ export type GraphMessagesAudioPayload = {
|
|
|
283
285
|
};
|
|
284
286
|
export type GraphMessagesDocumentPayload = {
|
|
285
287
|
messaging_product: "whatsapp";
|
|
288
|
+
recipient_type?: GraphMessagesRecipientType;
|
|
286
289
|
to: string;
|
|
287
290
|
type: "document";
|
|
288
291
|
document: GraphMessagesMediaReferencePayload;
|
|
@@ -292,6 +295,7 @@ export type GraphMessagesDocumentPayload = {
|
|
|
292
295
|
};
|
|
293
296
|
export type GraphMessagesStickerPayload = {
|
|
294
297
|
messaging_product: "whatsapp";
|
|
298
|
+
recipient_type?: GraphMessagesRecipientType;
|
|
295
299
|
to: string;
|
|
296
300
|
type: "sticker";
|
|
297
301
|
sticker: GraphMessagesMediaReferencePayload;
|
|
@@ -302,6 +306,7 @@ export type GraphMessagesStickerPayload = {
|
|
|
302
306
|
export type GraphMessagesMediaPayload = GraphMessagesImagePayload | GraphMessagesVideoPayload | GraphMessagesAudioPayload | GraphMessagesDocumentPayload | GraphMessagesStickerPayload;
|
|
303
307
|
export type GraphMessagesLocationPayload = {
|
|
304
308
|
messaging_product: "whatsapp";
|
|
309
|
+
recipient_type?: GraphMessagesRecipientType;
|
|
305
310
|
to: string;
|
|
306
311
|
type: "location";
|
|
307
312
|
location: {
|
|
@@ -316,6 +321,7 @@ export type GraphMessagesLocationPayload = {
|
|
|
316
321
|
};
|
|
317
322
|
export type GraphMessagesContactsPayload = {
|
|
318
323
|
messaging_product: "whatsapp";
|
|
324
|
+
recipient_type?: GraphMessagesRecipientType;
|
|
319
325
|
to: string;
|
|
320
326
|
type: "contacts";
|
|
321
327
|
contacts: readonly Record<string, unknown>[];
|
|
@@ -325,6 +331,7 @@ export type GraphMessagesContactsPayload = {
|
|
|
325
331
|
};
|
|
326
332
|
export type GraphMessagesReactionPayload = {
|
|
327
333
|
messaging_product: "whatsapp";
|
|
334
|
+
recipient_type?: GraphMessagesRecipientType;
|
|
328
335
|
to: string;
|
|
329
336
|
type: "reaction";
|
|
330
337
|
reaction: {
|
|
@@ -334,6 +341,7 @@ export type GraphMessagesReactionPayload = {
|
|
|
334
341
|
};
|
|
335
342
|
export type GraphMessagesInteractivePayload = {
|
|
336
343
|
messaging_product: "whatsapp";
|
|
344
|
+
recipient_type?: GraphMessagesRecipientType;
|
|
337
345
|
to: string;
|
|
338
346
|
type: "interactive";
|
|
339
347
|
interactive: Record<string, unknown>;
|
|
@@ -343,6 +351,7 @@ export type GraphMessagesInteractivePayload = {
|
|
|
343
351
|
};
|
|
344
352
|
export type GraphMessagesTemplatePayload = {
|
|
345
353
|
messaging_product: "whatsapp";
|
|
354
|
+
recipient_type?: GraphMessagesRecipientType;
|
|
346
355
|
to: string;
|
|
347
356
|
type: "template";
|
|
348
357
|
template: Record<string, unknown>;
|
|
@@ -358,6 +367,17 @@ export type GraphMessagesStatusPayload = {
|
|
|
358
367
|
type: "text";
|
|
359
368
|
};
|
|
360
369
|
};
|
|
361
|
-
export type
|
|
370
|
+
export type GraphMessagesPinPayload = {
|
|
371
|
+
messaging_product: "whatsapp";
|
|
372
|
+
recipient_type: "group";
|
|
373
|
+
to: string;
|
|
374
|
+
type: "pin";
|
|
375
|
+
pin: {
|
|
376
|
+
type: "pin" | "unpin";
|
|
377
|
+
message_id: string;
|
|
378
|
+
expiration_days: number;
|
|
379
|
+
};
|
|
380
|
+
};
|
|
381
|
+
export type GraphMessagesRemainingPayload = GraphMessagesLocationPayload | GraphMessagesContactsPayload | GraphMessagesReactionPayload | GraphMessagesInteractivePayload | GraphMessagesTemplatePayload | GraphMessagesStatusPayload | GraphMessagesPinPayload;
|
|
362
382
|
export type GraphMessagesSendBody = GraphMessagesTextPayload | GraphMessagesMediaPayload | GraphMessagesRemainingPayload;
|
|
363
383
|
export {};
|
|
@@ -17,9 +17,20 @@ export declare const GRAPH_MESSAGES_MAX_LIST_SECTIONS = 10;
|
|
|
17
17
|
export declare const GRAPH_MESSAGES_MAX_LIST_ROWS = 10;
|
|
18
18
|
export declare const GRAPH_MESSAGES_MAX_CONTACTS = 257;
|
|
19
19
|
export declare const GRAPH_MESSAGES_MAX_PRODUCT_ITEMS = 30;
|
|
20
|
+
export declare const GRAPH_MESSAGES_GROUP_ID_MAX_LENGTH = 256;
|
|
20
21
|
export declare function isPlainOptionsObject(value: unknown): value is Record<string, unknown>;
|
|
21
22
|
export declare function hasControlChar(value: string): boolean;
|
|
23
|
+
export declare function assertValidGroupId(to: unknown, helperName?: string): string;
|
|
22
24
|
export declare function assertValidRecipient(to: unknown, helperName?: string): string;
|
|
25
|
+
export declare function assertRecipientType(value: unknown, helperName: string): "individual" | "group" | undefined;
|
|
26
|
+
export declare function assertMessageRecipient(record: Record<string, unknown>, helperName: string): {
|
|
27
|
+
readonly to: string;
|
|
28
|
+
readonly recipientType?: "individual" | "group";
|
|
29
|
+
};
|
|
30
|
+
export declare function applyRecipientType<T extends object>(payload: T, recipientType: "individual" | "group" | undefined): T & {
|
|
31
|
+
recipient_type?: "individual" | "group";
|
|
32
|
+
};
|
|
33
|
+
export declare function rejectGroupRecipient(record: Record<string, unknown>, helperName: string, reason: string): void;
|
|
23
34
|
export declare function assertValidText(text: unknown): string;
|
|
24
35
|
export declare function assertValidReplyToMessageId(value: unknown): string;
|
|
25
36
|
export declare function assertNonEmptyControlFreeString(value: unknown, fieldName: string, maxLength: number, helperName: string): string;
|
|
@@ -18,6 +18,7 @@ export const GRAPH_MESSAGES_MAX_LIST_SECTIONS = 10;
|
|
|
18
18
|
export const GRAPH_MESSAGES_MAX_LIST_ROWS = 10;
|
|
19
19
|
export const GRAPH_MESSAGES_MAX_CONTACTS = 257;
|
|
20
20
|
export const GRAPH_MESSAGES_MAX_PRODUCT_ITEMS = 30;
|
|
21
|
+
export const GRAPH_MESSAGES_GROUP_ID_MAX_LENGTH = 256;
|
|
21
22
|
export function isPlainOptionsObject(value) {
|
|
22
23
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
23
24
|
}
|
|
@@ -29,6 +30,27 @@ export function hasControlChar(value) {
|
|
|
29
30
|
}
|
|
30
31
|
return false;
|
|
31
32
|
}
|
|
33
|
+
export function assertValidGroupId(to, helperName = "sendText") {
|
|
34
|
+
if (typeof to !== "string") {
|
|
35
|
+
throw new GraphRequestValidationError(`Invalid ${helperName} input: to must be a group id string when recipientType is group.`);
|
|
36
|
+
}
|
|
37
|
+
if (to.length === 0 || to.trim().length === 0) {
|
|
38
|
+
throw new GraphRequestValidationError(`Invalid ${helperName} input: to must be a non-empty group id string when recipientType is group.`);
|
|
39
|
+
}
|
|
40
|
+
if (hasControlChar(to)) {
|
|
41
|
+
throw new GraphRequestValidationError(`Invalid ${helperName} input: group to must not contain control characters (CR/LF/NUL/etc.).`);
|
|
42
|
+
}
|
|
43
|
+
if (to.includes("/") || to.includes("\\") || to.includes("?") || to.includes("#") || to.includes(":") || to.includes("@")) {
|
|
44
|
+
throw new GraphRequestValidationError(`Invalid ${helperName} input: group to must be an opaque id, not a path, URL, or address.`);
|
|
45
|
+
}
|
|
46
|
+
if (to.length > GRAPH_MESSAGES_GROUP_ID_MAX_LENGTH) {
|
|
47
|
+
throw new GraphRequestValidationError(`Invalid ${helperName} input: group to exceeds ${GRAPH_MESSAGES_GROUP_ID_MAX_LENGTH}-character limit.`);
|
|
48
|
+
}
|
|
49
|
+
if (/^\+?\d{1,15}$/.test(to)) {
|
|
50
|
+
throw new GraphRequestValidationError(`Invalid ${helperName} input: recipientType group requires a group-id-shaped to, not a phone number.`);
|
|
51
|
+
}
|
|
52
|
+
return to;
|
|
53
|
+
}
|
|
32
54
|
export function assertValidRecipient(to, helperName = "sendText") {
|
|
33
55
|
if (typeof to !== "string") {
|
|
34
56
|
throw new GraphRequestValidationError(`Invalid ${helperName} input: to must be a phone-number string.`);
|
|
@@ -47,6 +69,32 @@ export function assertValidRecipient(to, helperName = "sendText") {
|
|
|
47
69
|
}
|
|
48
70
|
return to;
|
|
49
71
|
}
|
|
72
|
+
export function assertRecipientType(value, helperName) {
|
|
73
|
+
if (value === undefined)
|
|
74
|
+
return;
|
|
75
|
+
if (value === "individual" || value === "group")
|
|
76
|
+
return value;
|
|
77
|
+
throw new GraphRequestValidationError(`Invalid ${helperName} input: recipientType must be individual or group when provided.`);
|
|
78
|
+
}
|
|
79
|
+
export function assertMessageRecipient(record, helperName) {
|
|
80
|
+
const recipientType = assertRecipientType(record.recipientType, helperName);
|
|
81
|
+
if (recipientType === "group") {
|
|
82
|
+
return { to: assertValidGroupId(record.to, helperName), recipientType };
|
|
83
|
+
}
|
|
84
|
+
const to = assertValidRecipient(record.to, helperName);
|
|
85
|
+
return recipientType === "individual" ? { to, recipientType } : { to };
|
|
86
|
+
}
|
|
87
|
+
export function applyRecipientType(payload, recipientType) {
|
|
88
|
+
const next = payload;
|
|
89
|
+
if (recipientType !== undefined)
|
|
90
|
+
next.recipient_type = recipientType;
|
|
91
|
+
return next;
|
|
92
|
+
}
|
|
93
|
+
export function rejectGroupRecipient(record, helperName, reason) {
|
|
94
|
+
if (assertRecipientType(record.recipientType, helperName) === "group") {
|
|
95
|
+
throw new GraphRequestValidationError(`Invalid ${helperName} input: ${reason} is not supported for group recipients.`);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
50
98
|
export function assertValidText(text) {
|
|
51
99
|
if (typeof text !== "string") {
|
|
52
100
|
throw new GraphRequestValidationError("Invalid sendText input: text must be a string.");
|