# `valstorm-plugin-telnyx` — Hybrid Telnyx Telephony, Messaging & Voice AI Plugin (`NF-476`) Hybrid (**Python + Go**) Telnyx carrier integration for **Open Valstorm**, mounted at `platform.telnyx` (`VALSTORM_PLUGINS=telnyx` or `*`). Provides full-stack enterprise telephony on Open Valstorm's provider-agnostic schemas (`phone_number`, `sms_thread`, `sms_message`, `sms_participant`, `phone_application`, `call`, `call_recording`): - **Programmable Messaging (1-on-1 SMS & Group MMS)**: Deterministic `tconv_` thread fingerprinting, automatic CRM contact matching, VFS MMS media attachments, and 10DLC campaign assignment. - **Call Control v2 & Custom Python Function IVR**: Outbound & inbound calling, `answer`, `bridge`, `transfer`, `speak`, `gather_using_speak`, `playback_start`, `record_start`, and `hangup`. - **High-Throughput Go Webhook Engine (`plugins/valstorm-plugin-telnyx/go`)**: Processes `telnyx:webhook_event` tasks for call state transitions, inbound/outbound SMS/MMS, S3 call recording & voicemail persistence, and **Deepgram Nova-2** speaker-diarized transcription. - **US Local & Toll-Free Number Porting Wizard**: Portability checks, draft porting orders, LOA PDF generation, bill/CSR uploads, FOC windows, and FastPort activation. - **Telnyx Voice AI Assistants**: Assistant provisioning, phone number binding, outbound AI test calls, and turn-by-turn transcript extraction. --- ## 🤖 Agent-Assisted Quick Setup (Let the Valstorm Agent Set It Up) You only need your **Telnyx API v2 Key** (`KEY019...`) from the Telnyx Mission Control Portal. Your **Valstorm AI Agent** can use that single key to automatically discover your Call Control Application, Messaging Profile, and active Phone Numbers, configure your tenant webhook URLs, save the encrypted credentials in `platform.credentials`, sync your `phone_number` (`phnu_`) records, register $O(1)$ webhook routing, and verify live connectivity. ### 1. Quick Human Prerequisite (60 Seconds in Telnyx Mission Control) 1. Sign in to [portal.telnyx.com](https://portal.telnyx.com/). 2. Go to **Account Settings -> Keys & Credentials -> API Keys** and click **Create API Key** (copy the `KEY...` string). 3. *(Optional)* Switch to the **Public Key** tab and copy your Base64 **Ed25519 Public Key** for webhook signature verification. ### 2. Copy-Paste Prompt for Your Valstorm Agent Drop this prompt into Valstorm Chat: > *"Set up `valstorm-plugin-telnyx` using my Telnyx API Key `` (optional Public Key: ``). Follow `plugins/valstorm-plugin-telnyx/README.md` to save the encrypted credentials via `platform.credentials.save('telnyx', ...)`, auto-discover my Messaging Profile, Call Control App, and Phone Numbers, sync them into `phone_number` records with $O(1)$ webhook routing, and run `telnyx_verify_connection`."* ### 3. Agent Execution Runbook (Instructions for the AI Agent) When a user asks you to set up `valstorm-plugin-telnyx`, execute this sequence autonomously: 1. **Validate & Save Encrypted Telnyx Credentials**: - Call `await platform.telnyx.save_credentials(api_key=..., public_key=..., validate_live=True)` (or `POST /v1/telnyx/admin/credentials`). 2. **Discover & Sync Account Inventory (Dry-Run -> Execute)**: - Call `await platform.telnyx.get_account_overview()` (`telnyx_get_account_overview`) to inspect existing Messaging Profiles, Call Control Applications, and Phone Numbers on the Telnyx account. - Call `await platform.telnyx.sync_phone_numbers()` to upsert `phone_number` (`phnu_`) records and register every E.164 phone number and `connection_id` in `external_account_routing` (`provider="telnyx"`). 3. **Configure Tenant Webhook URL**: - Configure the Call Control Application and Messaging Profile `webhook_url` to point to `https:///v1/webhooks/telnyx/`. 4. **Verify Live Connection**: - Call `telnyx_verify_connection` and report the account balance, active phone numbers, messaging profile, and webhook routing status back to the user. --- ## 🛠️ End-to-End DIY Setup Guide (Manual) ### Step 1: Create a Telnyx API v2 Key & Ed25519 Public Key 1. Log in to [portal.telnyx.com](https://portal.telnyx.com/). 2. Navigate to **Account Settings -> Keys & Credentials**: - Copy your **API v2 Key** (`KEY...`). - Copy your **Public Key** (Base64 Ed25519 key used to verify `telnyx-signature-ed25519` headers). ### Step 2: Configure a Messaging Profile & Call Control Application 1. **Messaging -> Programmable Messaging -> Messaging Profiles**: - Create or select a Messaging Profile and set the **Webhook URL** to: `https://api.yourdomain.com/v1/webhooks/telnyx/` 2. **Voice -> Programmable Voice -> Call Control Applications**: - Create or select a Call Control Application and set the **Webhook URL** to: `https://api.yourdomain.com/v1/webhooks/telnyx/` ### Step 3: Save Encrypted Credentials in Open Valstorm ```python await platform.credentials.save( "telnyx", { "api_key": "KEY0192837465...", "public_key": "YOUR_ED25519_PUBLIC_KEY", "connection_id": "YOUR_CALL_CONTROL_APP_ID", "messaging_profile_id": "YOUR_MESSAGING_PROFILE_ID", "default_from_number": "+18335832008", }, ) ``` --- ## Python & Go Usage Examples ### Python (`platform.telnyx`) ```python # 1. Send 1-on-1 SMS or Multi-Party Group MMS (auto-creates `sms_thread` & `sms_message`) msg = await platform.telnyx.send_sms( to_phone="+18605551234", from_number="+18335832008", message="Hi from Open Valstorm!", ) # 2. Initiate an Outbound Call (auto-creates `call` record) call = await platform.telnyx.initiate_call( to_phone="+18605551234", from_phone="+18335832008", ) # 3. Search & Order a New Phone Number available = await platform.telnyx.search_available_numbers(area_code="860", limit=5) ordered = await platform.telnyx.order_phone_number(phone_number=available[0]["phone_number"]) # 4. Check US Local & Toll-Free Portability portability = await platform.telnyx.check_portability(["+18605551234", "+18335832008"]) ``` --- ## REST API Endpoints (`/v1/telnyx/*` & `/v1/webhooks/telnyx/{org_id}`) | Method | Endpoint | Description | | :--- | :--- | :--- | | `GET` | `/v1/telnyx/setup-guide` | Interactive Telnyx Mission Control setup guide | | `POST` | `/v1/telnyx/verify` | Verifies Telnyx API Key, balance, and active phone inventory | | `POST` | `/v1/telnyx/message` | Sends 1-on-1 SMS or Group MMS & logs `sms_message` (`sms_`) | | `POST` | `/v1/telnyx/conversation` | Creates or resolves deterministic `sms_thread` (`smst_`) | | `POST` | `/v1/telnyx/call` | Initiates outbound Call Control v2 call & logs `call` (`call_`) | | `POST` | `/v1/telnyx/calls/{call_control_id}/{action}` | Executes Call Control action (`answer`, `hangup`, `bridge`, `transfer`, `speak`, `gather`, `record_start`) | | `GET` | `/v1/telnyx/admin/overview` | Returns Telnyx account balance, numbers, profiles, and apps | | `POST` | `/v1/telnyx/admin/credentials` | Validates and saves encrypted Telnyx API & Public keys | | `GET` | `/v1/telnyx/admin/numbers/search` | Searches available US/Toll-Free phone numbers | | `POST` | `/v1/telnyx/admin/numbers/order` | Orders a phone number and creates `phone_number` (`phnu_`) | | `POST` | `/v1/telnyx/admin/porting/portability-check` | Checks US Local & Toll-Free number portability | | `GET/POST` | `/v1/telnyx/admin/porting/orders` | Lists or creates Telnyx number porting orders | | `GET/POST` | `/v1/telnyx/admin/ai-assistants` | Lists or upserts Telnyx Voice AI Assistants | | `POST` | `/v1/webhooks/telnyx/{org_id}` | High-throughput webhook ingress -> enqueues `telnyx:webhook_event` to Go worker | --- ## Running Tests (Python & Go) ```bash # Run Python unit & route integration tests cd apps/open-api .venv/bin/pytest tests/test_plugin_telnyx.py -v # Run Go worker Call Control, SMS/MMS, & Deepgram transcription race tests cd ../../plugins/valstorm-plugin-telnyx/go go test -race -v ./... ```