@elizaos/plugin-whatsapp 0.1.7 → 0.1.8
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 +29 -23
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -5,6 +5,7 @@ A plugin for integrating WhatsApp Cloud API with your application, providing com
|
|
|
5
5
|
## Overview
|
|
6
6
|
|
|
7
7
|
This plugin provides functionality to:
|
|
8
|
+
|
|
8
9
|
- Send text and template messages via WhatsApp
|
|
9
10
|
- Handle incoming webhook events
|
|
10
11
|
- Manage message status updates
|
|
@@ -36,10 +37,10 @@ WHATSAPP_BUSINESS_ID=your_business_id # Optional: Business account ID
|
|
|
36
37
|
import { WhatsAppPlugin } from "@elizaos/plugin-whatsapp";
|
|
37
38
|
|
|
38
39
|
const whatsappPlugin = new WhatsAppPlugin({
|
|
39
|
-
accessToken:
|
|
40
|
-
phoneNumberId:
|
|
41
|
-
webhookVerifyToken:
|
|
42
|
-
businessAccountId:
|
|
40
|
+
accessToken: "your_access_token",
|
|
41
|
+
phoneNumberId: "your_phone_number_id",
|
|
42
|
+
webhookVerifyToken: "your_webhook_verify_token",
|
|
43
|
+
businessAccountId: "your_business_account_id",
|
|
43
44
|
});
|
|
44
45
|
```
|
|
45
46
|
|
|
@@ -48,21 +49,21 @@ const whatsappPlugin = new WhatsAppPlugin({
|
|
|
48
49
|
```typescript
|
|
49
50
|
// Send a text message
|
|
50
51
|
await whatsappPlugin.sendMessage({
|
|
51
|
-
type:
|
|
52
|
-
to:
|
|
53
|
-
content:
|
|
52
|
+
type: "text",
|
|
53
|
+
to: "1234567890",
|
|
54
|
+
content: "Hello from WhatsApp!",
|
|
54
55
|
});
|
|
55
56
|
|
|
56
57
|
// Send a template message
|
|
57
58
|
await whatsappPlugin.sendMessage({
|
|
58
|
-
type:
|
|
59
|
-
to:
|
|
59
|
+
type: "template",
|
|
60
|
+
to: "1234567890",
|
|
60
61
|
content: {
|
|
61
|
-
name:
|
|
62
|
+
name: "hello_world",
|
|
62
63
|
language: {
|
|
63
|
-
code:
|
|
64
|
-
}
|
|
65
|
-
}
|
|
64
|
+
code: "en",
|
|
65
|
+
},
|
|
66
|
+
},
|
|
66
67
|
});
|
|
67
68
|
```
|
|
68
69
|
|
|
@@ -70,17 +71,19 @@ await whatsappPlugin.sendMessage({
|
|
|
70
71
|
|
|
71
72
|
```typescript
|
|
72
73
|
// Verify webhook
|
|
73
|
-
app.get(
|
|
74
|
-
const verified = await whatsappPlugin.verifyWebhook(
|
|
74
|
+
app.get("/webhook", (req, res) => {
|
|
75
|
+
const verified = await whatsappPlugin.verifyWebhook(
|
|
76
|
+
req.query["hub.verify_token"]
|
|
77
|
+
);
|
|
75
78
|
if (verified) {
|
|
76
|
-
res.send(req.query[
|
|
79
|
+
res.send(req.query["hub.challenge"]);
|
|
77
80
|
} else {
|
|
78
81
|
res.sendStatus(403);
|
|
79
82
|
}
|
|
80
83
|
});
|
|
81
84
|
|
|
82
85
|
// Handle webhook events
|
|
83
|
-
app.post(
|
|
86
|
+
app.post("/webhook", (req, res) => {
|
|
84
87
|
await whatsappPlugin.handleWebhook(req.body);
|
|
85
88
|
res.sendStatus(200);
|
|
86
89
|
});
|
|
@@ -101,16 +104,17 @@ The plugin throws errors in the following cases:
|
|
|
101
104
|
```typescript
|
|
102
105
|
try {
|
|
103
106
|
await whatsappPlugin.sendMessage({
|
|
104
|
-
type:
|
|
105
|
-
to:
|
|
106
|
-
content:
|
|
107
|
+
type: "text",
|
|
108
|
+
to: "1234567890",
|
|
109
|
+
content: "Hello!",
|
|
107
110
|
});
|
|
108
111
|
} catch (error) {
|
|
109
|
-
console.error(
|
|
112
|
+
console.error("Failed to send message:", error.message);
|
|
110
113
|
}
|
|
111
114
|
```
|
|
112
115
|
|
|
113
116
|
Common error cases:
|
|
117
|
+
|
|
114
118
|
- Invalid configuration
|
|
115
119
|
- Failed message sending
|
|
116
120
|
- Webhook verification failure
|
|
@@ -138,7 +142,7 @@ interface WhatsAppConfig {
|
|
|
138
142
|
}
|
|
139
143
|
|
|
140
144
|
interface WhatsAppMessage {
|
|
141
|
-
type:
|
|
145
|
+
type: "text" | "template";
|
|
142
146
|
to: string;
|
|
143
147
|
content: string | WhatsAppTemplate;
|
|
144
148
|
}
|
|
@@ -208,13 +212,15 @@ This plugin integrates with and builds upon several key technologies:
|
|
|
208
212
|
- [Meta for Developers](https://developers.facebook.com/): Meta's developer platform and tools
|
|
209
213
|
|
|
210
214
|
Special thanks to:
|
|
215
|
+
|
|
211
216
|
- The Eliza community for their contributions and feedback
|
|
212
217
|
|
|
213
218
|
For more information about WhatsApp Cloud API and its capabilities, visit:
|
|
219
|
+
|
|
214
220
|
- [WhatsApp Business Platform Documentation](https://developers.facebook.com/docs/whatsapp/cloud-api/overview)
|
|
215
221
|
- [Meta for Developers Blog](https://developers.facebook.com/blog/)
|
|
216
222
|
- [WhatsApp Business API GitHub](https://github.com/WhatsApp/WhatsApp-Business-API-Setup-Scripts)
|
|
217
223
|
|
|
218
224
|
## License
|
|
219
225
|
|
|
220
|
-
This plugin is part of the Eliza project. See the main project repository for license information.
|
|
226
|
+
This plugin is part of the Eliza project. See the main project repository for license information.
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client.ts","../src/handlers/message.handler.ts","../src/handlers/webhook.handler.ts","../src/index.ts"],"sourcesContent":["import axios, { AxiosInstance } from \"axios\";\nimport { 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 { WhatsAppClient } from \"../client\";\nimport { 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(\n `Failed to send WhatsApp message: ${error.message}`\n );\n }\n throw new Error(
|
|
1
|
+
{"version":3,"sources":["../src/client.ts","../src/handlers/message.handler.ts","../src/handlers/webhook.handler.ts","../src/index.ts"],"sourcesContent":["import axios, { AxiosInstance } from \"axios\";\nimport { 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 { WhatsAppClient } from \"../client\";\nimport { 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(\n `Failed to send WhatsApp message: ${error.message}`\n );\n }\n throw new Error(\"Failed to send WhatsApp message\");\n }\n }\n}\n","import { WhatsAppClient } from \"../client\";\nimport { 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(\n `Failed to send WhatsApp message: ${error.message}`\n );\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 { Plugin } from \"@elizaos/core\";\nimport { WhatsAppClient } from \"./client\";\nimport { WhatsAppConfig, WhatsAppMessage, WhatsAppWebhookEvent } 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(private 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,WAA8B;AAG9B,IAAM,iBAAN,MAAqB;AAAA,EAChB;AAAA,EACA;AAAA,EAER,YAAY,QAAwB;AAChC,SAAK,SAAS;AACd,SAAK,SAAS,MAAM,OAAO;AAAA,MACvB,SAAS;AAAA,MACT,SAAS;AAAA,QACL,eAAe,UAAU,OAAO,WAAW;AAAA,QAC3C,gBAAgB;AAAA,MACpB;AAAA,IACJ,CAAC;AAAA,EACL;AAAA,EAEA,MAAM,YAAY,SAAwC;AACtD,UAAM,WAAW,IAAI,KAAK,OAAO,aAAa;AAE9C,UAAM,UAAU;AAAA,MACZ,mBAAmB;AAAA,MACnB,gBAAgB;AAAA,MAChB,IAAI,QAAQ;AAAA,MACZ,MAAM,QAAQ;AAAA,MACd,GAAI,QAAQ,SAAS,SACf,EAAE,MAAM,EAAE,MAAM,QAAQ,QAAQ,EAAE,IAClC,EAAE,UAAU,QAAQ,QAAQ;AAAA,IACtC;AAEA,WAAO,KAAK,OAAO,KAAK,UAAU,OAAO;AAAA,EAC7C;AAAA,EAEA,MAAM,cAAc,OAAiC;AACjD,WAAO,UAAU,KAAK,OAAO;AAAA,EACjC;AACJ;;;AClCO,IAAM,iBAAN,MAAqB;AAAA,EACxB,YAAoB,QAAwB;AAAxB;AAAA,EAAyB;AAAA,EAE7C,MAAM,KAAK,SAAwC;AAC/C,QAAI;AACA,YAAM,WAAW,MAAM,KAAK,OAAO,YAAY,OAAO;AACtD,aAAO,SAAS;AAAA,IACpB,SAAS,OAAgB;AACrB,UAAI,iBAAiB,OAAO;AACxB,cAAM,IAAI;AAAA,UACN,oCAAoC,MAAM,OAAO;AAAA,QACrD;AAAA,MACJ;AAC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AA,EAER;AAAA,EACA;AAAA,EAWA,MAAM,YAAY,SAAwC;AACtD,WAAO,KAAK,eAAe,KAAK,OAAO;AAAA,EAC3C;AAAA,EAEA,MAAM,cAAc,OAA4C;AAC5D,WAAO,KAAK,eAAe,OAAO,KAAK;AAAA,EAC3C;AAAA,EAEA,MAAM,cAAc,OAAiC;AACjD,WAAO,KAAK,OAAO,cAAc,KAAK;AAAA,EAC1C;AACJ;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@elizaos/plugin-whatsapp",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.8+build.1",
|
|
4
4
|
"description": "WhatsApp Cloud API plugin",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"lint": "eslint --fix --cache ."
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@elizaos/core": "0.1.
|
|
29
|
+
"@elizaos/core": "0.1.8+build.1",
|
|
30
30
|
"axios": "1.7.8"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"ts-jest": "29.2.5",
|
|
39
39
|
"typescript": "5.6.3"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "d55c86c961960b4b34528c358eb34b2ff4b34d87"
|
|
42
42
|
}
|