|
| 1 | +# Telnyx Plugin |
| 2 | + |
| 3 | +Telnyx plugin for Vision Agents enabling inbound and outbound phone calls with |
| 4 | +real-time bidirectional media streaming. |
| 5 | + |
| 6 | +## Features |
| 7 | + |
| 8 | +- **Media Streaming**: Bidirectional audio streaming via Telnyx Media Streaming |
| 9 | +- **Call Control**: Support for programmable inbound and outbound phone calls |
| 10 | +- **Call Registry**: Track active calls with metadata, stream objects, and |
| 11 | + validation tokens |
| 12 | +- **Audio Conversion**: PCMU, PCMA, and L16 RTP payload conversion |
| 13 | +- **WebSocket Management**: Handle Telnyx WebSocket media events |
| 14 | +- **Stream Bridge**: Attach a Telnyx phone participant to a Stream call |
| 15 | + |
| 16 | +## Installation |
| 17 | + |
| 18 | +```bash |
| 19 | +uv add "vision-agents[telnyx]" |
| 20 | +# or directly |
| 21 | +uv add vision-agents-plugins-telnyx |
| 22 | +``` |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +```python |
| 27 | +from vision_agents.plugins import telnyx |
| 28 | + |
| 29 | +# Create a call registry to track active calls |
| 30 | +registry = telnyx.CallRegistry() |
| 31 | + |
| 32 | +# Register a call from your Telnyx webhook handler |
| 33 | +call = registry.create( |
| 34 | + call_control_id="v2:abc123", |
| 35 | + webhook_data={"data": {"payload": {"from": "+15551234567"}}}, |
| 36 | +) |
| 37 | + |
| 38 | +# Create a media stream for the WebSocket connection |
| 39 | +stream = telnyx.MediaStream(websocket) |
| 40 | +await stream.accept() |
| 41 | + |
| 42 | +# Associate stream with call |
| 43 | +call.telnyx_stream = stream |
| 44 | + |
| 45 | +# Run the stream until Telnyx sends a stop event |
| 46 | +await stream.run() |
| 47 | +``` |
| 48 | + |
| 49 | +## Examples |
| 50 | + |
| 51 | +See [examples/](examples/) for minimal inbound and outbound Telnyx phone |
| 52 | +examples. |
| 53 | + |
| 54 | +```bash |
| 55 | +# Outbound call |
| 56 | +uv run plugins/telnyx/examples/outbound_call.py \ |
| 57 | + --setup-telnyx \ |
| 58 | + --from +15551234567 \ |
| 59 | + --to +15557654321 |
| 60 | + |
| 61 | +# Inbound call server |
| 62 | +uv run plugins/telnyx/examples/inbound_call.py \ |
| 63 | + --setup-telnyx \ |
| 64 | + --phone-number +15551234567 |
| 65 | +``` |
| 66 | + |
| 67 | +Telnyx phone calls require a Call Control App. The Call Control App is where |
| 68 | +Telnyx sends call webhooks such as `call.initiated`, `call.answered`, and |
| 69 | +`call.hangup`. It is also the `connection_id` used by the outbound Dial API. A |
| 70 | +forwarding-only phone-number connection is not enough for media streaming through |
| 71 | +this plugin. |
| 72 | + |
| 73 | +With `--setup-telnyx`, the examples create a temporary Call Control App and |
| 74 | +delete it on normal shutdown. The inbound example also routes the Telnyx number |
| 75 | +to the temporary app and restores the previous routing on shutdown. |
| 76 | + |
| 77 | +Without `--setup-telnyx`, the examples validate the common setup requirements: |
| 78 | + |
| 79 | +- `TELNYX_CALL_CONTROL_APP_ID` exists and is active |
| 80 | +- the Call Control App webhook URL matches `https://<NGROK_URL>/telnyx/events` |
| 81 | +- inbound phone numbers are routed to the Call Control App |
| 82 | +- restricted accounts verify outbound destination numbers before dialing |
| 83 | + |
| 84 | +## Components |
| 85 | + |
| 86 | +### TelnyxCall |
| 87 | + |
| 88 | +Dataclass representing an active call session: |
| 89 | + |
| 90 | +```python |
| 91 | +@dataclass |
| 92 | +class TelnyxCall: |
| 93 | + call_control_id: str |
| 94 | + token: str |
| 95 | + webhook_data: Optional[dict[str, Any]] |
| 96 | + telnyx_stream: Optional[TelnyxMediaStream] |
| 97 | + stream_call: Optional[Any] |
| 98 | + started_at: datetime |
| 99 | + ended_at: Optional[datetime] |
| 100 | + |
| 101 | + # Convenience properties from Telnyx webhook payloads |
| 102 | + from_number: Optional[str] |
| 103 | + to_number: Optional[str] |
| 104 | + call_status: Optional[str] |
| 105 | +``` |
| 106 | + |
| 107 | +### TelnyxCallRegistry |
| 108 | + |
| 109 | +In-memory registry for managing active calls: |
| 110 | + |
| 111 | +```python |
| 112 | +registry = telnyx.CallRegistry() |
| 113 | +registry.create(call_control_id, webhook_data=webhook_data) # Register new call |
| 114 | +registry.get(call_control_id) # Look up call |
| 115 | +registry.require(call_control_id) # Look up or raise |
| 116 | +registry.validate(call_control_id, token) # Validate media URL token |
| 117 | +registry.remove(call_control_id) # Remove and mark ended |
| 118 | +registry.list_active() # List active calls |
| 119 | +``` |
| 120 | + |
| 121 | +### TelnyxMediaStream |
| 122 | + |
| 123 | +Manages Telnyx Media Streaming WebSocket connections: |
| 124 | + |
| 125 | +```python |
| 126 | +stream = telnyx.MediaStream(websocket) |
| 127 | +await stream.accept() |
| 128 | + |
| 129 | +# Access the audio track for publishing |
| 130 | +stream.audio_track # AudioStreamTrack matching the Telnyx media format |
| 131 | + |
| 132 | +# Send audio back to Telnyx when bidirectional RTP streaming is enabled |
| 133 | +await stream.send_audio(pcm_data) |
| 134 | + |
| 135 | +# Run until the stream ends |
| 136 | +await stream.run() |
| 137 | +``` |
| 138 | + |
| 139 | +To send audio back to the call, start Telnyx streaming with |
| 140 | +`stream_bidirectional_mode=rtp`. The plugin supports PCMU and PCMA at 8 kHz, and |
| 141 | +L16 at 16 kHz. |
| 142 | + |
| 143 | +Use `attach_phone_to_call` to bridge audio between a Telnyx media stream and a |
| 144 | +Stream call: |
| 145 | + |
| 146 | +```python |
| 147 | +await telnyx.attach_phone_to_call(stream_call, stream, user_id="phone-user") |
| 148 | +``` |
| 149 | + |
| 150 | +## Audio Utilities |
| 151 | + |
| 152 | +```python |
| 153 | +from vision_agents.plugins.telnyx import ( |
| 154 | + TELNYX_DEFAULT_SAMPLE_RATE, |
| 155 | + TELNYX_L16_SAMPLE_RATE, |
| 156 | + l16_to_pcm, |
| 157 | + pcma_to_pcm, |
| 158 | + pcm_to_l16, |
| 159 | + pcm_to_pcma, |
| 160 | + pcm_to_pcmu, |
| 161 | + pcm_to_telnyx_payload, |
| 162 | + pcmu_to_pcm, |
| 163 | + telnyx_payload_to_pcm, |
| 164 | +) |
| 165 | + |
| 166 | +pcm = pcmu_to_pcm(payload) |
| 167 | +payload = pcm_to_pcmu(pcm) |
| 168 | +``` |
| 169 | + |
| 170 | +## Configuration |
| 171 | + |
| 172 | +| Parameter | Description | Default | |
| 173 | +|------------------------------|--------------------------------------|---------| |
| 174 | +| `TELNYX_DEFAULT_SAMPLE_RATE` | Telnyx PCMU and PCMA sample rate | `8000` | |
| 175 | +| `TELNYX_L16_SAMPLE_RATE` | Telnyx L16 bidirectional sample rate | `16000` | |
| 176 | + |
| 177 | +## Environment Variables |
| 178 | + |
| 179 | +- `TELNYX_API_KEY`: Your Telnyx API key for Call Control API requests. |
| 180 | +- `TELNYX_PHONE_NUMBER`: Telnyx caller ID or inbound number, in E.164 format. |
| 181 | + You can also pass this as `--from` or `--phone-number`. |
| 182 | +- `NGROK_URL`: Public HTTPS hostname that forwards to your local example server. |
| 183 | + The examples can also auto-detect a local ngrok tunnel. |
| 184 | +- `TELNYX_CALL_CONTROL_APP_ID`: Existing Call Control App ID. Required only when |
| 185 | + running without `--setup-telnyx`. |
| 186 | +- `TELNYX_PHONE_NUMBER_ID`: Telnyx phone number resource ID. Required for |
| 187 | + inbound only when running without `--setup-telnyx`. |
| 188 | + |
| 189 | +## Dependencies |
| 190 | + |
| 191 | +- vision-agents |
| 192 | +- numpy |
| 193 | +- fastapi |
0 commit comments