@green-api/greenapi-integration 0.2.0 → 0.4.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.
@@ -2,10 +2,11 @@
2
2
  * Base interface for GREEN-API WhatsApp instances.
3
3
  * Contains the essential credentials needed to interact with the API.
4
4
  */
5
- interface BaseInstance {
5
+ export interface BaseInstance {
6
6
  idInstance: number | bigint;
7
7
  apiTokenInstance: string;
8
- settings?: Settings;
8
+ stateInstance?: InstanceState;
9
+ settings?: Settings | Record<string, any>;
9
10
  }
10
11
  /**
11
12
  * Extended instance interface that allows for additional platform-specific properties.
@@ -21,6 +22,7 @@ export interface Instance extends BaseInstance {
21
22
  export type Message = ({
22
23
  type: "text";
23
24
  message: string;
25
+ linkPreview?: boolean;
24
26
  } & BaseMessage) | ({
25
27
  type: "upload-file";
26
28
  caption?: string;
@@ -96,7 +98,57 @@ export type SendPoll = Extract<Message, {
96
98
  export type ForwardMessages = Extract<Message, {
97
99
  type: "forward";
98
100
  }>;
99
- export type MessageType = "textMessage" | "extendedTextMessage" | "imageMessage" | "videoMessage" | "documentMessage" | "audioMessage" | "contactMessage" | "locationMessage" | "pollMessage";
101
+ export type QueueMessageType = "sendMessage" | "sendPoll" | "sendFileByUrl" | "sendLocation" | "sendContact" | "ForwardMessages";
102
+ export type QueueMessageBody = SendMessage | SendPoll | SendFileByUrl | SendLocation | SendContact | ForwardMessages;
103
+ export interface QueueMessage {
104
+ messageID?: string;
105
+ messagesIDs?: string[];
106
+ type: QueueMessageType;
107
+ body: QueueMessageBody;
108
+ }
109
+ export interface ClearMessagesQueue {
110
+ isCleared: boolean;
111
+ }
112
+ export type MessageType = "textMessage" | "extendedTextMessage" | "imageMessage" | "videoMessage" | "documentMessage" | "audioMessage" | "contactMessage" | "locationMessage" | "pollMessage" | "reactionMessage" | "pollUpdateMessage" | "quotedMessage" | "stickerMessage";
113
+ export interface GetMessage {
114
+ chatId: string;
115
+ idMessage: string;
116
+ }
117
+ export interface GetChatHistory {
118
+ chatId: string;
119
+ count?: number;
120
+ }
121
+ export interface BaseJournalMessage {
122
+ idMessage: string;
123
+ timestamp: number;
124
+ typeMessage: MessageType;
125
+ chatId: string;
126
+ isForwarded: boolean;
127
+ forwardingScore: number;
128
+ }
129
+ export interface IncomingJournalFields {
130
+ type: "incoming";
131
+ senderId: string;
132
+ senderName: string;
133
+ senderContactName: string;
134
+ }
135
+ export interface OutgoingJournalFields {
136
+ type: "outgoing";
137
+ statusMessage: OutgoingMessageStatus;
138
+ sendByApi: boolean;
139
+ }
140
+ export type BaseIncomingJournalMessage = BaseJournalMessage & IncomingJournalFields;
141
+ export type BaseOutgoingJournalMessage = BaseJournalMessage & OutgoingJournalFields;
142
+ export type BaseJournalResponse = BaseIncomingJournalMessage | BaseOutgoingJournalMessage;
143
+ export type OutgoingJournalResponse = BaseOutgoingJournalMessage & WebhookMessageData & {
144
+ quotedMessage?: QuotedMessage;
145
+ };
146
+ export type IncomingJournalResponse = BaseIncomingJournalMessage & WebhookMessageData & {
147
+ quotedMessage?: QuotedMessage;
148
+ };
149
+ export type JournalResponse = BaseJournalResponse & WebhookMessageData & {
150
+ quotedMessage?: QuotedMessage;
151
+ };
100
152
  export interface ForwardableMessage {
101
153
  forwardingScore: number;
102
154
  isForwarded: boolean;
@@ -172,6 +224,7 @@ export interface PollUpdateMessageData {
172
224
  multipleAnswers: boolean;
173
225
  }
174
226
  export type OutgoingMessageStatus = "sent" | "delivered" | "read" | "failed" | "noAccount" | "notInGroup" | "yellowCard";
227
+ export type WebhookType = "stateInstanceChanged" | "outgoingMessageStatus" | "outgoingAPIMessageReceived" | "outgoingMessageReceived" | "incomingMessageReceived";
175
228
  /**
176
229
  * Webhook payload received when a message status changes.
177
230
  * Used to track delivery and read receipts.
@@ -190,6 +243,16 @@ export interface OutgoingMessageStatusWebhook {
190
243
  description?: string;
191
244
  sendByApi: boolean;
192
245
  }
246
+ export interface StateInstanceWebhook {
247
+ typeWebhook: "stateInstanceChanged";
248
+ instanceData: {
249
+ idInstance: number;
250
+ wid: string;
251
+ typeInstance: string;
252
+ };
253
+ timestamp: number;
254
+ stateInstance: InstanceState;
255
+ }
193
256
  export type WebhookMessageData = {
194
257
  typeMessage: "textMessage";
195
258
  textMessageData: TextMessageData;
@@ -235,7 +298,7 @@ export interface MessageWebhook {
235
298
  /**
236
299
  * Primary webhook types received from GREEN-API.
237
300
  */
238
- export type GreenApiWebhook = MessageWebhook | OutgoingMessageStatusWebhook;
301
+ export type GreenApiWebhook = MessageWebhook | OutgoingMessageStatusWebhook | StateInstanceWebhook;
239
302
  /**
240
303
  * Configuration settings for a GREEN-API instance.
241
304
  * Controls webhook behavior, message handling, and other instance features.
@@ -262,6 +325,83 @@ export interface Reboot {
262
325
  export interface Logout {
263
326
  isLogout: boolean;
264
327
  }
328
+ export interface ReadChat {
329
+ chatId: string;
330
+ idMessage?: string;
331
+ }
332
+ export interface ReadChatResponse {
333
+ setRead: boolean;
334
+ }
335
+ export interface CheckWhatsapp {
336
+ phoneNumber: number;
337
+ }
338
+ export interface CheckWhatsappResponse {
339
+ existsWhatsapp: boolean;
340
+ }
341
+ export interface GetAvatar {
342
+ chatId: string;
343
+ }
344
+ export interface GetAvatarResponse {
345
+ urlAvatar: string;
346
+ available: boolean;
347
+ }
348
+ export type ContactType = "user" | "group";
349
+ export interface Contact {
350
+ id: string;
351
+ name: string;
352
+ contactName: string;
353
+ type: ContactType;
354
+ }
355
+ export interface ProductImageUrls {
356
+ requested: string;
357
+ original: string;
358
+ }
359
+ export interface ProductReviewStatus {
360
+ whatsapp: string;
361
+ }
362
+ export interface Product {
363
+ id: string;
364
+ imageUrls: ProductImageUrls;
365
+ reviewStatus: ProductReviewStatus;
366
+ availability: string;
367
+ name: string;
368
+ description?: string;
369
+ price: string | null;
370
+ isHidden: boolean;
371
+ }
372
+ export interface ContactInfo {
373
+ avatar: string;
374
+ name: string;
375
+ contactName: string;
376
+ email: string;
377
+ category: string;
378
+ description: string;
379
+ products: Product[];
380
+ chatId: string;
381
+ lastSeen: string | null;
382
+ isArchive: boolean;
383
+ isDisappearing: boolean;
384
+ isMute: boolean;
385
+ messageExpiration: number;
386
+ muteExpiration: number | null;
387
+ isBusiness: boolean;
388
+ }
389
+ export interface ArchiveChat {
390
+ chatId: string;
391
+ }
392
+ export interface UnarchiveChat {
393
+ chatId: string;
394
+ }
395
+ export type EphemeralExpiration = 0 | 86400 | 604800 | 7776000;
396
+ export interface SetDisappearingChat {
397
+ chatId: string;
398
+ ephemeralExpiration: EphemeralExpiration;
399
+ }
400
+ export interface SetDisappearingChatResponse {
401
+ chatId: string;
402
+ disappearingMessagesInChat: boolean;
403
+ ephemeralExpiration: EphemeralExpiration;
404
+ }
265
405
  /**
266
406
  * Represents an instance state in the GREEN-API system.
267
407
  */
@@ -301,6 +441,84 @@ export interface SetProfilePicture {
301
441
  urlAvatar: string;
302
442
  setProfilePicture: boolean;
303
443
  }
444
+ export interface CreateGroup {
445
+ groupName: string;
446
+ chatIds: string[];
447
+ }
448
+ export interface CreateGroupResponse {
449
+ created: boolean;
450
+ chatId: string;
451
+ groupInviteLink: string;
452
+ }
453
+ export interface UpdateGroupName {
454
+ groupId: string;
455
+ groupName: string;
456
+ }
457
+ export interface UpdateGroupNameResponse {
458
+ updateGroupName: boolean;
459
+ }
460
+ export interface GroupParticipant {
461
+ id: string;
462
+ isAdmin: boolean;
463
+ isSuperAdmin: boolean;
464
+ }
465
+ export interface GroupData {
466
+ groupId: string;
467
+ owner: string;
468
+ subject: string;
469
+ creation: number;
470
+ participants: GroupParticipant[];
471
+ subjectTime: number;
472
+ subjectOwner: string;
473
+ groupInviteLink: string;
474
+ }
475
+ export interface GetGroupData {
476
+ groupId: string;
477
+ }
478
+ export interface AddGroupParticipant {
479
+ groupId: string;
480
+ participantChatId: string;
481
+ }
482
+ export interface AddGroupParticipantResponse {
483
+ addParticipant: boolean;
484
+ }
485
+ export interface RemoveGroupParticipant {
486
+ groupId: string;
487
+ participantChatId: string;
488
+ }
489
+ export interface RemoveGroupParticipantResponse {
490
+ removeParticipant: boolean;
491
+ }
492
+ export interface SetGroupAdmin {
493
+ groupId: string;
494
+ participantChatId: string;
495
+ }
496
+ export interface SetGroupAdminResponse {
497
+ setGroupAdmin: boolean;
498
+ }
499
+ export interface RemoveAdmin {
500
+ groupId: string;
501
+ participantChatId: string;
502
+ }
503
+ export interface RemoveAdminResponse {
504
+ removeAdmin: boolean;
505
+ }
506
+ export interface SetGroupPicture {
507
+ groupId: string;
508
+ file: Blob | File;
509
+ }
510
+ export interface SetGroupPictureResponse {
511
+ setGroupPicture: boolean;
512
+ urlAvatar: string | null;
513
+ reason: string;
514
+ }
515
+ export interface LeaveGroup {
516
+ groupId: string;
517
+ }
518
+ export interface LeaveGroupResponse {
519
+ leaveGroup?: boolean;
520
+ removeAdmin?: boolean;
521
+ }
304
522
  export interface BaseRequest {
305
523
  headers: Record<string, any>;
306
524
  body: any;
@@ -1,3 +1,4 @@
1
+ import { Settings } from "../types/types";
1
2
  /**
2
3
  * Utility functions for working with phone numbers, tokens, and vCards.
3
4
  *
@@ -39,3 +40,34 @@ export declare function generateRandomToken(length?: number): string;
39
40
  * extractPhoneNumberFromVCard(vcard) // Returns '+1234567890'
40
41
  */
41
42
  export declare function extractPhoneNumberFromVCard(vcard: string): string | null;
43
+ /**
44
+ * Validates if a value is appropriate for a specific Settings interface key.
45
+ * Checks type compatibility for numbers, strings, and yes/no enums.
46
+ *
47
+ * @param key - The settings key to validate
48
+ * @param value - The value to check against the key's expected type
49
+ * @returns Boolean indicating if the value is valid for the given key
50
+ *
51
+ * @example
52
+ * isValidSettingValue('delaySendMessagesMilliseconds', 1000) // Returns true
53
+ * isValidSettingValue('outgoingWebhook', 'maybe') // Returns false
54
+ * isValidSettingValue('webhookUrl', 'https://example.com') // Returns true
55
+ */
56
+ export declare function isValidSettingValue(key: keyof Settings, value: any): boolean;
57
+ /**
58
+ * Validates and cleans a settings object against the Settings interface.
59
+ * Removes any properties that don't match the interface or have invalid values.
60
+ *
61
+ * @param settings - The settings object to validate and clean
62
+ * @returns A new Settings object containing only valid properties and values
63
+ *
64
+ * @example
65
+ * const input = {
66
+ * webhookUrl: 'https://example.com',
67
+ * outgoingWebhook: 'yes',
68
+ * invalidKey: 'value',
69
+ * delaySendMessagesMilliseconds: 'invalid'
70
+ * };
71
+ * validateAndCleanSettings(input) // Returns { webhookUrl: 'https://example.com', outgoingWebhook: 'yes' }
72
+ */
73
+ export declare function validateAndCleanSettings(settings: any): Settings;
@@ -36,6 +36,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.formatPhoneNumber = formatPhoneNumber;
37
37
  exports.generateRandomToken = generateRandomToken;
38
38
  exports.extractPhoneNumberFromVCard = extractPhoneNumberFromVCard;
39
+ exports.isValidSettingValue = isValidSettingValue;
40
+ exports.validateAndCleanSettings = validateAndCleanSettings;
39
41
  const crypto = __importStar(require("crypto"));
40
42
  /**
41
43
  * Utility functions for working with phone numbers, tokens, and vCards.
@@ -86,3 +88,65 @@ function extractPhoneNumberFromVCard(vcard) {
86
88
  const phoneMatch = vcard.match(/TEL(?:;[^:]+)?:([+\d\s-]+)/);
87
89
  return phoneMatch ? phoneMatch[1] : null;
88
90
  }
91
+ /**
92
+ * Validates if a value is appropriate for a specific Settings interface key.
93
+ * Checks type compatibility for numbers, strings, and yes/no enums.
94
+ *
95
+ * @param key - The settings key to validate
96
+ * @param value - The value to check against the key's expected type
97
+ * @returns Boolean indicating if the value is valid for the given key
98
+ *
99
+ * @example
100
+ * isValidSettingValue('delaySendMessagesMilliseconds', 1000) // Returns true
101
+ * isValidSettingValue('outgoingWebhook', 'maybe') // Returns false
102
+ * isValidSettingValue('webhookUrl', 'https://example.com') // Returns true
103
+ */
104
+ function isValidSettingValue(key, value) {
105
+ switch (key) {
106
+ case "delaySendMessagesMilliseconds":
107
+ return typeof value === "number";
108
+ case "wid":
109
+ case "webhookUrl":
110
+ case "webhookUrlToken":
111
+ return typeof value === "string";
112
+ case "markIncomingMessagesReaded":
113
+ case "markIncomingMessagesReadedOnReply":
114
+ case "outgoingWebhook":
115
+ case "outgoingMessageWebhook":
116
+ case "outgoingAPIMessageWebhook":
117
+ case "stateWebhook":
118
+ case "incomingWebhook":
119
+ case "keepOnlineStatus":
120
+ case "pollMessageWebhook":
121
+ case "incomingCallWebhook":
122
+ return value === "yes" || value === "no";
123
+ default:
124
+ return false;
125
+ }
126
+ }
127
+ /**
128
+ * Validates and cleans a settings object against the Settings interface.
129
+ * Removes any properties that don't match the interface or have invalid values.
130
+ *
131
+ * @param settings - The settings object to validate and clean
132
+ * @returns A new Settings object containing only valid properties and values
133
+ *
134
+ * @example
135
+ * const input = {
136
+ * webhookUrl: 'https://example.com',
137
+ * outgoingWebhook: 'yes',
138
+ * invalidKey: 'value',
139
+ * delaySendMessagesMilliseconds: 'invalid'
140
+ * };
141
+ * validateAndCleanSettings(input) // Returns { webhookUrl: 'https://example.com', outgoingWebhook: 'yes' }
142
+ */
143
+ function validateAndCleanSettings(settings) {
144
+ const validSettings = {};
145
+ const settingsKeys = Object.keys(settings);
146
+ for (const key of settingsKeys) {
147
+ if (key in validSettings && isValidSettingValue(key, settings[key])) {
148
+ validSettings[key] = settings[key];
149
+ }
150
+ }
151
+ return validSettings;
152
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@green-api/greenapi-integration",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "GREEN-API Integration library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",