serializers(twilio): handle transport message frames
This commit is contained in:
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
|
- `TwilioSerializer` now supports transport message frames. With this we can
|
||||||
|
create Twilio emulators.
|
||||||
|
|
||||||
- Added a new transport: `WebsocketClientTransport`.
|
- Added a new transport: `WebsocketClientTransport`.
|
||||||
|
|
||||||
- Added a `metadata` field to `Frame` which makes it possible to pass custom
|
- Added a `metadata` field to `Frame` which makes it possible to pass custom
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ from pipecat.frames.frames import (
|
|||||||
InputDTMFFrame,
|
InputDTMFFrame,
|
||||||
KeypadEntry,
|
KeypadEntry,
|
||||||
StartInterruptionFrame,
|
StartInterruptionFrame,
|
||||||
|
TransportMessageFrame,
|
||||||
|
TransportMessageUrgentFrame,
|
||||||
)
|
)
|
||||||
from pipecat.serializers.base_serializer import FrameSerializer, FrameSerializerType
|
from pipecat.serializers.base_serializer import FrameSerializer, FrameSerializerType
|
||||||
|
|
||||||
@@ -35,7 +37,10 @@ class TwilioFrameSerializer(FrameSerializer):
|
|||||||
return FrameSerializerType.TEXT
|
return FrameSerializerType.TEXT
|
||||||
|
|
||||||
def serialize(self, frame: Frame) -> str | bytes | None:
|
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
|
data = frame.audio
|
||||||
|
|
||||||
serialized_data = pcm_to_ulaw(data, frame.sample_rate, self._params.twilio_sample_rate)
|
serialized_data = pcm_to_ulaw(data, frame.sample_rate, self._params.twilio_sample_rate)
|
||||||
@@ -47,10 +52,8 @@ class TwilioFrameSerializer(FrameSerializer):
|
|||||||
}
|
}
|
||||||
|
|
||||||
return json.dumps(answer)
|
return json.dumps(answer)
|
||||||
|
elif isinstance(frame, (TransportMessageFrame, TransportMessageUrgentFrame)):
|
||||||
if isinstance(frame, StartInterruptionFrame):
|
return json.dumps(frame.message)
|
||||||
answer = {"event": "clear", "streamSid": self._stream_sid}
|
|
||||||
return json.dumps(answer)
|
|
||||||
|
|
||||||
def deserialize(self, data: str | bytes) -> Frame | None:
|
def deserialize(self, data: str | bytes) -> Frame | None:
|
||||||
message = json.loads(data)
|
message = json.loads(data)
|
||||||
|
|||||||
Reference in New Issue
Block a user