Formatting adjusted and the encoding selection moved from TelnyFrameSerilaizer to websocket_endpoint function in server.py

This commit is contained in:
Rafal Skorski
2025-01-30 12:52:30 +01:00
parent 9c22bd8df1
commit b93e4ab9cb
5 changed files with 24 additions and 22 deletions

View File

@@ -76,7 +76,7 @@ This project is a FastAPI-based chatbot that integrates with Telnyx to handle We
```
- In `templates/streams.xml`, replace `<your server url>` with your ngrok URL (without `https://`)
- The final URL should look like: `wss://abc123.ngrok.io/ws`
- The encoding (`bidirectionalCodec`) should be `PCMU` or `PCMA` depending on your needs. Based on selected encoding, set the outbound_encoding in the `TelnyxFrameSerializer` in `src/pipecat/serializers/telnyx.py`
- The encoding (`bidirectionalCodec`) should be `PCMU` or `PCMA` depending on your needs. Based on selected encoding, set the outbound_encoding in `server.py` when the bot is initialized.
- The inbound encoding can be controlled from the application configuration for inbound calls and dial/transfer commands for outbound calls.
## Running the Application

View File

@@ -34,7 +34,7 @@ logger.remove(0)
logger.add(sys.stderr, level="DEBUG")
async def run_bot(websocket_client, stream_id, encoding):
async def run_bot(websocket_client, stream_id, outbound_encoding, inbound_encoding):
transport = FastAPIWebsocketTransport(
websocket=websocket_client,
params=FastAPIWebsocketParams(
@@ -43,7 +43,7 @@ async def run_bot(websocket_client, stream_id, encoding):
vad_enabled=True,
vad_analyzer=SileroVADAnalyzer(),
vad_audio_passthrough=True,
serializer=TelnyxFrameSerializer(stream_id, encoding),
serializer=TelnyxFrameSerializer(stream_id, outbound_encoding, inbound_encoding),
),
)
@@ -55,9 +55,7 @@ async def run_bot(websocket_client, stream_id, encoding):
api_key=os.getenv("ELEVENLABS_API_KEY"),
voice_id="CwhRBWXzGAHq8TQ4Fs17",
output_format="pcm_24000",
params=ElevenLabsTTSService.InputParams(
language=Language.EN
)
params=ElevenLabsTTSService.InputParams(language=Language.EN),
)
messages = [

View File

@@ -16,13 +16,11 @@ app.add_middleware(
allow_headers=["*"],
)
@app.post("/")
async def start_call():
print("POST TeXML")
return HTMLResponse(content=open("templates/streams.xml").read(), media_type="application/xml")
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
@@ -31,10 +29,9 @@ async def websocket_endpoint(websocket: WebSocket):
call_data = json.loads(await start_data.__anext__())
print(call_data, flush=True)
stream_id = call_data["stream_id"]
encoding = call_data["start"]["media_format"]["encoding"]
outbound_encoding = call_data["start"]["media_format"]["encoding"]
print("WebSocket connection accepted")
await run_bot(websocket, stream_id, encoding)
await run_bot(websocket, stream_id, outbound_encoding, "PCMU")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8765)