Overview
TelnyxFrameSerializer
enables integration with Telnyx’s WebSocket media streaming protocol, allowing your Pipecat application to handle phone calls via Telnyx’s voice services.
Features
- Bidirectional audio conversion between Pipecat and Telnyx
- DTMF (touch-tone) event handling
- Automatic call termination via Telnyx’s REST API
- Support for multiple audio encodings (PCMU, PCMA)
Installation
The TelnyxFrameSerializer
does not require any additional dependencies beyond the core Pipecat library.
Configuration
Constructor Parameters
The encoding type for outbound audio (e.g., “PCMU”, “PCMA”)
The encoding type for inbound audio (e.g., “PCMU”, “PCMA”)
call_control_id
Optional[str]
default:"None"
The Call Control ID for the Telnyx call (required for auto hang-up)
api_key
Optional[str]
default:"None"
Your Telnyx API key (required for auto hang-up)
params
InputParams
default:"InputParams()"
Sample rate used by Telnyx (typically 8kHz)
Optional override for pipeline input sample rate
Audio encoding for data sent to Telnyx
Audio encoding for data received from Telnyx
Whether to automatically terminate call on EndFrame
Basic Usage
from pipecat.serializers.telnyx import TelnyxFrameSerializer
from pipecat.transports.network.fastapi_websocket import (
FastAPIWebsocketTransport,
FastAPIWebsocketParams
)
# Extract required values from Telnyx WebSocket connection
stream_id = call_data["stream_id"]
call_control_id = call_data["start"]["call_control_id"]
outbound_encoding = call_data["start"]["media_format"]["encoding"]
# Create serializer
serializer = TelnyxFrameSerializer(
stream_id=stream_id,
outbound_encoding=outbound_encoding,
inbound_encoding="PCMU",
call_control_id=call_control_id,
api_key=os.getenv("TELNYX_API_KEY")
)
# Use with FastAPIWebsocketTransport
transport = FastAPIWebsocketTransport(
websocket=websocket,
params=FastAPIWebsocketParams(
audio_in_enabled=True,
audio_out_enabled=True,
vad_analyzer=SileroVADAnalyzer(),
serializer=serializer,
)
)
Hang-up Functionality
When auto_hang_up
is enabled, the serializer will automatically hang up the Telnyx call when an EndFrame
or CancelFrame
is processed, using Telnyx’s REST API:
# Properly configured with hang-up support
serializer = TelnyxFrameSerializer(
stream_id=stream_id,
outbound_encoding=outbound_encoding,
inbound_encoding="PCMU",
call_control_id=call_control_id, # Required for auto hang-up
api_key=os.getenv("TELNYX_API_KEY") # Required for auto hang-up
)
Server Code Example
Here’s a complete example of handling a Telnyx WebSocket connection:
from fastapi import FastAPI, WebSocket
from pipecat.serializers.telnyx import TelnyxFrameSerializer
import json
import os
app = FastAPI()
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
# Read initial messages from Telnyx
start_data = websocket.iter_text()
await start_data.__anext__() # Skip first message
# Parse the second message to get call details
call_data = json.loads(await start_data.__anext__())
# Extract Telnyx-specific IDs and encoding
stream_id = call_data["stream_id"]
call_control_id = call_data["start"]["call_control_id"]
outbound_encoding = call_data["start"]["media_format"]["encoding"]
# Create serializer with API key for auto hang-up
serializer = TelnyxFrameSerializer(
stream_id=stream_id,
outbound_encoding=outbound_encoding,
inbound_encoding="PCMU",
call_control_id=call_control_id,
api_key=os.getenv("TELNYX_API_KEY"),
)
# Continue with transport and pipeline setup...