@green-api/greenapi-integration 0.9.0 → 0.10.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/dist/core/base-adapter.js +2 -0
- package/dist/core/green-api.client.d.ts +64 -7
- package/dist/core/green-api.client.js +122 -45
- package/dist/core/guard.js +4 -4
- package/dist/core/logger.js +0 -2
- package/dist/defaults.d.ts +2 -0
- package/dist/defaults.js +3 -1
- package/dist/types/types.d.ts +17 -2
- package/package.json +10 -13
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Instance, Settings, SendMessage, SendFileByUrl, SendFileByUpload, SendPoll, StateInstance, Reboot, Logout, QR, SendResponse, SendFileByUploadResponse, SetSettingsResponse, GetAuthorizationCode, SetProfilePicture, WaSettings, AccountSettings, UploadFile, SendLocation, SendContact, ForwardMessages, ForwardMessagesResponse, QueueMessage, ClearMessagesQueue, ReadChatResponse, ReadChat, CheckWhatsapp, CheckWhatsappResponse, CheckAccount, CheckAccountResponse, GetAvatarResponse, GetAvatar, Contact, ContactInfo, ArchiveChat, UnarchiveChat, SetDisappearingChat, SetDisappearingChatResponse, CreateGroupResponse, CreateGroup, UpdateGroupName, UpdateGroupNameResponse, GetGroupData, GroupData, AddGroupParticipant, AddGroupParticipantResponse, RemoveGroupParticipant, RemoveGroupParticipantResponse, SetGroupAdmin, SetGroupAdminResponse, RemoveAdminResponse, RemoveAdmin, SetGroupPicture, SetGroupPictureResponse, LeaveGroup, LeaveGroupResponse, GetMessage, JournalResponse, GetChatHistory, IncomingJournalResponse, OutgoingJournalResponse, SendInteractiveButtons, SendInteractiveButtonsReply, SendTextStatus, SendVoiceStatus, SendMediaStatus, GetStatusStatisticResponse } from "../types/types";
|
|
1
|
+
import { Instance, Settings, SendMessage, SendFileByUrl, SendFileByUpload, SendPoll, StateInstance, Reboot, Logout, QR, SendResponse, SendFileByUploadResponse, SetSettingsResponse, GetAuthorizationCode, SetProfilePicture, WaSettings, AccountSettings, UploadFile, SendLocation, SendContact, ForwardMessages, ForwardMessagesResponse, QueueMessage, ClearMessagesQueue, ReadChatResponse, ReadChat, CheckWhatsapp, CheckWhatsappResponse, CheckAccount, CheckAccountResponse, GetAvatarResponse, GetAvatar, Contact, ContactInfo, ArchiveChat, UnarchiveChat, SetDisappearingChat, SetDisappearingChatResponse, CreateGroupResponse, CreateGroup, UpdateGroupName, UpdateGroupNameResponse, GetGroupData, GroupData, AddGroupParticipant, AddGroupParticipantResponse, RemoveGroupParticipant, RemoveGroupParticipantResponse, SetGroupAdmin, SetGroupAdminResponse, RemoveAdminResponse, RemoveAdmin, SetGroupPicture, SetGroupPictureResponse, LeaveGroup, LeaveGroupResponse, GetMessage, JournalResponse, GetChatHistory, IncomingJournalResponse, OutgoingJournalResponse, SendInteractiveButtons, SendInteractiveButtonsReply, SendTextStatus, SendVoiceStatus, SendMediaStatus, GetStatusStatisticResponse, GreenApiClientOptions, ReceiveNotificationResponse, DeleteNotificationResponse } from "../types/types";
|
|
2
2
|
/**
|
|
3
3
|
* Client for direct interaction with GREEN-API's WhatsApp gateway.
|
|
4
4
|
* Provides methods for sending messages, managing instances, and handling files.
|
|
@@ -22,16 +22,22 @@ import { Instance, Settings, SendMessage, SendFileByUrl, SendFileByUpload, SendP
|
|
|
22
22
|
export declare class GreenApiClient {
|
|
23
23
|
private instance;
|
|
24
24
|
private client;
|
|
25
|
+
private mediaClient;
|
|
25
26
|
private readonly baseUrl;
|
|
27
|
+
private readonly mediaBaseUrl;
|
|
26
28
|
private readonly gaLogger;
|
|
27
29
|
/**
|
|
28
30
|
* Creates a GREEN-API client instance.
|
|
29
31
|
*
|
|
30
|
-
* @param instance - Configuration containing idInstance and
|
|
32
|
+
* @param instance - Configuration containing idInstance, apiTokenInstance and optionally apiUrl and mediaUrl
|
|
33
|
+
* @param options - Client options, e.g. `logger: null` to disable request logging
|
|
31
34
|
*/
|
|
32
|
-
constructor(instance: Instance);
|
|
35
|
+
constructor(instance: Instance, options?: GreenApiClientOptions);
|
|
33
36
|
private buildUrl;
|
|
37
|
+
private toBlob;
|
|
38
|
+
private toBuffer;
|
|
34
39
|
private buildEndpoint;
|
|
40
|
+
private redactToken;
|
|
35
41
|
private makeRequest;
|
|
36
42
|
private makeFileUploadRequest;
|
|
37
43
|
/**
|
|
@@ -266,18 +272,32 @@ export declare class GreenApiClient {
|
|
|
266
272
|
/**
|
|
267
273
|
* Sets the profile picture for the WhatsApp account.
|
|
268
274
|
*
|
|
269
|
-
* @param file - Image file to use as profile picture
|
|
275
|
+
* @param file - Image file (jpg) to use as profile picture
|
|
276
|
+
* @param fileName - File name with extension
|
|
270
277
|
* @returns Promise resolving to profile picture update response
|
|
271
278
|
*/
|
|
272
|
-
setProfilePicture(file: Blob | File): Promise<SetProfilePicture>;
|
|
279
|
+
setProfilePicture(file: Blob | File | Uint8Array, fileName?: string): Promise<SetProfilePicture>;
|
|
273
280
|
/**
|
|
274
281
|
* Uploads a file to GREEN-API servers.
|
|
275
282
|
*
|
|
276
283
|
* @param file - File to upload
|
|
277
|
-
* @param
|
|
284
|
+
* @param fileName - File name with extension
|
|
278
285
|
* @returns Promise resolving to upload response with file URL
|
|
279
286
|
*/
|
|
280
|
-
uploadFile(file: Blob | File,
|
|
287
|
+
uploadFile(file: Blob | File | Uint8Array, fileName?: string): Promise<UploadFile>;
|
|
288
|
+
/**
|
|
289
|
+
* Calls any GREEN-API method that takes a file.
|
|
290
|
+
*
|
|
291
|
+
* @param endpoint - GREEN-API method name, e.g. "setGroupPicture"
|
|
292
|
+
* @param file - File content and file name
|
|
293
|
+
* @param fields - Other form fields
|
|
294
|
+
* @returns Promise resolving to the raw response body
|
|
295
|
+
*/
|
|
296
|
+
requestWithFile<T = unknown>(endpoint: string, file: {
|
|
297
|
+
data: Blob | File | Uint8Array;
|
|
298
|
+
fileName: string;
|
|
299
|
+
}, fields?: Record<string, string | number | boolean>): Promise<T>;
|
|
300
|
+
private pictureFileName;
|
|
281
301
|
/**
|
|
282
302
|
* Gets authorization code for a phone number.
|
|
283
303
|
*
|
|
@@ -315,6 +335,43 @@ export declare class GreenApiClient {
|
|
|
315
335
|
* ```
|
|
316
336
|
*/
|
|
317
337
|
clearMessagesQueue(): Promise<ClearMessagesQueue>;
|
|
338
|
+
/**
|
|
339
|
+
* Calls any GREEN-API method by name.
|
|
340
|
+
*
|
|
341
|
+
* @param method - HTTP method
|
|
342
|
+
* @param endpoint - GREEN-API method name, e.g. "getContacts"
|
|
343
|
+
* @param data - Request body for POST
|
|
344
|
+
* @param queryParams - Query parameters
|
|
345
|
+
* @returns Promise resolving to the raw response body
|
|
346
|
+
*
|
|
347
|
+
* @example
|
|
348
|
+
* ```typescript
|
|
349
|
+
* const contacts = await client.request("get", "getContacts");
|
|
350
|
+
* ```
|
|
351
|
+
*/
|
|
352
|
+
request<T = unknown>(method: "get" | "post" | "delete", endpoint: string, data?: unknown, queryParams?: Record<string, string | number>): Promise<T>;
|
|
353
|
+
/**
|
|
354
|
+
* Receives the next notification from the instance queue.
|
|
355
|
+
*
|
|
356
|
+
* @param receiveTimeout - Seconds to wait for a notification, 5 to 60
|
|
357
|
+
* @returns Promise resolving to the notification, or null when the queue stayed empty
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* ```typescript
|
|
361
|
+
* const notification = await client.receiveNotification(20);
|
|
362
|
+
* if (notification) {
|
|
363
|
+
* await client.deleteNotification(notification.receiptId);
|
|
364
|
+
* }
|
|
365
|
+
* ```
|
|
366
|
+
*/
|
|
367
|
+
receiveNotification(receiveTimeout?: number): Promise<ReceiveNotificationResponse | null>;
|
|
368
|
+
/**
|
|
369
|
+
* Deletes a processed notification from the instance queue.
|
|
370
|
+
*
|
|
371
|
+
* @param receiptId - Receipt ID from receiveNotification
|
|
372
|
+
* @returns Promise resolving to deletion result
|
|
373
|
+
*/
|
|
374
|
+
deleteNotification(receiptId: number): Promise<DeleteNotificationResponse>;
|
|
318
375
|
/**
|
|
319
376
|
* Marks messages in a chat as read.
|
|
320
377
|
* For this to work, "Receive webhooks on incoming messages and files" setting must be enabled.
|
|
@@ -32,47 +32,71 @@ class GreenApiClient {
|
|
|
32
32
|
/**
|
|
33
33
|
* Creates a GREEN-API client instance.
|
|
34
34
|
*
|
|
35
|
-
* @param instance - Configuration containing idInstance and
|
|
35
|
+
* @param instance - Configuration containing idInstance, apiTokenInstance and optionally apiUrl and mediaUrl
|
|
36
|
+
* @param options - Client options, e.g. `logger: null` to disable request logging
|
|
36
37
|
*/
|
|
37
|
-
constructor(instance) {
|
|
38
|
+
constructor(instance, options = {}) {
|
|
38
39
|
this.instance = instance;
|
|
39
|
-
this.gaLogger = logger_1.GreenApiLogger.getInstance(GreenApiClient.name);
|
|
40
40
|
this.baseUrl = (instance.apiUrl || defaults_1.DEFAULT_API_URL).replace(/\/+$/, "");
|
|
41
|
+
this.mediaBaseUrl = (instance.mediaUrl || this.baseUrl).replace(/\/+$/, "");
|
|
41
42
|
this.client = axios_1.default.create({
|
|
42
|
-
baseURL: this.buildUrl(),
|
|
43
|
+
baseURL: this.buildUrl(this.baseUrl),
|
|
43
44
|
});
|
|
45
|
+
this.mediaClient = axios_1.default.create({
|
|
46
|
+
baseURL: this.buildUrl(this.mediaBaseUrl),
|
|
47
|
+
});
|
|
48
|
+
this.gaLogger = options.logger === undefined ? logger_1.GreenApiLogger.getInstance(GreenApiClient.name) : options.logger;
|
|
49
|
+
}
|
|
50
|
+
buildUrl(host) {
|
|
51
|
+
return `${host}/waInstance${this.instance.idInstance}`;
|
|
52
|
+
}
|
|
53
|
+
toBlob(data) {
|
|
54
|
+
return data instanceof Blob ? data : new Blob([data]);
|
|
44
55
|
}
|
|
45
|
-
|
|
46
|
-
return
|
|
56
|
+
toBuffer(data) {
|
|
57
|
+
return Buffer.isBuffer(data) ? data : Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
47
58
|
}
|
|
48
59
|
buildEndpoint(endpoint) {
|
|
49
60
|
return `/${endpoint}/${this.instance.apiTokenInstance}`;
|
|
50
61
|
}
|
|
51
|
-
|
|
62
|
+
redactToken(text) {
|
|
63
|
+
const token = this.instance.apiTokenInstance;
|
|
64
|
+
return token ? text.split(token).join(defaults_1.REDACTED_TOKEN) : text;
|
|
65
|
+
}
|
|
66
|
+
async makeRequest(method, endpoint, data, queryParams, config, pathAfterToken, host = "api") {
|
|
52
67
|
try {
|
|
53
|
-
this.gaLogger
|
|
68
|
+
this.gaLogger?.info("Making a request", {
|
|
54
69
|
idInstance: this.instance.idInstance,
|
|
55
70
|
endpoint,
|
|
56
71
|
method,
|
|
57
|
-
data,
|
|
72
|
+
data: Buffer.isBuffer(data) ? `${data.length} bytes` : data,
|
|
58
73
|
queryParams,
|
|
59
74
|
config,
|
|
60
75
|
});
|
|
61
|
-
const url = this.buildEndpoint(endpoint) + (queryParams ? "?" + new URLSearchParams(Object.entries(queryParams).map(([key, value]) => [key, value.toString()])).toString() : "");
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
76
|
+
const url = this.buildEndpoint(endpoint) + (pathAfterToken ? `/${pathAfterToken}` : "") + (queryParams ? "?" + new URLSearchParams(Object.entries(queryParams).map(([key, value]) => [key, value.toString()])).toString() : "");
|
|
77
|
+
const client = host === "media" ? this.mediaClient : this.client;
|
|
78
|
+
let response;
|
|
79
|
+
if (method === "get") {
|
|
80
|
+
response = await client.get(url, config);
|
|
81
|
+
}
|
|
82
|
+
else if (method === "delete") {
|
|
83
|
+
response = await client.delete(url, config);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
response = await client.post(url, data, config);
|
|
87
|
+
}
|
|
65
88
|
return response.data;
|
|
66
89
|
}
|
|
67
90
|
catch (error) {
|
|
68
|
-
|
|
91
|
+
const responseData = error.response?.data === undefined
|
|
92
|
+
? undefined
|
|
93
|
+
: JSON.parse(this.redactToken(JSON.stringify(error.response.data)));
|
|
94
|
+
const reason = this.redactToken(String(error.message));
|
|
95
|
+
throw new errors_1.IntegrationError(`Failed to ${endpoint.replace(/([A-Z])/g, " $1").toLowerCase()}: ${reason}${responseData === undefined ? "" : `. ${JSON.stringify(responseData)}`}`, "INTEGRATION_ERROR", error.response?.status ?? 500, responseData);
|
|
69
96
|
}
|
|
70
97
|
}
|
|
71
|
-
async makeFileUploadRequest(endpoint, formData,
|
|
72
|
-
return this.makeRequest("post", endpoint, formData, undefined, {
|
|
73
|
-
headers: { "Content-Type": "multipart/form-data" },
|
|
74
|
-
...headers,
|
|
75
|
-
});
|
|
98
|
+
async makeFileUploadRequest(endpoint, formData, host = "api") {
|
|
99
|
+
return this.makeRequest("post", endpoint, formData, undefined, { headers: { "Content-Type": "multipart/form-data" } }, undefined, host);
|
|
76
100
|
}
|
|
77
101
|
/**
|
|
78
102
|
* Sends a text message to a WhatsApp chat.
|
|
@@ -143,14 +167,14 @@ class GreenApiClient {
|
|
|
143
167
|
*/
|
|
144
168
|
async sendFileByUpload(message) {
|
|
145
169
|
const formData = new FormData();
|
|
146
|
-
formData.append("file", message.file.data, message.file.fileName);
|
|
170
|
+
formData.append("file", this.toBlob(message.file.data), message.file.fileName);
|
|
147
171
|
formData.append("chatId", message.chatId);
|
|
148
172
|
formData.append("fileName", message.file.fileName);
|
|
149
173
|
if (message.caption)
|
|
150
174
|
formData.append("caption", message.caption);
|
|
151
175
|
if (message.quotedMessageId)
|
|
152
176
|
formData.append("quotedMessageId", message.quotedMessageId);
|
|
153
|
-
return this.makeFileUploadRequest("sendFileByUpload", formData);
|
|
177
|
+
return this.makeFileUploadRequest("sendFileByUpload", formData, "media");
|
|
154
178
|
}
|
|
155
179
|
/**
|
|
156
180
|
* Creates a poll in a WhatsApp chat.
|
|
@@ -393,38 +417,50 @@ class GreenApiClient {
|
|
|
393
417
|
/**
|
|
394
418
|
* Sets the profile picture for the WhatsApp account.
|
|
395
419
|
*
|
|
396
|
-
* @param file - Image file to use as profile picture
|
|
420
|
+
* @param file - Image file (jpg) to use as profile picture
|
|
421
|
+
* @param fileName - File name with extension
|
|
397
422
|
* @returns Promise resolving to profile picture update response
|
|
398
423
|
*/
|
|
399
|
-
async setProfilePicture(file) {
|
|
400
|
-
|
|
401
|
-
formData.append("file", file);
|
|
402
|
-
return this.makeFileUploadRequest("setProfilePicture", formData);
|
|
424
|
+
async setProfilePicture(file, fileName) {
|
|
425
|
+
return this.requestWithFile("setProfilePicture", { data: file, fileName: this.pictureFileName(file, fileName) });
|
|
403
426
|
}
|
|
404
427
|
/**
|
|
405
428
|
* Uploads a file to GREEN-API servers.
|
|
406
429
|
*
|
|
407
430
|
* @param file - File to upload
|
|
408
|
-
* @param
|
|
431
|
+
* @param fileName - File name with extension
|
|
409
432
|
* @returns Promise resolving to upload response with file URL
|
|
410
433
|
*/
|
|
411
|
-
async uploadFile(file,
|
|
412
|
-
const
|
|
413
|
-
formData.append("file", file);
|
|
434
|
+
async uploadFile(file, fileName) {
|
|
435
|
+
const mimeType = file instanceof Blob ? file.type : "";
|
|
414
436
|
const headers = {};
|
|
415
|
-
if (
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
else if (customFileName) {
|
|
421
|
-
headers["GA-Filename"] = customFileName;
|
|
422
|
-
}
|
|
437
|
+
if (mimeType) {
|
|
438
|
+
headers["Content-Type"] = mimeType;
|
|
439
|
+
}
|
|
440
|
+
else if (fileName) {
|
|
441
|
+
headers["GA-Filename"] = fileName;
|
|
423
442
|
}
|
|
424
|
-
|
|
425
|
-
|
|
443
|
+
const body = file instanceof Blob ? Buffer.from(await file.arrayBuffer()) : this.toBuffer(file);
|
|
444
|
+
return this.makeRequest("post", "uploadFile", body, undefined, { headers }, undefined, "media");
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Calls any GREEN-API method that takes a file.
|
|
448
|
+
*
|
|
449
|
+
* @param endpoint - GREEN-API method name, e.g. "setGroupPicture"
|
|
450
|
+
* @param file - File content and file name
|
|
451
|
+
* @param fields - Other form fields
|
|
452
|
+
* @returns Promise resolving to the raw response body
|
|
453
|
+
*/
|
|
454
|
+
async requestWithFile(endpoint, file, fields = {}) {
|
|
455
|
+
const formData = new FormData();
|
|
456
|
+
formData.append("file", this.toBlob(file.data), file.fileName);
|
|
457
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
458
|
+
formData.append(key, String(value));
|
|
426
459
|
}
|
|
427
|
-
return this.makeFileUploadRequest(
|
|
460
|
+
return this.makeFileUploadRequest(endpoint, formData);
|
|
461
|
+
}
|
|
462
|
+
pictureFileName(file, fileName) {
|
|
463
|
+
return fileName || (file instanceof File && file.name ? file.name : defaults_1.DEFAULT_PICTURE_FILE_NAME);
|
|
428
464
|
}
|
|
429
465
|
/**
|
|
430
466
|
* Gets authorization code for a phone number.
|
|
@@ -472,6 +508,50 @@ class GreenApiClient {
|
|
|
472
508
|
async clearMessagesQueue() {
|
|
473
509
|
return this.makeRequest("get", "clearMessagesQueue");
|
|
474
510
|
}
|
|
511
|
+
/**
|
|
512
|
+
* Calls any GREEN-API method by name.
|
|
513
|
+
*
|
|
514
|
+
* @param method - HTTP method
|
|
515
|
+
* @param endpoint - GREEN-API method name, e.g. "getContacts"
|
|
516
|
+
* @param data - Request body for POST
|
|
517
|
+
* @param queryParams - Query parameters
|
|
518
|
+
* @returns Promise resolving to the raw response body
|
|
519
|
+
*
|
|
520
|
+
* @example
|
|
521
|
+
* ```typescript
|
|
522
|
+
* const contacts = await client.request("get", "getContacts");
|
|
523
|
+
* ```
|
|
524
|
+
*/
|
|
525
|
+
async request(method, endpoint, data, queryParams) {
|
|
526
|
+
return this.makeRequest(method, endpoint, data, queryParams);
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Receives the next notification from the instance queue.
|
|
530
|
+
*
|
|
531
|
+
* @param receiveTimeout - Seconds to wait for a notification, 5 to 60
|
|
532
|
+
* @returns Promise resolving to the notification, or null when the queue stayed empty
|
|
533
|
+
*
|
|
534
|
+
* @example
|
|
535
|
+
* ```typescript
|
|
536
|
+
* const notification = await client.receiveNotification(20);
|
|
537
|
+
* if (notification) {
|
|
538
|
+
* await client.deleteNotification(notification.receiptId);
|
|
539
|
+
* }
|
|
540
|
+
* ```
|
|
541
|
+
*/
|
|
542
|
+
async receiveNotification(receiveTimeout) {
|
|
543
|
+
const response = await this.makeRequest("get", "receiveNotification", undefined, receiveTimeout === undefined ? undefined : { receiveTimeout });
|
|
544
|
+
return response || null;
|
|
545
|
+
}
|
|
546
|
+
/**
|
|
547
|
+
* Deletes a processed notification from the instance queue.
|
|
548
|
+
*
|
|
549
|
+
* @param receiptId - Receipt ID from receiveNotification
|
|
550
|
+
* @returns Promise resolving to deletion result
|
|
551
|
+
*/
|
|
552
|
+
async deleteNotification(receiptId) {
|
|
553
|
+
return this.makeRequest("delete", "deleteNotification", undefined, undefined, undefined, receiptId.toString());
|
|
554
|
+
}
|
|
475
555
|
/**
|
|
476
556
|
* Marks messages in a chat as read.
|
|
477
557
|
* For this to work, "Receive webhooks on incoming messages and files" setting must be enabled.
|
|
@@ -673,10 +753,7 @@ class GreenApiClient {
|
|
|
673
753
|
* @returns Promise resolving to picture update status
|
|
674
754
|
*/
|
|
675
755
|
async setGroupPicture(params) {
|
|
676
|
-
|
|
677
|
-
formData.append("file", params.file);
|
|
678
|
-
formData.append("groupId", params.groupId);
|
|
679
|
-
return this.makeFileUploadRequest("setGroupPicture", formData);
|
|
756
|
+
return this.requestWithFile("setGroupPicture", { data: params.file, fileName: this.pictureFileName(params.file, params.fileName) }, { groupId: params.groupId });
|
|
680
757
|
}
|
|
681
758
|
/**
|
|
682
759
|
* Makes the current account leave a group chat.
|
package/dist/core/guard.js
CHANGED
|
@@ -56,22 +56,22 @@ class BaseGreenApiAuthGuard {
|
|
|
56
56
|
async validateRequest(request) {
|
|
57
57
|
const token = request.headers["authorization"];
|
|
58
58
|
if (!token) {
|
|
59
|
-
this.gaLogger.warn("Authentication header is missing", { body: request.body
|
|
59
|
+
this.gaLogger.warn("Authentication header is missing", { body: request.body });
|
|
60
60
|
throw new errors_1.AuthenticationError("Authentication header is missing");
|
|
61
61
|
}
|
|
62
62
|
this.gaLogger.info("Request from GREEN-API", { body: request.body });
|
|
63
63
|
const idInstance = request.body?.instanceData?.idInstance;
|
|
64
64
|
if (!idInstance) {
|
|
65
|
-
this.gaLogger.warn("Invalid webhook format", { body: request.body
|
|
65
|
+
this.gaLogger.warn("Invalid webhook format", { body: request.body });
|
|
66
66
|
throw new errors_1.AuthenticationError("Invalid webhook format");
|
|
67
67
|
}
|
|
68
68
|
const instance = await this.storage.getInstance(idInstance);
|
|
69
69
|
if (!instance) {
|
|
70
|
-
this.gaLogger.warn(`No instance with such ID ${idInstance}`, { body: request.body
|
|
70
|
+
this.gaLogger.warn(`No instance with such ID ${idInstance}`, { body: request.body });
|
|
71
71
|
throw new errors_1.AuthenticationError(`No instance with such ID ${idInstance}`);
|
|
72
72
|
}
|
|
73
73
|
if (instance.settings?.webhookUrlToken !== token.split(" ")[1]) {
|
|
74
|
-
this.gaLogger.warn("Invalid token", { body: request.body
|
|
74
|
+
this.gaLogger.warn("Invalid token", { body: request.body });
|
|
75
75
|
throw new errors_1.AuthenticationError("Invalid token");
|
|
76
76
|
}
|
|
77
77
|
return true;
|
package/dist/core/logger.js
CHANGED
|
@@ -195,9 +195,7 @@ class GreenApiLogger {
|
|
|
195
195
|
status: axiosError.response?.status,
|
|
196
196
|
statusText: axiosError.response?.statusText,
|
|
197
197
|
data: axiosError.response?.data,
|
|
198
|
-
url: axiosError.config?.url,
|
|
199
198
|
method: axiosError.config?.method,
|
|
200
|
-
headers: axiosError.response?.headers,
|
|
201
199
|
timestamp: new Date().toISOString(),
|
|
202
200
|
...additionalContext,
|
|
203
201
|
});
|
package/dist/defaults.d.ts
CHANGED
package/dist/defaults.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DEFAULT_API_URL = void 0;
|
|
3
|
+
exports.DEFAULT_PICTURE_FILE_NAME = exports.REDACTED_TOKEN = exports.DEFAULT_API_URL = void 0;
|
|
4
4
|
exports.DEFAULT_API_URL = "https://api.green-api.com";
|
|
5
|
+
exports.REDACTED_TOKEN = "***";
|
|
6
|
+
exports.DEFAULT_PICTURE_FILE_NAME = "picture.jpg";
|
package/dist/types/types.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export interface BaseInstance {
|
|
|
8
8
|
stateInstance?: InstanceState | null;
|
|
9
9
|
settings?: Settings | Record<string, any> | null;
|
|
10
10
|
apiUrl?: string | null;
|
|
11
|
+
mediaUrl?: string | null;
|
|
11
12
|
}
|
|
12
13
|
/**
|
|
13
14
|
* Extended instance interface that allows for additional platform-specific properties.
|
|
@@ -36,7 +37,7 @@ export interface SendMessage extends BaseMessage {
|
|
|
36
37
|
export interface SendFileByUpload extends BaseMessage {
|
|
37
38
|
caption?: string;
|
|
38
39
|
file: {
|
|
39
|
-
data: Blob | File;
|
|
40
|
+
data: Blob | File | Uint8Array;
|
|
40
41
|
fileName: string;
|
|
41
42
|
};
|
|
42
43
|
}
|
|
@@ -124,6 +125,19 @@ export interface QueueMessage {
|
|
|
124
125
|
export interface ClearMessagesQueue {
|
|
125
126
|
isCleared: boolean;
|
|
126
127
|
}
|
|
128
|
+
export interface ReceiveNotificationResponse {
|
|
129
|
+
receiptId: number;
|
|
130
|
+
body: GreenApiWebhook;
|
|
131
|
+
}
|
|
132
|
+
export interface DeleteNotificationResponse {
|
|
133
|
+
result: boolean;
|
|
134
|
+
}
|
|
135
|
+
export interface GreenApiClientLogger {
|
|
136
|
+
info(message: string, context?: Record<string, any>): void;
|
|
137
|
+
}
|
|
138
|
+
export interface GreenApiClientOptions {
|
|
139
|
+
logger?: GreenApiClientLogger | null;
|
|
140
|
+
}
|
|
127
141
|
export type MessageType = "textMessage" | "extendedTextMessage" | "imageMessage" | "videoMessage" | "documentMessage" | "audioMessage" | "contactMessage" | "locationMessage" | "contactsArrayMessage" | "pollMessage" | "reactionMessage" | "pollUpdateMessage" | "quotedMessage" | "stickerMessage" | "editedMessage" | "deletedMessage" | "buttonsMessage" | "listMessage" | "templateMessage" | "groupInviteMessage" | "interactiveButtons" | "interactiveButtonsReply" | "templateButtonsReplyMessage";
|
|
128
142
|
export interface GetMessage {
|
|
129
143
|
chatId: string;
|
|
@@ -773,7 +787,8 @@ export interface RemoveAdminResponse {
|
|
|
773
787
|
}
|
|
774
788
|
export interface SetGroupPicture {
|
|
775
789
|
groupId: string;
|
|
776
|
-
file: Blob | File;
|
|
790
|
+
file: Blob | File | Uint8Array;
|
|
791
|
+
fileName?: string;
|
|
777
792
|
}
|
|
778
793
|
export interface SetGroupPictureResponse {
|
|
779
794
|
setGroupPicture: boolean;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@green-api/greenapi-integration",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "GREEN-API Integration library",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -24,19 +24,16 @@
|
|
|
24
24
|
"author": "GREEN-API team",
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@types/
|
|
28
|
-
"@types/
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"jest": "^29.
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"typedoc": "^0.27.6",
|
|
36
|
-
"typedoc-plugin-markdown": "^4.4.1",
|
|
37
|
-
"typescript": "^5.7.2"
|
|
27
|
+
"@types/jest": "^30.0.0",
|
|
28
|
+
"@types/node": "^22.20.5",
|
|
29
|
+
"jest": "^30.5.2",
|
|
30
|
+
"rimraf": "^6.1.3",
|
|
31
|
+
"ts-jest": "^29.4.15",
|
|
32
|
+
"typedoc": "^0.28.20",
|
|
33
|
+
"typedoc-plugin-markdown": "^4.14.1",
|
|
34
|
+
"typescript": "^6.0.3"
|
|
38
35
|
},
|
|
39
36
|
"dependencies": {
|
|
40
|
-
"axios": "^1.
|
|
37
|
+
"axios": "^1.20.0"
|
|
41
38
|
}
|
|
42
39
|
}
|