@rempays/shared-core 1.0.2-beta.16 → 1.0.2-beta.18
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.
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import type { SendTextParams, SendTemplateParams, SendInteractiveParams, SendInteractiveListParams, SendImageParams, SendDocumentParams, SendLocationParams, SetTypingParams, MarkAsReadParams, SendAudioParams, SendVideoParams, MediaInfoResponse, CreateTemplateParams,
|
|
1
|
+
import type { SendTextParams, SendTemplateParams, SendInteractiveParams, SendInteractiveListParams, SendImageParams, SendDocumentParams, SendLocationParams, SetTypingParams, MarkAsReadParams, SendAudioParams, SendVideoParams, MediaInfoResponse, CreateTemplateParams, BusinessProfileResponse, UpdateDisplayNameParams, DisplayNameStatusResponse } from "./types";
|
|
2
2
|
export declare class FacebookApi {
|
|
3
3
|
private static token;
|
|
4
4
|
private static phoneNumberId;
|
|
5
5
|
private static apiVersion;
|
|
6
|
-
|
|
7
|
-
constructor(token?: string, phoneNumberId?: string, apiVersion?: string, appId?: string);
|
|
6
|
+
constructor(token?: string, phoneNumberId?: string, apiVersion?: string);
|
|
8
7
|
private static init;
|
|
9
8
|
private static post;
|
|
10
9
|
static sendText(params: SendTextParams): Promise<any>;
|
|
@@ -25,11 +24,10 @@ export declare class FacebookApi {
|
|
|
25
24
|
static createTemplate(params: CreateTemplateParams): Promise<any>;
|
|
26
25
|
static deleteTemplate(wabaId: string, name: string): Promise<any>;
|
|
27
26
|
/**
|
|
28
|
-
*
|
|
29
|
-
* @param
|
|
30
|
-
* @param mimeType Image MIME type (image/jpeg or image/png)
|
|
27
|
+
* Updates the business profile picture using a direct URL.
|
|
28
|
+
* @param url Public URL of the image
|
|
31
29
|
*/
|
|
32
|
-
static updateProfilePicture(
|
|
30
|
+
static updateProfilePicture(url: string): Promise<any>;
|
|
33
31
|
/**
|
|
34
32
|
* Uploads media for chat (messages).
|
|
35
33
|
* @param buffer Media binary data
|
|
@@ -41,7 +39,6 @@ export declare class FacebookApi {
|
|
|
41
39
|
static getBusinessProfile(): Promise<{
|
|
42
40
|
data: BusinessProfileResponse[];
|
|
43
41
|
}>;
|
|
44
|
-
static updateBusinessProfile(params: BusinessProfileParams): Promise<any>;
|
|
45
42
|
static updateDisplayName(params: UpdateDisplayNameParams): Promise<any>;
|
|
46
43
|
static getDisplayNameStatus(): Promise<DisplayNameStatusResponse>;
|
|
47
44
|
}
|
|
@@ -3,15 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.FacebookApi = void 0;
|
|
4
4
|
const http_1 = require("./http");
|
|
5
5
|
class FacebookApi {
|
|
6
|
-
constructor(token, phoneNumberId, apiVersion
|
|
6
|
+
constructor(token, phoneNumberId, apiVersion) {
|
|
7
7
|
if (token)
|
|
8
8
|
FacebookApi.token = token;
|
|
9
9
|
if (phoneNumberId)
|
|
10
10
|
FacebookApi.phoneNumberId = phoneNumberId;
|
|
11
11
|
if (apiVersion)
|
|
12
12
|
FacebookApi.apiVersion = apiVersion;
|
|
13
|
-
if (appId)
|
|
14
|
-
FacebookApi.appId = appId;
|
|
15
13
|
}
|
|
16
14
|
static async init() {
|
|
17
15
|
if (this.token && this.phoneNumberId && this.apiVersion)
|
|
@@ -21,7 +19,6 @@ class FacebookApi {
|
|
|
21
19
|
this.phoneNumberId || process.env.FACEBOOK_PHONE_NUMBER_ID || null;
|
|
22
20
|
this.apiVersion =
|
|
23
21
|
this.apiVersion || process.env.FACEBOOK_API_VERSION || "v18.0";
|
|
24
|
-
this.appId = this.appId || process.env.FACEBOOK_APP_ID || null;
|
|
25
22
|
if (!this.token || !this.phoneNumberId) {
|
|
26
23
|
throw new Error("Facebook API environment variables are not properly configured (FACEBOOK_API_TOKEN, FACEBOOK_PHONE_NUMBER_ID)");
|
|
27
24
|
}
|
|
@@ -39,6 +36,10 @@ class FacebookApi {
|
|
|
39
36
|
await this.init();
|
|
40
37
|
const url = `https://graph.facebook.com/${this.apiVersion}/${this.phoneNumberId}/${path}`;
|
|
41
38
|
try {
|
|
39
|
+
console.log("[layer-core/facebook-api] Sending message", {
|
|
40
|
+
url,
|
|
41
|
+
payload,
|
|
42
|
+
});
|
|
42
43
|
const http = (0, http_1.getHttpClient)(this.token);
|
|
43
44
|
const { data } = await http.post(url, payload);
|
|
44
45
|
return data;
|
|
@@ -51,8 +52,8 @@ class FacebookApi {
|
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
54
|
/*
|
|
54
|
-
|
|
55
|
-
|
|
55
|
+
TEXT
|
|
56
|
+
*/
|
|
56
57
|
static async sendText(params) {
|
|
57
58
|
const payload = {
|
|
58
59
|
messaging_product: "whatsapp",
|
|
@@ -67,8 +68,8 @@ class FacebookApi {
|
|
|
67
68
|
return await this.post("messages", payload);
|
|
68
69
|
}
|
|
69
70
|
/*
|
|
70
|
-
|
|
71
|
-
|
|
71
|
+
TEMPLATE
|
|
72
|
+
*/
|
|
72
73
|
static async sendTemplate(params) {
|
|
73
74
|
const payload = {
|
|
74
75
|
messaging_product: "whatsapp",
|
|
@@ -83,8 +84,8 @@ class FacebookApi {
|
|
|
83
84
|
return await this.post("messages", payload);
|
|
84
85
|
}
|
|
85
86
|
/*
|
|
86
|
-
|
|
87
|
-
|
|
87
|
+
BUTTONS
|
|
88
|
+
*/
|
|
88
89
|
static async sendInteractiveButtons(params) {
|
|
89
90
|
const payload = {
|
|
90
91
|
messaging_product: "whatsapp",
|
|
@@ -106,8 +107,8 @@ class FacebookApi {
|
|
|
106
107
|
return await this.post("messages", payload);
|
|
107
108
|
}
|
|
108
109
|
/*
|
|
109
|
-
|
|
110
|
-
|
|
110
|
+
LIST
|
|
111
|
+
*/
|
|
111
112
|
static async sendInteractiveList(params) {
|
|
112
113
|
const payload = {
|
|
113
114
|
messaging_product: "whatsapp",
|
|
@@ -138,8 +139,8 @@ class FacebookApi {
|
|
|
138
139
|
return await this.post("messages", payload);
|
|
139
140
|
}
|
|
140
141
|
/*
|
|
141
|
-
|
|
142
|
-
|
|
142
|
+
IMAGE
|
|
143
|
+
*/
|
|
143
144
|
static async sendImage(params) {
|
|
144
145
|
const payload = {
|
|
145
146
|
messaging_product: "whatsapp",
|
|
@@ -148,14 +149,14 @@ class FacebookApi {
|
|
|
148
149
|
image: {
|
|
149
150
|
link: params.link,
|
|
150
151
|
id: params.id,
|
|
151
|
-
caption: params.caption
|
|
152
|
+
caption: params.caption,
|
|
152
153
|
},
|
|
153
154
|
};
|
|
154
155
|
return await this.post("messages", payload);
|
|
155
156
|
}
|
|
156
157
|
/*
|
|
157
|
-
|
|
158
|
-
|
|
158
|
+
DOCUMENT
|
|
159
|
+
*/
|
|
159
160
|
static async sendDocument(params) {
|
|
160
161
|
const payload = {
|
|
161
162
|
messaging_product: "whatsapp",
|
|
@@ -171,8 +172,8 @@ class FacebookApi {
|
|
|
171
172
|
return await this.post("messages", payload);
|
|
172
173
|
}
|
|
173
174
|
/*
|
|
174
|
-
|
|
175
|
-
|
|
175
|
+
AUDIO (NEW)
|
|
176
|
+
*/
|
|
176
177
|
static async sendAudio(params) {
|
|
177
178
|
const payload = {
|
|
178
179
|
messaging_product: "whatsapp",
|
|
@@ -186,8 +187,8 @@ class FacebookApi {
|
|
|
186
187
|
return await this.post("messages", payload);
|
|
187
188
|
}
|
|
188
189
|
/*
|
|
189
|
-
|
|
190
|
-
|
|
190
|
+
VIDEO (NEW)
|
|
191
|
+
*/
|
|
191
192
|
static async sendVideo(params) {
|
|
192
193
|
const payload = {
|
|
193
194
|
messaging_product: "whatsapp",
|
|
@@ -202,8 +203,8 @@ class FacebookApi {
|
|
|
202
203
|
return await this.post("messages", payload);
|
|
203
204
|
}
|
|
204
205
|
/*
|
|
205
|
-
|
|
206
|
-
|
|
206
|
+
LOCATION
|
|
207
|
+
*/
|
|
207
208
|
static async sendLocation(params) {
|
|
208
209
|
const payload = {
|
|
209
210
|
messaging_product: "whatsapp",
|
|
@@ -219,8 +220,8 @@ class FacebookApi {
|
|
|
219
220
|
return await this.post("messages", payload);
|
|
220
221
|
}
|
|
221
222
|
/*
|
|
222
|
-
|
|
223
|
-
|
|
223
|
+
TYPING
|
|
224
|
+
*/
|
|
224
225
|
static async setTyping(params) {
|
|
225
226
|
const payload = {
|
|
226
227
|
messaging_product: "whatsapp",
|
|
@@ -231,8 +232,8 @@ class FacebookApi {
|
|
|
231
232
|
return await this.post("messages", payload);
|
|
232
233
|
}
|
|
233
234
|
/*
|
|
234
|
-
|
|
235
|
-
|
|
235
|
+
READ
|
|
236
|
+
*/
|
|
236
237
|
static async markAsRead(params) {
|
|
237
238
|
const payload = {
|
|
238
239
|
messaging_product: "whatsapp",
|
|
@@ -242,8 +243,8 @@ class FacebookApi {
|
|
|
242
243
|
return await this.post("messages", payload);
|
|
243
244
|
}
|
|
244
245
|
/*
|
|
245
|
-
|
|
246
|
-
|
|
246
|
+
MEDIA INFO (NEW)
|
|
247
|
+
*/
|
|
247
248
|
static async getMediaInfo(mediaId) {
|
|
248
249
|
await this.init();
|
|
249
250
|
const http = (0, http_1.getHttpClient)(this.token);
|
|
@@ -252,8 +253,8 @@ class FacebookApi {
|
|
|
252
253
|
return data;
|
|
253
254
|
}
|
|
254
255
|
/*
|
|
255
|
-
|
|
256
|
-
|
|
256
|
+
DOWNLOAD MEDIA (NEW)
|
|
257
|
+
*/
|
|
257
258
|
static async downloadMedia(mediaId) {
|
|
258
259
|
await this.init();
|
|
259
260
|
const media = await this.getMediaInfo(mediaId);
|
|
@@ -264,8 +265,8 @@ class FacebookApi {
|
|
|
264
265
|
return data;
|
|
265
266
|
}
|
|
266
267
|
/*
|
|
267
|
-
|
|
268
|
-
|
|
268
|
+
GOOGLE MAP URL (NEW)
|
|
269
|
+
*/
|
|
269
270
|
static buildGoogleMapsUrl(latitude, longitude) {
|
|
270
271
|
return `https://www.google.com/maps?q=${latitude},${longitude}`;
|
|
271
272
|
}
|
|
@@ -287,9 +288,9 @@ class FacebookApi {
|
|
|
287
288
|
components: [
|
|
288
289
|
{
|
|
289
290
|
type: "BODY",
|
|
290
|
-
text: params.bodyText
|
|
291
|
-
}
|
|
292
|
-
]
|
|
291
|
+
text: params.bodyText,
|
|
292
|
+
},
|
|
293
|
+
],
|
|
293
294
|
};
|
|
294
295
|
const { data } = await http.post(url, payload);
|
|
295
296
|
return data;
|
|
@@ -299,51 +300,30 @@ class FacebookApi {
|
|
|
299
300
|
const http = (0, http_1.getHttpClient)(this.token);
|
|
300
301
|
const url = `https://graph.facebook.com/${this.apiVersion}/${wabaId}/message_templates`;
|
|
301
302
|
const { data } = await http.delete(url, {
|
|
302
|
-
params: { name }
|
|
303
|
+
params: { name },
|
|
303
304
|
});
|
|
304
305
|
return data;
|
|
305
306
|
}
|
|
306
307
|
/*
|
|
307
|
-
|
|
308
|
-
|
|
308
|
+
PROFILE PICTURE UPLOAD (TWO-STEP)
|
|
309
|
+
*/
|
|
309
310
|
/**
|
|
310
|
-
*
|
|
311
|
-
* @param
|
|
312
|
-
* @param mimeType Image MIME type (image/jpeg or image/png)
|
|
311
|
+
* Updates the business profile picture using a direct URL.
|
|
312
|
+
* @param url Public URL of the image
|
|
313
313
|
*/
|
|
314
|
-
static async updateProfilePicture(
|
|
314
|
+
static async updateProfilePicture(url) {
|
|
315
315
|
await this.init();
|
|
316
|
-
if (!this.appId) {
|
|
317
|
-
throw new Error("FACEBOOK_APP_ID is required for profile picture uploads");
|
|
318
|
-
}
|
|
319
316
|
const http = (0, http_1.getHttpClient)(this.token);
|
|
320
|
-
|
|
321
|
-
const
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
access_token: this.token
|
|
327
|
-
}
|
|
328
|
-
});
|
|
329
|
-
const sessionId = sessionData.id;
|
|
330
|
-
// 2. Upload the file data
|
|
331
|
-
const uploadUrl = `https://graph.facebook.com/${this.apiVersion}/${sessionId}`;
|
|
332
|
-
const { data: uploadData } = await http.post(uploadUrl, buffer, {
|
|
333
|
-
headers: {
|
|
334
|
-
'file_offset': 0,
|
|
335
|
-
'Content-Type': mimeType
|
|
336
|
-
}
|
|
337
|
-
});
|
|
338
|
-
const handle = uploadData.h;
|
|
339
|
-
// 3. Update the business profile with the handle
|
|
340
|
-
return await this.updateBusinessProfile({
|
|
341
|
-
profile_picture_handle: handle
|
|
342
|
-
});
|
|
317
|
+
const urlEnv = `https://graph.facebook.com/${this.apiVersion}/${this.phoneNumberId}/profile_picture`;
|
|
318
|
+
const payload = {
|
|
319
|
+
profile_picture_url: url,
|
|
320
|
+
};
|
|
321
|
+
const { data } = await http.post(urlEnv, payload);
|
|
322
|
+
return data;
|
|
343
323
|
}
|
|
344
324
|
/*
|
|
345
|
-
|
|
346
|
-
|
|
325
|
+
CHAT MEDIA UPLOAD
|
|
326
|
+
*/
|
|
347
327
|
/**
|
|
348
328
|
* Uploads media for chat (messages).
|
|
349
329
|
* @param buffer Media binary data
|
|
@@ -355,19 +335,19 @@ class FacebookApi {
|
|
|
355
335
|
const url = `https://graph.facebook.com/${this.apiVersion}/${this.phoneNumberId}/media`;
|
|
356
336
|
const formData = new FormData();
|
|
357
337
|
const blob = new Blob([buffer], { type: mimeType });
|
|
358
|
-
formData.append(
|
|
359
|
-
formData.append(
|
|
360
|
-
formData.append(
|
|
338
|
+
formData.append("file", blob, "media");
|
|
339
|
+
formData.append("type", mimeType);
|
|
340
|
+
formData.append("messaging_product", "whatsapp");
|
|
361
341
|
const { data } = await http.post(url, formData, {
|
|
362
342
|
headers: {
|
|
363
|
-
|
|
364
|
-
}
|
|
343
|
+
"Content-Type": "multipart/form-data",
|
|
344
|
+
},
|
|
365
345
|
});
|
|
366
346
|
return data; // Returns { id: "media_id" }
|
|
367
347
|
}
|
|
368
348
|
/*
|
|
369
|
-
|
|
370
|
-
|
|
349
|
+
BUSINESS PROFILE
|
|
350
|
+
*/
|
|
371
351
|
static async getBusinessProfile() {
|
|
372
352
|
await this.init();
|
|
373
353
|
const http = (0, http_1.getHttpClient)(this.token);
|
|
@@ -378,16 +358,9 @@ class FacebookApi {
|
|
|
378
358
|
// Returning the full structure { data: [ ... ] } as requested
|
|
379
359
|
return data;
|
|
380
360
|
}
|
|
381
|
-
static async updateBusinessProfile(params) {
|
|
382
|
-
const payload = {
|
|
383
|
-
messaging_product: "whatsapp",
|
|
384
|
-
...params
|
|
385
|
-
};
|
|
386
|
-
return await this.post("whatsapp_business_profile", payload);
|
|
387
|
-
}
|
|
388
361
|
/*
|
|
389
|
-
|
|
390
|
-
|
|
362
|
+
DISPLAY NAME
|
|
363
|
+
*/
|
|
391
364
|
static async updateDisplayName(params) {
|
|
392
365
|
await this.init();
|
|
393
366
|
const http = (0, http_1.getHttpClient)(this.token);
|
|
@@ -395,8 +368,8 @@ class FacebookApi {
|
|
|
395
368
|
const url = `https://graph.facebook.com/${this.apiVersion}/${this.phoneNumberId}`;
|
|
396
369
|
const { data } = await http.post(url, null, {
|
|
397
370
|
params: {
|
|
398
|
-
new_display_name: params.new_display_name
|
|
399
|
-
}
|
|
371
|
+
new_display_name: params.new_display_name,
|
|
372
|
+
},
|
|
400
373
|
});
|
|
401
374
|
return data;
|
|
402
375
|
}
|
|
@@ -407,8 +380,8 @@ class FacebookApi {
|
|
|
407
380
|
const url = `https://graph.facebook.com/${this.apiVersion}/${this.phoneNumberId}`;
|
|
408
381
|
const { data } = await http.get(url, {
|
|
409
382
|
params: {
|
|
410
|
-
fields: "verified_name,name_status,new_display_name,new_name_status"
|
|
411
|
-
}
|
|
383
|
+
fields: "verified_name,name_status,new_display_name,new_name_status",
|
|
384
|
+
},
|
|
412
385
|
});
|
|
413
386
|
return data;
|
|
414
387
|
}
|
|
@@ -417,4 +390,3 @@ exports.FacebookApi = FacebookApi;
|
|
|
417
390
|
FacebookApi.token = null;
|
|
418
391
|
FacebookApi.phoneNumberId = null;
|
|
419
392
|
FacebookApi.apiVersion = null;
|
|
420
|
-
FacebookApi.appId = null;
|
|
@@ -147,6 +147,7 @@ export interface BusinessProfileParams {
|
|
|
147
147
|
websites?: string[];
|
|
148
148
|
vertical?: WhatsAppBusinessVertical;
|
|
149
149
|
profile_picture_handle?: string;
|
|
150
|
+
profile_picture_url?: string;
|
|
150
151
|
}
|
|
151
152
|
export type WhatsAppBusinessVertical = "" | "UNDEFINED" | "OTHER" | "AUTO" | "BEAUTY" | "APPAREL" | "EDU" | "ENTERTAIN" | "EVENT_PLAN" | "FINANCE" | "GROCERY" | "GOVT" | "HOTEL" | "HEALTH" | "NONPROFIT" | "PROF_SERVICES" | "RETAIL" | "TRAVEL" | "RESTAURANT" | "NOT_A_BIZ";
|
|
152
153
|
export interface BusinessProfileResponse {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rempays/shared-core",
|
|
3
|
-
"version": "1.0.2-beta.
|
|
3
|
+
"version": "1.0.2-beta.18",
|
|
4
4
|
"description": "Core utilities layer for RemPays platform with AWS services integration (Cognito, S3, Secrets Manager, Textract, Facebook API, DynamoDB)",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|