@elizaos/plugin-twilio 2.0.0-alpha.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Shaw Walters and elizaOS Contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,343 @@
1
+ # Twilio Plugin for ElizaOS
2
+
3
+ A comprehensive Twilio integration plugin for ElizaOS that provides bidirectional voice and SMS/MMS messaging capabilities.
4
+
5
+ ## Overview
6
+
7
+ The Twilio plugin enables ElizaOS agents to interact with users through:
8
+
9
+ - SMS text messaging (sending and receiving)
10
+ - MMS multimedia messaging with attachments
11
+ - Voice calls with real-time audio streaming
12
+ - Webhook integration for incoming messages and calls
13
+ - Conversation history tracking
14
+ - Phone number validation and formatting
15
+
16
+ ## Installation
17
+
18
+ ```bash
19
+ npm install @elizaos/plugin-twilio
20
+ ```
21
+
22
+ ## Configuration
23
+
24
+ ### Environment Variables
25
+
26
+ ```env
27
+ # Required
28
+ TWILIO_ACCOUNT_SID=your_account_sid_here
29
+ TWILIO_AUTH_TOKEN=your_auth_token_here
30
+ TWILIO_PHONE_NUMBER=+18885551234 # Your Twilio phone number in E.164 format
31
+ TWILIO_WEBHOOK_URL=https://your-domain.com/webhooks/twilio # Public URL for webhooks
32
+
33
+ # Optional
34
+ TWILIO_WEBHOOK_PORT=3000 # Port for webhook server (default: 3000)
35
+ TWILIO_TEST_PHONE_NUMBER=+15555551234 # Phone number for testing
36
+ ```
37
+
38
+ ### Character Configuration
39
+
40
+ ```typescript
41
+ {
42
+ name: "MyAgent",
43
+ clients: [],
44
+ plugins: ["@elizaos/plugin-twilio"],
45
+ settings: {
46
+ // Plugin will use runtime.getSetting() to access the above env vars
47
+ }
48
+ }
49
+ ```
50
+
51
+ ## Service Architecture
52
+
53
+ ### TwilioService
54
+
55
+ The main service class that implements the Twilio integration:
56
+
57
+ ```typescript
58
+ export class TwilioService extends Service implements TwilioServiceInterface {
59
+ // Core Twilio functionality
60
+ client: TwilioClient;
61
+
62
+ // Webhook server for receiving messages/calls
63
+ private app: Express;
64
+ private wss: WebSocketServer;
65
+
66
+ // Caching for conversation context
67
+ private cache: NodeCache;
68
+
69
+ // Voice stream management
70
+ private voiceStreams: Map<string, TwilioVoiceStream>;
71
+
72
+ // Public methods
73
+ async sendSms(to: string, body: string, mediaUrl?: string[]): Promise<TwilioMessage>;
74
+ async makeCall(to: string, twiml?: string, url?: string): Promise<TwilioCall>;
75
+ }
76
+ ```
77
+
78
+ ### Webhook Server
79
+
80
+ The plugin automatically sets up an Express server to receive Twilio webhooks:
81
+
82
+ - `/webhooks/twilio/sms` - Receives incoming SMS/MMS messages
83
+ - `/webhooks/twilio/voice` - Receives incoming voice calls
84
+ - `/webhooks/twilio/status` - Receives status updates for messages and calls
85
+
86
+ ### Voice Streaming
87
+
88
+ Voice calls are handled via WebSocket connections for real-time audio streaming:
89
+
90
+ - Supports μ-law audio format
91
+ - Bidirectional audio streaming
92
+ - Real-time transcription capabilities
93
+
94
+ ## Actions
95
+
96
+ ### Send SMS
97
+
98
+ Send text messages to phone numbers.
99
+
100
+ ```typescript
101
+ {
102
+ name: "SEND_SMS",
103
+ description: "Send an SMS message to a phone number",
104
+ examples: [
105
+ ["Send a text to +15555551234 saying Hello!", "I'll send an SMS to +15555551234 with the message 'Hello!'"]
106
+ ]
107
+ }
108
+ ```
109
+
110
+ ### Send MMS
111
+
112
+ Send multimedia messages with attachments.
113
+
114
+ ```typescript
115
+ {
116
+ name: "SEND_MMS",
117
+ description: "Send an MMS message with media attachments",
118
+ examples: [
119
+ ["Send a picture to +15555551234", "I'll send an MMS with the image to +15555551234"]
120
+ ]
121
+ }
122
+ ```
123
+
124
+ ### Make Call
125
+
126
+ Initiate outbound voice calls.
127
+
128
+ ```typescript
129
+ {
130
+ name: "MAKE_CALL",
131
+ description: "Make a voice call to a phone number",
132
+ examples: [
133
+ ["Call +15555551234", "I'll initiate a call to +15555551234"]
134
+ ]
135
+ }
136
+ ```
137
+
138
+ ## Providers
139
+
140
+ ### Conversation History Provider
141
+
142
+ Provides recent SMS conversation context for a phone number.
143
+
144
+ ```typescript
145
+ {
146
+ name: "conversationHistory",
147
+ description: "Provides recent SMS conversation history with a phone number"
148
+ }
149
+ ```
150
+
151
+ ### Call State Provider
152
+
153
+ Provides information about active voice calls.
154
+
155
+ ```typescript
156
+ {
157
+ name: "callState",
158
+ description: "Provides current voice call state information"
159
+ }
160
+ ```
161
+
162
+ ## Events
163
+
164
+ The plugin emits standardized events that can be consumed by other plugins:
165
+
166
+ - `SMS_RECEIVED` - When an SMS/MMS is received
167
+ - `SMS_SENT` - When an SMS/MMS is sent successfully
168
+ - `CALL_RECEIVED` - When an incoming call is received
169
+ - `VOICE_STREAM_STARTED` - When voice streaming begins
170
+ - `VOICE_STREAM_ENDED` - When voice streaming ends
171
+
172
+ ## Testing
173
+
174
+ The plugin includes a comprehensive testing suite covering both unit and end-to-end scenarios.
175
+
176
+ ### Unit Tests
177
+
178
+ Unit tests use `vitest` to test individual components in isolation by mocking all external dependencies, including the Twilio client.
179
+
180
+ ```bash
181
+ npm run test:unit
182
+ ```
183
+
184
+ ### E2E / Interactive Tests
185
+
186
+ The plugin features a robust end-to-end (E2E) test suite that runs against a live Twilio account and can be run in an interactive mode for real-world validation.
187
+
188
+ ```bash
189
+ # Run all tests (unit and E2E)
190
+ npm test
191
+ ```
192
+
193
+ **Prerequisites:**
194
+ - Valid Twilio credentials in your `.env` file.
195
+ - `TWILIO_TEST_PHONE_NUMBER` configured in `.env`.
196
+ - The webhook server must be publicly accessible for tests involving incoming messages or calls.
197
+
198
+ #### Interactive Test Mode
199
+
200
+ This mode allows you to test SMS and voice functionality with a real phone.
201
+
202
+ **To run:**
203
+ ```bash
204
+ # From your project root
205
+ elizaos test --name "Interactive Test Mode"
206
+ ```
207
+
208
+ **What it does:**
209
+ 1. **Starts a webhook server** to handle incoming replies and calls.
210
+ 2. **Sends a test SMS** to your `TWILIO_TEST_PHONE_NUMBER`.
211
+ 3. **Makes a test voice call** to the same number.
212
+ 4. **Waits for 30 seconds** for you to send an SMS *to* the Twilio number to test inbound message handling.
213
+
214
+ #### Local Testing with Ngrok
215
+
216
+ To test incoming webhooks on your local machine, use [ngrok](https://ngrok.com/) to expose your local server.
217
+
218
+ 1. **Start the ElizaOS agent:**
219
+ ```bash
220
+ elizaos start
221
+ ```
222
+ This will start the webhook server (typically on port 3000).
223
+
224
+ 2. **Expose the port with ngrok:**
225
+ ```bash
226
+ ngrok http 3000
227
+ ```
228
+
229
+ 3. **Update your Twilio number's configuration:** In the Twilio console, set your number's webhooks to the public ngrok URL:
230
+ - **SMS:** `https://<your-ngrok-url>.ngrok.io/webhooks/twilio/sms`
231
+ - **Voice:** `https://<your-ngrok-url>.ngrok.io/webhooks/twilio/voice`
232
+
233
+ ## Usage Example
234
+
235
+ ```typescript
236
+ import { TwilioService } from '@elizaos/plugin-twilio';
237
+
238
+ // The service is automatically registered when the plugin loads
239
+ const twilioService = runtime.getService('twilio') as TwilioService;
240
+
241
+ // Send an SMS
242
+ const message = await twilioService.sendSms(
243
+ '+15555551234',
244
+ 'Hello from your assistant!'
245
+ );
246
+
247
+ // Send an MMS with media
248
+ const mms = await twilioService.sendSms(
249
+ '+15555551234',
250
+ 'Check out this image!',
251
+ ['https://example.com/image.png']
252
+ );
253
+
254
+ // Make a call with TwiML
255
+ const call = await twilioService.makeCall(
256
+ '+15555551234',
257
+ '<Response><Say>Hello from your assistant!</Say></Response>'
258
+ );
259
+ ```
260
+
261
+ ## Advanced Features
262
+
263
+ ### Message Processing
264
+
265
+ - Automatic message chunking for long SMS (160 character limit)
266
+ - E.164 phone number validation and formatting
267
+ - Media attachment handling for MMS
268
+
269
+ ### Voice Features
270
+
271
+ - Real-time audio streaming via WebSocket
272
+ - TwiML generation helpers
273
+ - Voice transcription support
274
+ - Call state management
275
+
276
+ ### Conversation Management
277
+
278
+ - Automatic conversation history caching
279
+ - Context preservation across messages
280
+ - Integration with ElizaOS memory system
281
+
282
+ ## Error Handling
283
+
284
+ The plugin implements comprehensive error handling:
285
+
286
+ - Graceful webhook server failures
287
+ - Twilio API error handling with retry logic
288
+ - Phone number validation
289
+ - Connection state management
290
+
291
+ ## Troubleshooting
292
+
293
+ ### SMS Not Delivered (Error 30034: Carrier Filtering)
294
+
295
+ A common issue with new Twilio numbers is that outbound SMS messages are blocked by mobile carriers (e.g., T-Mobile, AT&T, Verizon). This is a standard fraud prevention measure, not an error in the plugin.
296
+
297
+ **Symptoms:**
298
+ - The `sendSms` action succeeds without errors from the plugin.
299
+ - The message status in the Twilio console logs is `undelivered` or `failed` with error code `30034`.
300
+
301
+ **Immediate Solutions:**
302
+
303
+ 1. **Establish Two-Way Communication:** The easiest fix is to prove to the carrier that a legitimate conversation is occurring.
304
+ - Add your Twilio phone number to your personal phone's contacts.
305
+ - Send a text message (e.g., "Hello") *from your personal phone* **to** your Twilio number.
306
+ - This creates a conversation thread, signaling to the carrier that the number is trusted. Subsequent outbound messages are much more likely to be delivered.
307
+
308
+ 2. **Wait 24-48 Hours:** New numbers often have sending restrictions that are automatically lifted after a short period of low-volume use.
309
+
310
+ **Long-Term Solutions:**
311
+
312
+ For production applications, especially those with high volume, you must register your use case with carriers to ensure reliable delivery:
313
+ - **A2P 10DLC Registration:** The US standard for Application-to-Person messaging over standard 10-digit long code numbers. This is essential for business messaging.
314
+ - **Toll-Free Verification:** If using a toll-free number, it must be verified with Twilio to increase throughput and reduce the risk of filtering.
315
+
316
+ ## Security Considerations
317
+
318
+ - Always use environment variables for credentials
319
+ - Implement webhook signature validation in production
320
+ - Use HTTPS for webhook URLs
321
+ - Restrict webhook endpoints to Twilio IP ranges
322
+
323
+ ## Contributing
324
+
325
+ When contributing to the Twilio plugin:
326
+
327
+ 1. Follow the ElizaOS development workflow
328
+ 2. Write tests for new features
329
+ 3. Update this documentation
330
+ 4. Test with real Twilio credentials
331
+ 5. Run `npm run format` before committing
332
+
333
+ ## Cleaning Up
334
+
335
+ To remove all build artifacts and node modules, you can run the `clean` script:
336
+
337
+ ```bash
338
+ npm run clean
339
+ ```
340
+
341
+ ## License
342
+
343
+ This plugin is part of the ElizaOS project and follows the same license terms.
@@ -0,0 +1,5 @@
1
+ import { Plugin } from '@elizaos/core';
2
+
3
+ declare const twilioPlugin: Plugin;
4
+
5
+ export { twilioPlugin as default };