@raphaelvserafim/client-api-whatsapp 1.0.9 → 1.3.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.
Files changed (66) hide show
  1. package/README.md +512 -297
  2. package/dist/WhatsApp.d.ts +177 -33
  3. package/dist/WhatsApp.js +146 -167
  4. package/dist/WhatsApp.js.map +1 -1
  5. package/dist/client/HttpClient.d.ts +6 -0
  6. package/dist/client/HttpClient.js +29 -0
  7. package/dist/client/HttpClient.js.map +1 -0
  8. package/dist/client/IHttpClient.d.ts +9 -0
  9. package/dist/client/IHttpClient.js +3 -0
  10. package/dist/client/IHttpClient.js.map +1 -0
  11. package/dist/errors.d.ts +5 -0
  12. package/dist/errors.js +13 -0
  13. package/dist/errors.js.map +1 -0
  14. package/dist/index.d.ts +16 -1
  15. package/dist/index.js +37 -3
  16. package/dist/index.js.map +1 -1
  17. package/dist/services/ActionService.d.ts +9 -0
  18. package/dist/services/ActionService.js +32 -0
  19. package/dist/services/ActionService.js.map +1 -0
  20. package/dist/services/BusinessService.d.ts +10 -0
  21. package/dist/services/BusinessService.js +43 -0
  22. package/dist/services/BusinessService.js.map +1 -0
  23. package/dist/services/CallService.d.ts +10 -0
  24. package/dist/services/CallService.js +38 -0
  25. package/dist/services/CallService.js.map +1 -0
  26. package/dist/services/ChatService.d.ts +19 -0
  27. package/dist/services/ChatService.js +63 -0
  28. package/dist/services/ChatService.js.map +1 -0
  29. package/dist/services/CommunityService.d.ts +35 -0
  30. package/dist/services/CommunityService.js +125 -0
  31. package/dist/services/CommunityService.js.map +1 -0
  32. package/dist/services/ContactService.d.ts +21 -0
  33. package/dist/services/ContactService.js +68 -0
  34. package/dist/services/ContactService.js.map +1 -0
  35. package/dist/services/GroupService.d.ts +37 -0
  36. package/dist/services/GroupService.js +117 -0
  37. package/dist/services/GroupService.js.map +1 -0
  38. package/dist/services/InstanceService.d.ts +29 -0
  39. package/dist/services/InstanceService.js +130 -0
  40. package/dist/services/InstanceService.js.map +1 -0
  41. package/dist/services/LabelService.d.ts +18 -0
  42. package/dist/services/LabelService.js +49 -0
  43. package/dist/services/LabelService.js.map +1 -0
  44. package/dist/services/MessageService.d.ts +60 -0
  45. package/dist/services/MessageService.js +171 -0
  46. package/dist/services/MessageService.js.map +1 -0
  47. package/dist/services/NewsletterService.d.ts +23 -0
  48. package/dist/services/NewsletterService.js +129 -0
  49. package/dist/services/NewsletterService.js.map +1 -0
  50. package/dist/services/StatusService.d.ts +14 -0
  51. package/dist/services/StatusService.js +46 -0
  52. package/dist/services/StatusService.js.map +1 -0
  53. package/dist/types/index.d.ts +206 -11
  54. package/dist/types/index.js +12 -0
  55. package/dist/types/index.js.map +1 -1
  56. package/package.json +6 -11
  57. package/.babelrc +0 -3
  58. package/dist/exemple.d.ts +0 -1
  59. package/dist/exemple.js +0 -264
  60. package/dist/exemple.js.map +0 -1
  61. package/src/WhatsApp.ts +0 -245
  62. package/src/exemple.ts +0 -312
  63. package/src/index.ts +0 -2
  64. package/src/types/index.ts +0 -190
  65. package/tsconfig.compile.json +0 -15
  66. package/tsconfig.json +0 -35
package/src/WhatsApp.ts DELETED
@@ -1,245 +0,0 @@
1
- import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
2
- import { Init, HttpMethod, WebhookBody, Routes, TypeMessage, StatusPresence, Contact, Section, InfoInstance, SendMessageRoot, Connect, Location, Buttons, Items } from './types';
3
-
4
- export class WhatsApp {
5
- private readonly server: string;
6
- private readonly key: string;
7
- private route: string;
8
- private method: HttpMethod | null = null;
9
- private body: any = null;
10
-
11
- constructor(data: Init) {
12
- this.server = data.server;
13
- this.key = data.key;
14
- }
15
-
16
- private async request(): Promise<any> {
17
- if (!this.route || !this.method) {
18
- throw new Error('Path and method must be defined before making a request.');
19
- }
20
- const options: AxiosRequestConfig = {
21
- url: `${this.server}/${this.key}/${this.route}`,
22
- method: this.method,
23
- headers: {
24
- 'Content-Type': 'application/json'
25
- }
26
- };
27
- if ((this.method === 'POST' || this.method === 'PUT') && this.body) {
28
- options.data = JSON.stringify(this.body);
29
- }
30
- try {
31
- const response = await axios(options);
32
- return response.data;
33
- } catch (error: any) {
34
- throw new Error(`HTTP Request Error: ${error.message} (status: ${error.response?.status})`);
35
- }
36
- }
37
-
38
-
39
- async connect(): Promise<Connect> {
40
- this.route = Routes.INSTANCES;
41
- this.method = HttpMethod.POST;
42
- return await this.request();
43
- }
44
-
45
- async info(): Promise<InfoInstance> {
46
- this.route = Routes.INSTANCES;
47
- this.method = HttpMethod.GET;
48
- return await this.request();
49
- }
50
-
51
- async logout(): Promise<any> {
52
- this.route = Routes.INSTANCES;
53
- this.method = HttpMethod.DELETE;
54
- return await this.request();
55
- }
56
-
57
-
58
- async setting({ markMessageRead, saveMedia, receiveStatusMessage, receivePresence }: { markMessageRead: boolean, receiveStatusMessage: boolean, receivePresence: boolean; saveMedia: boolean, }): Promise<{ status: number, message: string }> {
59
- this.route = Routes.INSTANCES + `/?markMessageRead=${markMessageRead}&saveMedia=${saveMedia}&receiveStatusMessage=${receiveStatusMessage}&receivePresence=${receivePresence}`;
60
- this.method = HttpMethod.PATCH;
61
- return await this.request();
62
- }
63
-
64
-
65
- async updateWebhook(body: WebhookBody): Promise<any> {
66
- this.route = Routes.INSTANCES;
67
- this.method = HttpMethod.PUT;
68
- this.body = body;
69
- return await this.request();
70
- }
71
-
72
-
73
- async sendMessage(
74
- data: {
75
- type: TypeMessage,
76
- body: {
77
- to: string,
78
- msgId?: string,
79
- header?: {
80
- title?: string,
81
- },
82
- status?: StatusPresence,
83
- text?: string,
84
- url?: string,
85
- caption?: string,
86
- mimetype?: string,
87
- fileName?: string,
88
- contact?: Contact,
89
- location?: Location,
90
- name?: string,
91
- options?: string[],
92
- sections?: Section[],
93
- buttons?: Buttons[],
94
- footer?: string,
95
- description?: string,
96
- title?: string,
97
- buttonText?: string,
98
- thumbnailUrl?: string,
99
- sourceUrl?: string,
100
- referenceId?: string,
101
- code?: string,
102
- key?: string,
103
- merchantName?: string,
104
- keyType?: "CNPJ" | "CPF" | "EMAIL" | "PHONE",
105
- subtotal?: string,
106
- totalAmount?: string,
107
- items?: Items[],
108
- }
109
- }, reply: boolean = false): Promise<SendMessageRoot> {
110
-
111
- if (data.type === TypeMessage.BUTTON_PIX) {
112
- const { key, keyType, merchantName, subtotal, totalAmount, items } = data.body;
113
- if (!key || !keyType || !merchantName || !subtotal || !totalAmount || !items || items.length === 0) {
114
- throw new Error("Campos obrigatórios para pagamento via PIX estão ausentes.");
115
- }
116
- }
117
-
118
- if (reply) {
119
- this.route = Routes.MESSAGES + "/" + data.body.msgId + "/" + data.type;
120
- } else {
121
- this.route = Routes.MESSAGES + "/" + data.type;
122
- }
123
-
124
- this.method = HttpMethod.POST;
125
- this.body = data.body;
126
- return await this.request();
127
- }
128
-
129
-
130
- async forwardingMessage(to: string, msgId: string) {
131
- this.route = Routes.MESSAGES + "/" + msgId + "/forwarding";
132
- this.method = HttpMethod.POST;
133
- this.body = {
134
- to,
135
- }
136
- return await this.request();
137
- }
138
-
139
- async rejectCall(id: string, from: string) {
140
- this.route = Routes.CALL + `/${id}/${from}`;
141
- this.method = HttpMethod.DELETE;
142
- return await this.request();
143
- }
144
-
145
- async pairingCode(phoneNumber: string) {
146
- this.route = Routes.INSTANCES + "/pairing-code";
147
- this.body = {
148
- phoneNumber,
149
- }
150
- this.method = HttpMethod.POST;
151
- return await this.request();
152
- }
153
-
154
- async checkRegisteredWhatsapp(number: string) {
155
- this.route = Routes.ACTIONS + "/registered?number=" + number;
156
- this.method = HttpMethod.GET;
157
- return await this.request();
158
- }
159
-
160
-
161
- async contacts() {
162
- this.route = Routes.CONTACTS;
163
- this.method = HttpMethod.GET;
164
- return await this.request();
165
- }
166
-
167
- async contactProfile(id: string) {
168
- this.route = Routes.CONTACTS + "/" + id;
169
- this.method = HttpMethod.GET;
170
- return await this.request();
171
- }
172
-
173
-
174
- async groups() {
175
- this.route = Routes.GROUPS;
176
- this.method = HttpMethod.GET;
177
- return await this.request();
178
- }
179
-
180
-
181
- async infoGroup(id: string) {
182
- this.route = Routes.GROUPS + "/" + id;
183
- this.method = HttpMethod.GET;
184
- return await this.request();
185
- }
186
-
187
- async getInviteGroup(id: string) {
188
- this.route = Routes.GROUPS + "/" + id + "/invite";
189
- this.method = HttpMethod.GET;
190
- return await this.request();
191
- }
192
-
193
-
194
-
195
- async updateGroup(id: string, name: string, description: string) {
196
- this.route = Routes.GROUPS + "/" + id;
197
- this.method = HttpMethod.PUT;
198
- this.body = {
199
- name,
200
- description
201
- }
202
- return await this.request();
203
- }
204
-
205
- async updateGroupPicture(id: string, url: string) {
206
- this.route = Routes.GROUPS + "/" + id + "/picture";
207
- this.method = HttpMethod.PUT;
208
- this.body = {
209
- url
210
- }
211
- return await this.request();
212
- }
213
-
214
- async leaveGroup(id: string) {
215
- this.route = Routes.GROUPS + "/" + id;
216
- this.method = HttpMethod.DELETE;
217
- return await this.request();
218
- }
219
-
220
- async createGroup(name: string, participants: string[]) {
221
- this.route = Routes.GROUPS;
222
- this.method = HttpMethod.POST;
223
- this.body = {
224
- name,
225
- participants
226
- }
227
- return await this.request();
228
- }
229
-
230
-
231
- async addParticipantsGroup(id: string, participants: string[]) {
232
- this.route = Routes.GROUPS + "/" + id + "/participants";
233
- this.method = HttpMethod.POST;
234
- this.body = {
235
- participants
236
- }
237
- return await this.request();
238
- }
239
-
240
- }
241
-
242
-
243
- export default WhatsApp;
244
-
245
-
package/src/exemple.ts DELETED
@@ -1,312 +0,0 @@
1
- import WhatsApp from "./WhatsApp";
2
- import { StatusPresence, TypeMessage } from "./types";
3
-
4
- const whatsapp = new WhatsApp({ server: "", key: "" });
5
-
6
- const to = "559999999999" // If you want to send it to the group = 123456789@us
7
-
8
- whatsapp.info().then((response) => {
9
- console.log(response)
10
- }).catch(console.error)
11
-
12
-
13
- whatsapp.connect().then((response) => {
14
- console.log(response)
15
- }).catch(console.error);
16
-
17
-
18
- whatsapp.logout().then((response) => {
19
- console.log(response)
20
- }).catch(console.error);
21
-
22
-
23
- whatsapp.contacts().then((response) => {
24
- console.log(response)
25
- }).catch(console.error)
26
-
27
-
28
- whatsapp.sendMessage({
29
- type: TypeMessage.PRESENCE,
30
- body: {
31
- to: to,
32
- status: StatusPresence.COMPOSING
33
- }
34
- }).then((response) => {
35
- console.log(response)
36
- }).catch(console.error)
37
-
38
-
39
- whatsapp.sendMessage({
40
- type: TypeMessage.TEXT,
41
- body: {
42
- to: to,
43
- text: "Hey"
44
- }
45
- }).then((response) => {
46
- console.log(response)
47
-
48
- }).catch(console.error)
49
-
50
- whatsapp.sendMessage({
51
- type: TypeMessage.BUTTON_PIX,
52
- body: {
53
- to: "14375223417",
54
- title: "Pizza",
55
- text: "Pizza ",
56
- referenceId: "apiwame",
57
- code: "0020",
58
- key: "23711695000115",
59
- merchantName: "CACHE SISTEMAS WEB",
60
- keyType: "CNPJ",
61
- items: [
62
- {
63
- id: "123",
64
- name: "Pizza G",
65
- price: 10,
66
- quantity: 10
67
- },
68
- ],
69
- subtotal: "90",
70
- totalAmount: "100"
71
- }
72
- }).then((response) => {
73
- console.log(response)
74
- }).catch(console.error)
75
-
76
- whatsapp.sendMessage({
77
- type: TypeMessage.BUTTON_PIX,
78
- body: {
79
- to: "14375223417",
80
- title: "Pizza",
81
- text: "Pizza ",
82
- referenceId: "apiwame",
83
- code: "0020",
84
- key: "23711695000115",
85
- merchantName: "CACHE SISTEMAS WEB",
86
- keyType: "CNPJ",
87
- items: [
88
- {
89
- id: "123",
90
- name: "Pizza G",
91
- price: 10,
92
- quantity: 10
93
- },
94
- ],
95
- subtotal: "90",
96
- totalAmount: "100"
97
- }
98
- }).then((response) => {
99
- console.log(response)
100
- }).catch(console.error)
101
-
102
-
103
- whatsapp.sendMessage({
104
- type: TypeMessage.BUTTON_REPLY,
105
- body: {
106
- to: "14375223417",
107
- header: {
108
- title: "Example Header",
109
- },
110
- text: "This is a test",
111
- footer: "choose an option",
112
- buttons: [
113
- {
114
- type: "quick_reply",
115
- id: "uniqId1",
116
- text: "Yes"
117
- },
118
- {
119
- type: "quick_reply",
120
- id: "uniqId2",
121
- text: "No"
122
- }
123
- ]
124
- }
125
- }).then((response) => {
126
- console.log(response)
127
- }).catch(console.error)
128
-
129
-
130
- whatsapp.sendMessage({
131
- type: TypeMessage.BUTTON_ACTION,
132
- body: {
133
- to: "14375223417",
134
- header: {
135
- title: "Example Header",
136
- },
137
- text: "This is a test",
138
- footer: "choose an option",
139
- buttons: [
140
- {
141
- type: "cta_copy",
142
- copy_code: "1234567890",
143
- text: "Copy barcode"
144
- },
145
- {
146
- type: "cta_url",
147
- url: "https://api-wa.me",
148
- text: "Access the website"
149
- },
150
- {
151
- type: "cta_call",
152
- phone_number: "+5566996852025",
153
- text: "Call us"
154
- }
155
- ]
156
- }
157
- }).then((response) => {
158
- console.log(response)
159
- }).catch(console.error)
160
-
161
-
162
- whatsapp.sendMessage({
163
- type: TypeMessage.MENU,
164
- body: {
165
- to: "14375223417",
166
- buttonText: "Menu",
167
- text: "string",
168
- title: "Menu",
169
- description: "Description",
170
- footer: "footer",
171
- sections: [
172
- {
173
- title: "Pizza",
174
- rows: [
175
- {
176
- title: "Pizza 01",
177
- description: "Example pizza 01",
178
- rowId: "1"
179
- }
180
- ]
181
- }
182
- ]
183
- }
184
- }).then((response) => {
185
- console.log(response)
186
- }).catch(console.error)
187
-
188
-
189
- whatsapp.sendMessage({
190
- type: TypeMessage.POLL,
191
- body: {
192
- to: "14375223417",
193
- name: "Do you like PHP?",
194
- options: [
195
- "Yes",
196
- "No"
197
- ],
198
- }
199
- }).then((response) => {
200
- console.log(response)
201
- }).catch(console.error)
202
-
203
-
204
- whatsapp.sendMessage({
205
- type: TypeMessage.TEXT,
206
- body: {
207
- msgId: "SKJH455AJKJ",
208
- to: to,
209
- text: "Hey"
210
- }
211
- }, true).then((response) => {
212
- console.log(response)
213
- }).catch(console.error)
214
-
215
-
216
-
217
- whatsapp.sendMessage({
218
- type: TypeMessage.AUDIO,
219
- body: {
220
- to: to,
221
- url: ""
222
- }
223
- }).then((response) => {
224
- console.log(response)
225
- }).catch(console.error)
226
-
227
-
228
- whatsapp.sendMessage({
229
- type: TypeMessage.IMAGE,
230
- body: {
231
- to: to,
232
- url: ""
233
- }
234
- }).then((response) => {
235
- console.log(response)
236
- }).catch(console.error)
237
-
238
-
239
- whatsapp.sendMessage({
240
- type: TypeMessage.VIDEO,
241
- body: {
242
- to: to,
243
- url: ""
244
- }
245
- }).then((response) => {
246
- console.log(response)
247
- }).catch(console.error)
248
-
249
-
250
-
251
- whatsapp.sendMessage({
252
- type: TypeMessage.DOCUMENT,
253
- body: {
254
- to: to,
255
- url: "",
256
- mimetype: "",
257
- fileName: ""
258
- }
259
- }).then((response) => {
260
- console.log(response)
261
- }).catch(console.error)
262
-
263
-
264
- whatsapp.sendMessage({
265
- type: TypeMessage.CONTACT,
266
- body: {
267
- to: to,
268
- contact: {
269
- fullName: "Raphael",
270
- phoneNumber: to,
271
- organization: "api-wa.me"
272
- }
273
- }
274
- }).then((response) => {
275
- console.log(response)
276
- }).catch(console.error)
277
-
278
-
279
- whatsapp.sendMessage({
280
- type: TypeMessage.REACTION,
281
- body: {
282
- to: to,
283
- msgId: "ASDDF872AHDURBSG",
284
- text: "🤖"
285
- }
286
- }).then((response) => {
287
- console.log(response)
288
- }).catch(console.error)
289
-
290
-
291
- whatsapp.groups().then((response) => {
292
- console.log(response)
293
- }).catch(console.error)
294
-
295
-
296
- whatsapp.infoGroup("123456789@us").then((response) => {
297
- console.log(response)
298
- }).catch(console.error)
299
-
300
-
301
-
302
- whatsapp.createGroup("Devs", [to]).then((response) => {
303
- console.log(response)
304
- }).catch(console.error)
305
-
306
-
307
- whatsapp.updateGroup("123456789@us", "Devs", "Only developers !").then((response) => {
308
- console.log(response)
309
- }).catch(console.error)
310
-
311
-
312
-
package/src/index.ts DELETED
@@ -1,2 +0,0 @@
1
- export { TypeMessage, Init, StatusPresence, Contact, Location } from './types';
2
- export { WhatsApp } from './WhatsApp';