@elizaos/plugin-whatsapp 0.1.7-alpha.2 → 0.1.7
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 +220 -0
- package/dist/{index.mjs → index.js} +1 -1
- package/package.json +40 -25
- package/.turbo/turbo-build.log +0 -17
- package/Readme.md +0 -154
- package/eslint.config.mjs +0 -9
- package/src/client.ts +0 -38
- package/src/handlers/index.ts +0 -2
- package/src/handlers/message.handler.ts +0 -20
- package/src/handlers/webhook.handler.ts +0 -45
- package/src/index.ts +0 -36
- package/src/types.ts +0 -58
- package/src/utils/index.ts +0 -1
- package/src/utils/validators.ts +0 -44
- package/tsconfig.json +0 -20
- package/tsup.config.ts +0 -19
- /package/dist/{index.d.mts → index.d.ts} +0 -0
- /package/dist/{index.mjs.map → index.js.map} +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
# @elizaos/plugin-whatsapp
|
|
2
|
+
|
|
3
|
+
A plugin for integrating WhatsApp Cloud API with your application, providing comprehensive messaging capabilities and webhook handling.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This plugin provides functionality to:
|
|
8
|
+
- Send text and template messages via WhatsApp
|
|
9
|
+
- Handle incoming webhook events
|
|
10
|
+
- Manage message status updates
|
|
11
|
+
- Process message delivery notifications
|
|
12
|
+
- Handle authentication and session management
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @elizaos/plugin-whatsapp
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Configuration
|
|
21
|
+
|
|
22
|
+
The plugin requires the following environment variables:
|
|
23
|
+
|
|
24
|
+
```env
|
|
25
|
+
WHATSAPP_ACCESS_TOKEN=your_access_token # Required: WhatsApp Cloud API access token
|
|
26
|
+
WHATSAPP_PHONE_NUMBER_ID=your_phone_number_id # Required: WhatsApp business phone number ID
|
|
27
|
+
WHATSAPP_WEBHOOK_TOKEN=your_webhook_token # Optional: Webhook verification token
|
|
28
|
+
WHATSAPP_BUSINESS_ID=your_business_id # Optional: Business account ID
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
### Basic Setup
|
|
34
|
+
|
|
35
|
+
```typescript
|
|
36
|
+
import { WhatsAppPlugin } from "@elizaos/plugin-whatsapp";
|
|
37
|
+
|
|
38
|
+
const whatsappPlugin = new WhatsAppPlugin({
|
|
39
|
+
accessToken: 'your_access_token',
|
|
40
|
+
phoneNumberId: 'your_phone_number_id',
|
|
41
|
+
webhookVerifyToken: 'your_webhook_verify_token',
|
|
42
|
+
businessAccountId: 'your_business_account_id'
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Sending Messages
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
// Send a text message
|
|
50
|
+
await whatsappPlugin.sendMessage({
|
|
51
|
+
type: 'text',
|
|
52
|
+
to: '1234567890',
|
|
53
|
+
content: 'Hello from WhatsApp!'
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// Send a template message
|
|
57
|
+
await whatsappPlugin.sendMessage({
|
|
58
|
+
type: 'template',
|
|
59
|
+
to: '1234567890',
|
|
60
|
+
content: {
|
|
61
|
+
name: 'hello_world',
|
|
62
|
+
language: {
|
|
63
|
+
code: 'en'
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Handling Webhooks
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
// Verify webhook
|
|
73
|
+
app.get('/webhook', (req, res) => {
|
|
74
|
+
const verified = await whatsappPlugin.verifyWebhook(req.query['hub.verify_token']);
|
|
75
|
+
if (verified) {
|
|
76
|
+
res.send(req.query['hub.challenge']);
|
|
77
|
+
} else {
|
|
78
|
+
res.sendStatus(403);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Handle webhook events
|
|
83
|
+
app.post('/webhook', (req, res) => {
|
|
84
|
+
await whatsappPlugin.handleWebhook(req.body);
|
|
85
|
+
res.sendStatus(200);
|
|
86
|
+
});
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Features
|
|
90
|
+
|
|
91
|
+
- Send text messages
|
|
92
|
+
- Send template messages
|
|
93
|
+
- Webhook verification
|
|
94
|
+
- Webhook event handling
|
|
95
|
+
- Message status updates
|
|
96
|
+
|
|
97
|
+
## Error Handling
|
|
98
|
+
|
|
99
|
+
The plugin throws errors in the following cases:
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
try {
|
|
103
|
+
await whatsappPlugin.sendMessage({
|
|
104
|
+
type: 'text',
|
|
105
|
+
to: '1234567890',
|
|
106
|
+
content: 'Hello!'
|
|
107
|
+
});
|
|
108
|
+
} catch (error) {
|
|
109
|
+
console.error('Failed to send message:', error.message);
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Common error cases:
|
|
114
|
+
- Invalid configuration
|
|
115
|
+
- Failed message sending
|
|
116
|
+
- Webhook verification failure
|
|
117
|
+
- Invalid webhook payload
|
|
118
|
+
|
|
119
|
+
## Best Practices
|
|
120
|
+
|
|
121
|
+
1. Always validate phone numbers before sending messages
|
|
122
|
+
2. Use template messages for first-time messages to users
|
|
123
|
+
3. Store message IDs for tracking delivery status
|
|
124
|
+
4. Implement proper error handling
|
|
125
|
+
5. Set up webhook retry mechanisms
|
|
126
|
+
6. Keep your access tokens secure
|
|
127
|
+
|
|
128
|
+
## API Reference
|
|
129
|
+
|
|
130
|
+
### Core Interfaces
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
interface WhatsAppConfig {
|
|
134
|
+
accessToken: string;
|
|
135
|
+
phoneNumberId: string;
|
|
136
|
+
webhookVerifyToken?: string;
|
|
137
|
+
businessAccountId?: string;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
interface WhatsAppMessage {
|
|
141
|
+
type: 'text' | 'template';
|
|
142
|
+
to: string;
|
|
143
|
+
content: string | WhatsAppTemplate;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
interface WhatsAppTemplate {
|
|
147
|
+
name: string;
|
|
148
|
+
language: {
|
|
149
|
+
code: string;
|
|
150
|
+
};
|
|
151
|
+
components?: Array<{
|
|
152
|
+
type: string;
|
|
153
|
+
parameters: Array<{
|
|
154
|
+
type: string;
|
|
155
|
+
text?: string;
|
|
156
|
+
}>;
|
|
157
|
+
}>;
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
### Plugin Methods
|
|
162
|
+
|
|
163
|
+
- `sendMessage`: Send WhatsApp messages
|
|
164
|
+
- `handleWebhook`: Process incoming webhook events
|
|
165
|
+
- `verifyWebhook`: Verify webhook authenticity
|
|
166
|
+
- Message and status handlers
|
|
167
|
+
|
|
168
|
+
## Development
|
|
169
|
+
|
|
170
|
+
### Building
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
npm run build
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### Testing
|
|
177
|
+
|
|
178
|
+
```bash
|
|
179
|
+
npm run test
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### Linting
|
|
183
|
+
|
|
184
|
+
```bash
|
|
185
|
+
npm run lint
|
|
186
|
+
```
|
|
187
|
+
|
|
188
|
+
## Security Best Practices
|
|
189
|
+
|
|
190
|
+
- Store credentials securely using environment variables
|
|
191
|
+
- Validate all phone numbers before sending messages
|
|
192
|
+
- Use template messages for first-time contacts
|
|
193
|
+
- Implement proper error handling
|
|
194
|
+
- Keep dependencies updated
|
|
195
|
+
- Monitor API usage and rate limits
|
|
196
|
+
- Use HTTPS for all API communication
|
|
197
|
+
|
|
198
|
+
## Contributing
|
|
199
|
+
|
|
200
|
+
Contributions are welcome! Please see the [CONTRIBUTING.md](CONTRIBUTING.md) file for more information.
|
|
201
|
+
|
|
202
|
+
## Credits
|
|
203
|
+
|
|
204
|
+
This plugin integrates with and builds upon several key technologies:
|
|
205
|
+
|
|
206
|
+
- [WhatsApp Cloud API](https://developers.facebook.com/docs/whatsapp/cloud-api): Meta's official WhatsApp Business Platform
|
|
207
|
+
- [Axios](https://axios-http.com/): Promise-based HTTP client for API requests
|
|
208
|
+
- [Meta for Developers](https://developers.facebook.com/): Meta's developer platform and tools
|
|
209
|
+
|
|
210
|
+
Special thanks to:
|
|
211
|
+
- The Eliza community for their contributions and feedback
|
|
212
|
+
|
|
213
|
+
For more information about WhatsApp Cloud API and its capabilities, visit:
|
|
214
|
+
- [WhatsApp Business Platform Documentation](https://developers.facebook.com/docs/whatsapp/cloud-api/overview)
|
|
215
|
+
- [Meta for Developers Blog](https://developers.facebook.com/blog/)
|
|
216
|
+
- [WhatsApp Business API GitHub](https://github.com/WhatsApp/WhatsApp-Business-API-Setup-Scripts)
|
|
217
|
+
|
|
218
|
+
## License
|
|
219
|
+
|
|
220
|
+
This plugin is part of the Eliza project. See the main project repository for license information.
|
package/package.json
CHANGED
|
@@ -1,27 +1,42 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
"
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
2
|
+
"name": "@elizaos/plugin-whatsapp",
|
|
3
|
+
"version": "0.1.7",
|
|
4
|
+
"description": "WhatsApp Cloud API plugin",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
"./package.json": "./package.json",
|
|
11
|
+
".": {
|
|
12
|
+
"import": {
|
|
13
|
+
"@elizaos/source": "./src/index.ts",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsup --format esm --dts",
|
|
24
|
+
"dev": "tsup --format esm --dts --watch",
|
|
25
|
+
"test": "jest",
|
|
26
|
+
"lint": "eslint --fix --cache ."
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@elizaos/core": "0.1.7",
|
|
30
|
+
"axios": "1.7.8"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"@types/jest": "29.5.14",
|
|
34
|
+
"@types/node": "20.17.9",
|
|
35
|
+
"@typescript-eslint/eslint-plugin": "8.16.0",
|
|
36
|
+
"@typescript-eslint/parser": "8.16.0",
|
|
37
|
+
"jest": "29.7.0",
|
|
38
|
+
"ts-jest": "29.2.5",
|
|
39
|
+
"typescript": "5.6.3"
|
|
40
|
+
},
|
|
41
|
+
"gitHead": "e15421524dde4f2778b529effb212eebea8c98b6"
|
|
27
42
|
}
|
package/.turbo/turbo-build.log
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @elizaos/plugin-whatsapp@0.1.7-alpha.2 build /home/runner/work/eliza/eliza/packages/plugin-whatsapp
|
|
3
|
-
> tsup --format esm --dts
|
|
4
|
-
|
|
5
|
-
[34mCLI[39m Building entry: src/index.ts
|
|
6
|
-
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
-
[34mCLI[39m tsup v8.3.5
|
|
8
|
-
[34mCLI[39m Using tsup config: /home/runner/work/eliza/eliza/packages/plugin-whatsapp/tsup.config.ts
|
|
9
|
-
[34mCLI[39m Target: esnext
|
|
10
|
-
[34mCLI[39m Cleaning output folder
|
|
11
|
-
[34mESM[39m Build start
|
|
12
|
-
[32mESM[39m [1mdist/index.mjs [22m[32m3.06 KB[39m
|
|
13
|
-
[32mESM[39m [1mdist/index.mjs.map [22m[32m7.09 KB[39m
|
|
14
|
-
[32mESM[39m ⚡️ Build success in 31ms
|
|
15
|
-
[34mDTS[39m Build start
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in 6502ms
|
|
17
|
-
[32mDTS[39m [1mdist/index.d.mts [22m[32m1.90 KB[39m
|
package/Readme.md
DELETED
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
# WhatsApp Cloud API Plugin
|
|
2
|
-
|
|
3
|
-
A plugin for integrating WhatsApp Cloud API with your application.
|
|
4
|
-
|
|
5
|
-
## Installation
|
|
6
|
-
|
|
7
|
-
</file>
|
|
8
|
-
|
|
9
|
-
npm install @eliza/plugin-whatsapp
|
|
10
|
-
|
|
11
|
-
## Configuration
|
|
12
|
-
|
|
13
|
-
typescript
|
|
14
|
-
import { WhatsAppPlugin } from '@eliza/plugin-whatsapp';
|
|
15
|
-
const whatsappPlugin = new WhatsAppPlugin({
|
|
16
|
-
accessToken: 'your_access_token',
|
|
17
|
-
phoneNumberId: 'your_phone_number_id',
|
|
18
|
-
webhookVerifyToken: 'your_webhook_verify_token',
|
|
19
|
-
businessAccountId: 'your_business_account_id'
|
|
20
|
-
});
|
|
21
|
-
|
|
22
|
-
## Usage
|
|
23
|
-
|
|
24
|
-
### Sending Messages
|
|
25
|
-
|
|
26
|
-
typescript
|
|
27
|
-
// Send a text message
|
|
28
|
-
await whatsappPlugin.sendMessage({
|
|
29
|
-
type: 'text',
|
|
30
|
-
to: '1234567890',
|
|
31
|
-
content: 'Hello from WhatsApp!'
|
|
32
|
-
});
|
|
33
|
-
// Send a template message
|
|
34
|
-
await whatsappPlugin.sendMessage({
|
|
35
|
-
type: 'template',
|
|
36
|
-
to: '1234567890',
|
|
37
|
-
content: {
|
|
38
|
-
name: 'hello_world',
|
|
39
|
-
language: {
|
|
40
|
-
code: 'en'
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
### Handling Webhooks
|
|
46
|
-
|
|
47
|
-
typescript
|
|
48
|
-
// Verify webhook
|
|
49
|
-
app.get('/webhook', (req, res) => {
|
|
50
|
-
const verified = await whatsappPlugin.verifyWebhook(req.query['hub.verify_token']);
|
|
51
|
-
if (verified) {
|
|
52
|
-
res.send(req.query['hub.challenge']);
|
|
53
|
-
} else {
|
|
54
|
-
res.sendStatus(403);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
|
-
// Handle webhook events
|
|
58
|
-
app.post('/webhook', (req, res) => {
|
|
59
|
-
await whatsappPlugin.handleWebhook(req.body);
|
|
60
|
-
res.sendStatus(200);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
## Features
|
|
64
|
-
|
|
65
|
-
- Send text messages
|
|
66
|
-
- Send template messages
|
|
67
|
-
- Webhook verification
|
|
68
|
-
- Webhook event handling
|
|
69
|
-
- Message status updates
|
|
70
|
-
|
|
71
|
-
## API Reference
|
|
72
|
-
|
|
73
|
-
### WhatsAppPlugin
|
|
74
|
-
|
|
75
|
-
#### Constructor
|
|
76
|
-
|
|
77
|
-
- `config: WhatsAppConfig` - Configuration object for the plugin
|
|
78
|
-
|
|
79
|
-
#### Methods
|
|
80
|
-
|
|
81
|
-
- `sendMessage(message: WhatsAppMessage): Promise<any>` - Send a WhatsApp message
|
|
82
|
-
- `handleWebhook(event: WhatsAppWebhookEvent): Promise<void>` - Process incoming webhook events
|
|
83
|
-
- `verifyWebhook(token: string): Promise<boolean>` - Verify webhook token
|
|
84
|
-
|
|
85
|
-
### Types
|
|
86
|
-
|
|
87
|
-
typescript
|
|
88
|
-
interface WhatsAppConfig {
|
|
89
|
-
accessToken: string;
|
|
90
|
-
phoneNumberId: string;
|
|
91
|
-
webhookVerifyToken?: string;
|
|
92
|
-
businessAccountId?: string;
|
|
93
|
-
}
|
|
94
|
-
interface WhatsAppMessage {
|
|
95
|
-
type: 'text' | 'template';
|
|
96
|
-
to: string;
|
|
97
|
-
content: string | WhatsAppTemplate;
|
|
98
|
-
}
|
|
99
|
-
interface WhatsAppTemplate {
|
|
100
|
-
name: string;
|
|
101
|
-
language: {
|
|
102
|
-
code: string;
|
|
103
|
-
};
|
|
104
|
-
components?: Array<{
|
|
105
|
-
type: string;
|
|
106
|
-
parameters: Array<{
|
|
107
|
-
type: string;
|
|
108
|
-
text?: string;
|
|
109
|
-
}>;
|
|
110
|
-
}>;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
## Error Handling
|
|
114
|
-
|
|
115
|
-
The plugin throws errors in the following cases:
|
|
116
|
-
|
|
117
|
-
- Invalid configuration
|
|
118
|
-
- Failed message sending
|
|
119
|
-
- Webhook verification failure
|
|
120
|
-
- Invalid webhook payload
|
|
121
|
-
|
|
122
|
-
Example error handling:
|
|
123
|
-
|
|
124
|
-
typescript
|
|
125
|
-
try {
|
|
126
|
-
await whatsappPlugin.sendMessage({
|
|
127
|
-
type: 'text',
|
|
128
|
-
to: '1234567890',
|
|
129
|
-
content: 'Hello!'
|
|
130
|
-
});
|
|
131
|
-
} catch (error) {
|
|
132
|
-
console.error('Failed to send message:', error.message);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
## Best Practices
|
|
136
|
-
|
|
137
|
-
1. Always validate phone numbers before sending messages
|
|
138
|
-
2. Use template messages for first-time messages to users
|
|
139
|
-
3. Store message IDs for tracking delivery status
|
|
140
|
-
4. Implement proper error handling
|
|
141
|
-
5. Set up webhook retry mechanisms
|
|
142
|
-
6. Keep your access tokens secure
|
|
143
|
-
|
|
144
|
-
## Contributing
|
|
145
|
-
|
|
146
|
-
1. Fork the repository
|
|
147
|
-
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
148
|
-
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
149
|
-
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
150
|
-
5. Open a Pull Request
|
|
151
|
-
|
|
152
|
-
## License
|
|
153
|
-
|
|
154
|
-
MIT
|
package/eslint.config.mjs
DELETED
package/src/client.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import axios, { AxiosInstance } from "axios";
|
|
2
|
-
import { WhatsAppConfig, WhatsAppMessage } from "./types";
|
|
3
|
-
|
|
4
|
-
export class WhatsAppClient {
|
|
5
|
-
private client: AxiosInstance;
|
|
6
|
-
private config: WhatsAppConfig;
|
|
7
|
-
|
|
8
|
-
constructor(config: WhatsAppConfig) {
|
|
9
|
-
this.config = config;
|
|
10
|
-
this.client = axios.create({
|
|
11
|
-
baseURL: "https://graph.facebook.com/v17.0",
|
|
12
|
-
headers: {
|
|
13
|
-
Authorization: `Bearer ${config.accessToken}`,
|
|
14
|
-
"Content-Type": "application/json",
|
|
15
|
-
},
|
|
16
|
-
});
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
async sendMessage(message: WhatsAppMessage): Promise<any> {
|
|
20
|
-
const endpoint = `/${this.config.phoneNumberId}/messages`;
|
|
21
|
-
|
|
22
|
-
const payload = {
|
|
23
|
-
messaging_product: "whatsapp",
|
|
24
|
-
recipient_type: "individual",
|
|
25
|
-
to: message.to,
|
|
26
|
-
type: message.type,
|
|
27
|
-
...(message.type === "text"
|
|
28
|
-
? { text: { body: message.content } }
|
|
29
|
-
: { template: message.content }),
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
return this.client.post(endpoint, payload);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
async verifyWebhook(token: string): Promise<boolean> {
|
|
36
|
-
return token === this.config.webhookVerifyToken;
|
|
37
|
-
}
|
|
38
|
-
}
|
package/src/handlers/index.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { WhatsAppClient } from "../client";
|
|
2
|
-
import { WhatsAppMessage } from "../types";
|
|
3
|
-
|
|
4
|
-
export class MessageHandler {
|
|
5
|
-
constructor(private client: WhatsAppClient) {}
|
|
6
|
-
|
|
7
|
-
async send(message: WhatsAppMessage): Promise<any> {
|
|
8
|
-
try {
|
|
9
|
-
const response = await this.client.sendMessage(message);
|
|
10
|
-
return response.data;
|
|
11
|
-
} catch (error: unknown) {
|
|
12
|
-
if (error instanceof Error) {
|
|
13
|
-
throw new Error(
|
|
14
|
-
`Failed to send WhatsApp message: ${error.message}`
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
throw new Error('Failed to send WhatsApp message');
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import { WhatsAppClient } from "../client";
|
|
2
|
-
import { WhatsAppWebhookEvent } from "../types";
|
|
3
|
-
|
|
4
|
-
export class WebhookHandler {
|
|
5
|
-
constructor(private client: WhatsAppClient) {}
|
|
6
|
-
|
|
7
|
-
async handle(event: WhatsAppWebhookEvent): Promise<void> {
|
|
8
|
-
try {
|
|
9
|
-
// Process messages
|
|
10
|
-
if (event.entry?.[0]?.changes?.[0]?.value?.messages) {
|
|
11
|
-
const messages = event.entry[0].changes[0].value.messages;
|
|
12
|
-
for (const message of messages) {
|
|
13
|
-
await this.handleMessage(message);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
// Process status updates
|
|
18
|
-
if (event.entry?.[0]?.changes?.[0]?.value?.statuses) {
|
|
19
|
-
const statuses = event.entry[0].changes[0].value.statuses;
|
|
20
|
-
for (const status of statuses) {
|
|
21
|
-
await this.handleStatus(status);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
} catch (error: unknown) {
|
|
25
|
-
if (error instanceof Error) {
|
|
26
|
-
throw new Error(
|
|
27
|
-
`Failed to send WhatsApp message: ${error.message}`
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
throw new Error("Failed to send WhatsApp message");
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
private async handleMessage(message: any): Promise<void> {
|
|
35
|
-
// Implement message handling logic
|
|
36
|
-
// This could emit events or trigger callbacks based on your framework's needs
|
|
37
|
-
console.log("Received message:", message);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
private async handleStatus(status: any): Promise<void> {
|
|
41
|
-
// Implement status update handling logic
|
|
42
|
-
// This could emit events or trigger callbacks based on your framework's needs
|
|
43
|
-
console.log("Received status update:", status);
|
|
44
|
-
}
|
|
45
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { Plugin } from "@elizaos/core";
|
|
2
|
-
import { WhatsAppClient } from "./client";
|
|
3
|
-
import { WhatsAppConfig, WhatsAppMessage, WhatsAppWebhookEvent } from "./types";
|
|
4
|
-
import { MessageHandler, WebhookHandler } from "./handlers";
|
|
5
|
-
|
|
6
|
-
export class WhatsAppPlugin implements Plugin {
|
|
7
|
-
private client: WhatsAppClient;
|
|
8
|
-
private messageHandler: MessageHandler;
|
|
9
|
-
private webhookHandler: WebhookHandler;
|
|
10
|
-
|
|
11
|
-
name: string;
|
|
12
|
-
description: string;
|
|
13
|
-
|
|
14
|
-
constructor(private config: WhatsAppConfig) {
|
|
15
|
-
this.name = "WhatsApp Cloud API Plugin";
|
|
16
|
-
this.description =
|
|
17
|
-
"A plugin for integrating WhatsApp Cloud API with your application.";
|
|
18
|
-
this.client = new WhatsAppClient(config);
|
|
19
|
-
this.messageHandler = new MessageHandler(this.client);
|
|
20
|
-
this.webhookHandler = new WebhookHandler(this.client);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async sendMessage(message: WhatsAppMessage): Promise<any> {
|
|
24
|
-
return this.messageHandler.send(message);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async handleWebhook(event: WhatsAppWebhookEvent): Promise<void> {
|
|
28
|
-
return this.webhookHandler.handle(event);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async verifyWebhook(token: string): Promise<boolean> {
|
|
32
|
-
return this.client.verifyWebhook(token);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export * from "./types";
|
package/src/types.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
export interface WhatsAppConfig {
|
|
2
|
-
accessToken: string;
|
|
3
|
-
phoneNumberId: string;
|
|
4
|
-
webhookVerifyToken?: string;
|
|
5
|
-
businessAccountId?: string;
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
export interface WhatsAppMessage {
|
|
9
|
-
type: "text" | "template";
|
|
10
|
-
to: string;
|
|
11
|
-
content: string | WhatsAppTemplate;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface WhatsAppTemplate {
|
|
15
|
-
name: string;
|
|
16
|
-
language: {
|
|
17
|
-
code: string;
|
|
18
|
-
};
|
|
19
|
-
components?: Array<{
|
|
20
|
-
type: string;
|
|
21
|
-
parameters: Array<{
|
|
22
|
-
type: string;
|
|
23
|
-
text?: string;
|
|
24
|
-
}>;
|
|
25
|
-
}>;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export interface WhatsAppWebhookEvent {
|
|
29
|
-
object: string;
|
|
30
|
-
entry: Array<{
|
|
31
|
-
id: string;
|
|
32
|
-
changes: Array<{
|
|
33
|
-
value: {
|
|
34
|
-
messaging_product: string;
|
|
35
|
-
metadata: {
|
|
36
|
-
display_phone_number: string;
|
|
37
|
-
phone_number_id: string;
|
|
38
|
-
};
|
|
39
|
-
statuses?: Array<{
|
|
40
|
-
id: string;
|
|
41
|
-
status: string;
|
|
42
|
-
timestamp: string;
|
|
43
|
-
recipient_id: string;
|
|
44
|
-
}>;
|
|
45
|
-
messages?: Array<{
|
|
46
|
-
from: string;
|
|
47
|
-
id: string;
|
|
48
|
-
timestamp: string;
|
|
49
|
-
text?: {
|
|
50
|
-
body: string;
|
|
51
|
-
};
|
|
52
|
-
type: string;
|
|
53
|
-
}>;
|
|
54
|
-
};
|
|
55
|
-
field: string;
|
|
56
|
-
}>;
|
|
57
|
-
}>;
|
|
58
|
-
}
|
package/src/utils/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from "./validators";
|
package/src/utils/validators.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { WhatsAppMessage, WhatsAppTemplate, WhatsAppConfig } from "../types";
|
|
2
|
-
|
|
3
|
-
export function validateConfig(config: WhatsAppConfig): void {
|
|
4
|
-
if (!config.accessToken) {
|
|
5
|
-
throw new Error("WhatsApp access token is required");
|
|
6
|
-
}
|
|
7
|
-
if (!config.phoneNumberId) {
|
|
8
|
-
throw new Error("WhatsApp phone number ID is required");
|
|
9
|
-
}
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
export function validateMessage(message: WhatsAppMessage): void {
|
|
13
|
-
if (!message.to) {
|
|
14
|
-
throw new Error("Recipient phone number is required");
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
if (!message.type) {
|
|
18
|
-
throw new Error("Message type is required");
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (!message.content) {
|
|
22
|
-
throw new Error("Message content is required");
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
if (message.type === "template") {
|
|
26
|
-
validateTemplate(message.content as WhatsAppTemplate);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function validateTemplate(template: WhatsAppTemplate): void {
|
|
31
|
-
if (!template.name) {
|
|
32
|
-
throw new Error("Template name is required");
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
if (!template.language || !template.language.code) {
|
|
36
|
-
throw new Error("Template language code is required");
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export function validatePhoneNumber(phoneNumber: string): boolean {
|
|
41
|
-
// Basic phone number validation - can be enhanced based on requirements
|
|
42
|
-
const phoneRegex = /^\d{1,15}$/;
|
|
43
|
-
return phoneRegex.test(phoneNumber);
|
|
44
|
-
}
|
package/tsconfig.json
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "../core/tsconfig.json",
|
|
3
|
-
"compilerOptions": {
|
|
4
|
-
"outDir": "./dist",
|
|
5
|
-
"rootDir": "src",
|
|
6
|
-
"baseUrl": ".",
|
|
7
|
-
"types": [
|
|
8
|
-
"node",
|
|
9
|
-
"jest"
|
|
10
|
-
]
|
|
11
|
-
},
|
|
12
|
-
"include": [
|
|
13
|
-
"src/**/*.ts"
|
|
14
|
-
],
|
|
15
|
-
"exclude": [
|
|
16
|
-
"node_modules",
|
|
17
|
-
"dist",
|
|
18
|
-
"**/*.test.ts"
|
|
19
|
-
]
|
|
20
|
-
}
|
package/tsup.config.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { defineConfig } from "tsup";
|
|
2
|
-
|
|
3
|
-
export default defineConfig({
|
|
4
|
-
entry: ["src/index.ts"],
|
|
5
|
-
outDir: "dist",
|
|
6
|
-
sourcemap: true,
|
|
7
|
-
clean: true,
|
|
8
|
-
format: ["esm"], // Ensure you're targeting CommonJS
|
|
9
|
-
external: [
|
|
10
|
-
"dotenv", // Externalize dotenv to prevent bundling
|
|
11
|
-
"fs", // Externalize fs to use Node.js built-in module
|
|
12
|
-
"path", // Externalize other built-ins if necessary
|
|
13
|
-
"@reflink/reflink",
|
|
14
|
-
"@node-llama-cpp",
|
|
15
|
-
"https",
|
|
16
|
-
"http",
|
|
17
|
-
"agentkeepalive",
|
|
18
|
-
],
|
|
19
|
-
});
|
|
File without changes
|
|
File without changes
|