@elizaos/plugin-whatsapp 1.0.0 → 2.0.0-alpha.2
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/LICENSE +2 -2
- package/dist/index.d.ts +2 -71
- package/package.json +58 -75
- package/README.md +0 -226
- package/dist/index.js +0 -111
- package/dist/index.js.map +0 -1
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c)
|
|
3
|
+
Copyright (c) 2026 Shaw Walters and elizaOS Contributors
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
6
|
of this software and associated documentation files (the "Software"), to deal
|
|
@@ -18,4 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
18
18
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
19
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
20
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
CHANGED
|
@@ -1,71 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
interface WhatsAppConfig {
|
|
4
|
-
accessToken: string;
|
|
5
|
-
phoneNumberId: string;
|
|
6
|
-
webhookVerifyToken?: string;
|
|
7
|
-
businessAccountId?: string;
|
|
8
|
-
}
|
|
9
|
-
interface WhatsAppMessage {
|
|
10
|
-
type: "text" | "template";
|
|
11
|
-
to: string;
|
|
12
|
-
content: string | WhatsAppTemplate;
|
|
13
|
-
}
|
|
14
|
-
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
|
-
interface WhatsAppWebhookEvent {
|
|
28
|
-
object: string;
|
|
29
|
-
entry: Array<{
|
|
30
|
-
id: string;
|
|
31
|
-
changes: Array<{
|
|
32
|
-
value: {
|
|
33
|
-
messaging_product: string;
|
|
34
|
-
metadata: {
|
|
35
|
-
display_phone_number: string;
|
|
36
|
-
phone_number_id: string;
|
|
37
|
-
};
|
|
38
|
-
statuses?: Array<{
|
|
39
|
-
id: string;
|
|
40
|
-
status: string;
|
|
41
|
-
timestamp: string;
|
|
42
|
-
recipient_id: string;
|
|
43
|
-
}>;
|
|
44
|
-
messages?: Array<{
|
|
45
|
-
from: string;
|
|
46
|
-
id: string;
|
|
47
|
-
timestamp: string;
|
|
48
|
-
text?: {
|
|
49
|
-
body: string;
|
|
50
|
-
};
|
|
51
|
-
type: string;
|
|
52
|
-
}>;
|
|
53
|
-
};
|
|
54
|
-
field: string;
|
|
55
|
-
}>;
|
|
56
|
-
}>;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
declare class WhatsAppPlugin implements Plugin {
|
|
60
|
-
private client;
|
|
61
|
-
private messageHandler;
|
|
62
|
-
private webhookHandler;
|
|
63
|
-
name: string;
|
|
64
|
-
description: string;
|
|
65
|
-
constructor(config: WhatsAppConfig);
|
|
66
|
-
sendMessage(message: WhatsAppMessage): Promise<any>;
|
|
67
|
-
handleWebhook(event: WhatsAppWebhookEvent): Promise<void>;
|
|
68
|
-
verifyWebhook(token: string): Promise<boolean>;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export { type WhatsAppConfig, type WhatsAppMessage, WhatsAppPlugin, type WhatsAppTemplate, type WhatsAppWebhookEvent };
|
|
1
|
+
export * from "./index";
|
|
2
|
+
export { default } from "./index";
|
package/package.json
CHANGED
|
@@ -1,77 +1,60 @@
|
|
|
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
|
-
"
|
|
27
|
-
|
|
28
|
-
"format:check": "prettier --check ./src",
|
|
29
|
-
"test": "bun test",
|
|
30
|
-
"test:watch": "bun test --watch",
|
|
31
|
-
"test:coverage": "bun test --coverage"
|
|
32
|
-
},
|
|
33
|
-
"dependencies": {
|
|
34
|
-
"@elizaos/core": "latest",
|
|
35
|
-
"axios": "1.7.8"
|
|
36
|
-
},
|
|
37
|
-
"devDependencies": {
|
|
38
|
-
"tsup": "8.3.5",
|
|
39
|
-
"prettier": "^3.0.0",
|
|
40
|
-
"bun": "^1.2.15",
|
|
41
|
-
"@types/bun": "latest",
|
|
42
|
-
"@types/node": "20.17.9",
|
|
43
|
-
"typescript": "5.6.3"
|
|
44
|
-
},
|
|
45
|
-
"publishConfig": {
|
|
46
|
-
"access": "public"
|
|
47
|
-
},
|
|
48
|
-
"agentConfig": {
|
|
49
|
-
"pluginType": "elizaos:plugin:1.0.0",
|
|
50
|
-
"pluginParameters": {
|
|
51
|
-
"WHATSAPP_ACCESS_TOKEN": {
|
|
52
|
-
"type": "string",
|
|
53
|
-
"description": "WhatsApp Cloud API access token",
|
|
54
|
-
"required": true,
|
|
55
|
-
"sensitive": true
|
|
56
|
-
},
|
|
57
|
-
"WHATSAPP_PHONE_NUMBER_ID": {
|
|
58
|
-
"type": "string",
|
|
59
|
-
"description": "WhatsApp business phone number ID",
|
|
60
|
-
"required": true,
|
|
61
|
-
"sensitive": true
|
|
62
|
-
},
|
|
63
|
-
"WHATSAPP_WEBHOOK_TOKEN": {
|
|
64
|
-
"type": "string",
|
|
65
|
-
"description": "Webhook verification token",
|
|
66
|
-
"required": false,
|
|
67
|
-
"sensitive": true
|
|
68
|
-
},
|
|
69
|
-
"WHATSAPP_BUSINESS_ID": {
|
|
70
|
-
"type": "string",
|
|
71
|
-
"description": "Business account ID",
|
|
72
|
-
"required": false,
|
|
73
|
-
"sensitive": true
|
|
74
|
-
}
|
|
75
|
-
}
|
|
2
|
+
"name": "@elizaos/plugin-whatsapp",
|
|
3
|
+
"description": "WhatsApp Cloud API plugin for ElizaOS",
|
|
4
|
+
"version": "2.0.0-alpha.2",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"module": "dist/index.js",
|
|
8
|
+
"types": "dist/index.d.ts",
|
|
9
|
+
"packageType": "plugin",
|
|
10
|
+
"platform": "node",
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"keywords": [
|
|
13
|
+
"plugin",
|
|
14
|
+
"elizaos",
|
|
15
|
+
"whatsapp"
|
|
16
|
+
],
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/elizaos/eliza"
|
|
20
|
+
},
|
|
21
|
+
"exports": {
|
|
22
|
+
"./package.json": "./package.json",
|
|
23
|
+
".": {
|
|
24
|
+
"import": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"default": "./dist/index.js"
|
|
27
|
+
}
|
|
76
28
|
}
|
|
77
|
-
}
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"dist",
|
|
32
|
+
"README.md",
|
|
33
|
+
"package.json"
|
|
34
|
+
],
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"axios": "1.7.8"
|
|
37
|
+
},
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@elizaos/core": "2.0.0-alpha.2"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@biomejs/biome": "^2.3.11",
|
|
43
|
+
"@types/node": "^25.0.3",
|
|
44
|
+
"typescript": "^5.9.3"
|
|
45
|
+
},
|
|
46
|
+
"scripts": {
|
|
47
|
+
"dev": "bun --hot build.ts",
|
|
48
|
+
"test": "vitest run || echo 'TypeScript tests skipped - no tests found'",
|
|
49
|
+
"lint": "bunx @biomejs/biome check --write --unsafe .",
|
|
50
|
+
"typecheck": "tsc --noEmit",
|
|
51
|
+
"clean": "rm -rf dist .turbo",
|
|
52
|
+
"lint:check": "bunx @biomejs/biome check .",
|
|
53
|
+
"build": "bun run build.ts",
|
|
54
|
+
"build:ts": "bun run build.ts"
|
|
55
|
+
},
|
|
56
|
+
"publishConfig": {
|
|
57
|
+
"access": "public"
|
|
58
|
+
},
|
|
59
|
+
"gitHead": "bc6cac8d36845d7cbde51a64307c6a57c16378ad"
|
|
60
|
+
}
|
package/README.md
DELETED
|
@@ -1,226 +0,0 @@
|
|
|
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
|
-
|
|
9
|
-
- Send text and template messages via WhatsApp
|
|
10
|
-
- Handle incoming webhook events
|
|
11
|
-
- Manage message status updates
|
|
12
|
-
- Process message delivery notifications
|
|
13
|
-
- Handle authentication and session management
|
|
14
|
-
|
|
15
|
-
## Installation
|
|
16
|
-
|
|
17
|
-
```bash
|
|
18
|
-
bun add @elizaos/plugin-whatsapp
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## Configuration
|
|
22
|
-
|
|
23
|
-
The plugin requires the following environment variables:
|
|
24
|
-
|
|
25
|
-
```env
|
|
26
|
-
WHATSAPP_ACCESS_TOKEN=your_access_token # Required: WhatsApp Cloud API access token
|
|
27
|
-
WHATSAPP_PHONE_NUMBER_ID=your_phone_number_id # Required: WhatsApp business phone number ID
|
|
28
|
-
WHATSAPP_WEBHOOK_TOKEN=your_webhook_token # Optional: Webhook verification token
|
|
29
|
-
WHATSAPP_BUSINESS_ID=your_business_id # Optional: Business account ID
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
## Usage
|
|
33
|
-
|
|
34
|
-
### Basic Setup
|
|
35
|
-
|
|
36
|
-
```typescript
|
|
37
|
-
import { WhatsAppPlugin } from "@elizaos/plugin-whatsapp";
|
|
38
|
-
|
|
39
|
-
const whatsappPlugin = new WhatsAppPlugin({
|
|
40
|
-
accessToken: "your_access_token",
|
|
41
|
-
phoneNumberId: "your_phone_number_id",
|
|
42
|
-
webhookVerifyToken: "your_webhook_verify_token",
|
|
43
|
-
businessAccountId: "your_business_account_id",
|
|
44
|
-
});
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
### Sending Messages
|
|
48
|
-
|
|
49
|
-
```typescript
|
|
50
|
-
// Send a text message
|
|
51
|
-
await whatsappPlugin.sendMessage({
|
|
52
|
-
type: "text",
|
|
53
|
-
to: "1234567890",
|
|
54
|
-
content: "Hello from WhatsApp!",
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
// Send a template message
|
|
58
|
-
await whatsappPlugin.sendMessage({
|
|
59
|
-
type: "template",
|
|
60
|
-
to: "1234567890",
|
|
61
|
-
content: {
|
|
62
|
-
name: "hello_world",
|
|
63
|
-
language: {
|
|
64
|
-
code: "en",
|
|
65
|
-
},
|
|
66
|
-
},
|
|
67
|
-
});
|
|
68
|
-
```
|
|
69
|
-
|
|
70
|
-
### Handling Webhooks
|
|
71
|
-
|
|
72
|
-
```typescript
|
|
73
|
-
// Verify webhook
|
|
74
|
-
app.get("/webhook", (req, res) => {
|
|
75
|
-
const verified = await whatsappPlugin.verifyWebhook(
|
|
76
|
-
req.query["hub.verify_token"]
|
|
77
|
-
);
|
|
78
|
-
if (verified) {
|
|
79
|
-
res.send(req.query["hub.challenge"]);
|
|
80
|
-
} else {
|
|
81
|
-
res.sendStatus(403);
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
// Handle webhook events
|
|
86
|
-
app.post("/webhook", (req, res) => {
|
|
87
|
-
await whatsappPlugin.handleWebhook(req.body);
|
|
88
|
-
res.sendStatus(200);
|
|
89
|
-
});
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
## Features
|
|
93
|
-
|
|
94
|
-
- Send text messages
|
|
95
|
-
- Send template messages
|
|
96
|
-
- Webhook verification
|
|
97
|
-
- Webhook event handling
|
|
98
|
-
- Message status updates
|
|
99
|
-
|
|
100
|
-
## Error Handling
|
|
101
|
-
|
|
102
|
-
The plugin throws errors in the following cases:
|
|
103
|
-
|
|
104
|
-
```typescript
|
|
105
|
-
try {
|
|
106
|
-
await whatsappPlugin.sendMessage({
|
|
107
|
-
type: "text",
|
|
108
|
-
to: "1234567890",
|
|
109
|
-
content: "Hello!",
|
|
110
|
-
});
|
|
111
|
-
} catch (error) {
|
|
112
|
-
console.error("Failed to send message:", error.message);
|
|
113
|
-
}
|
|
114
|
-
```
|
|
115
|
-
|
|
116
|
-
Common error cases:
|
|
117
|
-
|
|
118
|
-
- Invalid configuration
|
|
119
|
-
- Failed message sending
|
|
120
|
-
- Webhook verification failure
|
|
121
|
-
- Invalid webhook payload
|
|
122
|
-
|
|
123
|
-
## Best Practices
|
|
124
|
-
|
|
125
|
-
1. Always validate phone numbers before sending messages
|
|
126
|
-
2. Use template messages for first-time messages to users
|
|
127
|
-
3. Store message IDs for tracking delivery status
|
|
128
|
-
4. Implement proper error handling
|
|
129
|
-
5. Set up webhook retry mechanisms
|
|
130
|
-
6. Keep your access tokens secure
|
|
131
|
-
|
|
132
|
-
## API Reference
|
|
133
|
-
|
|
134
|
-
### Core Interfaces
|
|
135
|
-
|
|
136
|
-
```typescript
|
|
137
|
-
interface WhatsAppConfig {
|
|
138
|
-
accessToken: string;
|
|
139
|
-
phoneNumberId: string;
|
|
140
|
-
webhookVerifyToken?: string;
|
|
141
|
-
businessAccountId?: string;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
interface WhatsAppMessage {
|
|
145
|
-
type: "text" | "template";
|
|
146
|
-
to: string;
|
|
147
|
-
content: string | WhatsAppTemplate;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
interface WhatsAppTemplate {
|
|
151
|
-
name: string;
|
|
152
|
-
language: {
|
|
153
|
-
code: string;
|
|
154
|
-
};
|
|
155
|
-
components?: Array<{
|
|
156
|
-
type: string;
|
|
157
|
-
parameters: Array<{
|
|
158
|
-
type: string;
|
|
159
|
-
text?: string;
|
|
160
|
-
}>;
|
|
161
|
-
}>;
|
|
162
|
-
}
|
|
163
|
-
```
|
|
164
|
-
|
|
165
|
-
### Plugin Methods
|
|
166
|
-
|
|
167
|
-
- `sendMessage`: Send WhatsApp messages
|
|
168
|
-
- `handleWebhook`: Process incoming webhook events
|
|
169
|
-
- `verifyWebhook`: Verify webhook authenticity
|
|
170
|
-
- Message and status handlers
|
|
171
|
-
|
|
172
|
-
## Development
|
|
173
|
-
|
|
174
|
-
### Building
|
|
175
|
-
|
|
176
|
-
```bash
|
|
177
|
-
bun run build
|
|
178
|
-
```
|
|
179
|
-
|
|
180
|
-
### Testing
|
|
181
|
-
|
|
182
|
-
```bash
|
|
183
|
-
bun test
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
### Linting
|
|
187
|
-
|
|
188
|
-
```bash
|
|
189
|
-
bun run lint
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
## Security Best Practices
|
|
193
|
-
|
|
194
|
-
- Store credentials securely using environment variables
|
|
195
|
-
- Validate all phone numbers before sending messages
|
|
196
|
-
- Use template messages for first-time contacts
|
|
197
|
-
- Implement proper error handling
|
|
198
|
-
- Keep dependencies updated
|
|
199
|
-
- Monitor API usage and rate limits
|
|
200
|
-
- Use HTTPS for all API communication
|
|
201
|
-
|
|
202
|
-
## Contributing
|
|
203
|
-
|
|
204
|
-
Contributions are welcome! Please see the [CONTRIBUTING.md](CONTRIBUTING.md) file for more information.
|
|
205
|
-
|
|
206
|
-
## Credits
|
|
207
|
-
|
|
208
|
-
This plugin integrates with and builds upon several key technologies:
|
|
209
|
-
|
|
210
|
-
- [WhatsApp Cloud API](https://developers.facebook.com/docs/whatsapp/cloud-api): Meta's official WhatsApp Business Platform
|
|
211
|
-
- [Axios](https://axios-http.com/): Promise-based HTTP client for API requests
|
|
212
|
-
- [Meta for Developers](https://developers.facebook.com/): Meta's developer platform and tools
|
|
213
|
-
|
|
214
|
-
Special thanks to:
|
|
215
|
-
|
|
216
|
-
- The Eliza community for their contributions and feedback
|
|
217
|
-
|
|
218
|
-
For more information about WhatsApp Cloud API and its capabilities, visit:
|
|
219
|
-
|
|
220
|
-
- [WhatsApp Business Platform Documentation](https://developers.facebook.com/docs/whatsapp/cloud-api/overview)
|
|
221
|
-
- [Meta for Developers Blog](https://developers.facebook.com/blog/)
|
|
222
|
-
- [WhatsApp Business API GitHub](https://github.com/WhatsApp/WhatsApp-Business-API-Setup-Scripts)
|
|
223
|
-
|
|
224
|
-
## License
|
|
225
|
-
|
|
226
|
-
This plugin is part of the Eliza project. See the main project repository for license information.
|
package/dist/index.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
// src/client.ts
|
|
2
|
-
import axios from "axios";
|
|
3
|
-
var WhatsAppClient = class {
|
|
4
|
-
client;
|
|
5
|
-
config;
|
|
6
|
-
constructor(config) {
|
|
7
|
-
this.config = config;
|
|
8
|
-
this.client = axios.create({
|
|
9
|
-
baseURL: "https://graph.facebook.com/v17.0",
|
|
10
|
-
headers: {
|
|
11
|
-
Authorization: `Bearer ${config.accessToken}`,
|
|
12
|
-
"Content-Type": "application/json"
|
|
13
|
-
}
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
async sendMessage(message) {
|
|
17
|
-
const endpoint = `/${this.config.phoneNumberId}/messages`;
|
|
18
|
-
const payload = {
|
|
19
|
-
messaging_product: "whatsapp",
|
|
20
|
-
recipient_type: "individual",
|
|
21
|
-
to: message.to,
|
|
22
|
-
type: message.type,
|
|
23
|
-
...message.type === "text" ? { text: { body: message.content } } : { template: message.content }
|
|
24
|
-
};
|
|
25
|
-
return this.client.post(endpoint, payload);
|
|
26
|
-
}
|
|
27
|
-
async verifyWebhook(token) {
|
|
28
|
-
return token === this.config.webhookVerifyToken;
|
|
29
|
-
}
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
// src/handlers/message.handler.ts
|
|
33
|
-
var MessageHandler = class {
|
|
34
|
-
constructor(client) {
|
|
35
|
-
this.client = client;
|
|
36
|
-
}
|
|
37
|
-
async send(message) {
|
|
38
|
-
try {
|
|
39
|
-
const response = await this.client.sendMessage(message);
|
|
40
|
-
return response.data;
|
|
41
|
-
} catch (error) {
|
|
42
|
-
if (error instanceof Error) {
|
|
43
|
-
throw new Error(`Failed to send WhatsApp message: ${error.message}`);
|
|
44
|
-
}
|
|
45
|
-
throw new Error("Failed to send WhatsApp message");
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
// src/handlers/webhook.handler.ts
|
|
51
|
-
var WebhookHandler = class {
|
|
52
|
-
constructor(client) {
|
|
53
|
-
this.client = client;
|
|
54
|
-
}
|
|
55
|
-
async handle(event) {
|
|
56
|
-
try {
|
|
57
|
-
if (event.entry?.[0]?.changes?.[0]?.value?.messages) {
|
|
58
|
-
const messages = event.entry[0].changes[0].value.messages;
|
|
59
|
-
for (const message of messages) {
|
|
60
|
-
await this.handleMessage(message);
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
if (event.entry?.[0]?.changes?.[0]?.value?.statuses) {
|
|
64
|
-
const statuses = event.entry[0].changes[0].value.statuses;
|
|
65
|
-
for (const status of statuses) {
|
|
66
|
-
await this.handleStatus(status);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
} catch (error) {
|
|
70
|
-
if (error instanceof Error) {
|
|
71
|
-
throw new Error(`Failed to send WhatsApp message: ${error.message}`);
|
|
72
|
-
}
|
|
73
|
-
throw new Error("Failed to send WhatsApp message");
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
async handleMessage(message) {
|
|
77
|
-
console.log("Received message:", message);
|
|
78
|
-
}
|
|
79
|
-
async handleStatus(status) {
|
|
80
|
-
console.log("Received status update:", status);
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
// src/index.ts
|
|
85
|
-
var WhatsAppPlugin = class {
|
|
86
|
-
client;
|
|
87
|
-
messageHandler;
|
|
88
|
-
webhookHandler;
|
|
89
|
-
name;
|
|
90
|
-
description;
|
|
91
|
-
constructor(config) {
|
|
92
|
-
this.name = "WhatsApp Cloud API Plugin";
|
|
93
|
-
this.description = "A plugin for integrating WhatsApp Cloud API with your application.";
|
|
94
|
-
this.client = new WhatsAppClient(config);
|
|
95
|
-
this.messageHandler = new MessageHandler(this.client);
|
|
96
|
-
this.webhookHandler = new WebhookHandler(this.client);
|
|
97
|
-
}
|
|
98
|
-
async sendMessage(message) {
|
|
99
|
-
return this.messageHandler.send(message);
|
|
100
|
-
}
|
|
101
|
-
async handleWebhook(event) {
|
|
102
|
-
return this.webhookHandler.handle(event);
|
|
103
|
-
}
|
|
104
|
-
async verifyWebhook(token) {
|
|
105
|
-
return this.client.verifyWebhook(token);
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
export {
|
|
109
|
-
WhatsAppPlugin
|
|
110
|
-
};
|
|
111
|
-
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client.ts","../src/handlers/message.handler.ts","../src/handlers/webhook.handler.ts","../src/index.ts"],"sourcesContent":["import axios, { type AxiosInstance } from \"axios\";\nimport type { WhatsAppConfig, WhatsAppMessage } from \"./types\";\n\nexport class WhatsAppClient {\n private client: AxiosInstance;\n private config: WhatsAppConfig;\n\n constructor(config: WhatsAppConfig) {\n this.config = config;\n this.client = axios.create({\n baseURL: \"https://graph.facebook.com/v17.0\",\n headers: {\n Authorization: `Bearer ${config.accessToken}`,\n \"Content-Type\": \"application/json\",\n },\n });\n }\n\n async sendMessage(message: WhatsAppMessage): Promise<any> {\n const endpoint = `/${this.config.phoneNumberId}/messages`;\n\n const payload = {\n messaging_product: \"whatsapp\",\n recipient_type: \"individual\",\n to: message.to,\n type: message.type,\n ...(message.type === \"text\"\n ? { text: { body: message.content } }\n : { template: message.content }),\n };\n\n return this.client.post(endpoint, payload);\n }\n\n async verifyWebhook(token: string): Promise<boolean> {\n return token === this.config.webhookVerifyToken;\n }\n}\n","import type { WhatsAppClient } from \"../client\";\nimport type { WhatsAppMessage } from \"../types\";\n\nexport class MessageHandler {\n constructor(private client: WhatsAppClient) {}\n\n async send(message: WhatsAppMessage): Promise<any> {\n try {\n const response = await this.client.sendMessage(message);\n return response.data;\n } catch (error: unknown) {\n if (error instanceof Error) {\n throw new Error(`Failed to send WhatsApp message: ${error.message}`);\n }\n throw new Error(\"Failed to send WhatsApp message\");\n }\n }\n}\n","import type { WhatsAppClient } from \"../client\";\nimport type { WhatsAppWebhookEvent } from \"../types\";\n\nexport class WebhookHandler {\n constructor(private client: WhatsAppClient) {}\n\n async handle(event: WhatsAppWebhookEvent): Promise<void> {\n try {\n // Process messages\n if (event.entry?.[0]?.changes?.[0]?.value?.messages) {\n const messages = event.entry[0].changes[0].value.messages;\n for (const message of messages) {\n await this.handleMessage(message);\n }\n }\n\n // Process status updates\n if (event.entry?.[0]?.changes?.[0]?.value?.statuses) {\n const statuses = event.entry[0].changes[0].value.statuses;\n for (const status of statuses) {\n await this.handleStatus(status);\n }\n }\n } catch (error: unknown) {\n if (error instanceof Error) {\n throw new Error(`Failed to send WhatsApp message: ${error.message}`);\n }\n throw new Error(\"Failed to send WhatsApp message\");\n }\n }\n\n private async handleMessage(message: any): Promise<void> {\n // Implement message handling logic\n // This could emit events or trigger callbacks based on your framework's needs\n console.log(\"Received message:\", message);\n }\n\n private async handleStatus(status: any): Promise<void> {\n // Implement status update handling logic\n // This could emit events or trigger callbacks based on your framework's needs\n console.log(\"Received status update:\", status);\n }\n}\n","import type { Plugin } from \"@elizaos/core\";\nimport { WhatsAppClient } from \"./client\";\nimport type {\n WhatsAppConfig,\n WhatsAppMessage,\n WhatsAppWebhookEvent,\n} from \"./types\";\nimport { MessageHandler, WebhookHandler } from \"./handlers\";\n\nexport class WhatsAppPlugin implements Plugin {\n private client: WhatsAppClient;\n private messageHandler: MessageHandler;\n private webhookHandler: WebhookHandler;\n\n name: string;\n description: string;\n\n constructor(config: WhatsAppConfig) {\n this.name = \"WhatsApp Cloud API Plugin\";\n this.description =\n \"A plugin for integrating WhatsApp Cloud API with your application.\";\n this.client = new WhatsAppClient(config);\n this.messageHandler = new MessageHandler(this.client);\n this.webhookHandler = new WebhookHandler(this.client);\n }\n\n async sendMessage(message: WhatsAppMessage): Promise<any> {\n return this.messageHandler.send(message);\n }\n\n async handleWebhook(event: WhatsAppWebhookEvent): Promise<void> {\n return this.webhookHandler.handle(event);\n }\n\n async verifyWebhook(token: string): Promise<boolean> {\n return this.client.verifyWebhook(token);\n }\n}\n\nexport * from \"./types\";\n"],"mappings":";AAAA,OAAO,WAAmC;AAGnC,IAAM,iBAAN,MAAqB;AAAA,EAClB;AAAA,EACA;AAAA,EAER,YAAY,QAAwB;AAClC,SAAK,SAAS;AACd,SAAK,SAAS,MAAM,OAAO;AAAA,MACzB,SAAS;AAAA,MACT,SAAS;AAAA,QACP,eAAe,UAAU,OAAO,WAAW;AAAA,QAC3C,gBAAgB;AAAA,MAClB;AAAA,IACF,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,YAAY,SAAwC;AACxD,UAAM,WAAW,IAAI,KAAK,OAAO,aAAa;AAE9C,UAAM,UAAU;AAAA,MACd,mBAAmB;AAAA,MACnB,gBAAgB;AAAA,MAChB,IAAI,QAAQ;AAAA,MACZ,M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