valstorm-plugin-twilio — Hybrid Twilio Voice (TwiML), Conversations & Messaging Plugin (NF-475)
Hybrid (Python + Go) Twilio integration for Open Valstorm, mounted at platform.twilio (VALSTORM_PLUGINS=twilio or *).
Combines programmable TwiML Voice Call Flows (platform.twilio.twiml), Twilio Conversations v1 (1-on-1 SMS & Group MMS), Programmable Voice Outbound Dialing, Lookup v2, and High-Throughput Go Webhook Processing on Open Valstorm's provider-agnostic telephony schemas (phone_number, sms_thread, sms_message, sms_participant, phone_application, call, call_recording):
- Composable TwiML Engine (
platform.twilio.twiml): Build serverless PythonfunctionIVRs returningtext/xml(say_and_gather,forward_or_voicemail,business_hours_router,voice_ai_conversation_relay,dial,conference,enqueue). - Twilio Conversations & SMS/MMS (
platform.twilio.send_sms): Automatic 1-on-1 and Group MMS thread resolution (CH.../smst_), participant self-healing, and VFS media attachments. - High-Throughput Go Worker (
plugins/valstorm-plugin-twilio/go): Processestwilio:webhook_conversation_added,twilio:webhook_participant_added,twilio:webhook_message_added,twilio:webhook_delivery_updated, andtwilio:webhook_voice_statuswith HMAC-SHA1X-Twilio-Signatureverification.
🤖 Agent-Assisted Quick Setup (Let the Valstorm Agent Set It Up)
If you have the Twilio CLI installed (brew tap twilio/brew && brew install twilio) or copy your Account SID (AC...) and Auth Token from the Twilio Console, your Valstorm AI Agent can automatically discover your Conversations Service SID (IS...), sync your active Twilio phone numbers into phone_number (phnu_) records, configure your Conversations post-webhook URL, register $O(1)$ webhook routing, and verify live connectivity.
1. Quick Human Prerequisite (60 Seconds)
Copy your Account SID (AC...) and Auth Token from console.twilio.com (or authenticate the Twilio CLI via twilio login).
2. Copy-Paste Prompt for Your Valstorm Agent
Drop this prompt into Valstorm Chat:
"Set up
valstorm-plugin-twiliousing my Twilio Account SID<AC...>and Auth Token<AUTH_TOKEN>(optional Conversations Service SID:<IS...>). Followplugins/valstorm-plugin-twilio/README.mdto save the encrypted credentials viaplatform.twilio.save_credentials(...), sync my Twilio phone numbers intophone_numberrecords with $O(1)$ webhook routing, configure my Conversations webhook URL, and runtwilio_verify_connection."
3. Agent Execution Runbook (Instructions for the AI Agent)
When a user asks you to set up valstorm-plugin-twilio, execute this sequence autonomously:
- Validate & Save Encrypted Credentials:
- Call
await platform.twilio.save_credentials(account_sid=..., auth_token=..., chat_service_sid=..., validate_live=True)(orPOST /v1/twilio/admin/credentials).
- Sync Active Twilio Phone Numbers & $O(1)$ Routing:
- Call
await platform.twilio.sync_phone_numbers()(twilio_sync_phone_numbers) to upsertphone_number(phnu_) records and registeraccount_sid,chat_service_sid, and every E.164 phone number inexternal_account_routing(provider="twilio").
- Configure Conversations Post-Webhook:
- Call
await platform.twilio.configure_conversation_webhooks(base_url="https://<YOUR_API_DOMAIN>")soonConversationAdded,onParticipantAdded,onMessageAdded, andonDeliveryUpdatedpost to/v1/twilio/conversation/<ORG_ID>/post-webhook.
- Verify Live Connection:
- Call
twilio_verify_connectionand report the account status, friendly name, active phone numbers, and Conversations Service SID back to the user.
🛠️ End-to-End DIY Setup Guide (Manual)
Step 1: Retrieve Your Twilio Account SID, Auth Token & Conversations Service SID
- Log in to console.twilio.com.
- On the main dashboard under Account Info, copy your Account SID (
AC...) and Auth Token. - Navigate to Messaging -> Services (or Conversations -> Manage -> Services) and copy your default Conversations Service SID (
IS...).
Step 2: Configure Webhook URLs in Twilio Console
- Conversations Webhooks:
- Go to Conversations -> Manage -> Global Webhooks.
- Set Post-Event URL to:
https://api.yourdomain.com/v1/twilio/conversation/<YOUR_ORG_ID>/post-webhook
- Enable
onConversationAdded,onParticipantAdded,onMessageAdded, andonDeliveryUpdated.
- Voice Call Flow (TwiML) Webhooks:
- Point your Twilio phone number's A call comes in webhook to your Open Valstorm custom HTTP function endpoint (
/v1/functions/custom/<your-ivr-route>).
Step 3: Save Encrypted Credentials in Open Valstorm
await platform.credentials.save(
"twilio",
{
"account_sid": "AC0123456789abcdef0123456789abcdef",
"auth_token": "YOUR_TWILIO_AUTH_TOKEN",
"chat_service_sid": "IS0123456789abcdef0123456789abcdef",
"default_from_number": "+18605551234",
},
)Python Usage Examples (platform.twilio)
# 1. Build a TwiML IVR response inside a serverless Valstorm Function
def handler(request, platform):
return platform.twilio.twiml.say_and_gather(
prompt="Thanks for calling Valstorm. Press 1 for Sales, or 2 for Support.",
action_url="/v1/functions/custom/ivr-menu-choice",
num_digits=1,
)
# 2. Send an SMS or Group MMS via Twilio Conversations / Programmable Messaging
msg = await platform.twilio.send_sms(
to_phone="+18605559876",
from_number="+18605551234",
message="Your appointment is confirmed for tomorrow at 10:00 AM.",
)
# 3. Perform a Twilio Lookup v2 (Line Type & Carrier Intelligence)
lookup = await platform.twilio.lookup_phone_number("+18605559876")REST API Endpoints (/v1/twilio/*)
| Method | Endpoint | Description |
|---|---|---|
GET | /v1/twilio/setup-guide | Step-by-step Twilio Console & Conversations webhook setup guide |
POST | /v1/twilio/verify | Verifies Twilio Account SID, Auth Token, and phone inventory |
POST | /v1/twilio/admin/credentials | Validates and saves encrypted Twilio credentials |
POST | /v1/twilio/admin/numbers/sync | Syncs Twilio IncomingPhoneNumbers into phone_number (phnu_) |
GET | /v1/twilio/admin/numbers/search | Searches available Twilio phone numbers by area code |
POST | /v1/twilio/admin/numbers/order | Orders a Twilio phone number & creates phone_number (phnu_) |
POST | /v1/twilio/message | Sends 1-on-1 SMS or Group MMS & logs sms_message (sms_) |
POST | /v1/twilio/conversation | Creates a Twilio Conversation with participants (smst_) |
POST | /v1/twilio/call | Initiates an outbound Twilio Voice call & logs call (call_) |
GET | /v1/twilio/lookup/{phone_number} | Runs Twilio Lookup v2 line type intelligence |
POST | /v1/twilio/conversation/{org_id}/post-webhook | Validates X-Twilio-Signature & dispatches twilio:webhook_* to Go worker |
POST | /v1/webhooks/twilio/{org_id} | General Twilio Voice/Messaging webhook relay to Go worker |
Running Tests (Python & Go)
# Run Python TwiML, Context, & Webhook route tests
cd apps/open-api
.venv/bin/pytest tests/test_plugin_twilio.py tests/test_twilio_twiml_context.py -v
# Run Go worker Conversations & Voice webhook race tests
cd ../../plugins/valstorm-plugin-twilio/go
go test -race -v ./...