@green-api/greenapi-integration 0.9.0 → 0.10.1
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 +65 -7
- package/dist/core/green-api.client.js +131 -46
- 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 +12 -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,23 @@ 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 mimeType;
|
|
38
|
+
private toBlob;
|
|
39
|
+
private toBuffer;
|
|
34
40
|
private buildEndpoint;
|
|
41
|
+
private redactToken;
|
|
35
42
|
private makeRequest;
|
|
36
43
|
private makeFileUploadRequest;
|
|
37
44
|
/**
|
|
@@ -266,18 +273,32 @@ export declare class GreenApiClient {
|
|
|
266
273
|
/**
|
|
267
274
|
* Sets the profile picture for the WhatsApp account.
|
|
268
275
|
*
|
|
269
|
-
* @param file - Image file to use as profile picture
|
|
276
|
+
* @param file - Image file (jpg) to use as profile picture
|
|
277
|
+
* @param fileName - File name with extension
|
|
270
278
|
* @returns Promise resolving to profile picture update response
|
|
271
279
|
*/
|
|
272
|
-
setProfilePicture(file: Blob | File): Promise<SetProfilePicture>;
|
|
280
|
+
setProfilePicture(file: Blob | File | Uint8Array, fileName?: string): Promise<SetProfilePicture>;
|
|
273
281
|
/**
|
|
274
282
|
* Uploads a file to GREEN-API servers.
|
|
275
283
|
*
|
|
276
284
|
* @param file - File to upload
|
|
277
|
-
* @param
|
|
285
|
+
* @param fileName - File name with extension
|
|
278
286
|
* @returns Promise resolving to upload response with file URL
|
|
279
287
|
*/
|
|
280
|
-
uploadFile(file: Blob | File,
|
|
288
|
+
uploadFile(file: Blob | File | Uint8Array, fileName?: string): Promise<UploadFile>;
|
|
289
|
+
/**
|
|
290
|
+
* Calls any GREEN-API method that takes a file.
|
|
291
|
+
*
|
|
292
|
+
* @param endpoint - GREEN-API method name, e.g. "setGroupPicture"
|
|
293
|
+
* @param file - File content and file name
|
|
294
|
+
* @param fields - Other form fields
|
|
295
|
+
* @returns Promise resolving to the raw response body
|
|
296
|
+
*/
|
|
297
|
+
requestWithFile<T = unknown>(endpoint: string, file: {
|
|
298
|
+
data: Blob | File | Uint8Array;
|
|
299
|
+
fileName: string;
|
|
300
|
+
}, fields?: Record<string, string | number | boolean>): Promise<T>;
|
|
301
|
+
private pictureFileName;
|
|
281
302
|
/**
|
|
282
303
|
* Gets authorization code for a phone number.
|
|
283
304
|
*
|
|
@@ -315,6 +336,43 @@ export declare class GreenApiClient {
|
|
|
315
336
|
* ```
|
|
316
337
|
*/
|
|
317
338
|
clearMessagesQueue(): Promise<ClearMessagesQueue>;
|
|
339
|
+
/**
|
|
340
|
+
* Calls any GREEN-API method by name.
|
|
341
|
+
*
|
|
342
|
+
* @param method - HTTP method
|
|
343
|
+
* @param endpoint - GREEN-API method name, e.g. "getContacts"
|
|
344
|
+
* @param data - Request body for POST
|
|
345
|
+
* @param queryParams - Query parameters
|
|
346
|
+
* @returns Promise resolving to the raw response body
|
|
347
|
+
*
|
|
348
|
+
* @example
|
|
349
|
+
* ```typescript
|
|
350
|
+
* const contacts = await client.request("get", "getContacts");
|
|
351
|
+
* ```
|
|
352
|
+
*/
|
|
353
|
+
request<T = unknown>(method: "get" | "post" | "delete", endpoint: string, data?: unknown, queryParams?: Record<string, string | number>): Promise<T>;
|
|
354
|
+
/**
|
|
355
|
+
* Receives the next notification from the instance queue.
|
|
356
|
+
*
|
|
357
|
+
* @param receiveTimeout - Seconds to wait for a notification, 5 to 60
|
|
358
|
+
* @returns Promise resolving to the notification, or null when the queue stayed empty
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
* ```typescript
|
|
362
|
+
* const notification = await client.receiveNotification(20);
|
|
363
|
+
* if (notification) {
|
|
364
|
+
* await client.deleteNotification(notification.receiptId);
|
|
365
|
+
* }
|
|
366
|
+
* ```
|
|
367
|
+
*/
|
|
368
|
+
receiveNotification(receiveTimeout?: number): Promise<ReceiveNotificationResponse | null>;
|
|
369
|
+
/**
|
|
370
|
+
* Deletes a processed notification from the instance queue.
|
|
371
|
+
*
|
|
372
|
+
* @param receiptId - Receipt ID from receiveNotification
|
|
373
|
+
* @returns Promise resolving to deletion result
|
|
374
|
+
*/
|
|
375
|
+
deleteNotification(receiptId: number): Promise<DeleteNotificationResponse>;
|
|
318
376
|
/**
|
|
319
377
|
* Marks messages in a chat as read.
|
|
320
378
|
* For this to work, "Receive webhooks on incoming messages and files" setting must be enabled.
|
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.GreenApiClient = void 0;
|
|
7
7
|
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const mime_types_1 = require("mime-types");
|
|
8
9
|
const errors_1 = require("./errors");
|
|
9
10
|
const logger_1 = require("./logger");
|
|
10
11
|
const defaults_1 = require("../defaults");
|
|
@@ -32,47 +33,81 @@ class GreenApiClient {
|
|
|
32
33
|
/**
|
|
33
34
|
* Creates a GREEN-API client instance.
|
|
34
35
|
*
|
|
35
|
-
* @param instance - Configuration containing idInstance and
|
|
36
|
+
* @param instance - Configuration containing idInstance, apiTokenInstance and optionally apiUrl and mediaUrl
|
|
37
|
+
* @param options - Client options, e.g. `logger: null` to disable request logging
|
|
36
38
|
*/
|
|
37
|
-
constructor(instance) {
|
|
39
|
+
constructor(instance, options = {}) {
|
|
38
40
|
this.instance = instance;
|
|
39
|
-
this.gaLogger = logger_1.GreenApiLogger.getInstance(GreenApiClient.name);
|
|
40
41
|
this.baseUrl = (instance.apiUrl || defaults_1.DEFAULT_API_URL).replace(/\/+$/, "");
|
|
42
|
+
this.mediaBaseUrl = (instance.mediaUrl || this.baseUrl).replace(/\/+$/, "");
|
|
41
43
|
this.client = axios_1.default.create({
|
|
42
|
-
baseURL: this.buildUrl(),
|
|
44
|
+
baseURL: this.buildUrl(this.baseUrl),
|
|
43
45
|
});
|
|
46
|
+
this.mediaClient = axios_1.default.create({
|
|
47
|
+
baseURL: this.buildUrl(this.mediaBaseUrl),
|
|
48
|
+
});
|
|
49
|
+
this.gaLogger = options.logger === undefined ? logger_1.GreenApiLogger.getInstance(GreenApiClient.name) : options.logger;
|
|
50
|
+
}
|
|
51
|
+
buildUrl(host) {
|
|
52
|
+
return `${host}/waInstance${this.instance.idInstance}`;
|
|
53
|
+
}
|
|
54
|
+
mimeType(data, fileName) {
|
|
55
|
+
if (data instanceof Blob && data.type) {
|
|
56
|
+
return data.type;
|
|
57
|
+
}
|
|
58
|
+
return (fileName && (0, mime_types_1.lookup)(fileName)) || undefined;
|
|
59
|
+
}
|
|
60
|
+
toBlob(data, fileName) {
|
|
61
|
+
const type = this.mimeType(data, fileName);
|
|
62
|
+
if (data instanceof Blob && (data.type || !type)) {
|
|
63
|
+
return data;
|
|
64
|
+
}
|
|
65
|
+
return new Blob([data instanceof Blob ? data : data], type ? { type } : undefined);
|
|
44
66
|
}
|
|
45
|
-
|
|
46
|
-
return
|
|
67
|
+
toBuffer(data) {
|
|
68
|
+
return Buffer.isBuffer(data) ? data : Buffer.from(data.buffer, data.byteOffset, data.byteLength);
|
|
47
69
|
}
|
|
48
70
|
buildEndpoint(endpoint) {
|
|
49
71
|
return `/${endpoint}/${this.instance.apiTokenInstance}`;
|
|
50
72
|
}
|
|
51
|
-
|
|
73
|
+
redactToken(text) {
|
|
74
|
+
const token = this.instance.apiTokenInstance;
|
|
75
|
+
return token ? text.split(token).join(defaults_1.REDACTED_TOKEN) : text;
|
|
76
|
+
}
|
|
77
|
+
async makeRequest(method, endpoint, data, queryParams, config, pathAfterToken, host = "api") {
|
|
52
78
|
try {
|
|
53
|
-
this.gaLogger
|
|
79
|
+
this.gaLogger?.info("Making a request", {
|
|
54
80
|
idInstance: this.instance.idInstance,
|
|
55
81
|
endpoint,
|
|
56
82
|
method,
|
|
57
|
-
data,
|
|
83
|
+
data: Buffer.isBuffer(data) ? `${data.length} bytes` : data,
|
|
58
84
|
queryParams,
|
|
59
85
|
config,
|
|
60
86
|
});
|
|
61
|
-
const url = this.buildEndpoint(endpoint) + (queryParams ? "?" + new URLSearchParams(Object.entries(queryParams).map(([key, value]) => [key, value.toString()])).toString() : "");
|
|
62
|
-
const
|
|
63
|
-
|
|
64
|
-
|
|
87
|
+
const url = this.buildEndpoint(endpoint) + (pathAfterToken ? `/${pathAfterToken}` : "") + (queryParams ? "?" + new URLSearchParams(Object.entries(queryParams).map(([key, value]) => [key, value.toString()])).toString() : "");
|
|
88
|
+
const client = host === "media" ? this.mediaClient : this.client;
|
|
89
|
+
let response;
|
|
90
|
+
if (method === "get") {
|
|
91
|
+
response = await client.get(url, config);
|
|
92
|
+
}
|
|
93
|
+
else if (method === "delete") {
|
|
94
|
+
response = await client.delete(url, config);
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
response = await client.post(url, data, config);
|
|
98
|
+
}
|
|
65
99
|
return response.data;
|
|
66
100
|
}
|
|
67
101
|
catch (error) {
|
|
68
|
-
|
|
102
|
+
const responseData = error.response?.data === undefined
|
|
103
|
+
? undefined
|
|
104
|
+
: JSON.parse(this.redactToken(JSON.stringify(error.response.data)));
|
|
105
|
+
const reason = this.redactToken(String(error.message));
|
|
106
|
+
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
107
|
}
|
|
70
108
|
}
|
|
71
|
-
async makeFileUploadRequest(endpoint, formData,
|
|
72
|
-
return this.makeRequest("post", endpoint, formData, undefined, {
|
|
73
|
-
headers: { "Content-Type": "multipart/form-data" },
|
|
74
|
-
...headers,
|
|
75
|
-
});
|
|
109
|
+
async makeFileUploadRequest(endpoint, formData, host = "api") {
|
|
110
|
+
return this.makeRequest("post", endpoint, formData, undefined, { headers: { "Content-Type": "multipart/form-data" } }, undefined, host);
|
|
76
111
|
}
|
|
77
112
|
/**
|
|
78
113
|
* Sends a text message to a WhatsApp chat.
|
|
@@ -143,14 +178,14 @@ class GreenApiClient {
|
|
|
143
178
|
*/
|
|
144
179
|
async sendFileByUpload(message) {
|
|
145
180
|
const formData = new FormData();
|
|
146
|
-
formData.append("file", message.file.data, message.file.fileName);
|
|
181
|
+
formData.append("file", this.toBlob(message.file.data, message.file.fileName), message.file.fileName);
|
|
147
182
|
formData.append("chatId", message.chatId);
|
|
148
183
|
formData.append("fileName", message.file.fileName);
|
|
149
184
|
if (message.caption)
|
|
150
185
|
formData.append("caption", message.caption);
|
|
151
186
|
if (message.quotedMessageId)
|
|
152
187
|
formData.append("quotedMessageId", message.quotedMessageId);
|
|
153
|
-
return this.makeFileUploadRequest("sendFileByUpload", formData);
|
|
188
|
+
return this.makeFileUploadRequest("sendFileByUpload", formData, "media");
|
|
154
189
|
}
|
|
155
190
|
/**
|
|
156
191
|
* Creates a poll in a WhatsApp chat.
|
|
@@ -393,38 +428,47 @@ class GreenApiClient {
|
|
|
393
428
|
/**
|
|
394
429
|
* Sets the profile picture for the WhatsApp account.
|
|
395
430
|
*
|
|
396
|
-
* @param file - Image file to use as profile picture
|
|
431
|
+
* @param file - Image file (jpg) to use as profile picture
|
|
432
|
+
* @param fileName - File name with extension
|
|
397
433
|
* @returns Promise resolving to profile picture update response
|
|
398
434
|
*/
|
|
399
|
-
async setProfilePicture(file) {
|
|
400
|
-
|
|
401
|
-
formData.append("file", file);
|
|
402
|
-
return this.makeFileUploadRequest("setProfilePicture", formData);
|
|
435
|
+
async setProfilePicture(file, fileName) {
|
|
436
|
+
return this.requestWithFile("setProfilePicture", { data: file, fileName: this.pictureFileName(file, fileName) });
|
|
403
437
|
}
|
|
404
438
|
/**
|
|
405
439
|
* Uploads a file to GREEN-API servers.
|
|
406
440
|
*
|
|
407
441
|
* @param file - File to upload
|
|
408
|
-
* @param
|
|
442
|
+
* @param fileName - File name with extension
|
|
409
443
|
* @returns Promise resolving to upload response with file URL
|
|
410
444
|
*/
|
|
411
|
-
async uploadFile(file,
|
|
412
|
-
const
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
const mimeType = file.type;
|
|
417
|
-
if (mimeType) {
|
|
418
|
-
headers["Content-Type"] = mimeType;
|
|
419
|
-
}
|
|
420
|
-
else if (customFileName) {
|
|
421
|
-
headers["GA-Filename"] = customFileName;
|
|
422
|
-
}
|
|
445
|
+
async uploadFile(file, fileName) {
|
|
446
|
+
const mimeType = this.mimeType(file, fileName);
|
|
447
|
+
const headers = { "Content-Type": mimeType || false };
|
|
448
|
+
if (!mimeType && fileName) {
|
|
449
|
+
headers["GA-Filename"] = fileName;
|
|
423
450
|
}
|
|
424
|
-
|
|
425
|
-
|
|
451
|
+
const body = file instanceof Blob ? Buffer.from(await file.arrayBuffer()) : this.toBuffer(file);
|
|
452
|
+
return this.makeRequest("post", "uploadFile", body, undefined, { headers }, undefined, "media");
|
|
453
|
+
}
|
|
454
|
+
/**
|
|
455
|
+
* Calls any GREEN-API method that takes a file.
|
|
456
|
+
*
|
|
457
|
+
* @param endpoint - GREEN-API method name, e.g. "setGroupPicture"
|
|
458
|
+
* @param file - File content and file name
|
|
459
|
+
* @param fields - Other form fields
|
|
460
|
+
* @returns Promise resolving to the raw response body
|
|
461
|
+
*/
|
|
462
|
+
async requestWithFile(endpoint, file, fields = {}) {
|
|
463
|
+
const formData = new FormData();
|
|
464
|
+
formData.append("file", this.toBlob(file.data, file.fileName), file.fileName);
|
|
465
|
+
for (const [key, value] of Object.entries(fields)) {
|
|
466
|
+
formData.append(key, String(value));
|
|
426
467
|
}
|
|
427
|
-
return this.makeFileUploadRequest(
|
|
468
|
+
return this.makeFileUploadRequest(endpoint, formData);
|
|
469
|
+
}
|
|
470
|
+
pictureFileName(file, fileName) {
|
|
471
|
+
return fileName || (file instanceof File && file.name ? file.name : defaults_1.DEFAULT_PICTURE_FILE_NAME);
|
|
428
472
|
}
|
|
429
473
|
/**
|
|
430
474
|
* Gets authorization code for a phone number.
|
|
@@ -472,6 +516,50 @@ class GreenApiClient {
|
|
|
472
516
|
async clearMessagesQueue() {
|
|
473
517
|
return this.makeRequest("get", "clearMessagesQueue");
|
|
474
518
|
}
|
|
519
|
+
/**
|
|
520
|
+
* Calls any GREEN-API method by name.
|
|
521
|
+
*
|
|
522
|
+
* @param method - HTTP method
|
|
523
|
+
* @param endpoint - GREEN-API method name, e.g. "getContacts"
|
|
524
|
+
* @param data - Request body for POST
|
|
525
|
+
* @param queryParams - Query parameters
|
|
526
|
+
* @returns Promise resolving to the raw response body
|
|
527
|
+
*
|
|
528
|
+
* @example
|
|
529
|
+
* ```typescript
|
|
530
|
+
* const contacts = await client.request("get", "getContacts");
|
|
531
|
+
* ```
|
|
532
|
+
*/
|
|
533
|
+
async request(method, endpoint, data, queryParams) {
|
|
534
|
+
return this.makeRequest(method, endpoint, data, queryParams);
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Receives the next notification from the instance queue.
|
|
538
|
+
*
|
|
539
|
+
* @param receiveTimeout - Seconds to wait for a notification, 5 to 60
|
|
540
|
+
* @returns Promise resolving to the notification, or null when the queue stayed empty
|
|
541
|
+
*
|
|
542
|
+
* @example
|
|
543
|
+
* ```typescript
|
|
544
|
+
* const notification = await client.receiveNotification(20);
|
|
545
|
+
* if (notification) {
|
|
546
|
+
* await client.deleteNotification(notification.receiptId);
|
|
547
|
+
* }
|
|
548
|
+
* ```
|
|
549
|
+
*/
|
|
550
|
+
async receiveNotification(receiveTimeout) {
|
|
551
|
+
const response = await this.makeRequest("get", "receiveNotification", undefined, receiveTimeout === undefined ? undefined : { receiveTimeout });
|
|
552
|
+
return response || null;
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Deletes a processed notification from the instance queue.
|
|
556
|
+
*
|
|
557
|
+
* @param receiptId - Receipt ID from receiveNotification
|
|
558
|
+
* @returns Promise resolving to deletion result
|
|
559
|
+
*/
|
|
560
|
+
async deleteNotification(receiptId) {
|
|
561
|
+
return this.makeRequest("delete", "deleteNotification", undefined, undefined, undefined, receiptId.toString());
|
|
562
|
+
}
|
|
475
563
|
/**
|
|
476
564
|
* Marks messages in a chat as read.
|
|
477
565
|
* For this to work, "Receive webhooks on incoming messages and files" setting must be enabled.
|
|
@@ -673,10 +761,7 @@ class GreenApiClient {
|
|
|
673
761
|
* @returns Promise resolving to picture update status
|
|
674
762
|
*/
|
|
675
763
|
async setGroupPicture(params) {
|
|
676
|
-
|
|
677
|
-
formData.append("file", params.file);
|
|
678
|
-
formData.append("groupId", params.groupId);
|
|
679
|
-
return this.makeFileUploadRequest("setGroupPicture", formData);
|
|
764
|
+
return this.requestWithFile("setGroupPicture", { data: params.file, fileName: this.pictureFileName(params.file, params.fileName) }, { groupId: params.groupId });
|
|
680
765
|
}
|
|
681
766
|
/**
|
|
682
767
|
* 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.1",
|
|
4
4
|
"description": "GREEN-API Integration library",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -24,19 +24,18 @@
|
|
|
24
24
|
"author": "GREEN-API team",
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@types/
|
|
28
|
-
"@types/
|
|
29
|
-
"@types/node": "^22.
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"typedoc-plugin-markdown": "^4.4.1",
|
|
37
|
-
"typescript": "^5.7.2"
|
|
27
|
+
"@types/jest": "^30.0.0",
|
|
28
|
+
"@types/mime-types": "^3.0.1",
|
|
29
|
+
"@types/node": "^22.20.5",
|
|
30
|
+
"jest": "^30.5.2",
|
|
31
|
+
"rimraf": "^6.1.3",
|
|
32
|
+
"ts-jest": "^29.4.15",
|
|
33
|
+
"typedoc": "^0.28.20",
|
|
34
|
+
"typedoc-plugin-markdown": "^4.14.1",
|
|
35
|
+
"typescript": "^6.0.3"
|
|
38
36
|
},
|
|
39
37
|
"dependencies": {
|
|
40
|
-
"axios": "^1.
|
|
38
|
+
"axios": "^1.20.0",
|
|
39
|
+
"mime-types": "^3.0.2"
|
|
41
40
|
}
|
|
42
41
|
}
|