Skip to content

Commit

Permalink
Merge pull request #255 from Viking5274/main
Browse files Browse the repository at this point in the history
Fix twilio error
  • Loading branch information
aconchillo authored Jun 26, 2024
2 parents 2fd0424 + dd402da commit 8691d14
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
6 changes: 4 additions & 2 deletions examples/twilio-chatbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from pipecat.services.elevenlabs import ElevenLabsTTSService
from pipecat.transports.network.fastapi_websocket import FastAPIWebsocketTransport, FastAPIWebsocketParams
from pipecat.vad.silero import SileroVADAnalyzer
from pipecat.serializers.twilio import TwilioFrameSerializer

from loguru import logger

Expand All @@ -25,7 +26,7 @@
logger.add(sys.stderr, level="DEBUG")


async def run_bot(websocket_client):
async def run_bot(websocket_client, stream_sid):
async with aiohttp.ClientSession() as session:
transport = FastAPIWebsocketTransport(
websocket=websocket_client,
Expand All @@ -34,7 +35,8 @@ async def run_bot(websocket_client):
add_wav_header=False,
vad_enabled=True,
vad_analyzer=SileroVADAnalyzer(),
vad_audio_passthrough=True
vad_audio_passthrough=True,
serializer=TwilioFrameSerializer(stream_sid)
)
)

Expand Down
9 changes: 8 additions & 1 deletion examples/twilio-chatbot/server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

import uvicorn

from fastapi import FastAPI, WebSocket
Expand Down Expand Up @@ -26,8 +28,13 @@ async def start_call():
@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
await websocket.accept()
start_data = websocket.iter_text()
await start_data.__anext__()
call_data = json.loads(await start_data.__anext__())
print(call_data, flush=True)
stream_sid = call_data['start']['streamSid']
print("WebSocket connection accepted")
await run_bot(websocket)
await run_bot(websocket, stream_sid)


if __name__ == "__main__":
Expand Down
9 changes: 3 additions & 6 deletions src/pipecat/serializers/twilio.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class TwilioFrameSerializer(FrameSerializer):
AudioRawFrame: "audio",
}

def __init__(self):
self._sid = None
def __init__(self, stream_sid: str):
self._stream_sid = stream_sid

def serialize(self, frame: Frame) -> str | bytes | None:
if not isinstance(frame, AudioRawFrame):
Expand All @@ -30,7 +30,7 @@ def serialize(self, frame: Frame) -> str | bytes | None:
payload = base64.b64encode(serialized_data).decode("utf-8")
answer = {
"event": "media",
"streamSid": self._sid,
"streamSid": self._stream_sid,
"media": {
"payload": payload
}
Expand All @@ -41,9 +41,6 @@ def serialize(self, frame: Frame) -> str | bytes | None:
def deserialize(self, data: str | bytes) -> Frame | None:
message = json.loads(data)

if not self._sid:
self._sid = message["streamSid"] if "streamSid" in message else None

if message["event"] != "media":
return None
else:
Expand Down
4 changes: 2 additions & 2 deletions src/pipecat/transports/network/fastapi_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
class FastAPIWebsocketParams(TransportParams):
add_wav_header: bool = False
audio_frame_size: int = 6400 # 200ms
serializer: FrameSerializer = TwilioFrameSerializer()
serializer: FrameSerializer


class FastAPIWebsocketCallbacks(BaseModel):
Expand Down Expand Up @@ -125,7 +125,7 @@ class FastAPIWebsocketTransport(BaseTransport):
def __init__(
self,
websocket: WebSocket,
params: FastAPIWebsocketParams = FastAPIWebsocketParams(),
params: FastAPIWebsocketParams,
input_name: str | None = None,
output_name: str | None = None,
loop: asyncio.AbstractEventLoop | None = None):
Expand Down

0 comments on commit 8691d14

Please sign in to comment.