@green-api/greenapi-integration 0.6.1 → 0.7.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/README.md CHANGED
@@ -1020,7 +1020,6 @@ async function main() {
1020
1020
  await visitorClient.sendMessage({
1021
1021
  chatId: formatPhoneNumber(process.env.AGENT_PHONE_NUMBER!),
1022
1022
  message: "Hello! This is a test message from a visitor.",
1023
- type: "text",
1024
1023
  });
1025
1024
 
1026
1025
  console.log("Initial message sent! Check the agent WhatsApp app to see the response.");
@@ -56,18 +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, headers: request.headers });
59
60
  throw new errors_1.AuthenticationError("Authentication header is missing");
60
61
  }
61
62
  this.gaLogger.info("Request from GREEN-API", { body: request.body });
62
63
  const idInstance = request.body?.instanceData?.idInstance;
63
64
  if (!idInstance) {
65
+ this.gaLogger.warn("Invalid webhook format", { body: request.body, headers: request.headers });
64
66
  throw new errors_1.AuthenticationError("Invalid webhook format");
65
67
  }
66
68
  const instance = await this.storage.getInstance(idInstance);
67
69
  if (!instance) {
70
+ this.gaLogger.warn(`No instance with such ID ${idInstance}`, { body: request.body, headers: request.headers });
68
71
  throw new errors_1.AuthenticationError(`No instance with such ID ${idInstance}`);
69
72
  }
70
73
  if (instance.settings?.webhookUrlToken !== token.split(" ")[1]) {
74
+ this.gaLogger.warn("Invalid token", { body: request.body, headers: request.headers });
71
75
  throw new errors_1.AuthenticationError("Invalid token");
72
76
  }
73
77
  return true;
@@ -16,40 +16,43 @@ export interface Instance extends BaseInstance {
16
16
  [key: string]: any;
17
17
  }
18
18
  /**
19
- * Union type representing all possible message formats that can be sent through GREEN-API.
20
- * Each message type has its own specific structure and required fields.
19
+ * Common properties shared by all message types.
21
20
  */
22
- export type Message = ({
23
- type: "text";
21
+ export interface BaseMessage {
22
+ chatId: string;
23
+ quotedMessageId?: string;
24
+ }
25
+ export interface ForwardMessagesResponse {
26
+ messages: string[];
27
+ }
28
+ export interface PollOption {
29
+ optionName: string;
30
+ }
31
+ export interface SendMessage extends BaseMessage {
24
32
  message: string;
25
33
  linkPreview?: boolean;
26
- } & BaseMessage) | ({
27
- type: "upload-file";
34
+ }
35
+ export interface SendFileByUpload extends BaseMessage {
28
36
  caption?: string;
29
37
  file: {
30
38
  data: Blob | File;
31
39
  fileName: string;
32
40
  };
33
- } & BaseMessage) | ({
34
- type: "url-file";
41
+ }
42
+ export interface SendFileByUrl extends BaseMessage {
35
43
  caption?: string;
36
44
  file: {
37
45
  url: string;
38
46
  fileName: string;
39
47
  };
40
- } & BaseMessage) | ({
41
- type: "poll";
42
- message: string;
43
- options: PollOption[];
44
- multipleAnswers?: boolean;
45
- } & BaseMessage) | ({
46
- type: "location";
48
+ }
49
+ export interface SendLocation extends BaseMessage {
47
50
  nameLocation?: string;
48
51
  address?: string;
49
52
  latitude: number;
50
53
  longitude: number;
51
- } & BaseMessage) | ({
52
- type: "contact";
54
+ }
55
+ export interface SendContact extends BaseMessage {
53
56
  contact: {
54
57
  phoneContact: number;
55
58
  firstName?: string;
@@ -57,47 +60,32 @@ export type Message = ({
57
60
  lastName?: string;
58
61
  company?: string;
59
62
  };
60
- } & BaseMessage) | {
61
- type: "forward";
63
+ }
64
+ export interface SendPoll extends BaseMessage {
65
+ message: string;
66
+ options: PollOption[];
67
+ multipleAnswers?: boolean;
68
+ }
69
+ export interface ForwardMessages {
62
70
  chatId: string;
63
71
  chatIdFrom: string;
64
72
  messages: string[];
65
- };
66
- /**
67
- * Common properties shared by all message types.
68
- */
69
- export interface BaseMessage {
70
- chatId: string;
71
- quotedMessageId?: string;
72
- }
73
- export type SendMessageType = "text" | "upload-file" | "url-file" | "poll" | "location" | "contact" | "forward";
74
- export interface ForwardMessagesResponse {
75
- messages: string[];
76
- }
77
- export interface PollOption {
78
- optionName: string;
79
73
  }
80
- export type SendMessage = Extract<Message, {
74
+ export type Message = (SendMessage & {
81
75
  type: "text";
82
- }>;
83
- export type SendFileByUpload = Extract<Message, {
76
+ }) | (SendFileByUpload & {
84
77
  type: "upload-file";
85
- }>;
86
- export type SendFileByUrl = Extract<Message, {
78
+ }) | (SendFileByUrl & {
87
79
  type: "url-file";
88
- }>;
89
- export type SendLocation = Extract<Message, {
80
+ }) | (SendPoll & {
81
+ type: "poll";
82
+ }) | (SendLocation & {
90
83
  type: "location";
91
- }>;
92
- export type SendContact = Extract<Message, {
84
+ }) | (SendContact & {
93
85
  type: "contact";
94
- }>;
95
- export type SendPoll = Extract<Message, {
96
- type: "poll";
97
- }>;
98
- export type ForwardMessages = Extract<Message, {
86
+ }) | (ForwardMessages & {
99
87
  type: "forward";
100
- }>;
88
+ });
101
89
  export type QueueMessageType = "sendMessage" | "sendPoll" | "sendFileByUrl" | "sendLocation" | "sendContact" | "ForwardMessages";
102
90
  export type QueueMessageBody = SendMessage | SendPoll | SendFileByUrl | SendLocation | SendContact | ForwardMessages;
103
91
  export interface QueueMessage {
@@ -109,7 +97,7 @@ export interface QueueMessage {
109
97
  export interface ClearMessagesQueue {
110
98
  isCleared: boolean;
111
99
  }
112
- export type MessageType = "textMessage" | "extendedTextMessage" | "imageMessage" | "videoMessage" | "documentMessage" | "audioMessage" | "contactMessage" | "locationMessage" | "contactsArrayMessage" | "pollMessage" | "reactionMessage" | "pollUpdateMessage" | "quotedMessage" | "stickerMessage";
100
+ export type MessageType = "textMessage" | "extendedTextMessage" | "imageMessage" | "videoMessage" | "documentMessage" | "audioMessage" | "contactMessage" | "locationMessage" | "contactsArrayMessage" | "pollMessage" | "reactionMessage" | "pollUpdateMessage" | "quotedMessage" | "stickerMessage" | "editedMessage" | "deletedMessage" | "buttonsMessage" | "listMessage" | "templateMessage" | "groupInviteMessage";
113
101
  export interface GetMessage {
114
102
  chatId: string;
115
103
  idMessage: string;
@@ -140,13 +128,17 @@ export interface OutgoingJournalFields {
140
128
  export type BaseIncomingJournalMessage = BaseJournalMessage & IncomingJournalFields;
141
129
  export type BaseOutgoingJournalMessage = BaseJournalMessage & OutgoingJournalFields;
142
130
  export type BaseJournalResponse = BaseIncomingJournalMessage | BaseOutgoingJournalMessage;
143
- export type OutgoingJournalResponse = BaseOutgoingJournalMessage & WebhookMessageData & {
131
+ export type JournalMessageData<T> = T extends {
132
+ typeMessage: "imageMessage" | "videoMessage" | "documentMessage" | "audioMessage" | "stickerMessage";
133
+ fileMessageData: FileMessageData;
134
+ } ? Omit<T, "fileMessageData"> & FileMessageData : T;
135
+ export type OutgoingJournalResponse = BaseOutgoingJournalMessage & JournalMessageData<WebhookMessageData> & {
144
136
  quotedMessage?: QuotedMessage;
145
137
  };
146
- export type IncomingJournalResponse = BaseIncomingJournalMessage & WebhookMessageData & {
138
+ export type IncomingJournalResponse = BaseIncomingJournalMessage & JournalMessageData<WebhookMessageData> & {
147
139
  quotedMessage?: QuotedMessage;
148
140
  };
149
- export type JournalResponse = BaseJournalResponse & WebhookMessageData & {
141
+ export type JournalResponse = BaseJournalResponse & JournalMessageData<WebhookMessageData> & {
150
142
  quotedMessage?: QuotedMessage;
151
143
  };
152
144
  export interface ForwardableMessage {
@@ -159,6 +151,76 @@ export interface MediaMessage extends ForwardableMessage {
159
151
  export interface TextMessageData {
160
152
  textMessage: string;
161
153
  }
154
+ export interface EditedMessageData {
155
+ textMessage?: string;
156
+ caption?: string;
157
+ stanzaId: string;
158
+ }
159
+ export interface DeletedMessageData {
160
+ stanzaId: string;
161
+ }
162
+ export interface ReactionMessageData {
163
+ text: string;
164
+ }
165
+ export interface ButtonData {
166
+ buttonId: string;
167
+ buttonText: string;
168
+ }
169
+ export interface ButtonsMessageData extends ForwardableMessage {
170
+ contentText: string;
171
+ footer: string;
172
+ buttons: ButtonData[];
173
+ }
174
+ export interface ListRowData {
175
+ title: string;
176
+ rowId: string;
177
+ description?: string;
178
+ }
179
+ export interface ListSectionData {
180
+ title: string;
181
+ rows: ListRowData[];
182
+ }
183
+ export interface ListMessageData extends ForwardableMessage {
184
+ contentText: string;
185
+ title?: string;
186
+ footer?: string;
187
+ buttonText?: string;
188
+ sections: ListSectionData[];
189
+ }
190
+ export interface UrlButtonData {
191
+ displayText: string;
192
+ url: string;
193
+ }
194
+ export interface CallButtonData {
195
+ displayText: string;
196
+ phoneNumber: string;
197
+ }
198
+ export interface QuickReplyButtonData {
199
+ displayText: string;
200
+ id: string;
201
+ }
202
+ export interface TemplateButtonData {
203
+ index: number;
204
+ urlButton?: UrlButtonData;
205
+ callButton?: CallButtonData;
206
+ quickReplyButton?: QuickReplyButtonData;
207
+ }
208
+ export interface TemplateMessageData extends ForwardableMessage {
209
+ namespace?: string;
210
+ elementName?: string;
211
+ contentText: string;
212
+ footer?: string;
213
+ buttons: TemplateButtonData[];
214
+ }
215
+ export interface GroupInviteMessageData {
216
+ groupJid: string;
217
+ inviteCode: string;
218
+ inviteExpiration: string;
219
+ groupName: string;
220
+ caption: string;
221
+ name: string;
222
+ jpegThumbnail: string;
223
+ }
162
224
  export interface ExtendedTextMessageData extends MediaMessage {
163
225
  text: string;
164
226
  description: string;
@@ -169,6 +231,7 @@ export interface FileMessageData extends MediaMessage {
169
231
  caption: string;
170
232
  mimeType: string;
171
233
  fileName: string;
234
+ isAnimated?: boolean;
172
235
  }
173
236
  export interface LocationMessageData extends MediaMessage {
174
237
  nameLocation: string;
@@ -233,6 +296,32 @@ export type QuotedMessage = {
233
296
  downloadUrl: string;
234
297
  caption: string;
235
298
  jpegThumbnail: string;
299
+ } | {
300
+ typeMessage: "stickerMessage";
301
+ downloadUrl: string;
302
+ caption: string;
303
+ jpegThumbnail: string;
304
+ isAnimated: boolean;
305
+ } | {
306
+ typeMessage: "buttonsMessage";
307
+ contentText: string;
308
+ footer?: string;
309
+ buttons: ButtonData[];
310
+ } | {
311
+ typeMessage: "listMessage";
312
+ contentText: string;
313
+ title?: string;
314
+ footer?: string;
315
+ buttonText?: string;
316
+ sections: ListSectionData[];
317
+ } | {
318
+ typeMessage: "templateMessage";
319
+ contentText: string;
320
+ footer?: string;
321
+ buttons: TemplateButtonData[];
322
+ } | {
323
+ typeMessage: "groupInviteMessage";
324
+ groupInviteMessageData: GroupInviteMessageData;
236
325
  });
237
326
  export interface PollVote {
238
327
  optionName: string;
@@ -245,7 +334,7 @@ export interface PollUpdateMessageData {
245
334
  multipleAnswers: boolean;
246
335
  }
247
336
  export type OutgoingMessageStatus = "sent" | "delivered" | "read" | "failed" | "noAccount" | "notInGroup" | "yellowCard";
248
- export type WebhookType = "stateInstanceChanged" | "outgoingMessageStatus" | "outgoingAPIMessageReceived" | "outgoingMessageReceived" | "incomingMessageReceived";
337
+ export type WebhookType = "stateInstanceChanged" | "outgoingMessageStatus" | "outgoingAPIMessageReceived" | "outgoingMessageReceived" | "incomingMessageReceived" | "incomingCall";
249
338
  /**
250
339
  * Webhook payload received when a message status changes.
251
340
  * Used to track delivery and read receipts.
@@ -302,6 +391,31 @@ export type WebhookMessageData = {
302
391
  } | {
303
392
  typeMessage: "contactsArrayMessage";
304
393
  messageData: ContactsArrayMessageData;
394
+ } | {
395
+ typeMessage: "editedMessage";
396
+ editedMessageData: EditedMessageData;
397
+ } | {
398
+ typeMessage: "deletedMessage";
399
+ deletedMessageData: DeletedMessageData;
400
+ } | {
401
+ typeMessage: "reactionMessage";
402
+ extendedTextMessageData: ReactionMessageData;
403
+ quotedMessage: QuotedMessage;
404
+ } | {
405
+ typeMessage: "buttonsMessage";
406
+ buttonsMessage: ButtonsMessageData;
407
+ } | {
408
+ typeMessage: "listMessage";
409
+ listMessage: ListMessageData;
410
+ } | {
411
+ typeMessage: "templateMessage";
412
+ templateMessage: TemplateMessageData;
413
+ } | {
414
+ typeMessage: "stickerMessage";
415
+ fileMessageData: FileMessageData;
416
+ } | {
417
+ typeMessage: "groupInviteMessage";
418
+ groupInviteMessageData: GroupInviteMessageData;
305
419
  };
306
420
  export interface MessageWebhook {
307
421
  typeWebhook: "incomingMessageReceived" | "outgoingMessageReceived" | "outgoingAPIMessageReceived";
@@ -323,10 +437,23 @@ export interface MessageWebhook {
323
437
  quotedMessage?: QuotedMessage;
324
438
  };
325
439
  }
440
+ export type CallStatus = "offer" | "pickUp" | "hangUp" | "missed" | "declined";
441
+ export interface IncomingCallWebhook {
442
+ from: string;
443
+ typeWebhook: "incomingCall";
444
+ instanceData: {
445
+ idInstance: number;
446
+ wid: string;
447
+ typeInstance: string;
448
+ };
449
+ status: CallStatus;
450
+ timestamp: number;
451
+ idMessage: string;
452
+ }
326
453
  /**
327
454
  * Primary webhook types received from GREEN-API.
328
455
  */
329
- export type GreenApiWebhook = MessageWebhook | OutgoingMessageStatusWebhook | StateInstanceWebhook;
456
+ export type GreenApiWebhook = MessageWebhook | OutgoingMessageStatusWebhook | StateInstanceWebhook | IncomingCallWebhook;
330
457
  /**
331
458
  * Configuration settings for a GREEN-API instance.
332
459
  * Controls webhook behavior, message handling, and other instance features.
@@ -346,6 +473,9 @@ export interface Settings {
346
473
  keepOnlineStatus?: "yes" | "no";
347
474
  pollMessageWebhook?: "yes" | "no";
348
475
  incomingCallWebhook?: "yes" | "no";
476
+ incomingBlockWebhook?: "yes" | "no";
477
+ editedMessageWebhook?: "yes" | "no";
478
+ deletedMessageWebhook?: "yes" | "no";
349
479
  }
350
480
  export interface Reboot {
351
481
  isReboot: boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@green-api/greenapi-integration",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
4
4
  "description": "GREEN-API Integration library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",