meta-cloud-api 0.0.1-beta.0 → 0.0.2
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/api/base.d.mts +12 -0
- package/dist/api/base.d.ts +12 -0
- package/dist/api/base.js +2 -0
- package/dist/api/base.js.map +1 -0
- package/dist/api/base.mjs +2 -0
- package/dist/api/base.mjs.map +1 -0
- package/dist/api/encryption.d.mts +15 -0
- package/dist/api/encryption.d.ts +15 -0
- package/dist/api/encryption.js +2 -0
- package/dist/api/encryption.js.map +1 -0
- package/dist/api/encryption.mjs +2 -0
- package/dist/api/encryption.mjs.map +1 -0
- package/dist/api/flow.d.mts +114 -0
- package/dist/api/flow.d.ts +114 -0
- package/dist/api/flow.js +2 -0
- package/dist/api/flow.js.map +1 -0
- package/dist/api/flow.mjs +2 -0
- package/dist/api/flow.mjs.map +1 -0
- package/dist/api/index.d.mts +25 -0
- package/dist/api/index.d.ts +25 -0
- package/dist/api/index.js +2 -0
- package/dist/api/index.js.map +1 -0
- package/dist/api/index.mjs +2 -0
- package/dist/api/index.mjs.map +1 -0
- package/dist/api/media.d.mts +17 -0
- package/dist/api/media.d.ts +17 -0
- package/dist/api/media.js +2 -0
- package/dist/api/media.js.map +1 -0
- package/dist/api/media.mjs +2 -0
- package/dist/api/media.mjs.map +1 -0
- package/dist/api/messages.d.mts +27 -0
- package/dist/api/messages.d.ts +27 -0
- package/dist/api/messages.js +2 -0
- package/dist/api/messages.js.map +1 -0
- package/dist/api/messages.mjs +2 -0
- package/dist/api/messages.mjs.map +1 -0
- package/dist/api/phoneNumber.d.mts +17 -0
- package/dist/api/phoneNumber.d.ts +17 -0
- package/dist/api/phoneNumber.js +2 -0
- package/dist/api/phoneNumber.js.map +1 -0
- package/dist/api/phoneNumber.mjs +2 -0
- package/dist/api/phoneNumber.mjs.map +1 -0
- package/dist/api/qrCode.d.mts +18 -0
- package/dist/api/qrCode.d.ts +18 -0
- package/dist/api/qrCode.js +2 -0
- package/dist/api/qrCode.js.map +1 -0
- package/dist/api/qrCode.mjs +2 -0
- package/dist/api/qrCode.mjs.map +1 -0
- package/dist/api/registration.d.mts +14 -0
- package/dist/api/registration.d.ts +14 -0
- package/dist/api/registration.js +2 -0
- package/dist/api/registration.js.map +1 -0
- package/dist/api/registration.mjs +2 -0
- package/dist/api/registration.mjs.map +1 -0
- package/dist/api/template.d.mts +18 -0
- package/dist/api/template.d.ts +18 -0
- package/dist/api/template.js +2 -0
- package/dist/api/template.js.map +1 -0
- package/dist/api/template.mjs +2 -0
- package/dist/api/template.mjs.map +1 -0
- package/dist/api/twoStepVerification.d.mts +13 -0
- package/dist/api/twoStepVerification.d.ts +13 -0
- package/dist/api/twoStepVerification.js +2 -0
- package/dist/api/twoStepVerification.js.map +1 -0
- package/dist/api/twoStepVerification.mjs +2 -0
- package/dist/api/twoStepVerification.mjs.map +1 -0
- package/dist/api/waba.d.mts +16 -0
- package/dist/api/waba.d.ts +16 -0
- package/dist/api/waba.js +2 -0
- package/dist/api/waba.js.map +1 -0
- package/dist/api/waba.mjs +2 -0
- package/dist/api/waba.mjs.map +1 -0
- package/dist/enums-D6WRUWH9.d.mts +585 -0
- package/dist/enums-D6WRUWH9.d.ts +585 -0
- package/dist/httpsClient.d.mts +22 -0
- package/dist/httpsClient.d.ts +22 -0
- package/dist/httpsClient.js +2 -0
- package/dist/httpsClient.js.map +1 -0
- package/dist/httpsClient.mjs +2 -0
- package/dist/httpsClient.mjs.map +1 -0
- package/dist/index.d.mts +35 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -0
- package/dist/request.d.mts +22 -0
- package/dist/request.d.ts +22 -0
- package/dist/request.js +2 -0
- package/dist/request.js.map +1 -0
- package/dist/request.mjs +2 -0
- package/dist/request.mjs.map +1 -0
- package/dist/types/base.d.mts +8 -0
- package/dist/types/base.d.ts +8 -0
- package/dist/types/base.js +2 -0
- package/dist/types/base.js.map +1 -0
- package/dist/types/base.mjs +1 -0
- package/dist/types/base.mjs.map +1 -0
- package/dist/types/config.d.mts +59 -0
- package/dist/types/config.d.ts +59 -0
- package/dist/types/config.js +2 -0
- package/dist/types/config.js.map +1 -0
- package/dist/types/config.mjs +1 -0
- package/dist/types/config.mjs.map +1 -0
- package/dist/types/encryption.d.mts +13 -0
- package/dist/types/encryption.d.ts +13 -0
- package/dist/types/encryption.js +2 -0
- package/dist/types/encryption.js.map +1 -0
- package/dist/types/encryption.mjs +1 -0
- package/dist/types/encryption.mjs.map +1 -0
- package/dist/types/enums.d.mts +1 -0
- package/dist/types/enums.d.ts +1 -0
- package/dist/types/enums.js +2 -0
- package/dist/types/enums.js.map +1 -0
- package/dist/types/enums.mjs +2 -0
- package/dist/types/enums.mjs.map +1 -0
- package/dist/types/flow.d.mts +245 -0
- package/dist/types/flow.d.ts +245 -0
- package/dist/types/flow.js +2 -0
- package/dist/types/flow.js.map +1 -0
- package/dist/types/flow.mjs +2 -0
- package/dist/types/flow.mjs.map +1 -0
- package/dist/types/httpsClient.d.mts +19 -0
- package/dist/types/httpsClient.d.ts +19 -0
- package/dist/types/httpsClient.js +2 -0
- package/dist/types/httpsClient.js.map +1 -0
- package/dist/types/httpsClient.mjs +1 -0
- package/dist/types/httpsClient.mjs.map +1 -0
- package/dist/types/index.d.mts +16 -0
- package/dist/types/index.d.ts +16 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/index.mjs +2 -0
- package/dist/types/index.mjs.map +1 -0
- package/dist/types/logger.d.mts +9 -0
- package/dist/types/logger.d.ts +9 -0
- package/dist/types/logger.js +2 -0
- package/dist/types/logger.js.map +1 -0
- package/dist/types/logger.mjs +1 -0
- package/dist/types/logger.mjs.map +1 -0
- package/dist/types/media.d.mts +31 -0
- package/dist/types/media.d.ts +31 -0
- package/dist/types/media.js +2 -0
- package/dist/types/media.js.map +1 -0
- package/dist/types/media.mjs +1 -0
- package/dist/types/media.mjs.map +1 -0
- package/dist/types/messages.d.mts +322 -0
- package/dist/types/messages.d.ts +322 -0
- package/dist/types/messages.js +2 -0
- package/dist/types/messages.js.map +1 -0
- package/dist/types/messages.mjs +1 -0
- package/dist/types/messages.mjs.map +1 -0
- package/dist/types/phoneNumber.d.mts +82 -0
- package/dist/types/phoneNumber.d.ts +82 -0
- package/dist/types/phoneNumber.js +2 -0
- package/dist/types/phoneNumber.js.map +1 -0
- package/dist/types/phoneNumber.mjs +1 -0
- package/dist/types/phoneNumber.mjs.map +1 -0
- package/dist/types/qrCode.d.mts +27 -0
- package/dist/types/qrCode.d.ts +27 -0
- package/dist/types/qrCode.js +2 -0
- package/dist/types/qrCode.js.map +1 -0
- package/dist/types/qrCode.mjs +1 -0
- package/dist/types/qrCode.mjs.map +1 -0
- package/dist/types/registration.d.mts +14 -0
- package/dist/types/registration.d.ts +14 -0
- package/dist/types/registration.js +2 -0
- package/dist/types/registration.js.map +1 -0
- package/dist/types/registration.mjs +1 -0
- package/dist/types/registration.mjs.map +1 -0
- package/dist/types/request.d.mts +47 -0
- package/dist/types/request.d.ts +47 -0
- package/dist/types/request.js +2 -0
- package/dist/types/request.js.map +1 -0
- package/dist/types/request.mjs +1 -0
- package/dist/types/request.mjs.map +1 -0
- package/dist/types/template.d.mts +106 -0
- package/dist/types/template.d.ts +106 -0
- package/dist/types/template.js +2 -0
- package/dist/types/template.js.map +1 -0
- package/dist/types/template.mjs +1 -0
- package/dist/types/template.mjs.map +1 -0
- package/dist/types/twoStepVerification.d.mts +11 -0
- package/dist/types/twoStepVerification.d.ts +11 -0
- package/dist/types/twoStepVerification.js +2 -0
- package/dist/types/twoStepVerification.js.map +1 -0
- package/dist/types/twoStepVerification.mjs +1 -0
- package/dist/types/twoStepVerification.mjs.map +1 -0
- package/dist/types/waba.d.mts +19 -0
- package/dist/types/waba.d.ts +19 -0
- package/dist/types/waba.js +2 -0
- package/dist/types/waba.js.map +1 -0
- package/dist/types/waba.mjs +1 -0
- package/dist/types/waba.mjs.map +1 -0
- package/dist/types/whatsapp.d.mts +31 -0
- package/dist/types/whatsapp.d.ts +31 -0
- package/dist/types/whatsapp.js +2 -0
- package/dist/types/whatsapp.js.map +1 -0
- package/dist/types/whatsapp.mjs +1 -0
- package/dist/types/whatsapp.mjs.map +1 -0
- package/dist/utils/generateXHub256Sig.d.mts +3 -0
- package/dist/utils/generateXHub256Sig.d.ts +3 -0
- package/dist/utils/generateXHub256Sig.js +2 -0
- package/dist/utils/generateXHub256Sig.js.map +1 -0
- package/dist/utils/generateXHub256Sig.mjs +2 -0
- package/dist/utils/generateXHub256Sig.mjs.map +1 -0
- package/dist/utils/importConfig.d.mts +7 -0
- package/dist/utils/importConfig.d.ts +7 -0
- package/dist/utils/importConfig.js +2 -0
- package/dist/utils/importConfig.js.map +1 -0
- package/dist/utils/importConfig.mjs +2 -0
- package/dist/utils/importConfig.mjs.map +1 -0
- package/dist/utils/index.d.mts +5 -0
- package/dist/utils/index.d.ts +5 -0
- package/dist/utils/index.js +2 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/index.mjs +2 -0
- package/dist/utils/index.mjs.map +1 -0
- package/dist/utils/isMetaError.d.mts +21 -0
- package/dist/utils/isMetaError.d.ts +21 -0
- package/dist/utils/isMetaError.js +2 -0
- package/dist/utils/isMetaError.js.map +1 -0
- package/dist/utils/isMetaError.mjs +2 -0
- package/dist/utils/isMetaError.mjs.map +1 -0
- package/dist/utils/logger.d.mts +13 -0
- package/dist/utils/logger.d.ts +13 -0
- package/dist/utils/logger.js +2 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/logger.mjs +2 -0
- package/dist/utils/logger.mjs.map +1 -0
- package/dist/utils/objectToQueryString.d.mts +3 -0
- package/dist/utils/objectToQueryString.d.ts +3 -0
- package/dist/utils/objectToQueryString.js +2 -0
- package/dist/utils/objectToQueryString.js.map +1 -0
- package/dist/utils/objectToQueryString.mjs +2 -0
- package/dist/utils/objectToQueryString.mjs.map +1 -0
- package/dist/whatsapp.d.mts +54 -0
- package/dist/whatsapp.d.ts +54 -0
- package/dist/whatsapp.js +2 -0
- package/dist/whatsapp.js.map +1 -0
- package/dist/whatsapp.mjs +2 -0
- package/dist/whatsapp.mjs.map +1 -0
- package/package.json +51 -7
- package/src/api/base.ts +19 -0
- package/src/api/encryption.ts +34 -0
- package/src/api/flow.ts +253 -0
- package/src/api/index.ts +24 -0
- package/src/api/media.ts +52 -0
- package/src/api/messages.ts +160 -0
- package/src/api/phoneNumber.ts +59 -0
- package/src/api/qrCode.ts +64 -0
- package/src/api/registration.ts +38 -0
- package/src/api/template.ts +70 -0
- package/src/api/twoStepVerification.ts +24 -0
- package/src/api/waba.ts +55 -0
- package/src/httpsClient.ts +83 -0
- package/src/index.ts +3 -0
- package/src/request.ts +104 -0
- package/src/types/base.ts +5 -0
- package/src/types/config.ts +68 -0
- package/src/types/encryption.ts +11 -0
- package/src/types/enums.ts +563 -0
- package/src/types/flow.ts +274 -0
- package/src/types/httpsClient.ts +28 -0
- package/src/types/index.ts +16 -0
- package/src/types/logger.ts +7 -0
- package/src/types/media.ts +31 -0
- package/src/types/messages.ts +453 -0
- package/src/types/phoneNumber.ts +111 -0
- package/src/types/qrCode.ts +30 -0
- package/src/types/registration.ts +17 -0
- package/src/types/request.ts +66 -0
- package/src/types/template.ts +134 -0
- package/src/types/twoStepVerification.ts +9 -0
- package/src/types/waba.ts +18 -0
- package/src/types/whatsapp.ts +31 -0
- package/src/utils/.gitkeep +0 -0
- package/src/utils/generateXHub256Sig.ts +5 -0
- package/src/utils/importConfig.ts +55 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/isMetaError.ts +44 -0
- package/src/utils/logger.ts +46 -0
- package/src/utils/objectToQueryString.ts +16 -0
- package/src/whatsapp.ts +96 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { CategoryEnum, LanguagesEnum, TemplateStatusEnum } from './enums';
|
|
2
|
+
import { GeneralRequestBody, RequesterResponseInterface, ResponsePagination, ResponseSuccess } from './request';
|
|
3
|
+
|
|
4
|
+
export type TemplateFormat = 'TEXT' | 'IMAGE' | 'VIDEO' | 'DOCUMENT' | 'LOCATION';
|
|
5
|
+
|
|
6
|
+
export type PhoneNumberButton = {
|
|
7
|
+
type: 'PHONE_NUMBER';
|
|
8
|
+
text: string; // max 25 chars
|
|
9
|
+
phone_number: string; // max 20 chars
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type URLButton = {
|
|
13
|
+
type: 'URL';
|
|
14
|
+
text: string; // max 25 chars
|
|
15
|
+
url: string; // max 2000 chars
|
|
16
|
+
example?: string[]; // Required if url contains variable
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type QuickReplyButton = {
|
|
20
|
+
type: 'QUICK_REPLY';
|
|
21
|
+
text: string; // max 25 chars
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type CopyCodeButton = {
|
|
25
|
+
type: 'COPY_CODE';
|
|
26
|
+
example: string; // max 15 chars
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export type FlowButton = {
|
|
30
|
+
type: 'FLOW';
|
|
31
|
+
text: string; // max 25 chars
|
|
32
|
+
flow_id?: string;
|
|
33
|
+
flow_name?: string;
|
|
34
|
+
flow_json?: string;
|
|
35
|
+
flow_action?: 'navigate' | 'data_exchange';
|
|
36
|
+
navigate_screen?: string;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type MPMButton = {
|
|
40
|
+
type: 'MPM';
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type OTPButton = {
|
|
44
|
+
type: 'OTP';
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
export type SPMButton = {
|
|
48
|
+
type: 'SPM';
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type TemplateButton =
|
|
52
|
+
| PhoneNumberButton
|
|
53
|
+
| URLButton
|
|
54
|
+
| QuickReplyButton
|
|
55
|
+
| CopyCodeButton
|
|
56
|
+
| FlowButton
|
|
57
|
+
| MPMButton
|
|
58
|
+
| OTPButton
|
|
59
|
+
| SPMButton;
|
|
60
|
+
|
|
61
|
+
export type TemplateHeaderExample = {
|
|
62
|
+
header_text?: string[];
|
|
63
|
+
header_text_named_params?: Array<{
|
|
64
|
+
param_name: string;
|
|
65
|
+
example: string;
|
|
66
|
+
}>;
|
|
67
|
+
header_handle?: string[];
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export type TemplateHeader = {
|
|
71
|
+
type: 'HEADER';
|
|
72
|
+
format: TemplateFormat;
|
|
73
|
+
text?: string; // max 60 chars
|
|
74
|
+
example?: TemplateHeaderExample;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export type TemplateBody = {
|
|
78
|
+
type: 'BODY';
|
|
79
|
+
text: string; // max 1024 chars
|
|
80
|
+
example?: {
|
|
81
|
+
body_text?: Array<Array<string>>; // For positional parameters
|
|
82
|
+
body_text_named_params?: Array<{
|
|
83
|
+
param_name: string; // lowercase letters and underscores only
|
|
84
|
+
example: string;
|
|
85
|
+
}>; // For named parameters
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
export type TemplateFooter = {
|
|
90
|
+
type: 'FOOTER';
|
|
91
|
+
text: string; // max 60 chars
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export type ComponentTypes = TemplateHeader | TemplateBody | TemplateFooter | TemplateButton;
|
|
95
|
+
|
|
96
|
+
export type TemplateRequestBody = GeneralRequestBody & {
|
|
97
|
+
name: string;
|
|
98
|
+
language: LanguagesEnum;
|
|
99
|
+
category?: CategoryEnum;
|
|
100
|
+
components?: ComponentTypes[];
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
export type TemplateResponse = {
|
|
104
|
+
id: string;
|
|
105
|
+
status: string;
|
|
106
|
+
language: LanguagesEnum;
|
|
107
|
+
category: CategoryEnum;
|
|
108
|
+
name: string;
|
|
109
|
+
components: ComponentTypes[];
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export type TemplateGetParams = {
|
|
113
|
+
limit?: number;
|
|
114
|
+
name?: string;
|
|
115
|
+
language?: LanguagesEnum;
|
|
116
|
+
category?: CategoryEnum;
|
|
117
|
+
status?: TemplateStatusEnum;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export type TemplateDeleteParams = {
|
|
121
|
+
hsm_id: string;
|
|
122
|
+
name: string;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
export declare class TemplateClass {
|
|
126
|
+
getTemplate(templateId: string): Promise<RequesterResponseInterface<TemplateResponse>>;
|
|
127
|
+
updateTemplate(
|
|
128
|
+
templateId: string,
|
|
129
|
+
template: Partial<TemplateRequestBody>,
|
|
130
|
+
): Promise<RequesterResponseInterface<ResponseSuccess>>;
|
|
131
|
+
getTemplates(params: TemplateGetParams): Promise<RequesterResponseInterface<ResponsePagination<TemplateResponse>>>;
|
|
132
|
+
createTemplate(template: TemplateRequestBody): Promise<RequesterResponseInterface<TemplateResponse>>;
|
|
133
|
+
deleteTemplate(params: TemplateDeleteParams): Promise<RequesterResponseInterface<ResponseSuccess>>;
|
|
134
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { RequesterResponseInterface, ResponseSuccess } from './request';
|
|
2
|
+
|
|
3
|
+
export type TwoStepVerificationRequest = {
|
|
4
|
+
pin: string;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export interface TwoStepVerificationClass {
|
|
8
|
+
setTwoStepVerificationCode(pin: string): Promise<RequesterResponseInterface<ResponseSuccess>>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { RequesterResponseInterface, ResponseData, ResponseSuccess } from './request';
|
|
2
|
+
|
|
3
|
+
export type WabaSubscription = {
|
|
4
|
+
whatsapp_business_api_data: {
|
|
5
|
+
id: string;
|
|
6
|
+
link: string;
|
|
7
|
+
name: string;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type WabaSubscriptions = ResponseData<Array<WabaSubscription>>;
|
|
12
|
+
|
|
13
|
+
export declare class WABAClass {
|
|
14
|
+
getAllWabaSubscriptions(): Promise<RequesterResponseInterface<WabaSubscriptions>>;
|
|
15
|
+
subscribeToWaba(): Promise<RequesterResponseInterface<ResponseSuccess>>;
|
|
16
|
+
unsubscribeFromWaba(): Promise<RequesterResponseInterface<ResponseSuccess>>;
|
|
17
|
+
overrideWabaWebhook(webhookUrl: string, verifyToken: string): Promise<RequesterResponseInterface<ResponseSuccess>>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
* All rights reserved.
|
|
4
|
+
*
|
|
5
|
+
* This source code is licensed under the license found in the
|
|
6
|
+
* LICENSE file in the root directory of this source tree.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export type WhatsAppConfig = {
|
|
10
|
+
accessToken: string;
|
|
11
|
+
baseUrl?: string;
|
|
12
|
+
appId?: string;
|
|
13
|
+
appSecret?: string;
|
|
14
|
+
phoneNumberId?: number;
|
|
15
|
+
businessAcctId?: string;
|
|
16
|
+
apiVersion?: string;
|
|
17
|
+
webhookEndpoint?: string;
|
|
18
|
+
webhookVerificationToken?: string;
|
|
19
|
+
listenerPort?: number;
|
|
20
|
+
debug?: boolean;
|
|
21
|
+
maxRetriesAfterWait?: number;
|
|
22
|
+
requestTimeout?: number;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export declare class WhatsAppClass {
|
|
26
|
+
constructor(config?: WhatsAppConfig);
|
|
27
|
+
updateTimeout(ms: number): boolean;
|
|
28
|
+
updatePhoneNumberId(phoneNumberId: number): boolean;
|
|
29
|
+
updateAccessToken(accessToken: string): boolean;
|
|
30
|
+
updateWabaId(wabaId: string): boolean;
|
|
31
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import Logger from './logger';
|
|
2
|
+
import type { WabaConfigType } from '../types/config';
|
|
3
|
+
import { WabaConfigEnum } from '../types/enums';
|
|
4
|
+
import { WhatsAppConfig } from 'src/types/whatsapp';
|
|
5
|
+
|
|
6
|
+
const LIB_NAME = 'UTILS';
|
|
7
|
+
const LOG_LOCAL = false;
|
|
8
|
+
const LOGGER = new Logger(LIB_NAME, process.env.DEBUG === 'true' || LOG_LOCAL);
|
|
9
|
+
const DEFAULT_BASE_URL = 'graph.facebook.com';
|
|
10
|
+
const DEFAULT_APIVersion = 'v22.0';
|
|
11
|
+
const DEFAULT_LISTENER_PORT = 3000;
|
|
12
|
+
const DEFAULT_MAX_RETRIES_AFTER_WAIT = 30;
|
|
13
|
+
const DEFAULT_REQUEST_TIMEOUT = 20000;
|
|
14
|
+
|
|
15
|
+
const emptyConfigChecker = (phoneNumberId?: number, accessToken?: string) => {
|
|
16
|
+
if (!process.env[WabaConfigEnum.PhoneNumberId] && !phoneNumberId) {
|
|
17
|
+
LOGGER.log(`Environmental variable: WA_PHONE_NUMBER_ID and/or sender phone number id argument is undefined.`);
|
|
18
|
+
throw new Error('Missing WhatsApp sender phone number Id.');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (!process.env[WabaConfigEnum.AccessToken] && !accessToken) {
|
|
22
|
+
LOGGER.log(`Environmental variable: CLOUD_API_ACCESS_TOKEN and/or access token argument is undefined.`);
|
|
23
|
+
throw new Error('Missing WhatsApp access token.');
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!process.env[WabaConfigEnum.APIVersion]) {
|
|
27
|
+
LOGGER.log(`Environmental variable: CLOUD_API_VERSION is undefined.`);
|
|
28
|
+
throw new Error('Missing WhatsApp API version.');
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const importConfig = (inputConfig?: WhatsAppConfig) => {
|
|
33
|
+
emptyConfigChecker(inputConfig?.phoneNumberId, inputConfig?.accessToken);
|
|
34
|
+
|
|
35
|
+
const wabaConfig: WabaConfigType = {
|
|
36
|
+
[WabaConfigEnum.BaseURL]: process.env.WA_BASE_URL || DEFAULT_BASE_URL,
|
|
37
|
+
[WabaConfigEnum.AppId]: process.env.M4D_APP_ID || '',
|
|
38
|
+
[WabaConfigEnum.AppSecret]: process.env.M4D_APP_SECRET || '',
|
|
39
|
+
[WabaConfigEnum.PhoneNumberId]: inputConfig?.phoneNumberId || parseInt(process.env.WA_PHONE_NUMBER_ID || ''),
|
|
40
|
+
[WabaConfigEnum.BusinessAcctId]: process.env.WA_BUSINESS_ACCOUNT_ID || '',
|
|
41
|
+
[WabaConfigEnum.APIVersion]: process.env.CLOUD_API_VERSION || DEFAULT_APIVersion,
|
|
42
|
+
[WabaConfigEnum.AccessToken]: inputConfig?.accessToken || process.env.CLOUD_API_ACCESS_TOKEN || '',
|
|
43
|
+
[WabaConfigEnum.WebhookEndpoint]: process.env.WEBHOOK_ENDPOINT || '',
|
|
44
|
+
[WabaConfigEnum.WebhookVerificationToken]: process.env.WEBHOOK_VERIFICATION_TOKEN || '',
|
|
45
|
+
[WabaConfigEnum.ListenerPort]: parseInt(process.env.LISTENER_PORT || '') || DEFAULT_LISTENER_PORT,
|
|
46
|
+
[WabaConfigEnum.MaxRetriesAfterWait]:
|
|
47
|
+
parseInt(process.env.MAX_RETRIES_AFTER_WAIT || '') || DEFAULT_MAX_RETRIES_AFTER_WAIT,
|
|
48
|
+
[WabaConfigEnum.RequestTimeout]: parseInt(process.env.REQUEST_TIMEOUT || '') || DEFAULT_REQUEST_TIMEOUT,
|
|
49
|
+
[WabaConfigEnum.Debug]: process.env.DEBUG === 'true',
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
LOGGER.log(`Configuration loaded for App Id ${wabaConfig[WabaConfigEnum.AppId]}`);
|
|
53
|
+
|
|
54
|
+
return wabaConfig;
|
|
55
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export interface MetaError extends Error {
|
|
2
|
+
error: {
|
|
3
|
+
message: string;
|
|
4
|
+
type: string;
|
|
5
|
+
code: number;
|
|
6
|
+
error_data?: {
|
|
7
|
+
messaging_product: 'whatsapp';
|
|
8
|
+
details: string;
|
|
9
|
+
};
|
|
10
|
+
error_subcode?: number;
|
|
11
|
+
fbtrace_id: string;
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Determines if the error is a Meta API error response
|
|
17
|
+
* @param error Any error object to check
|
|
18
|
+
* @returns Type guard indicating if error is a Meta API error
|
|
19
|
+
*/
|
|
20
|
+
export function isMetaError(error: any): error is MetaError {
|
|
21
|
+
return (
|
|
22
|
+
error != null &&
|
|
23
|
+
typeof error === 'object' &&
|
|
24
|
+
'error' in error &&
|
|
25
|
+
typeof error.error === 'object' &&
|
|
26
|
+
error.error != null &&
|
|
27
|
+
'message' in error.error &&
|
|
28
|
+
typeof error.error.message === 'string' &&
|
|
29
|
+
'type' in error.error &&
|
|
30
|
+
typeof error.error.type === 'string' &&
|
|
31
|
+
'code' in error.error &&
|
|
32
|
+
typeof error.error.code === 'number' &&
|
|
33
|
+
'fbtrace_id' in error.error &&
|
|
34
|
+
typeof error.error.fbtrace_id === 'string' &&
|
|
35
|
+
(!('error_data' in error.error) ||
|
|
36
|
+
(typeof error.error.error_data === 'object' &&
|
|
37
|
+
error.error.error_data != null &&
|
|
38
|
+
'messaging_product' in error.error.error_data &&
|
|
39
|
+
error.error.error_data.messaging_product === 'whatsapp' &&
|
|
40
|
+
'details' in error.error.error_data &&
|
|
41
|
+
typeof error.error.error_data.details === 'string')) &&
|
|
42
|
+
(!('error_subcode' in error.error) || typeof error.error.error_subcode === 'number')
|
|
43
|
+
);
|
|
44
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
import type { LoggerInterface } from '../types/logger';
|
|
3
|
+
|
|
4
|
+
export default class Logger implements LoggerInterface {
|
|
5
|
+
private name: string;
|
|
6
|
+
private debug: boolean;
|
|
7
|
+
|
|
8
|
+
constructor(name: string, debug: boolean = false) {
|
|
9
|
+
this.name = name;
|
|
10
|
+
this.debug = debug;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
log(...data: any[]) {
|
|
14
|
+
if (this.debug) {
|
|
15
|
+
let prefix = `[ ${Date.now()} ]`;
|
|
16
|
+
if (this.name) {
|
|
17
|
+
prefix += ` - ${this.name}`;
|
|
18
|
+
}
|
|
19
|
+
console.log.apply(console, [prefix, ': ', ...data]);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
error(...data: any[]) {
|
|
24
|
+
let prefix = `[ ${Date.now()} ] - ERROR`;
|
|
25
|
+
if (this.name) {
|
|
26
|
+
prefix += ` - ${this.name}`;
|
|
27
|
+
}
|
|
28
|
+
console.error.apply(console, [prefix, ': ', ...data]);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
warn(...data: any[]) {
|
|
32
|
+
let prefix = `[ ${Date.now()} ] - WARN`;
|
|
33
|
+
if (this.name) {
|
|
34
|
+
prefix += ` - ${this.name}`;
|
|
35
|
+
}
|
|
36
|
+
console.warn.apply(console, [prefix, ': ', ...data]);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
info(...data: any[]) {
|
|
40
|
+
let prefix = `[ ${Date.now()} ] - INFO`;
|
|
41
|
+
if (this.name) {
|
|
42
|
+
prefix += ` - ${this.name}`;
|
|
43
|
+
}
|
|
44
|
+
console.info.apply(console, [prefix, ': ', ...data]);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export const objectToQueryString = (params: Record<string, any>): string => {
|
|
2
|
+
if (!params || Object.keys(params).length === 0) {
|
|
3
|
+
return '';
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
const searchParams = new URLSearchParams();
|
|
7
|
+
|
|
8
|
+
for (const [key, value] of Object.entries(params)) {
|
|
9
|
+
if (value !== undefined) {
|
|
10
|
+
searchParams.append(key, String(value));
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const queryString = searchParams.toString();
|
|
15
|
+
return queryString ? `?${queryString}` : '';
|
|
16
|
+
};
|
package/src/whatsapp.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { WabaConfigType } from './types/config';
|
|
2
|
+
import * as SDKEnums from './types/enums';
|
|
3
|
+
import Logger from './utils/logger';
|
|
4
|
+
|
|
5
|
+
import { importConfig } from './utils/importConfig';
|
|
6
|
+
import Requester from './request';
|
|
7
|
+
import { WhatsAppClass, WhatsAppConfig } from './types/whatsapp';
|
|
8
|
+
import {
|
|
9
|
+
MessagesAPI,
|
|
10
|
+
PhoneNumberAPI,
|
|
11
|
+
TemplateAPI,
|
|
12
|
+
QrCodeAPI,
|
|
13
|
+
EncryptionAPI,
|
|
14
|
+
TwoStepVerificationAPI,
|
|
15
|
+
RegistrationAPI,
|
|
16
|
+
MediaAPI,
|
|
17
|
+
WabaAPI,
|
|
18
|
+
FlowAPI,
|
|
19
|
+
} from './api';
|
|
20
|
+
|
|
21
|
+
const LIB_NAME = 'WHATSAPP';
|
|
22
|
+
const LOG_LOCAL = false;
|
|
23
|
+
const LOGGER = new Logger(LIB_NAME, process.env.DEBUG === 'true' || LOG_LOCAL);
|
|
24
|
+
|
|
25
|
+
const headerPrefix = 'WA_SDK';
|
|
26
|
+
|
|
27
|
+
export default class WhatsApp implements WhatsAppClass {
|
|
28
|
+
config: WabaConfigType;
|
|
29
|
+
|
|
30
|
+
requester: Readonly<Requester>;
|
|
31
|
+
|
|
32
|
+
readonly messages: MessagesAPI;
|
|
33
|
+
readonly templates: TemplateAPI;
|
|
34
|
+
readonly phoneNumber: PhoneNumberAPI;
|
|
35
|
+
readonly qrCode: QrCodeAPI;
|
|
36
|
+
readonly encryption: EncryptionAPI;
|
|
37
|
+
readonly twoStepVerification: TwoStepVerificationAPI;
|
|
38
|
+
readonly registration: RegistrationAPI;
|
|
39
|
+
readonly media: MediaAPI;
|
|
40
|
+
readonly waba: WabaAPI;
|
|
41
|
+
readonly flow: FlowAPI;
|
|
42
|
+
static readonly Enums = SDKEnums;
|
|
43
|
+
|
|
44
|
+
constructor(config?: WhatsAppConfig) {
|
|
45
|
+
this.config = importConfig(config);
|
|
46
|
+
this.requester = new Requester(
|
|
47
|
+
this.config[SDKEnums.WabaConfigEnum.BaseURL],
|
|
48
|
+
this.config[SDKEnums.WabaConfigEnum.APIVersion],
|
|
49
|
+
this.config[SDKEnums.WabaConfigEnum.PhoneNumberId],
|
|
50
|
+
this.config[SDKEnums.WabaConfigEnum.AccessToken],
|
|
51
|
+
this.config[SDKEnums.WabaConfigEnum.BusinessAcctId],
|
|
52
|
+
this.userAgent(),
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
this.messages = new MessagesAPI(this.config, this.requester);
|
|
56
|
+
this.templates = new TemplateAPI(this.config, this.requester);
|
|
57
|
+
this.phoneNumber = new PhoneNumberAPI(this.config, this.requester);
|
|
58
|
+
this.qrCode = new QrCodeAPI(this.config, this.requester);
|
|
59
|
+
this.encryption = new EncryptionAPI(this.config, this.requester);
|
|
60
|
+
this.twoStepVerification = new TwoStepVerificationAPI(this.config, this.requester);
|
|
61
|
+
this.registration = new RegistrationAPI(this.config, this.requester);
|
|
62
|
+
this.media = new MediaAPI(this.config, this.requester);
|
|
63
|
+
this.waba = new WabaAPI(this.config, this.requester);
|
|
64
|
+
this.flow = new FlowAPI(this.config, this.requester);
|
|
65
|
+
LOGGER.log('WhatsApp Node.js SDK instantiated!');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private userAgent(): string {
|
|
69
|
+
const userAgentString = `${headerPrefix}/${'0.0.1'} (Node.js ${process.version})`;
|
|
70
|
+
return userAgentString;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
updateTimeout(ms: number): boolean {
|
|
74
|
+
this.config[SDKEnums.WabaConfigEnum.RequestTimeout] = ms;
|
|
75
|
+
LOGGER.log(`Updated request timeout to ${ms}ms`);
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
updatePhoneNumberId(phoneNumberId: number): boolean {
|
|
80
|
+
this.config[SDKEnums.WabaConfigEnum.PhoneNumberId] = phoneNumberId;
|
|
81
|
+
LOGGER.log(`Updated sender phone number id to ${phoneNumberId}`);
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
updateAccessToken(accessToken: string): boolean {
|
|
86
|
+
this.config[SDKEnums.WabaConfigEnum.AccessToken] = accessToken;
|
|
87
|
+
LOGGER.log(`Updated access token`);
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
updateWabaId(wabaId: string): boolean {
|
|
92
|
+
this.config[SDKEnums.WabaConfigEnum.BusinessAcctId] = wabaId;
|
|
93
|
+
LOGGER.log(`Updated business account id to ${wabaId}`);
|
|
94
|
+
return true;
|
|
95
|
+
}
|
|
96
|
+
}
|