Skip to content

Commit

Permalink
serializers(twilio): handle transport message frames
Browse files Browse the repository at this point in the history
  • Loading branch information
aconchillo committed Jan 30, 2025
1 parent 6a50759 commit 2634b03
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- `TwilioSerializer` now supports transport message frames. With this we can
create Twilio emulators.

- Added a new transport: `WebsocketClientTransport`.

- Added a `metadata` field to `Frame` which makes it possible to pass custom
Expand Down
13 changes: 8 additions & 5 deletions src/pipecat/serializers/twilio.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
InputDTMFFrame,
KeypadEntry,
StartInterruptionFrame,
TransportMessageFrame,
TransportMessageUrgentFrame,
)
from pipecat.serializers.base_serializer import FrameSerializer, FrameSerializerType

Expand All @@ -35,7 +37,10 @@ def type(self) -> FrameSerializerType:
return FrameSerializerType.TEXT

def serialize(self, frame: Frame) -> str | bytes | None:
if isinstance(frame, AudioRawFrame):
if isinstance(frame, StartInterruptionFrame):
answer = {"event": "clear", "streamSid": self._stream_sid}
return json.dumps(answer)
elif isinstance(frame, AudioRawFrame):
data = frame.audio

serialized_data = pcm_to_ulaw(data, frame.sample_rate, self._params.twilio_sample_rate)
Expand All @@ -47,10 +52,8 @@ def serialize(self, frame: Frame) -> str | bytes | None:
}

return json.dumps(answer)

if isinstance(frame, StartInterruptionFrame):
answer = {"event": "clear", "streamSid": self._stream_sid}
return json.dumps(answer)
elif isinstance(frame, (TransportMessageFrame, TransportMessageUrgentFrame)):
return json.dumps(frame.message)

def deserialize(self, data: str | bytes) -> Frame | None:
message = json.loads(data)
Expand Down

0 comments on commit 2634b03

Please sign in to comment.