@elizaos/plugin-whatsapp 2.0.0-alpha.4 → 2.0.0-alpha.537
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 +278 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/index.js +2298 -0
- package/dist/index.js.map +26 -0
- package/dist/src/accounts.d.ts +217 -0
- package/dist/src/accounts.d.ts.map +1 -0
- package/dist/src/actions/index.d.ts +6 -0
- package/dist/src/actions/index.d.ts.map +1 -0
- package/dist/src/actions/sendMessage.d.ts +4 -0
- package/dist/src/actions/sendMessage.d.ts.map +1 -0
- package/dist/src/actions/sendReaction.d.ts +4 -0
- package/dist/src/actions/sendReaction.d.ts.map +1 -0
- package/dist/src/baileys/auth.d.ts +10 -0
- package/dist/src/baileys/auth.d.ts.map +1 -0
- package/dist/src/baileys/connection.d.ts +19 -0
- package/dist/src/baileys/connection.d.ts.map +1 -0
- package/dist/src/baileys/index.d.ts +5 -0
- package/dist/src/baileys/index.d.ts.map +1 -0
- package/dist/src/baileys/message-adapter.d.ts +14 -0
- package/dist/src/baileys/message-adapter.d.ts.map +1 -0
- package/dist/src/baileys/qr-code.d.ts +6 -0
- package/dist/src/baileys/qr-code.d.ts.map +1 -0
- package/dist/src/client.d.ts +139 -0
- package/dist/src/client.d.ts.map +1 -0
- package/dist/src/clients/baileys-client.d.ts +26 -0
- package/dist/src/clients/baileys-client.d.ts.map +1 -0
- package/dist/src/clients/factory.d.ts +6 -0
- package/dist/src/clients/factory.d.ts.map +1 -0
- package/dist/src/clients/index.d.ts +4 -0
- package/dist/src/clients/index.d.ts.map +1 -0
- package/dist/src/clients/interface.d.ts +10 -0
- package/dist/src/clients/interface.d.ts.map +1 -0
- package/dist/src/config.d.ts +144 -0
- package/dist/src/config.d.ts.map +1 -0
- package/dist/src/handlers/index.d.ts +3 -0
- package/dist/src/handlers/index.d.ts.map +1 -0
- package/dist/src/handlers/message.handler.d.ts +8 -0
- package/dist/src/handlers/message.handler.d.ts.map +1 -0
- package/dist/src/handlers/webhook.handler.d.ts +7 -0
- package/dist/src/handlers/webhook.handler.d.ts.map +1 -0
- package/dist/src/index.d.ts +62 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/normalize.d.ts +72 -0
- package/dist/src/normalize.d.ts.map +1 -0
- package/dist/src/pairing-service.d.ts +54 -0
- package/dist/src/pairing-service.d.ts.map +1 -0
- package/dist/src/runtime-service.d.ts +50 -0
- package/dist/src/runtime-service.d.ts.map +1 -0
- package/dist/src/service.d.ts +157 -0
- package/dist/src/service.d.ts.map +1 -0
- package/dist/src/setup-routes.d.ts +26 -0
- package/dist/src/setup-routes.d.ts.map +1 -0
- package/dist/src/types.d.ts +396 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/utils/config-detector.d.ts +5 -0
- package/dist/src/utils/config-detector.d.ts.map +1 -0
- package/dist/src/utils/index.d.ts +3 -0
- package/dist/src/utils/index.d.ts.map +1 -0
- package/dist/src/utils/validators.d.ts +14 -0
- package/dist/src/utils/validators.d.ts.map +1 -0
- package/package.json +84 -14
- package/dist/index.d.ts +0 -2
package/README.md
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
# plugin-whatsapp
|
|
2
|
+
|
|
3
|
+
WhatsApp plugin for ElizaOS. Supports both WhatsApp Cloud API and Baileys (QR authentication), enabling agents to send and receive messages, media, reactions, and interactive content via WhatsApp.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Text Messages**: Send and receive text messages with URL preview
|
|
8
|
+
- **Media Messages**: Send images, videos, audio, documents, and stickers
|
|
9
|
+
- **Reactions**: Send and remove emoji reactions on messages
|
|
10
|
+
- **Interactive Messages**: Send button and list messages for rich interactions
|
|
11
|
+
- **Location Messages**: Share location data with name and address
|
|
12
|
+
- **Template Messages**: Send pre-approved message templates
|
|
13
|
+
- **Webhooks**: Handle incoming messages and status updates
|
|
14
|
+
- **Baileys QR Auth**: Connect personal WhatsApp accounts with QR login + session persistence
|
|
15
|
+
- **Message Status**: Track sent, delivered, read, and failed statuses
|
|
16
|
+
- **Media Downloads**: Retrieve media URLs for incoming messages
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
### TypeScript
|
|
21
|
+
|
|
22
|
+
```bash
|
|
23
|
+
npm install @elizaos/plugin-whatsapp
|
|
24
|
+
```
|
|
25
|
+
## Configuration
|
|
26
|
+
|
|
27
|
+
### Environment Variables
|
|
28
|
+
|
|
29
|
+
| Variable | Required | Description |
|
|
30
|
+
|----------|----------|-------------|
|
|
31
|
+
| `WHATSAPP_ACCESS_TOKEN` | Yes | WhatsApp Business API access token |
|
|
32
|
+
| `WHATSAPP_PHONE_NUMBER_ID` | Yes | Phone number ID from WhatsApp Business API |
|
|
33
|
+
| `WHATSAPP_WEBHOOK_VERIFY_TOKEN` | No | Token for webhook verification |
|
|
34
|
+
| `WHATSAPP_BUSINESS_ACCOUNT_ID` | No | Business account ID |
|
|
35
|
+
| `WHATSAPP_API_VERSION` | No | Graph API version (default: v24.0) |
|
|
36
|
+
| `WHATSAPP_AUTH_METHOD` | No | `cloudapi` or `baileys` (auto-detected if omitted) |
|
|
37
|
+
| `WHATSAPP_AUTH_DIR` | No | Path for Baileys multi-file auth state |
|
|
38
|
+
| `WHATSAPP_SESSION_PATH` | No | Alternative to `WHATSAPP_AUTH_DIR` for Baileys auth state |
|
|
39
|
+
| `WHATSAPP_PRINT_QR` | No | Print QR in terminal for Baileys auth (default: true) |
|
|
40
|
+
| `WHATSAPP_DM_POLICY` | No | DM handling policy: `open`, `allowlist`, `pairing`, or `disabled` |
|
|
41
|
+
| `WHATSAPP_GROUP_POLICY` | No | Group handling policy: `open`, `allowlist`, or `disabled` |
|
|
42
|
+
| `WHATSAPP_ALLOW_FROM` | No | Comma-separated allowlist for DM senders (when DM policy is `allowlist`) |
|
|
43
|
+
| `WHATSAPP_GROUP_ALLOW_FROM` | No | Comma-separated allowlist for group senders (when group policy is `allowlist`) |
|
|
44
|
+
|
|
45
|
+
### TypeScript Configuration
|
|
46
|
+
|
|
47
|
+
The plugin self-registers `WhatsAppConnectorService` on the elizaOS runtime and reads its config from runtime settings / environment variables — no manual construction is required. Just register the default export on your character/agent:
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
import whatsappPlugin from "@elizaos/plugin-whatsapp";
|
|
51
|
+
|
|
52
|
+
export const character = {
|
|
53
|
+
// ...
|
|
54
|
+
plugins: [whatsappPlugin],
|
|
55
|
+
};
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The service picks up `WHATSAPP_ACCESS_TOKEN` + `WHATSAPP_PHONE_NUMBER_ID` (Cloud API) or `WHATSAPP_AUTH_DIR` (Baileys / QR) automatically. See the env-var table above.
|
|
59
|
+
|
|
60
|
+
To access the service at runtime (e.g. to send a message from your own code):
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import type { WhatsAppConnectorService } from "@elizaos/plugin-whatsapp";
|
|
64
|
+
|
|
65
|
+
const service = runtime.getService<WhatsAppConnectorService>("whatsapp");
|
|
66
|
+
await service?.sendMessage({ type: "text", to: "+14155552671", content: "hello" });
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Usage
|
|
70
|
+
|
|
71
|
+
### Sending Messages
|
|
72
|
+
|
|
73
|
+
The TypeScript snippets below use two variables:
|
|
74
|
+
|
|
75
|
+
- `service` — the registered `WhatsAppConnectorService`, obtained via `runtime.getService<WhatsAppConnectorService>("whatsapp")`. This is the recommended path: it routes through the same auth + policy stack the agent uses for incoming messages.
|
|
76
|
+
- `client` — the underlying low-level client (`IWhatsAppClient`), used only for advanced media APIs not exposed on the service. Construct one with the exported `ClientFactory.create({ accessToken, phoneNumberId })` (Cloud API) or `ClientFactory.create({ authDir })` (Baileys) — the concrete `WhatsAppClient` / `BaileysClient` classes are internal.
|
|
77
|
+
|
|
78
|
+
#### Text Message
|
|
79
|
+
|
|
80
|
+
**TypeScript**:
|
|
81
|
+
```typescript
|
|
82
|
+
const service = runtime.getService<WhatsAppConnectorService>("whatsapp");
|
|
83
|
+
await service?.sendMessage({ type: "text", to: "1234567890", content: "Hello, World!" });
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
**Python**:
|
|
87
|
+
|
|
88
|
+
**Rust**:
|
|
89
|
+
|
|
90
|
+
#### Image Message
|
|
91
|
+
|
|
92
|
+
**TypeScript**:
|
|
93
|
+
```typescript
|
|
94
|
+
await client.sendImage('1234567890', 'https://example.com/image.jpg', 'Caption');
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
**Python**:
|
|
98
|
+
|
|
99
|
+
**Rust**:
|
|
100
|
+
|
|
101
|
+
#### Interactive Button Message
|
|
102
|
+
|
|
103
|
+
**TypeScript**:
|
|
104
|
+
```typescript
|
|
105
|
+
await client.sendButtonMessage(
|
|
106
|
+
'1234567890',
|
|
107
|
+
'Choose an option:',
|
|
108
|
+
[
|
|
109
|
+
{ id: 'opt1', title: 'Option 1' },
|
|
110
|
+
{ id: 'opt2', title: 'Option 2' },
|
|
111
|
+
{ id: 'opt3', title: 'Option 3' },
|
|
112
|
+
],
|
|
113
|
+
'Header Text',
|
|
114
|
+
'Footer Text'
|
|
115
|
+
);
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
**Python**:
|
|
119
|
+
|
|
120
|
+
**Rust**:
|
|
121
|
+
|
|
122
|
+
### Sending Reactions
|
|
123
|
+
|
|
124
|
+
**TypeScript**:
|
|
125
|
+
```typescript
|
|
126
|
+
await client.sendReaction({
|
|
127
|
+
to: '1234567890',
|
|
128
|
+
messageId: 'wamid.xxx',
|
|
129
|
+
emoji: '👍',
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Remove reaction
|
|
133
|
+
await client.removeReaction('1234567890', 'wamid.xxx');
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
**Python**:
|
|
137
|
+
|
|
138
|
+
**Rust**:
|
|
139
|
+
|
|
140
|
+
### Handling Webhooks
|
|
141
|
+
|
|
142
|
+
**TypeScript**:
|
|
143
|
+
```typescript
|
|
144
|
+
import express from 'express';
|
|
145
|
+
|
|
146
|
+
const app = express();
|
|
147
|
+
|
|
148
|
+
const service = runtime.getService<WhatsAppConnectorService>("whatsapp");
|
|
149
|
+
|
|
150
|
+
// Verification endpoint
|
|
151
|
+
app.get('/webhook', (req, res) => {
|
|
152
|
+
const mode = String(req.query['hub.mode'] ?? '');
|
|
153
|
+
const token = String(req.query['hub.verify_token'] ?? '');
|
|
154
|
+
const challenge = String(req.query['hub.challenge'] ?? '');
|
|
155
|
+
|
|
156
|
+
const reply = service?.verifyWebhook(mode, token, challenge);
|
|
157
|
+
if (reply) {
|
|
158
|
+
res.status(200).send(reply);
|
|
159
|
+
} else {
|
|
160
|
+
res.sendStatus(403);
|
|
161
|
+
}
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// Message handling endpoint
|
|
165
|
+
app.post('/webhook', express.json(), async (req, res) => {
|
|
166
|
+
await service?.handleWebhook(req.body);
|
|
167
|
+
res.sendStatus(200);
|
|
168
|
+
});
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
**Python**:
|
|
172
|
+
|
|
173
|
+
**Rust**:
|
|
174
|
+
|
|
175
|
+
### Event Handling
|
|
176
|
+
|
|
177
|
+
**TypeScript** (using event emitter pattern):
|
|
178
|
+
```typescript
|
|
179
|
+
// Events are emitted by the webhook handler
|
|
180
|
+
webhookHandler.onMessage((message) => {
|
|
181
|
+
console.log('Message received:', message);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
webhookHandler.onStatus((status) => {
|
|
185
|
+
console.log('Status update:', status);
|
|
186
|
+
});
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
**Python**:
|
|
190
|
+
|
|
191
|
+
**Rust**:
|
|
192
|
+
|
|
193
|
+
## Actions
|
|
194
|
+
|
|
195
|
+
The plugin provides the following actions for use with ElizaOS agents:
|
|
196
|
+
|
|
197
|
+
| Action | Description |
|
|
198
|
+
|--------|-------------|
|
|
199
|
+
| `WHATSAPP_SEND_MESSAGE` | Send a text message |
|
|
200
|
+
| `WHATSAPP_SEND_REACTION` | Send a reaction to a message |
|
|
201
|
+
| `WHATSAPP_SEND_MEDIA` | Send an image, video, audio, or document |
|
|
202
|
+
| `WHATSAPP_SEND_INTERACTIVE` | Send a button or list message |
|
|
203
|
+
|
|
204
|
+
## Event Types
|
|
205
|
+
|
|
206
|
+
| Event | Description |
|
|
207
|
+
|-------|-------------|
|
|
208
|
+
| `MESSAGE_RECEIVED` | New message received |
|
|
209
|
+
| `MESSAGE_SENT` | Message was sent |
|
|
210
|
+
| `MESSAGE_DELIVERED` | Message was delivered |
|
|
211
|
+
| `MESSAGE_READ` | Message was read |
|
|
212
|
+
| `MESSAGE_FAILED` | Message delivery failed |
|
|
213
|
+
| `REACTION_RECEIVED` | Reaction received on a message |
|
|
214
|
+
| `REACTION_SENT` | Reaction was sent |
|
|
215
|
+
| `INTERACTIVE_REPLY` | User replied to interactive message |
|
|
216
|
+
| `WEBHOOK_VERIFIED` | Webhook was verified |
|
|
217
|
+
|
|
218
|
+
## Common Reactions
|
|
219
|
+
|
|
220
|
+
The plugin provides constants for common reaction emojis:
|
|
221
|
+
|
|
222
|
+
| Name | Emoji |
|
|
223
|
+
|------|-------|
|
|
224
|
+
| `THUMBS_UP` | 👍 |
|
|
225
|
+
| `THUMBS_DOWN` | 👎 |
|
|
226
|
+
| `HEART` | ❤️ |
|
|
227
|
+
| `LAUGHING` | 😂 |
|
|
228
|
+
| `SURPRISED` | 😮 |
|
|
229
|
+
| `SAD` | 😢 |
|
|
230
|
+
| `PRAYING` | 🙏 |
|
|
231
|
+
| `CLAPPING` | 👏 |
|
|
232
|
+
| `FIRE` | 🔥 |
|
|
233
|
+
| `CELEBRATION` | 🎉 |
|
|
234
|
+
|
|
235
|
+
## API Reference
|
|
236
|
+
|
|
237
|
+
### WhatsAppClient / WhatsAppService
|
|
238
|
+
|
|
239
|
+
| Method | Description |
|
|
240
|
+
|--------|-------------|
|
|
241
|
+
| `sendTextMessage(to, text)` | Send a text message |
|
|
242
|
+
| `sendImage(to, url, caption?)` | Send an image |
|
|
243
|
+
| `sendVideo(to, url, caption?)` | Send a video |
|
|
244
|
+
| `sendAudio(to, url)` | Send audio |
|
|
245
|
+
| `sendDocument(to, url, filename?, caption?)` | Send a document |
|
|
246
|
+
| `sendLocation(to, lat, lng, name?, address?)` | Send a location |
|
|
247
|
+
| `sendReaction(params)` | Send a reaction |
|
|
248
|
+
| `removeReaction(to, messageId)` | Remove a reaction |
|
|
249
|
+
| `sendButtonMessage(to, body, buttons, header?, footer?)` | Send button message |
|
|
250
|
+
| `sendListMessage(to, body, buttonText, sections, header?, footer?)` | Send list message |
|
|
251
|
+
| `markMessageAsRead(messageId)` | Mark a message as read |
|
|
252
|
+
| `getMediaUrl(mediaId)` | Get download URL for media |
|
|
253
|
+
| `verifyWebhook(token)` | Verify webhook token |
|
|
254
|
+
|
|
255
|
+
## Troubleshooting
|
|
256
|
+
|
|
257
|
+
### Common Issues
|
|
258
|
+
|
|
259
|
+
1. **Message not delivered**: Ensure the phone number is in international format without `+` prefix (e.g., `1234567890`).
|
|
260
|
+
|
|
261
|
+
2. **Webhook not verified**: Check that your `WHATSAPP_WEBHOOK_VERIFY_TOKEN` matches the token configured in the Meta Developer Portal.
|
|
262
|
+
|
|
263
|
+
3. **Media upload fails**: Ensure media URLs are publicly accessible and the file format is supported by WhatsApp.
|
|
264
|
+
|
|
265
|
+
4. **Rate limiting**: WhatsApp has rate limits on the number of messages. Implement exponential backoff for retries.
|
|
266
|
+
|
|
267
|
+
### Error Codes
|
|
268
|
+
|
|
269
|
+
| Code | Description |
|
|
270
|
+
|------|-------------|
|
|
271
|
+
| 130429 | Rate limit reached |
|
|
272
|
+
| 131000 | Something went wrong |
|
|
273
|
+
| 131030 | Invalid recipient |
|
|
274
|
+
| 131051 | Message type is not supported |
|
|
275
|
+
|
|
276
|
+
## License
|
|
277
|
+
|
|
278
|
+
MIT
|