@gymspace/evolution 1.0.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 +1273 -0
- package/dist/index.d.mts +1790 -0
- package/dist/index.d.ts +1790 -0
- package/dist/index.js +1091 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +1077 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +63 -0
- package/src/api/errors.ts +220 -0
- package/src/api/routes.ts +76 -0
- package/src/api/service.ts +141 -0
- package/src/index.ts +89 -0
- package/src/modules/chats/index.ts +253 -0
- package/src/modules/chats/schemas/archive.ts +15 -0
- package/src/modules/chats/schemas/check.ts +20 -0
- package/src/modules/chats/schemas/delete-message.ts +16 -0
- package/src/modules/chats/schemas/fetch-profile-picture.ts +13 -0
- package/src/modules/chats/schemas/find-all.ts +49 -0
- package/src/modules/chats/schemas/find-contacts.ts +17 -0
- package/src/modules/chats/schemas/find-messages.ts +55 -0
- package/src/modules/chats/schemas/find-status-message.ts +11 -0
- package/src/modules/chats/schemas/get-base64-from-media-message.ts +20 -0
- package/src/modules/chats/schemas/index.ts +33 -0
- package/src/modules/chats/schemas/mark-as-read.ts +20 -0
- package/src/modules/chats/schemas/mark-as-unread.ts +17 -0
- package/src/modules/chats/schemas/presence.ts +41 -0
- package/src/modules/chats/schemas/update-message.ts +16 -0
- package/src/modules/groups/index.ts +246 -0
- package/src/modules/groups/schemas/accept-invite-code.ts +14 -0
- package/src/modules/groups/schemas/common.ts +89 -0
- package/src/modules/groups/schemas/create.ts +15 -0
- package/src/modules/groups/schemas/fetch-invite-code.ts +13 -0
- package/src/modules/groups/schemas/find-all.ts +26 -0
- package/src/modules/groups/schemas/find-by-invite-code.ts +39 -0
- package/src/modules/groups/schemas/find-by-jid.ts +14 -0
- package/src/modules/groups/schemas/find-members.ts +16 -0
- package/src/modules/groups/schemas/index.ts +24 -0
- package/src/modules/groups/schemas/leave.ts +14 -0
- package/src/modules/groups/schemas/revoke-invite-code.ts +14 -0
- package/src/modules/groups/schemas/send-group-invite.ts +15 -0
- package/src/modules/groups/schemas/toggle-ephemeral.ts +15 -0
- package/src/modules/groups/schemas/update-description.ts +15 -0
- package/src/modules/groups/schemas/update-members.ts +18 -0
- package/src/modules/groups/schemas/update-picture.ts +15 -0
- package/src/modules/groups/schemas/update-setting.ts +16 -0
- package/src/modules/groups/schemas/update-subject.ts +15 -0
- package/src/modules/index.ts +7 -0
- package/src/modules/instance/index.ts +126 -0
- package/src/modules/instance/schemas/connect.ts +14 -0
- package/src/modules/instance/schemas/connection-state.ts +16 -0
- package/src/modules/instance/schemas/create.ts +99 -0
- package/src/modules/instance/schemas/delete.ts +21 -0
- package/src/modules/instance/schemas/fetch-all.ts +57 -0
- package/src/modules/instance/schemas/index.ts +8 -0
- package/src/modules/instance/schemas/logout.ts +22 -0
- package/src/modules/instance/schemas/restart.ts +13 -0
- package/src/modules/instance/schemas/set-presence.ts +20 -0
- package/src/modules/messages/index.ts +246 -0
- package/src/modules/messages/schemas/audio.ts +12 -0
- package/src/modules/messages/schemas/base.ts +11 -0
- package/src/modules/messages/schemas/contact.ts +70 -0
- package/src/modules/messages/schemas/document.ts +12 -0
- package/src/modules/messages/schemas/image.ts +12 -0
- package/src/modules/messages/schemas/index.ts +31 -0
- package/src/modules/messages/schemas/list.ts +36 -0
- package/src/modules/messages/schemas/location.ts +76 -0
- package/src/modules/messages/schemas/media.ts +51 -0
- package/src/modules/messages/schemas/poll.ts +87 -0
- package/src/modules/messages/schemas/reaction.ts +21 -0
- package/src/modules/messages/schemas/status.ts +18 -0
- package/src/modules/messages/schemas/sticker.ts +77 -0
- package/src/modules/messages/schemas/template.ts +39 -0
- package/src/modules/messages/schemas/text.ts +88 -0
- package/src/modules/messages/schemas/video.ts +12 -0
- package/src/modules/messages/schemas/voice.ts +94 -0
- package/src/modules/profile/index.ts +110 -0
- package/src/modules/profile/schemas/fetch-business-profile.ts +29 -0
- package/src/modules/profile/schemas/fetch-privacy-settings.ts +11 -0
- package/src/modules/profile/schemas/fetch-profile.ts +23 -0
- package/src/modules/profile/schemas/index.ts +8 -0
- package/src/modules/profile/schemas/remove-picture.ts +4 -0
- package/src/modules/profile/schemas/update-name.ts +11 -0
- package/src/modules/profile/schemas/update-picture.ts +11 -0
- package/src/modules/profile/schemas/update-privacy-settings.ts +18 -0
- package/src/modules/profile/schemas/update-status.ts +11 -0
- package/src/modules/settings/index.ts +28 -0
- package/src/modules/settings/schemas/find.ts +11 -0
- package/src/modules/settings/schemas/index.ts +2 -0
- package/src/modules/settings/schemas/set.ts +18 -0
- package/src/modules/webhook/index.ts +28 -0
- package/src/modules/webhook/schemas/find.ts +7 -0
- package/src/modules/webhook/schemas/index.ts +2 -0
- package/src/modules/webhook/schemas/set.ts +16 -0
- package/src/schemas/client.ts +23 -0
- package/src/schemas/common.ts +44 -0
- package/src/types/api.ts +17 -0
- package/src/types/events.ts +53 -0
- package/src/types/messages.ts +5 -0
- package/src/types/tags.ts +14 -0
- package/src/types/webhooks.ts +255 -0
- package/src/utils/phone-numer-from-jid.ts +9 -0
package/README.md
ADDED
|
@@ -0,0 +1,1273 @@
|
|
|
1
|
+
<h1 align="center">Evolution API SDK for Javascript</h1>
|
|
2
|
+
|
|
3
|
+
<p align="center">Unofficial SDK for the <a href="https://doc.evolution-api.com/v2" target="_blank">Evolution Whatsapp API</a> (v2).</p>
|
|
4
|
+
|
|
5
|
+
<p align="center"><em>This is a fork of <a href="https://github.com/solufyapp/evolution-sdk" target="_blank">@solufy/evolution-sdk</a> with additional features.</em></p>
|
|
6
|
+
|
|
7
|
+
<div align="center">
|
|
8
|
+
<div style="width: fit-content; display: flex; align-items: flex-start; gap: 4px;">
|
|
9
|
+
<img alt="NPM License" src="https://img.shields.io/npm/l/@gymspace/evolution">
|
|
10
|
+
<img alt="NPM Downloads" src="https://img.shields.io/npm/dw/@gymspace/evolution">
|
|
11
|
+
<a href="https://npmjs.com/package/@gymspace/evolution">
|
|
12
|
+
<img alt="NPM Version" src="https://img.shields.io/npm/v/@gymspace/evolution">
|
|
13
|
+
</a>
|
|
14
|
+
</div>
|
|
15
|
+
</div>
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
npm install @gymspace/evolution
|
|
21
|
+
// or
|
|
22
|
+
yarn add @gymspace/evolution
|
|
23
|
+
// or
|
|
24
|
+
bun add @gymspace/evolution
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Getting Started
|
|
28
|
+
|
|
29
|
+
```ts
|
|
30
|
+
import { EvolutionClient } from "@gymspace/evolution";
|
|
31
|
+
|
|
32
|
+
const client = new EvolutionClient({
|
|
33
|
+
serverUrl: "Your server url",
|
|
34
|
+
token: "Global api key or instance token",
|
|
35
|
+
instance: "Your instance", // optional
|
|
36
|
+
});
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage Examples
|
|
40
|
+
|
|
41
|
+
### Sending a Text Message
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
client.setInstance("my-instance-01");
|
|
45
|
+
|
|
46
|
+
await client.messages.sendText({
|
|
47
|
+
number: "5511999999999",
|
|
48
|
+
text: "Hello from the SDK!",
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Using Different Instances
|
|
53
|
+
|
|
54
|
+
You can override the default instance for any method call:
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
// Send message using a different instance
|
|
58
|
+
await client.messages.sendText(
|
|
59
|
+
{
|
|
60
|
+
number: "5511999999999",
|
|
61
|
+
text: "Hello from another instance!",
|
|
62
|
+
},
|
|
63
|
+
{ instance: "different-instance-name" }
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
// Check numbers on a specific instance
|
|
67
|
+
await client.chats.check(["5511999999999"], { instance: "my-instance" });
|
|
68
|
+
|
|
69
|
+
// Works with all methods across all modules
|
|
70
|
+
await client.groups.create(
|
|
71
|
+
{
|
|
72
|
+
subject: "My Group",
|
|
73
|
+
participants: ["5511999999999"],
|
|
74
|
+
},
|
|
75
|
+
{ instance: "work-instance" }
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
await client.profile.updateName(
|
|
79
|
+
{
|
|
80
|
+
name: "New Name",
|
|
81
|
+
},
|
|
82
|
+
{ instance: "personal-instance" }
|
|
83
|
+
);
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Sending an Image
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
await client.messages.sendImage({
|
|
90
|
+
number: "5511999999999",
|
|
91
|
+
image: "https://i.imgur.com/REo1ODy.png",
|
|
92
|
+
caption: "A cute cat",
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Creating a Group
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
await client.groups.create({
|
|
100
|
+
subject: "My Awesome Group",
|
|
101
|
+
participants: ["5511999999999", "5522988888888"],
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Checking if a Number has WhatsApp
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
const result = await client.chats.check(["5511999999999"]);
|
|
109
|
+
console.log(result);
|
|
110
|
+
// [{ jid: '5511999999999@s.whatsapp.net', exists: true }]
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
## API Reference
|
|
114
|
+
|
|
115
|
+
> **💡 Pro Tip:** All methods support an optional `{ instance: "instance-name" }` parameter to override the default instance.
|
|
116
|
+
|
|
117
|
+
### 🔧 Instance Management
|
|
118
|
+
|
|
119
|
+
#### Create Instance
|
|
120
|
+
|
|
121
|
+
##### Basic Instance Creation
|
|
122
|
+
|
|
123
|
+
```ts
|
|
124
|
+
await client.instance.create({
|
|
125
|
+
instanceName: "my-bot",
|
|
126
|
+
token: "optional-token",
|
|
127
|
+
});
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
##### Comprehensive Instance Creation
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
await client.instance.create({
|
|
134
|
+
instanceName: "my-advanced-bot",
|
|
135
|
+
token: "your-secure-api-token",
|
|
136
|
+
qrcode: true,
|
|
137
|
+
number: "5511999999999",
|
|
138
|
+
integration: "WHATSAPP-BAILEYS",
|
|
139
|
+
webhook: "https://your-domain.com/webhook",
|
|
140
|
+
webhook_by_events: true,
|
|
141
|
+
events: [
|
|
142
|
+
"APPLICATION_STARTUP",
|
|
143
|
+
"QRCODE_UPDATED",
|
|
144
|
+
"CONNECTION_UPDATE",
|
|
145
|
+
"MESSAGES_UPSERT",
|
|
146
|
+
"MESSAGES_UPDATE",
|
|
147
|
+
"CONTACTS_UPSERT"
|
|
148
|
+
],
|
|
149
|
+
reject_call: true,
|
|
150
|
+
msg_call: "Sorry, I can't take calls right now. Please send a message.",
|
|
151
|
+
groups_ignore: false,
|
|
152
|
+
always_online: true,
|
|
153
|
+
read_messages: true,
|
|
154
|
+
read_status: true,
|
|
155
|
+
websocket_enabled: false,
|
|
156
|
+
websocket_events: ["APPLICATION_STARTUP"],
|
|
157
|
+
rabbitmq_enabled: false,
|
|
158
|
+
rabbitmq_events: ["APPLICATION_STARTUP"],
|
|
159
|
+
sqs_enabled: false,
|
|
160
|
+
sqs_events: ["APPLICATION_STARTUP"],
|
|
161
|
+
typebot_url: "https://your-typebot-instance.com",
|
|
162
|
+
typebot: "customer-service-flow",
|
|
163
|
+
typebot_expire: 300,
|
|
164
|
+
typebot_keyword_finish: "exit",
|
|
165
|
+
typebot_delay_message: 1000,
|
|
166
|
+
typebot_unknown_message: "I didn't understand. Can you rephrase?",
|
|
167
|
+
typebot_listening_from_me: false,
|
|
168
|
+
proxy: {
|
|
169
|
+
host: "proxy.example.com",
|
|
170
|
+
port: "8080",
|
|
171
|
+
protocol: "http",
|
|
172
|
+
username: "proxy-user",
|
|
173
|
+
password: "proxy-password"
|
|
174
|
+
},
|
|
175
|
+
chatwoot_account_id: 12345,
|
|
176
|
+
chatwoot_token: "your-chatwoot-token",
|
|
177
|
+
chatwoot_url: "https://your-chatwoot-instance.com",
|
|
178
|
+
chatwoot_sign_msg: true,
|
|
179
|
+
chatwoot_reopen_conversation: true,
|
|
180
|
+
chatwoot_conversation_pending: false
|
|
181
|
+
});
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
##### Instance Creation Parameters
|
|
185
|
+
|
|
186
|
+
| Parameter | Type | Required | Default | Description |
|
|
187
|
+
|-----------|------|----------|---------|-------------|
|
|
188
|
+
| `instanceName` | string | Yes | - | Unique name for your instance |
|
|
189
|
+
| `token` | string | No | Auto-generated | API token for the instance |
|
|
190
|
+
| `qrcode` | boolean | No | true | Generate QR code automatically after creation |
|
|
191
|
+
| `number` | string | No | - | Instance owner number with country code (e.g., "5511999999999") |
|
|
192
|
+
| `integration` | enum | No | "WHATSAPP-BAILEYS" | WhatsApp engine to use. Options: `WHATSAPP-BAILEYS`, `WHATSAPP-BUSINESS` |
|
|
193
|
+
| `webhook` | string | No | - | Webhook URL to receive events |
|
|
194
|
+
| `webhook_by_events` | boolean | No | true | Enable webhook by events |
|
|
195
|
+
| `events` | enum[] | No | ["APPLICATION_STARTUP"] | Events to send to webhook |
|
|
196
|
+
| `reject_call` | boolean | No | false | Reject WhatsApp calls automatically |
|
|
197
|
+
| `msg_call` | string | No | "" | Message to send when rejecting calls |
|
|
198
|
+
| `groups_ignore` | boolean | No | true | Ignore group messages |
|
|
199
|
+
| `always_online` | boolean | No | false | Keep WhatsApp always online |
|
|
200
|
+
| `read_messages` | boolean | No | false | Send read receipts for received messages |
|
|
201
|
+
| `read_status` | boolean | No | false | Show read status for sent messages |
|
|
202
|
+
| `websocket_enabled` | boolean | No | false | Enable WebSocket connection |
|
|
203
|
+
| `websocket_events` | enum[] | No | ["APPLICATION_STARTUP"] | Events to send via WebSocket |
|
|
204
|
+
| `rabbitmq_enabled` | boolean | No | false | Enable RabbitMQ integration |
|
|
205
|
+
| `rabbitmq_events` | enum[] | No | ["APPLICATION_STARTUP"] | Events to send to RabbitMQ |
|
|
206
|
+
| `sqs_enabled` | boolean | No | false | Enable SQS integration |
|
|
207
|
+
| `sqs_events` | enum[] | No | ["APPLICATION_STARTUP"] | Events to send to SQS |
|
|
208
|
+
| `typebot_url` | string | No | - | URL for the Typebot instance |
|
|
209
|
+
| `typebot` | string | No | - | Typebot flow name |
|
|
210
|
+
| `typebot_expire` | integer | No | - | Seconds to expire Typebot session |
|
|
211
|
+
| `typebot_keyword_finish` | string | No | - | Keyword to finish the Typebot flow |
|
|
212
|
+
| `typebot_delay_message` | integer | No | - | Default delay for Typebot messages (ms) |
|
|
213
|
+
| `typebot_unknown_message` | string | No | - | Message for unrecognized input |
|
|
214
|
+
| `typebot_listening_from_me` | boolean | No | - | Whether Typebot listens to own messages |
|
|
215
|
+
| `proxy.host` | string | No | - | Proxy server host |
|
|
216
|
+
| `proxy.port` | string | No | - | Proxy server port |
|
|
217
|
+
| `proxy.protocol` | enum | No | "http" | Proxy protocol (http/https) |
|
|
218
|
+
| `proxy.username` | string | No | - | Proxy authentication username |
|
|
219
|
+
| `proxy.password` | string | No | - | Proxy authentication password |
|
|
220
|
+
| `chatwoot_account_id` | integer | No | - | Chatwoot account ID |
|
|
221
|
+
| `chatwoot_token` | string | No | - | Chatwoot authentication token |
|
|
222
|
+
| `chatwoot_url` | string | No | - | Chatwoot server URL |
|
|
223
|
+
| `chatwoot_sign_msg` | boolean | No | true | Send message signature on Chatwoot |
|
|
224
|
+
| `chatwoot_reopen_conversation` | boolean | No | true | Reopen conversation on Chatwoot |
|
|
225
|
+
| `chatwoot_conversation_pending` | boolean | No | true | Mark conversation as pending |
|
|
226
|
+
|
|
227
|
+
##### Available Events
|
|
228
|
+
|
|
229
|
+
You can configure the following events for webhooks, WebSocket, RabbitMQ, and SQS:
|
|
230
|
+
|
|
231
|
+
- `APPLICATION_STARTUP` - API startup event
|
|
232
|
+
- `QRCODE_UPDATED` - QR code update event
|
|
233
|
+
- `CONNECTION_UPDATE` - Connection status change event
|
|
234
|
+
- `MESSAGES_SET` - Initial message load event
|
|
235
|
+
- `MESSAGES_UPSERT` - New message event
|
|
236
|
+
- `MESSAGES_UPDATE` - Message status update event
|
|
237
|
+
- `MESSAGES_DELETE` - Message deletion event
|
|
238
|
+
- `SEND_MESSAGE` - Message sending event
|
|
239
|
+
- `CONTACTS_SET` - Initial contacts load event
|
|
240
|
+
- `CONTACTS_UPSERT` - New contact event
|
|
241
|
+
- `CONTACTS_UPDATE` - Contact update event
|
|
242
|
+
- `PRESENCE_UPDATE` - User presence change event
|
|
243
|
+
- `CHATS_SET` - Initial chats load event
|
|
244
|
+
- `CHATS_UPDATE` - Chat update event
|
|
245
|
+
- `CHATS_UPSERT` - New chat event
|
|
246
|
+
- `CHATS_DELETE` - Chat deletion event
|
|
247
|
+
- `GROUPS_UPSERT` - New group event
|
|
248
|
+
- `GROUPS_UPDATE` - Group update event
|
|
249
|
+
- `GROUP_PARTICIPANTS_UPDATE` - Group member change event
|
|
250
|
+
- `NEW_TOKEN` - JWT token update event
|
|
251
|
+
|
|
252
|
+
##### Response Structure
|
|
253
|
+
|
|
254
|
+
The instance creation returns a comprehensive response with the following structure:
|
|
255
|
+
|
|
256
|
+
```typescript
|
|
257
|
+
{
|
|
258
|
+
instance: {
|
|
259
|
+
instanceName: string,
|
|
260
|
+
instanceId: string,
|
|
261
|
+
webhook_wa_business: string | null,
|
|
262
|
+
access_token_wa_business: string,
|
|
263
|
+
status: "created" | "connected" | "disconnected"
|
|
264
|
+
},
|
|
265
|
+
hash: {
|
|
266
|
+
apikey: string
|
|
267
|
+
},
|
|
268
|
+
settings: {
|
|
269
|
+
reject_call: boolean,
|
|
270
|
+
msg_call: string,
|
|
271
|
+
groups_ignore: boolean,
|
|
272
|
+
always_online: boolean,
|
|
273
|
+
read_messages: boolean,
|
|
274
|
+
read_status: boolean,
|
|
275
|
+
sync_full_history: boolean
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
##### Authentication Requirements
|
|
281
|
+
|
|
282
|
+
All instance creation requests require authentication via:
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
const client = new EvolutionClient({
|
|
286
|
+
serverUrl: "https://your-evolution-server.com",
|
|
287
|
+
token: "your-global-api-key"
|
|
288
|
+
});
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
The `token` can be either:
|
|
292
|
+
- A global API key with admin privileges
|
|
293
|
+
- An instance-specific token (for existing instances)
|
|
294
|
+
|
|
295
|
+
##### Integration Examples
|
|
296
|
+
|
|
297
|
+
###### Typebot Integration
|
|
298
|
+
|
|
299
|
+
```ts
|
|
300
|
+
await client.instance.create({
|
|
301
|
+
instanceName: "typebot-instance",
|
|
302
|
+
token: "your-token",
|
|
303
|
+
webhook: "https://your-domain.com/webhook",
|
|
304
|
+
typebot_url: "https://your-typebot.com",
|
|
305
|
+
typebot: "customer-support",
|
|
306
|
+
typebot_expire: 300,
|
|
307
|
+
typebot_keyword_finish: "exit,stop,end",
|
|
308
|
+
typebot_delay_message: 1500,
|
|
309
|
+
typebot_unknown_message: "I'm sorry, I didn't understand. Can you please rephrase?",
|
|
310
|
+
typebot_listening_from_me: false
|
|
311
|
+
});
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
###### Chatwoot Integration
|
|
315
|
+
|
|
316
|
+
```ts
|
|
317
|
+
await client.instance.create({
|
|
318
|
+
instanceName: "chatwoot-instance",
|
|
319
|
+
token: "your-token",
|
|
320
|
+
webhook: "https://your-domain.com/webhook",
|
|
321
|
+
chatwoot_account_id: 12345,
|
|
322
|
+
chatwoot_token: "your-chatwoot-api-token",
|
|
323
|
+
chatwoot_url: "https://your-chatwoot-instance.com",
|
|
324
|
+
chatwoot_sign_msg: true,
|
|
325
|
+
chatwoot_reopen_conversation: true,
|
|
326
|
+
chatwoot_conversation_pending: false
|
|
327
|
+
});
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
###### RabbitMQ Integration
|
|
331
|
+
|
|
332
|
+
```ts
|
|
333
|
+
await client.instance.create({
|
|
334
|
+
instanceName: "rabbitmq-instance",
|
|
335
|
+
token: "your-token",
|
|
336
|
+
rabbitmq_enabled: true,
|
|
337
|
+
rabbitmq_events: [
|
|
338
|
+
"MESSAGES_UPSERT",
|
|
339
|
+
"CONNECTION_UPDATE",
|
|
340
|
+
"CONTACTS_UPSERT"
|
|
341
|
+
]
|
|
342
|
+
});
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
###### SQS Integration
|
|
346
|
+
|
|
347
|
+
```ts
|
|
348
|
+
await client.instance.create({
|
|
349
|
+
instanceName: "sqs-instance",
|
|
350
|
+
token: "your-token",
|
|
351
|
+
sqs_enabled: true,
|
|
352
|
+
sqs_events: [
|
|
353
|
+
"MESSAGES_UPSERT",
|
|
354
|
+
"CONNECTION_UPDATE",
|
|
355
|
+
"CONTACTS_UPSERT"
|
|
356
|
+
]
|
|
357
|
+
});
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
##### Webhook Configuration Examples
|
|
361
|
+
|
|
362
|
+
###### Basic Webhook Setup
|
|
363
|
+
|
|
364
|
+
```ts
|
|
365
|
+
await client.instance.create({
|
|
366
|
+
instanceName: "webhook-instance",
|
|
367
|
+
token: "your-token",
|
|
368
|
+
webhook: "https://your-domain.com/webhook",
|
|
369
|
+
webhook_by_events: true,
|
|
370
|
+
events: [
|
|
371
|
+
"MESSAGES_UPSERT",
|
|
372
|
+
"CONNECTION_UPDATE",
|
|
373
|
+
"CONTACTS_UPSERT"
|
|
374
|
+
]
|
|
375
|
+
});
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
###### Advanced Webhook with All Events
|
|
379
|
+
|
|
380
|
+
```ts
|
|
381
|
+
await client.instance.create({
|
|
382
|
+
instanceName: "full-webhook-instance",
|
|
383
|
+
token: "your-token",
|
|
384
|
+
webhook: "https://your-domain.com/webhook",
|
|
385
|
+
webhook_by_events: true,
|
|
386
|
+
events: [
|
|
387
|
+
"APPLICATION_STARTUP",
|
|
388
|
+
"QRCODE_UPDATED",
|
|
389
|
+
"CONNECTION_UPDATE",
|
|
390
|
+
"MESSAGES_SET",
|
|
391
|
+
"MESSAGES_UPSERT",
|
|
392
|
+
"MESSAGES_UPDATE",
|
|
393
|
+
"MESSAGES_DELETE",
|
|
394
|
+
"SEND_MESSAGE",
|
|
395
|
+
"CONTACTS_SET",
|
|
396
|
+
"CONTACTS_UPSERT",
|
|
397
|
+
"CONTACTS_UPDATE",
|
|
398
|
+
"PRESENCE_UPDATE",
|
|
399
|
+
"CHATS_SET",
|
|
400
|
+
"CHATS_UPDATE",
|
|
401
|
+
"CHATS_UPSERT",
|
|
402
|
+
"CHATS_DELETE",
|
|
403
|
+
"GROUPS_UPSERT",
|
|
404
|
+
"GROUPS_UPDATE",
|
|
405
|
+
"GROUP_PARTICIPANTS_UPDATE",
|
|
406
|
+
"NEW_TOKEN"
|
|
407
|
+
]
|
|
408
|
+
});
|
|
409
|
+
```
|
|
410
|
+
|
|
411
|
+
##### Use Case Examples
|
|
412
|
+
|
|
413
|
+
###### Customer Service Bot
|
|
414
|
+
|
|
415
|
+
```ts
|
|
416
|
+
await client.instance.create({
|
|
417
|
+
instanceName: "customer-service",
|
|
418
|
+
token: "secure-token",
|
|
419
|
+
number: "5511999999999",
|
|
420
|
+
webhook: "https://your-crm.com/webhook",
|
|
421
|
+
webhook_by_events: true,
|
|
422
|
+
events: ["MESSAGES_UPSERT", "CONTACTS_UPSERT"],
|
|
423
|
+
reject_call: true,
|
|
424
|
+
msg_call: "Please use our chat service. We don't accept calls.",
|
|
425
|
+
groups_ignore: true,
|
|
426
|
+
always_online: true,
|
|
427
|
+
read_messages: true,
|
|
428
|
+
read_status: true,
|
|
429
|
+
chatwoot_account_id: 12345,
|
|
430
|
+
chatwoot_token: "chatwoot-token",
|
|
431
|
+
chatwoot_url: "https://chatwoot.your-crm.com",
|
|
432
|
+
chatwoot_sign_msg: true,
|
|
433
|
+
chatwoot_reopen_conversation: true
|
|
434
|
+
});
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
###### Notification Bot
|
|
438
|
+
|
|
439
|
+
```ts
|
|
440
|
+
await client.instance.create({
|
|
441
|
+
instanceName: "notification-bot",
|
|
442
|
+
token: "notification-token",
|
|
443
|
+
webhook: "https://notification-service.com/webhook",
|
|
444
|
+
webhook_by_events: true,
|
|
445
|
+
events: ["CONNECTION_UPDATE", "MESSAGES_UPDATE"],
|
|
446
|
+
qrcode: true,
|
|
447
|
+
always_online: true,
|
|
448
|
+
read_messages: false,
|
|
449
|
+
read_status: true,
|
|
450
|
+
groups_ignore: false,
|
|
451
|
+
reject_call: true
|
|
452
|
+
});
|
|
453
|
+
```
|
|
454
|
+
|
|
455
|
+
###### E-commerce Bot with Typebot
|
|
456
|
+
|
|
457
|
+
```ts
|
|
458
|
+
await client.instance.create({
|
|
459
|
+
instanceName: "ecommerce-bot",
|
|
460
|
+
token: "ecommerce-token",
|
|
461
|
+
number: "5511999999999",
|
|
462
|
+
webhook: "https://ecommerce.com/webhook",
|
|
463
|
+
typebot_url: "https://typebot.ecommerce.com",
|
|
464
|
+
typebot: "shopping-assistant",
|
|
465
|
+
typebot_expire: 600,
|
|
466
|
+
typebot_keyword_finish: "checkout,exit,help",
|
|
467
|
+
typebot_delay_message: 1200,
|
|
468
|
+
typebot_unknown_message: "I'm here to help with your shopping. Can you tell me what you're looking for?",
|
|
469
|
+
always_online: true,
|
|
470
|
+
read_messages: true,
|
|
471
|
+
read_status: true,
|
|
472
|
+
reject_call: true,
|
|
473
|
+
msg_call: "For assistance, please chat with us here. We don't accept calls."
|
|
474
|
+
});
|
|
475
|
+
```
|
|
476
|
+
|
|
477
|
+
###### High-Volume Instance with Queue
|
|
478
|
+
|
|
479
|
+
```ts
|
|
480
|
+
await client.instance.create({
|
|
481
|
+
instanceName: "high-volume-bot",
|
|
482
|
+
token: "high-volume-token",
|
|
483
|
+
webhook: "https://high-volume-service.com/webhook",
|
|
484
|
+
rabbitmq_enabled: true,
|
|
485
|
+
rabbitmq_events: [
|
|
486
|
+
"MESSAGES_UPSERT",
|
|
487
|
+
"CONNECTION_UPDATE",
|
|
488
|
+
"CONTACTS_UPSERT",
|
|
489
|
+
"CHATS_UPSERT"
|
|
490
|
+
],
|
|
491
|
+
always_online: true,
|
|
492
|
+
read_messages: true,
|
|
493
|
+
read_status: false,
|
|
494
|
+
groups_ignore: false,
|
|
495
|
+
reject_call: true,
|
|
496
|
+
proxy: {
|
|
497
|
+
host: "proxy.company.com",
|
|
498
|
+
port: "8080",
|
|
499
|
+
protocol: "https",
|
|
500
|
+
username: "proxy-user",
|
|
501
|
+
password: "proxy-pass"
|
|
502
|
+
}
|
|
503
|
+
});
|
|
504
|
+
```
|
|
505
|
+
|
|
506
|
+
#### Setting the default Instance
|
|
507
|
+
|
|
508
|
+
```ts
|
|
509
|
+
await client.setInstance("InstanceName");
|
|
510
|
+
```
|
|
511
|
+
|
|
512
|
+
#### Connect to Instance
|
|
513
|
+
|
|
514
|
+
```ts
|
|
515
|
+
await client.instance.connect({ instanceName: "my-bot" });
|
|
516
|
+
```
|
|
517
|
+
|
|
518
|
+
#### Get Connection State
|
|
519
|
+
|
|
520
|
+
```ts
|
|
521
|
+
const state = await client.instance.connectionState({ instanceName: "my-bot" });
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
#### Manage Instance
|
|
525
|
+
|
|
526
|
+
```ts
|
|
527
|
+
// Logout
|
|
528
|
+
await client.instance.logout({ instanceName: "my-bot" });
|
|
529
|
+
|
|
530
|
+
// Restart
|
|
531
|
+
await client.instance.restart({ instanceName: "my-bot" });
|
|
532
|
+
|
|
533
|
+
// Delete
|
|
534
|
+
await client.instance.delete({ instanceName: "my-bot" });
|
|
535
|
+
|
|
536
|
+
// List all instances
|
|
537
|
+
const instances = await client.instance.fetchAll();
|
|
538
|
+
|
|
539
|
+
// Set presence
|
|
540
|
+
await client.instance.setPresence({
|
|
541
|
+
instanceName: "my-bot",
|
|
542
|
+
presence: "available",
|
|
543
|
+
});
|
|
544
|
+
```
|
|
545
|
+
|
|
546
|
+
---
|
|
547
|
+
|
|
548
|
+
### 💬 Chat Operations
|
|
549
|
+
|
|
550
|
+
#### Check WhatsApp Numbers
|
|
551
|
+
|
|
552
|
+
```ts
|
|
553
|
+
const result = await client.chats.check(["5511999999999", "5522888888888"]);
|
|
554
|
+
// Override instance: client.chats.check(numbers, { instance: "my-instance" })
|
|
555
|
+
```
|
|
556
|
+
|
|
557
|
+
#### Get All Chats
|
|
558
|
+
|
|
559
|
+
```ts
|
|
560
|
+
const chats = await client.chats.findAll();
|
|
561
|
+
```
|
|
562
|
+
|
|
563
|
+
#### Chat Actions
|
|
564
|
+
|
|
565
|
+
```ts
|
|
566
|
+
client.setInstance("my-instance-01");
|
|
567
|
+
// Update presence
|
|
568
|
+
await client.chats.updatePresence({
|
|
569
|
+
number: "5511999999999",
|
|
570
|
+
presence: "composing",
|
|
571
|
+
delay: 1000,
|
|
572
|
+
});
|
|
573
|
+
|
|
574
|
+
// Mark as read
|
|
575
|
+
await client.chats.markAsRead({
|
|
576
|
+
remoteJid: "5511999999999@s.whatsapp.net",
|
|
577
|
+
fromMe: false,
|
|
578
|
+
id: "message-id",
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
// Archive chat
|
|
582
|
+
await client.chats.archive({
|
|
583
|
+
remoteJid: "5511999999999@s.whatsapp.net",
|
|
584
|
+
archive: true,
|
|
585
|
+
});
|
|
586
|
+
```
|
|
587
|
+
|
|
588
|
+
#### Message Management
|
|
589
|
+
|
|
590
|
+
```ts
|
|
591
|
+
// Delete message
|
|
592
|
+
await client.chats.deleteMessage({
|
|
593
|
+
remoteJid: "5511999999999@s.whatsapp.net",
|
|
594
|
+
fromMe: true,
|
|
595
|
+
id: "message-id",
|
|
596
|
+
});
|
|
597
|
+
|
|
598
|
+
// Find messages
|
|
599
|
+
const messages = await client.chats.findMessages({
|
|
600
|
+
where: {
|
|
601
|
+
key: {
|
|
602
|
+
remoteJid: "5511999999999@s.whatsapp.net",
|
|
603
|
+
},
|
|
604
|
+
},
|
|
605
|
+
offset: 10, // limit
|
|
606
|
+
page: 1,
|
|
607
|
+
});
|
|
608
|
+
|
|
609
|
+
// Update message
|
|
610
|
+
await client.chats.updateMessage({
|
|
611
|
+
remoteJid: "5511999999999@s.whatsapp.net",
|
|
612
|
+
fromMe: true,
|
|
613
|
+
id: "message-id",
|
|
614
|
+
text: "Updated message",
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
// Get base64 from media message
|
|
618
|
+
const mediaBase64 = await client.chats.getBase64FromMediaMessage({
|
|
619
|
+
message: {
|
|
620
|
+
key: {
|
|
621
|
+
id: "message-id",
|
|
622
|
+
},
|
|
623
|
+
},
|
|
624
|
+
convertToMp4: true, // Optional: Convert video to MP4
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
// Response format:
|
|
628
|
+
// {
|
|
629
|
+
// "mediaType": "audioMessage",
|
|
630
|
+
// "fileName": "3A3B333C3EC7505284A8.oga",
|
|
631
|
+
// "size": {"fileLength": "7905"},
|
|
632
|
+
// "mimetype": "audio/mp4",
|
|
633
|
+
// "base64": "AAAAIGZ0eXBpc2....=",
|
|
634
|
+
// "buffer": null
|
|
635
|
+
// }
|
|
636
|
+
```
|
|
637
|
+
|
|
638
|
+
#### Contact & Profile
|
|
639
|
+
|
|
640
|
+
```ts
|
|
641
|
+
// Fetch profile picture
|
|
642
|
+
const profilePic = await client.chats.fetchProfilePicture({
|
|
643
|
+
number: "5511999999999",
|
|
644
|
+
});
|
|
645
|
+
|
|
646
|
+
// Find contacts
|
|
647
|
+
const contacts = await client.chats.findContacts({
|
|
648
|
+
where: { name: "John" },
|
|
649
|
+
});
|
|
650
|
+
```
|
|
651
|
+
|
|
652
|
+
---
|
|
653
|
+
|
|
654
|
+
### 📱 Messaging
|
|
655
|
+
|
|
656
|
+
#### Text Messages
|
|
657
|
+
|
|
658
|
+
```ts
|
|
659
|
+
await client.messages.sendText({
|
|
660
|
+
number: "5511999999999",
|
|
661
|
+
text: "Hello! 👋",
|
|
662
|
+
});
|
|
663
|
+
```
|
|
664
|
+
|
|
665
|
+
#### Media Messages
|
|
666
|
+
|
|
667
|
+
```ts
|
|
668
|
+
// Send image
|
|
669
|
+
await client.messages.sendImage({
|
|
670
|
+
number: "5511999999999",
|
|
671
|
+
image: "https://example.com/image.jpg",
|
|
672
|
+
caption: "Check this out!",
|
|
673
|
+
});
|
|
674
|
+
|
|
675
|
+
// Send video
|
|
676
|
+
await client.messages.sendVideo({
|
|
677
|
+
number: "5511999999999",
|
|
678
|
+
video: "https://example.com/video.mp4",
|
|
679
|
+
caption: "Amazing video!",
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
// Send document
|
|
683
|
+
await client.messages.sendDocument({
|
|
684
|
+
number: "5511999999999",
|
|
685
|
+
document: "https://example.com/document.pdf",
|
|
686
|
+
fileName: "report.pdf",
|
|
687
|
+
});
|
|
688
|
+
|
|
689
|
+
// Send audio
|
|
690
|
+
await client.messages.sendAudio({
|
|
691
|
+
number: "5511999999999",
|
|
692
|
+
audio: "https://example.com/audio.mp3",
|
|
693
|
+
});
|
|
694
|
+
```
|
|
695
|
+
|
|
696
|
+
#### Interactive Messages
|
|
697
|
+
|
|
698
|
+
```ts
|
|
699
|
+
// Send location
|
|
700
|
+
await client.messages.sendLocation({
|
|
701
|
+
number: "5511999999999",
|
|
702
|
+
latitude: -23.5505,
|
|
703
|
+
longitude: -46.6333,
|
|
704
|
+
name: "São Paulo",
|
|
705
|
+
});
|
|
706
|
+
|
|
707
|
+
// Send contact
|
|
708
|
+
await client.messages.sendContact({
|
|
709
|
+
number: "5511999999999",
|
|
710
|
+
contact: [
|
|
711
|
+
{
|
|
712
|
+
fullName: "John Doe",
|
|
713
|
+
phones: ["5511999999999"],
|
|
714
|
+
},
|
|
715
|
+
],
|
|
716
|
+
});
|
|
717
|
+
|
|
718
|
+
// Send reaction
|
|
719
|
+
await client.messages.sendReaction({
|
|
720
|
+
reactionMessage: {
|
|
721
|
+
key: { remoteJid: "5511999999999@s.whatsapp.net", id: "message-id" },
|
|
722
|
+
text: "👍",
|
|
723
|
+
},
|
|
724
|
+
});
|
|
725
|
+
```
|
|
726
|
+
|
|
727
|
+
#### Advanced Messages
|
|
728
|
+
|
|
729
|
+
```ts
|
|
730
|
+
// Send list
|
|
731
|
+
await client.messages.sendList({
|
|
732
|
+
number: "5511999999999",
|
|
733
|
+
title: "Choose an option",
|
|
734
|
+
description: "Select from the options below",
|
|
735
|
+
sections: [
|
|
736
|
+
{
|
|
737
|
+
title: "Options",
|
|
738
|
+
rows: [
|
|
739
|
+
{ title: "Option 1", description: "First option" },
|
|
740
|
+
{ title: "Option 2", description: "Second option" },
|
|
741
|
+
],
|
|
742
|
+
},
|
|
743
|
+
],
|
|
744
|
+
});
|
|
745
|
+
|
|
746
|
+
// Send template
|
|
747
|
+
await client.messages.sendTemplate({
|
|
748
|
+
number: "5511999999999",
|
|
749
|
+
template: {
|
|
750
|
+
name: "hello_world",
|
|
751
|
+
language: { code: "en_US" },
|
|
752
|
+
},
|
|
753
|
+
});
|
|
754
|
+
```
|
|
755
|
+
|
|
756
|
+
---
|
|
757
|
+
|
|
758
|
+
### 👥 Group Management
|
|
759
|
+
|
|
760
|
+
#### Group Operations
|
|
761
|
+
|
|
762
|
+
```ts
|
|
763
|
+
// Create group
|
|
764
|
+
await client.groups.create({
|
|
765
|
+
subject: "My Awesome Group",
|
|
766
|
+
participants: ["5511999999999", "5522888888888"],
|
|
767
|
+
});
|
|
768
|
+
|
|
769
|
+
// Get all groups
|
|
770
|
+
const groups = await client.groups.findAll(false); // without participants
|
|
771
|
+
const groupsWithMembers = await client.groups.findAll(true); // with participants
|
|
772
|
+
|
|
773
|
+
// Find group by invite code
|
|
774
|
+
const group = await client.groups.findByInviteCode("invite-code-here");
|
|
775
|
+
|
|
776
|
+
// Find group by JID
|
|
777
|
+
const group = await client.groups.findByJid("group-id@g.us");
|
|
778
|
+
```
|
|
779
|
+
|
|
780
|
+
#### Group Settings
|
|
781
|
+
|
|
782
|
+
```ts
|
|
783
|
+
// Update group subject
|
|
784
|
+
await client.groups.updateSubject({
|
|
785
|
+
groupJid: "group-id@g.us",
|
|
786
|
+
subject: "New Group Name",
|
|
787
|
+
});
|
|
788
|
+
|
|
789
|
+
// Update group description
|
|
790
|
+
await client.groups.updateDescription({
|
|
791
|
+
groupJid: "group-id@g.us",
|
|
792
|
+
description: "New group description",
|
|
793
|
+
});
|
|
794
|
+
|
|
795
|
+
// Update group picture
|
|
796
|
+
await client.groups.updatePicture({
|
|
797
|
+
groupJid: "group-id@g.us",
|
|
798
|
+
image: "https://example.com/group-pic.jpg",
|
|
799
|
+
});
|
|
800
|
+
```
|
|
801
|
+
|
|
802
|
+
#### Group Members
|
|
803
|
+
|
|
804
|
+
```ts
|
|
805
|
+
// Add/remove members
|
|
806
|
+
await client.groups.updateMembers({
|
|
807
|
+
groupJid: "group-id@g.us",
|
|
808
|
+
action: "add",
|
|
809
|
+
participants: ["5511999999999"],
|
|
810
|
+
});
|
|
811
|
+
|
|
812
|
+
// Get group members
|
|
813
|
+
const members = await client.groups.findMembers({
|
|
814
|
+
groupJid: "group-id@g.us",
|
|
815
|
+
});
|
|
816
|
+
|
|
817
|
+
// Leave group
|
|
818
|
+
await client.groups.leave({
|
|
819
|
+
groupJid: "group-id@g.us",
|
|
820
|
+
});
|
|
821
|
+
```
|
|
822
|
+
|
|
823
|
+
#### Group Invites
|
|
824
|
+
|
|
825
|
+
```ts
|
|
826
|
+
// Get invite code
|
|
827
|
+
const inviteCode = await client.groups.fetchInviteCode({
|
|
828
|
+
groupJid: "group-id@g.us",
|
|
829
|
+
});
|
|
830
|
+
|
|
831
|
+
// Revoke invite code
|
|
832
|
+
await client.groups.revokeInviteCode({
|
|
833
|
+
groupJid: "group-id@g.us",
|
|
834
|
+
});
|
|
835
|
+
|
|
836
|
+
// Accept invite
|
|
837
|
+
await client.groups.acceptInviteCode({
|
|
838
|
+
inviteCode: "invite-code-here",
|
|
839
|
+
});
|
|
840
|
+
```
|
|
841
|
+
|
|
842
|
+
---
|
|
843
|
+
|
|
844
|
+
### 👤 Profile Management
|
|
845
|
+
|
|
846
|
+
#### Profile Information
|
|
847
|
+
|
|
848
|
+
```ts
|
|
849
|
+
// Get profile
|
|
850
|
+
const profile = await client.profile.fetchProfile({
|
|
851
|
+
number: "5511999999999",
|
|
852
|
+
});
|
|
853
|
+
|
|
854
|
+
// Get business profile
|
|
855
|
+
const businessProfile = await client.profile.fetchBusinessProfile({
|
|
856
|
+
number: "5511999999999",
|
|
857
|
+
});
|
|
858
|
+
```
|
|
859
|
+
|
|
860
|
+
#### Update Profile
|
|
861
|
+
|
|
862
|
+
```ts
|
|
863
|
+
// Update name
|
|
864
|
+
await client.profile.updateName({
|
|
865
|
+
name: "My New Name",
|
|
866
|
+
});
|
|
867
|
+
|
|
868
|
+
// Update status
|
|
869
|
+
await client.profile.updateStatus({
|
|
870
|
+
status: "Hey there! I'm using WhatsApp",
|
|
871
|
+
});
|
|
872
|
+
|
|
873
|
+
// Update picture
|
|
874
|
+
await client.profile.updatePicture({
|
|
875
|
+
picture: "https://example.com/my-photo.jpg",
|
|
876
|
+
});
|
|
877
|
+
|
|
878
|
+
// Remove picture
|
|
879
|
+
await client.profile.removePicture();
|
|
880
|
+
```
|
|
881
|
+
|
|
882
|
+
#### Privacy Settings
|
|
883
|
+
|
|
884
|
+
```ts
|
|
885
|
+
// Get privacy settings
|
|
886
|
+
const privacy = await client.profile.fetchPrivacySettings();
|
|
887
|
+
|
|
888
|
+
// Update privacy settings
|
|
889
|
+
await client.profile.updatePrivacySettings({
|
|
890
|
+
privacySettings: {
|
|
891
|
+
readReceipts: "all",
|
|
892
|
+
profile: "contacts",
|
|
893
|
+
status: "contacts",
|
|
894
|
+
online: "all",
|
|
895
|
+
last: "contacts",
|
|
896
|
+
groupAdd: "contacts",
|
|
897
|
+
},
|
|
898
|
+
});
|
|
899
|
+
```
|
|
900
|
+
|
|
901
|
+
---
|
|
902
|
+
|
|
903
|
+
### ⚙️ Settings & Webhooks
|
|
904
|
+
|
|
905
|
+
#### Instance Settings
|
|
906
|
+
|
|
907
|
+
```ts
|
|
908
|
+
// Get settings
|
|
909
|
+
const settings = await client.settings.find();
|
|
910
|
+
|
|
911
|
+
// Update settings
|
|
912
|
+
await client.settings.set({
|
|
913
|
+
reject_call: true,
|
|
914
|
+
msg_call: "Sorry, I can't take calls right now",
|
|
915
|
+
groups_ignore: false,
|
|
916
|
+
});
|
|
917
|
+
```
|
|
918
|
+
|
|
919
|
+
#### Webhook Configuration
|
|
920
|
+
|
|
921
|
+
```ts
|
|
922
|
+
// Get webhook settings
|
|
923
|
+
const webhook = await client.webhook.find();
|
|
924
|
+
|
|
925
|
+
// Set webhook
|
|
926
|
+
await client.webhook.set({
|
|
927
|
+
url: "https://your-webhook-url.com/webhook",
|
|
928
|
+
webhook_by_events: true,
|
|
929
|
+
events: ["MESSAGES_UPSERT", "CONNECTION_UPDATE"],
|
|
930
|
+
});
|
|
931
|
+
```
|
|
932
|
+
|
|
933
|
+
---
|
|
934
|
+
|
|
935
|
+
### 🔄 Instance Override Examples
|
|
936
|
+
|
|
937
|
+
You can override the default instance for any method:
|
|
938
|
+
|
|
939
|
+
```ts
|
|
940
|
+
// Send message with different instance
|
|
941
|
+
await client.messages.sendText(
|
|
942
|
+
{
|
|
943
|
+
number: "5511999999999",
|
|
944
|
+
text: "Hello from work bot!",
|
|
945
|
+
},
|
|
946
|
+
{ instance: "work-instance" }
|
|
947
|
+
);
|
|
948
|
+
|
|
949
|
+
// Check numbers on personal instance
|
|
950
|
+
await client.chats.check(["5511999999999"], { instance: "personal-bot" });
|
|
951
|
+
|
|
952
|
+
// Create group on specific instance
|
|
953
|
+
await client.groups.create(
|
|
954
|
+
{
|
|
955
|
+
subject: "Team Meeting",
|
|
956
|
+
participants: ["5511999999999"],
|
|
957
|
+
},
|
|
958
|
+
{ instance: "team-bot" }
|
|
959
|
+
);
|
|
960
|
+
```
|
|
961
|
+
|
|
962
|
+
---
|
|
963
|
+
|
|
964
|
+
### 🔗 Webhook Integration
|
|
965
|
+
|
|
966
|
+
The SDK provides complete TypeScript support for handling webhooks from the Evolution API.
|
|
967
|
+
|
|
968
|
+
#### Basic Webhook Setup
|
|
969
|
+
|
|
970
|
+
```ts
|
|
971
|
+
import express from "express";
|
|
972
|
+
import {
|
|
973
|
+
WebhookData,
|
|
974
|
+
WebhookEvent,
|
|
975
|
+
WebhookEventSetup,
|
|
976
|
+
MessagePayload,
|
|
977
|
+
ContactPayload,
|
|
978
|
+
ConnectionUpdatePayload,
|
|
979
|
+
} from "@gymspace/evolution";
|
|
980
|
+
|
|
981
|
+
const app = express();
|
|
982
|
+
app.use(express.json());
|
|
983
|
+
|
|
984
|
+
// Webhook endpoint
|
|
985
|
+
app.post("/webhook", async (req, res) => {
|
|
986
|
+
try {
|
|
987
|
+
const webhookData: WebhookData = req.body;
|
|
988
|
+
await processWebhook(webhookData);
|
|
989
|
+
res.status(200).send("OK");
|
|
990
|
+
} catch (error) {
|
|
991
|
+
console.error("Webhook processing error:", error);
|
|
992
|
+
res.status(500).send("Error");
|
|
993
|
+
}
|
|
994
|
+
});
|
|
995
|
+
|
|
996
|
+
app.listen(3000, () => {
|
|
997
|
+
console.log("Webhook server running on port 3000");
|
|
998
|
+
});
|
|
999
|
+
```
|
|
1000
|
+
|
|
1001
|
+
#### Processing Webhook Events
|
|
1002
|
+
|
|
1003
|
+
```ts
|
|
1004
|
+
async function processWebhook(webhookData: WebhookData): Promise<void> {
|
|
1005
|
+
const { event, instance, data } = webhookData;
|
|
1006
|
+
|
|
1007
|
+
console.log(`[WEBHOOK] Event: ${event} | Instance: ${instance}`);
|
|
1008
|
+
|
|
1009
|
+
switch (event) {
|
|
1010
|
+
case WebhookEvent.MESSAGES_UPSERT:
|
|
1011
|
+
await handleNewMessage(data as MessagePayload, instance);
|
|
1012
|
+
break;
|
|
1013
|
+
|
|
1014
|
+
case WebhookEvent.MESSAGES_UPDATE:
|
|
1015
|
+
await handleMessageUpdate(data as MessagePayload, instance);
|
|
1016
|
+
break;
|
|
1017
|
+
|
|
1018
|
+
case WebhookEvent.CONNECTION_UPDATE:
|
|
1019
|
+
await handleConnectionUpdate(data as ConnectionUpdatePayload);
|
|
1020
|
+
break;
|
|
1021
|
+
|
|
1022
|
+
case WebhookEvent.CONTACTS_UPSERT:
|
|
1023
|
+
case WebhookEvent.CONTACTS_UPDATE:
|
|
1024
|
+
await handleContact(data as ContactPayload, instance);
|
|
1025
|
+
break;
|
|
1026
|
+
|
|
1027
|
+
case WebhookEvent.QRCODE_UPDATED:
|
|
1028
|
+
console.log(`[WEBHOOK] QR Code updated for instance: ${instance}`);
|
|
1029
|
+
break;
|
|
1030
|
+
|
|
1031
|
+
default:
|
|
1032
|
+
console.log(`[WEBHOOK] Unhandled event: ${event}`);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
```
|
|
1036
|
+
|
|
1037
|
+
#### Message Handling Examples
|
|
1038
|
+
|
|
1039
|
+
```ts
|
|
1040
|
+
async function handleNewMessage(
|
|
1041
|
+
messageData: MessagePayload,
|
|
1042
|
+
instance: string
|
|
1043
|
+
): Promise<void> {
|
|
1044
|
+
const { key, message, pushName } = messageData;
|
|
1045
|
+
|
|
1046
|
+
// Skip messages from groups (optional)
|
|
1047
|
+
if (key.remoteJid?.includes("@g.us")) {
|
|
1048
|
+
console.log("Skipping group message");
|
|
1049
|
+
return;
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
// Skip messages sent by bot itself
|
|
1053
|
+
if (key.fromMe) {
|
|
1054
|
+
console.log("Skipping outgoing message");
|
|
1055
|
+
return;
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
const from = key.remoteJid;
|
|
1059
|
+
const messageId = key.id;
|
|
1060
|
+
const contactName = pushName || "Unknown";
|
|
1061
|
+
|
|
1062
|
+
console.log(`Message from ${contactName} (${from}): ${messageId}`);
|
|
1063
|
+
|
|
1064
|
+
// Process different message types
|
|
1065
|
+
if (message?.conversation) {
|
|
1066
|
+
// Text message
|
|
1067
|
+
const text = message.conversation;
|
|
1068
|
+
console.log(`Text: ${text}`);
|
|
1069
|
+
|
|
1070
|
+
// Auto-reply example
|
|
1071
|
+
if (text.toLowerCase() === "hello") {
|
|
1072
|
+
await client.messages.sendText(
|
|
1073
|
+
{
|
|
1074
|
+
number: from.replace("@s.whatsapp.net", ""),
|
|
1075
|
+
text: "Hello! How can I help you?",
|
|
1076
|
+
},
|
|
1077
|
+
{ instance }
|
|
1078
|
+
);
|
|
1079
|
+
}
|
|
1080
|
+
} else if (message?.imageMessage) {
|
|
1081
|
+
// Image message
|
|
1082
|
+
console.log(`Received image from ${contactName}`);
|
|
1083
|
+
const imageUrl = message.imageMessage.url;
|
|
1084
|
+
// Process image...
|
|
1085
|
+
} else if (message?.audioMessage) {
|
|
1086
|
+
// Audio message
|
|
1087
|
+
console.log(`Received audio from ${contactName}`);
|
|
1088
|
+
// Process audio...
|
|
1089
|
+
}
|
|
1090
|
+
}
|
|
1091
|
+
|
|
1092
|
+
async function handleMessageUpdate(
|
|
1093
|
+
messageData: MessagePayload,
|
|
1094
|
+
instance: string
|
|
1095
|
+
): Promise<void> {
|
|
1096
|
+
const { key, status } = messageData;
|
|
1097
|
+
console.log(`Message ${key.id} status updated to: ${status}`);
|
|
1098
|
+
|
|
1099
|
+
// Handle read receipts, delivery confirmations, etc.
|
|
1100
|
+
switch (status) {
|
|
1101
|
+
case MessageUpdateStatus.DELIVERY_ACK:
|
|
1102
|
+
console.log("Message delivered");
|
|
1103
|
+
break;
|
|
1104
|
+
case MessageUpdateStatus.READ:
|
|
1105
|
+
console.log("Message read by recipient");
|
|
1106
|
+
break;
|
|
1107
|
+
case MessageUpdateStatus.SERVER_ACK:
|
|
1108
|
+
console.log("Message sent to server");
|
|
1109
|
+
break;
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
async function handleConnectionUpdate(
|
|
1114
|
+
connectionData: ConnectionUpdatePayload
|
|
1115
|
+
): Promise<void> {
|
|
1116
|
+
const { instance, state, statusReason } = connectionData;
|
|
1117
|
+
|
|
1118
|
+
console.log(`Instance ${instance} connection state: ${state}`);
|
|
1119
|
+
|
|
1120
|
+
switch (state) {
|
|
1121
|
+
case "open":
|
|
1122
|
+
console.log(`✅ Instance ${instance} connected successfully`);
|
|
1123
|
+
break;
|
|
1124
|
+
case "close":
|
|
1125
|
+
console.log(`❌ Instance ${instance} disconnected (${statusReason})`);
|
|
1126
|
+
break;
|
|
1127
|
+
case "connecting":
|
|
1128
|
+
console.log(`🔄 Instance ${instance} connecting...`);
|
|
1129
|
+
break;
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
|
|
1133
|
+
async function handleContact(
|
|
1134
|
+
contactData: ContactPayload,
|
|
1135
|
+
instance: string
|
|
1136
|
+
): Promise<void> {
|
|
1137
|
+
const { remoteJid, pushName, profilePicUrl } = contactData;
|
|
1138
|
+
|
|
1139
|
+
console.log(`Contact update: ${pushName} (${remoteJid})`);
|
|
1140
|
+
|
|
1141
|
+
// Store or update contact information
|
|
1142
|
+
// You might want to save this to your database
|
|
1143
|
+
}
|
|
1144
|
+
```
|
|
1145
|
+
|
|
1146
|
+
#### Advanced Webhook Processing
|
|
1147
|
+
|
|
1148
|
+
```ts
|
|
1149
|
+
class WebhookProcessor {
|
|
1150
|
+
private messageQueue: MessagePayload[] = [];
|
|
1151
|
+
private processing = false;
|
|
1152
|
+
|
|
1153
|
+
async processWebhook(webhookData: WebhookData): Promise<void> {
|
|
1154
|
+
const { event, data, instance } = webhookData;
|
|
1155
|
+
|
|
1156
|
+
// Add message to queue for batch processing
|
|
1157
|
+
if (event === WebhookEvent.MESSAGES_UPSERT) {
|
|
1158
|
+
this.messageQueue.push(data as MessagePayload);
|
|
1159
|
+
this.processQueue();
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
|
|
1163
|
+
private async processQueue(): Promise<void> {
|
|
1164
|
+
if (this.processing || this.messageQueue.length === 0) return;
|
|
1165
|
+
|
|
1166
|
+
this.processing = true;
|
|
1167
|
+
|
|
1168
|
+
try {
|
|
1169
|
+
// Process messages in batches
|
|
1170
|
+
const batch = this.messageQueue.splice(0, 10);
|
|
1171
|
+
|
|
1172
|
+
for (const message of batch) {
|
|
1173
|
+
await this.processMessage(message);
|
|
1174
|
+
// Add delay to avoid rate limiting
|
|
1175
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
1176
|
+
}
|
|
1177
|
+
} finally {
|
|
1178
|
+
this.processing = false;
|
|
1179
|
+
|
|
1180
|
+
// Continue processing if more messages arrived
|
|
1181
|
+
if (this.messageQueue.length > 0) {
|
|
1182
|
+
setTimeout(() => this.processQueue(), 1000);
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
private async processMessage(message: MessagePayload): Promise<void> {
|
|
1188
|
+
// Your message processing logic here
|
|
1189
|
+
console.log(`Processing message: ${message.key.id}`);
|
|
1190
|
+
}
|
|
1191
|
+
}
|
|
1192
|
+
```
|
|
1193
|
+
|
|
1194
|
+
#### Available Webhook Events
|
|
1195
|
+
|
|
1196
|
+
The SDK supports all Evolution API webhook events:
|
|
1197
|
+
|
|
1198
|
+
- `WebhookEvent.APPLICATION_STARTUP` - API startup
|
|
1199
|
+
- `WebhookEvent.QRCODE_UPDATED` - QR code updates
|
|
1200
|
+
- `WebhookEvent.CONNECTION_UPDATE` - Connection status changes
|
|
1201
|
+
- `WebhookEvent.MESSAGES_SET` - Initial message load
|
|
1202
|
+
- `WebhookEvent.MESSAGES_UPSERT` - New messages
|
|
1203
|
+
- `WebhookEvent.MESSAGES_UPDATE` - Message status updates
|
|
1204
|
+
- `WebhookEvent.MESSAGES_DELETE` - Message deletions
|
|
1205
|
+
- `WebhookEvent.SEND_MESSAGE` - Message sending events
|
|
1206
|
+
- `WebhookEvent.CONTACTS_SET` - Initial contacts load
|
|
1207
|
+
- `WebhookEvent.CONTACTS_UPSERT` - New contacts
|
|
1208
|
+
- `WebhookEvent.CONTACTS_UPDATE` - Contact updates
|
|
1209
|
+
- `WebhookEvent.PRESENCE_UPDATE` - User presence changes
|
|
1210
|
+
- `WebhookEvent.CHATS_SET` - Initial chats load
|
|
1211
|
+
- `WebhookEvent.CHATS_UPDATE` - Chat updates
|
|
1212
|
+
- `WebhookEvent.CHATS_UPSERT` - New chats
|
|
1213
|
+
- `WebhookEvent.CHATS_DELETE` - Chat deletions
|
|
1214
|
+
- `WebhookEvent.GROUPS_UPSERT` - New groups
|
|
1215
|
+
- `WebhookEvent.GROUPS_UPDATE` - Group updates
|
|
1216
|
+
- `WebhookEvent.GROUP_PARTICIPANTS_UPDATE` - Group member changes
|
|
1217
|
+
- `WebhookEvent.NEW_TOKEN` - JWT token updates
|
|
1218
|
+
|
|
1219
|
+
#### Available Webhook Events to Setup
|
|
1220
|
+
|
|
1221
|
+
To setup the WebHook, the constants are these ones:
|
|
1222
|
+
|
|
1223
|
+
- `WebhookEventSetup.APPLICATION_STARTUP` - API startup
|
|
1224
|
+
- `WebhookEventSetup.QRCODE_UPDATED` - QR code updates
|
|
1225
|
+
- `WebhookEventSetup.CONNECTION_UPDATE` - Connection status changes
|
|
1226
|
+
- `WebhookEventSetup.MESSAGES_SET` - Initial message load
|
|
1227
|
+
- `WebhookEventSetup.MESSAGES_UPSERT` - New messages
|
|
1228
|
+
- `WebhookEventSetup.MESSAGES_UPDATE` - Message status updates
|
|
1229
|
+
- `WebhookEventSetup.MESSAGES_DELETE` - Message deletions
|
|
1230
|
+
- `WebhookEventSetup.SEND_MESSAGE` - Message sending events
|
|
1231
|
+
- `WebhookEventSetup.CONTACTS_SET` - Initial contacts load
|
|
1232
|
+
- `WebhookEventSetup.CONTACTS_UPSERT` - New contacts
|
|
1233
|
+
- `WebhookEventSetup.CONTACTS_UPDATE` - Contact updates
|
|
1234
|
+
- `WebhookEventSetup.PRESENCE_UPDATE` - User presence changes
|
|
1235
|
+
- `WebhookEventSetup.CHATS_SET` - Initial chats load
|
|
1236
|
+
- `WebhookEventSetup.CHATS_UPDATE` - Chat updates
|
|
1237
|
+
- `WebhookEventSetup.CHATS_UPSERT` - New chats
|
|
1238
|
+
- `WebhookEventSetup.CHATS_DELETE` - Chat deletions
|
|
1239
|
+
- `WebhookEventSetup.GROUPS_UPSERT` - New groups
|
|
1240
|
+
- `WebhookEventSetup.GROUPS_UPDATE` - Group updates
|
|
1241
|
+
- `WebhookEventSetup.GROUP_PARTICIPANTS_UPDATE` - Group member changes
|
|
1242
|
+
- `WebhookEventSetup.NEW_TOKEN` - JWT token updates
|
|
1243
|
+
|
|
1244
|
+
#### Configure Webhook URL
|
|
1245
|
+
|
|
1246
|
+
Don't forget to configure your webhook URL in the Evolution API:
|
|
1247
|
+
|
|
1248
|
+
```ts
|
|
1249
|
+
// Set your webhook endpoint
|
|
1250
|
+
await client.webhook.set({
|
|
1251
|
+
url: "https://your-domain.com/webhook",
|
|
1252
|
+
webhook_by_events: true,
|
|
1253
|
+
events: [
|
|
1254
|
+
WebhookEventSetup.MESSAGES_UPSERT,
|
|
1255
|
+
WebhookEventSetup.MESSAGES_UPDATE,
|
|
1256
|
+
WebhookEventSetup.CONNECTION_UPDATE,
|
|
1257
|
+
WebhookEventSetup.CONTACTS_UPSERT,
|
|
1258
|
+
],
|
|
1259
|
+
});
|
|
1260
|
+
```
|
|
1261
|
+
|
|
1262
|
+
## API Documentation
|
|
1263
|
+
|
|
1264
|
+
Check the [official API documentation](https://doc.evolution-api.com/v2) for more information about their service.
|
|
1265
|
+
|
|
1266
|
+
## Contributing
|
|
1267
|
+
|
|
1268
|
+
Feel free to contribute with suggestions or bug reports at our [GitHub repository](https://github.com/gusnips/evolution-api-sdk).
|
|
1269
|
+
|
|
1270
|
+
## Authors
|
|
1271
|
+
|
|
1272
|
+
- [@joaotonaco](https://github.com/joaotonaco) (Original author)
|
|
1273
|
+
- [@gusnips](https://github.com/gusnips)
|