# `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 Python `function` IVRs returning `text/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`)**: Processes `twilio:webhook_conversation_added`, `twilio:webhook_participant_added`, `twilio:webhook_message_added`, `twilio:webhook_delivery_updated`, and `twilio:webhook_voice_status` with HMAC-SHA1 `X-Twilio-Signature` verification. --- ## 🤖 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](https://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-twilio` using my Twilio Account SID `` and Auth Token `` (optional Conversations Service SID: ``). Follow `plugins/valstorm-plugin-twilio/README.md` to save the encrypted credentials via `platform.twilio.save_credentials(...)`, sync my Twilio phone numbers into `phone_number` records with $O(1)$ webhook routing, configure my Conversations webhook URL, and run `twilio_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: 1. **Validate & Save Encrypted Credentials**: - Call `await platform.twilio.save_credentials(account_sid=..., auth_token=..., chat_service_sid=..., validate_live=True)` (or `POST /v1/twilio/admin/credentials`). 2. **Sync Active Twilio Phone Numbers & $O(1)$ Routing**: - Call `await platform.twilio.sync_phone_numbers()` (`twilio_sync_phone_numbers`) to upsert `phone_number` (`phnu_`) records and register `account_sid`, `chat_service_sid`, and every E.164 phone number in `external_account_routing` (`provider="twilio"`). 3. **Configure Conversations Post-Webhook**: - Call `await platform.twilio.configure_conversation_webhooks(base_url="https://")` so `onConversationAdded`, `onParticipantAdded`, `onMessageAdded`, and `onDeliveryUpdated` post to `/v1/twilio/conversation//post-webhook`. 4. **Verify Live Connection**: - Call `twilio_verify_connection` and 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 1. Log in to [console.twilio.com](https://console.twilio.com/). 2. On the main dashboard under **Account Info**, copy your **Account SID** (`AC...`) and **Auth Token**. 3. Navigate to **Messaging -> Services** (or **Conversations -> Manage -> Services**) and copy your default **Conversations Service SID** (`IS...`). ### Step 2: Configure Webhook URLs in Twilio Console 1. **Conversations Webhooks**: - Go to **Conversations -> Manage -> Global Webhooks**. - Set **Post-Event URL** to: `https://api.yourdomain.com/v1/twilio/conversation//post-webhook` - Enable `onConversationAdded`, `onParticipantAdded`, `onMessageAdded`, and `onDeliveryUpdated`. 2. **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/`). ### Step 3: Save Encrypted Credentials in Open Valstorm ```python 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`) ```python # 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) ```bash # 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 ./... ```