@green-api/greenapi-integration 0.1.0 → 0.2.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.
@@ -1,77 +1,219 @@
1
- export interface BaseInstance {
1
+ /**
2
+ * Base interface for GREEN-API WhatsApp instances.
3
+ * Contains the essential credentials needed to interact with the API.
4
+ */
5
+ interface BaseInstance {
2
6
  idInstance: number | bigint;
3
7
  apiTokenInstance: string;
4
- settings?: any;
8
+ settings?: Settings;
5
9
  }
10
+ /**
11
+ * Extended instance interface that allows for additional platform-specific properties.
12
+ * Use this when you need to store extra data with your instance.
13
+ */
6
14
  export interface Instance extends BaseInstance {
7
15
  [key: string]: any;
8
16
  }
9
- export type Message = SendMessage | SendFileByUpload | SendFileByUrl | SendPoll | SendLocation | SendContact | ForwardMessages;
10
- export interface BaseMessage {
11
- type: SendMessageType;
12
- chatId: string;
13
- quotedMessageId?: string;
14
- }
15
- export type SendMessageType = "text" | "upload-file" | "url-file" | "poll" | "location" | "contact" | "forward";
16
- export interface SendMessage extends BaseMessage {
17
+ /**
18
+ * Union type representing all possible message formats that can be sent through GREEN-API.
19
+ * Each message type has its own specific structure and required fields.
20
+ */
21
+ export type Message = ({
17
22
  type: "text";
18
23
  message: string;
19
- }
20
- export interface ForwardMessages {
21
- type: "forward";
22
- chatId: string;
23
- chatIdFrom: string;
24
- messages: string[];
25
- }
26
- export interface ForwardMessagesResponse {
27
- messages: string[];
28
- }
29
- export interface Contact {
30
- phoneContact: number;
31
- firstName?: string;
32
- middleName?: string;
33
- lastName?: string;
34
- company?: string;
35
- }
36
- export interface SendContact extends BaseMessage {
37
- type: "contact";
38
- contact: Contact;
39
- }
40
- export interface SendFileByUpload extends BaseMessage {
24
+ } & BaseMessage) | ({
41
25
  type: "upload-file";
42
26
  caption?: string;
43
27
  file: {
44
28
  data: Blob | File;
45
29
  fileName: string;
46
30
  };
47
- }
48
- export interface SendFileByUrl extends BaseMessage {
31
+ } & BaseMessage) | ({
49
32
  type: "url-file";
50
33
  caption?: string;
51
34
  file: {
52
35
  url: string;
53
36
  fileName: string;
54
37
  };
55
- }
56
- export interface SendLocation extends BaseMessage {
38
+ } & BaseMessage) | ({
39
+ type: "poll";
40
+ message: string;
41
+ options: PollOption[];
42
+ multipleAnswers?: boolean;
43
+ } & BaseMessage) | ({
57
44
  type: "location";
58
45
  nameLocation?: string;
59
46
  address?: string;
60
47
  latitude: number;
61
48
  longitude: number;
49
+ } & BaseMessage) | ({
50
+ type: "contact";
51
+ contact: {
52
+ phoneContact: number;
53
+ firstName?: string;
54
+ middleName?: string;
55
+ lastName?: string;
56
+ company?: string;
57
+ };
58
+ } & BaseMessage) | {
59
+ type: "forward";
60
+ chatId: string;
61
+ chatIdFrom: string;
62
+ messages: string[];
63
+ };
64
+ /**
65
+ * Common properties shared by all message types.
66
+ */
67
+ export interface BaseMessage {
68
+ chatId: string;
69
+ quotedMessageId?: string;
70
+ }
71
+ export type SendMessageType = "text" | "upload-file" | "url-file" | "poll" | "location" | "contact" | "forward";
72
+ export interface ForwardMessagesResponse {
73
+ messages: string[];
62
74
  }
63
75
  export interface PollOption {
64
76
  optionName: string;
65
77
  }
66
- export interface SendPoll extends BaseMessage {
78
+ export type SendMessage = Extract<Message, {
79
+ type: "text";
80
+ }>;
81
+ export type SendFileByUpload = Extract<Message, {
82
+ type: "upload-file";
83
+ }>;
84
+ export type SendFileByUrl = Extract<Message, {
85
+ type: "url-file";
86
+ }>;
87
+ export type SendLocation = Extract<Message, {
88
+ type: "location";
89
+ }>;
90
+ export type SendContact = Extract<Message, {
91
+ type: "contact";
92
+ }>;
93
+ export type SendPoll = Extract<Message, {
67
94
  type: "poll";
68
- message: string;
95
+ }>;
96
+ export type ForwardMessages = Extract<Message, {
97
+ type: "forward";
98
+ }>;
99
+ export type MessageType = "textMessage" | "extendedTextMessage" | "imageMessage" | "videoMessage" | "documentMessage" | "audioMessage" | "contactMessage" | "locationMessage" | "pollMessage";
100
+ export interface ForwardableMessage {
101
+ forwardingScore: number;
102
+ isForwarded: boolean;
103
+ }
104
+ export interface MediaMessage extends ForwardableMessage {
105
+ jpegThumbnail: string;
106
+ }
107
+ export interface TextMessageData {
108
+ textMessage: string;
109
+ }
110
+ export interface ExtendedTextMessageData extends MediaMessage {
111
+ text: string;
112
+ description: string;
113
+ title: string;
114
+ }
115
+ export interface FileMessageData extends MediaMessage {
116
+ downloadUrl: string;
117
+ caption: string;
118
+ mimeType: string;
119
+ fileName: string;
120
+ }
121
+ export interface LocationMessageData extends MediaMessage {
122
+ nameLocation: string;
123
+ address: string;
124
+ latitude: number;
125
+ longitude: number;
126
+ }
127
+ export interface ContactMessageData extends ForwardableMessage {
128
+ displayName: string;
129
+ vcard: string;
130
+ }
131
+ export interface PollMessageData {
132
+ name: string;
69
133
  options: PollOption[];
70
- multipleAnswers?: boolean;
134
+ multipleAnswers: boolean;
135
+ }
136
+ type QuotedMessage = {
137
+ stanzaId: string;
138
+ participant: string;
139
+ typeMessage: MessageType;
140
+ } & ({
141
+ typeMessage: "textMessage";
142
+ textMessage: string;
143
+ } | {
144
+ typeMessage: "contactMessage";
145
+ contact: {
146
+ displayName: string;
147
+ vcard: string;
148
+ };
149
+ } | {
150
+ typeMessage: "locationMessage";
151
+ location: {
152
+ nameLocation: string;
153
+ address: string;
154
+ jpegThumbnail: string;
155
+ latitude: number;
156
+ longitude: number;
157
+ };
158
+ } | {
159
+ typeMessage: "imageMessage" | "videoMessage" | "documentMessage" | "audioMessage";
160
+ downloadUrl: string;
161
+ caption: string;
162
+ jpegThumbnail: string;
163
+ });
164
+ export interface PollVote {
165
+ optionName: string;
166
+ optionVoters: string[];
167
+ }
168
+ export interface PollUpdateMessageData {
169
+ stanzaId: string;
170
+ name: string;
171
+ votes: PollVote[];
172
+ multipleAnswers: boolean;
173
+ }
174
+ export type OutgoingMessageStatus = "sent" | "delivered" | "read" | "failed" | "noAccount" | "notInGroup" | "yellowCard";
175
+ /**
176
+ * Webhook payload received when a message status changes.
177
+ * Used to track delivery and read receipts.
178
+ */
179
+ export interface OutgoingMessageStatusWebhook {
180
+ typeWebhook: "outgoingMessageStatus";
181
+ chatId: string;
182
+ instanceData: {
183
+ idInstance: number;
184
+ wid: string;
185
+ typeInstance: string;
186
+ };
187
+ timestamp: number;
188
+ idMessage: string;
189
+ status: OutgoingMessageStatus;
190
+ description?: string;
191
+ sendByApi: boolean;
71
192
  }
72
- export type MessageType = "textMessage" | "extendedTextMessage" | "imageMessage" | "videoMessage" | "documentMessage" | "audioMessage";
73
- export interface IncomingGreenApiWebhook {
74
- typeWebhook: string;
193
+ export type WebhookMessageData = {
194
+ typeMessage: "textMessage";
195
+ textMessageData: TextMessageData;
196
+ } | {
197
+ typeMessage: "extendedTextMessage";
198
+ extendedTextMessageData: ExtendedTextMessageData;
199
+ } | {
200
+ typeMessage: "imageMessage" | "videoMessage" | "documentMessage" | "audioMessage";
201
+ fileMessageData: FileMessageData;
202
+ } | {
203
+ typeMessage: "locationMessage";
204
+ locationMessageData: LocationMessageData;
205
+ } | {
206
+ typeMessage: "contactMessage";
207
+ contactMessageData: ContactMessageData;
208
+ } | {
209
+ typeMessage: "pollMessage";
210
+ pollMessageData: PollMessageData;
211
+ } | {
212
+ typeMessage: "pollUpdateMessage";
213
+ pollMessageData: PollUpdateMessageData;
214
+ };
215
+ export interface MessageWebhook {
216
+ typeWebhook: "incomingMessageReceived" | "outgoingMessageReceived" | "outgoingAPIMessageReceived";
75
217
  instanceData: {
76
218
  idInstance: number;
77
219
  wid: string;
@@ -82,34 +224,22 @@ export interface IncomingGreenApiWebhook {
82
224
  senderData: {
83
225
  chatId: string;
84
226
  sender: string;
85
- chatName?: string;
86
- senderName?: string;
227
+ chatName: string;
228
+ senderName: string;
87
229
  senderContactName?: string;
88
230
  };
89
- messageData: {
90
- typeMessage: MessageType;
91
- textMessageData?: {
92
- textMessage: string;
93
- };
94
- extendedTextMessageData?: {
95
- text: string;
96
- description?: string;
97
- title?: string;
98
- jpegThumbnail?: string;
99
- forwardingScore?: number;
100
- isForwarded?: boolean;
101
- };
102
- fileMessageData?: {
103
- downloadUrl: string;
104
- caption?: string;
105
- jpegThumbnail?: string;
106
- mimeType: string;
107
- forwardingScore?: number;
108
- isForwarded?: boolean;
109
- fileName: string;
110
- };
231
+ messageData: WebhookMessageData & {
232
+ quotedMessage?: QuotedMessage;
111
233
  };
112
234
  }
235
+ /**
236
+ * Primary webhook types received from GREEN-API.
237
+ */
238
+ export type GreenApiWebhook = MessageWebhook | OutgoingMessageStatusWebhook;
239
+ /**
240
+ * Configuration settings for a GREEN-API instance.
241
+ * Controls webhook behavior, message handling, and other instance features.
242
+ */
113
243
  export interface Settings {
114
244
  wid?: string;
115
245
  webhookUrl?: string;
@@ -132,6 +262,9 @@ export interface Reboot {
132
262
  export interface Logout {
133
263
  isLogout: boolean;
134
264
  }
265
+ /**
266
+ * Represents an instance state in the GREEN-API system.
267
+ */
135
268
  export type InstanceState = "notAuthorized" | "authorized" | "blocked" | "starting" | "yellowCard";
136
269
  export interface StateInstance {
137
270
  stateInstance: InstanceState;
@@ -176,3 +309,4 @@ export interface BaseUser {
176
309
  id: number | bigint;
177
310
  [key: string]: any;
178
311
  }
312
+ export {};
@@ -1,2 +1,41 @@
1
- export declare function formatPhoneNumber(phone: string): string;
1
+ /**
2
+ * Utility functions for working with phone numbers, tokens, and vCards.
3
+ *
4
+ * @category Utilities
5
+ */
6
+ /**
7
+ * Formats a phone number into GREEN-API's expected format.
8
+ * Removes all non-digit characters and adds @c.us or @g.us suffix.
9
+ *
10
+ * @param phone - The phone number to format
11
+ * @param chatType - The type of a chat, can be either "group" or "private"
12
+ * @returns Formatted phone number with @c.us suffix
13
+ *
14
+ * @example
15
+ * formatPhoneNumber('+1 (234) 567-8900') // Returns '12345678900@c.us'
16
+ * formatPhoneNumber('1234567890') // Returns '1234567890@c.us'
17
+ */
18
+ export declare function formatPhoneNumber(phone: string, chatType?: "group" | "private"): string;
19
+ /**
20
+ * Generates a cryptographically secure random token.
21
+ *
22
+ * @param length - Length of the token in bytes (default: 32)
23
+ * @returns Hexadecimal string of the specified length
24
+ *
25
+ * @example
26
+ * generateRandomToken() // Returns a 64-character hex string (32 bytes)
27
+ * generateRandomToken(16) // Returns a 32-character hex string (16 bytes)
28
+ */
2
29
  export declare function generateRandomToken(length?: number): string;
30
+ /**
31
+ * Extracts a phone number from a vCard string format.
32
+ * Supports various vCard formats and phone number notations.
33
+ *
34
+ * @param vcard - The vCard string containing contact information
35
+ * @returns The extracted phone number or null if not found
36
+ *
37
+ * @example
38
+ * const vcard = `BEGIN:VCARD\nTEL:+1234567890\nEND:VCARD`;
39
+ * extractPhoneNumberFromVCard(vcard) // Returns '+1234567890'
40
+ */
41
+ export declare function extractPhoneNumberFromVCard(vcard: string): string | null;
@@ -35,11 +35,54 @@ var __importStar = (this && this.__importStar) || (function () {
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.formatPhoneNumber = formatPhoneNumber;
37
37
  exports.generateRandomToken = generateRandomToken;
38
+ exports.extractPhoneNumberFromVCard = extractPhoneNumberFromVCard;
38
39
  const crypto = __importStar(require("crypto"));
39
- function formatPhoneNumber(phone) {
40
+ /**
41
+ * Utility functions for working with phone numbers, tokens, and vCards.
42
+ *
43
+ * @category Utilities
44
+ */
45
+ /**
46
+ * Formats a phone number into GREEN-API's expected format.
47
+ * Removes all non-digit characters and adds @c.us or @g.us suffix.
48
+ *
49
+ * @param phone - The phone number to format
50
+ * @param chatType - The type of a chat, can be either "group" or "private"
51
+ * @returns Formatted phone number with @c.us suffix
52
+ *
53
+ * @example
54
+ * formatPhoneNumber('+1 (234) 567-8900') // Returns '12345678900@c.us'
55
+ * formatPhoneNumber('1234567890') // Returns '1234567890@c.us'
56
+ */
57
+ function formatPhoneNumber(phone, chatType = "private") {
40
58
  const cleaned = phone.replace(/\D/g, "");
41
- return `${cleaned}@c.us`;
59
+ return chatType === "private" ? `${cleaned}@c.us` : `${cleaned}@g.us`;
42
60
  }
61
+ /**
62
+ * Generates a cryptographically secure random token.
63
+ *
64
+ * @param length - Length of the token in bytes (default: 32)
65
+ * @returns Hexadecimal string of the specified length
66
+ *
67
+ * @example
68
+ * generateRandomToken() // Returns a 64-character hex string (32 bytes)
69
+ * generateRandomToken(16) // Returns a 32-character hex string (16 bytes)
70
+ */
43
71
  function generateRandomToken(length = 32) {
44
72
  return crypto.randomBytes(length).toString("hex");
45
73
  }
74
+ /**
75
+ * Extracts a phone number from a vCard string format.
76
+ * Supports various vCard formats and phone number notations.
77
+ *
78
+ * @param vcard - The vCard string containing contact information
79
+ * @returns The extracted phone number or null if not found
80
+ *
81
+ * @example
82
+ * const vcard = `BEGIN:VCARD\nTEL:+1234567890\nEND:VCARD`;
83
+ * extractPhoneNumberFromVCard(vcard) // Returns '+1234567890'
84
+ */
85
+ function extractPhoneNumberFromVCard(vcard) {
86
+ const phoneMatch = vcard.match(/TEL(?:;[^:]+)?:([+\d\s-]+)/);
87
+ return phoneMatch ? phoneMatch[1] : null;
88
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@green-api/greenapi-integration",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "GREEN-API Integration library",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -11,7 +11,9 @@
11
11
  "build": "tsc",
12
12
  "clean": "rimraf dist",
13
13
  "prebuild": "npm run clean",
14
- "prepare": "npm run build"
14
+ "prepare": "npm run build",
15
+ "docs": "typedoc",
16
+ "docs:watch": "typedoc --watch"
15
17
  },
16
18
  "keywords": [
17
19
  "green-api",
@@ -29,6 +31,8 @@
29
31
  "rimraf": "^6.0.1",
30
32
  "ts-jest": "^29.2.5",
31
33
  "ts-node": "^10.9.2",
34
+ "typedoc": "^0.27.6",
35
+ "typedoc-plugin-markdown": "^4.4.1",
32
36
  "typescript": "^5.7.2"
33
37
  },
34
38
  "dependencies": {