Make call_sid optional in TwilioFrameSerializer
This commit is contained in:
@@ -13,6 +13,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
the Telnyx call when an `EndFrame` or `CancelFrame` is received. It is
|
the Telnyx call when an `EndFrame` or `CancelFrame` is received. It is
|
||||||
enabled by default and is configurable via the `auto_hang_up` `InputParam`.
|
enabled by default and is configurable via the `auto_hang_up` `InputParam`.
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- In `TwilioFrameSerializer`, `call_sid` is Optional so as to avoid a breaking
|
||||||
|
changed. `call_sid` is required to automatically hang up.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
- Fixed an issue where `TwilioFrameSerializer` would send two hang up commands:
|
- Fixed an issue where `TwilioFrameSerializer` would send two hang up commands:
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ class TwilioFrameSerializer(FrameSerializer):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
stream_sid: str,
|
stream_sid: str,
|
||||||
call_sid: str,
|
call_sid: Optional[str] = None,
|
||||||
account_sid: Optional[str] = None,
|
account_sid: Optional[str] = None,
|
||||||
auth_token: Optional[str] = None,
|
auth_token: Optional[str] = None,
|
||||||
params: InputParams = InputParams(),
|
params: InputParams = InputParams(),
|
||||||
@@ -75,9 +75,9 @@ class TwilioFrameSerializer(FrameSerializer):
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
stream_sid: The Twilio Media Stream SID.
|
stream_sid: The Twilio Media Stream SID.
|
||||||
call_sid: The associated Twilio Call SID.
|
call_sid: The associated Twilio Call SID (optional, but required for auto hang-up).
|
||||||
account_sid: Twilio account SID.
|
account_sid: Twilio account SID (required for auto hang-up).
|
||||||
auth_token: Twilio auth token.
|
auth_token: Twilio auth token (required for auto hang-up).
|
||||||
params: Configuration parameters.
|
params: Configuration parameters.
|
||||||
"""
|
"""
|
||||||
self._stream_sid = stream_sid
|
self._stream_sid = stream_sid
|
||||||
@@ -160,15 +160,26 @@ class TwilioFrameSerializer(FrameSerializer):
|
|||||||
|
|
||||||
account_sid = self._account_sid
|
account_sid = self._account_sid
|
||||||
auth_token = self._auth_token
|
auth_token = self._auth_token
|
||||||
|
call_sid = self._call_sid
|
||||||
|
|
||||||
|
if not call_sid or not account_sid or not auth_token:
|
||||||
|
missing = []
|
||||||
|
if not call_sid:
|
||||||
|
missing.append("call_sid")
|
||||||
|
if not account_sid:
|
||||||
|
missing.append("account_sid")
|
||||||
|
if not auth_token:
|
||||||
|
missing.append("auth_token")
|
||||||
|
|
||||||
if not account_sid or not auth_token:
|
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Cannot hang up Twilio call: account_sid and auth_token must be provided"
|
f"Cannot hang up Twilio call: missing required parameters: {', '.join(missing)}"
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
# Twilio API endpoint for updating calls
|
# Twilio API endpoint for updating calls
|
||||||
endpoint = f"https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Calls/{self._call_sid}.json"
|
endpoint = (
|
||||||
|
f"https://api.twilio.com/2010-04-01/Accounts/{account_sid}/Calls/{call_sid}.json"
|
||||||
|
)
|
||||||
|
|
||||||
# Create basic auth from account_sid and auth_token
|
# Create basic auth from account_sid and auth_token
|
||||||
auth = aiohttp.BasicAuth(account_sid, auth_token)
|
auth = aiohttp.BasicAuth(account_sid, auth_token)
|
||||||
@@ -180,12 +191,12 @@ class TwilioFrameSerializer(FrameSerializer):
|
|||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
async with session.post(endpoint, auth=auth, data=params) as response:
|
async with session.post(endpoint, auth=auth, data=params) as response:
|
||||||
if response.status == 200:
|
if response.status == 200:
|
||||||
logger.info(f"Successfully terminated Twilio call {self._call_sid}")
|
logger.info(f"Successfully terminated Twilio call {call_sid}")
|
||||||
else:
|
else:
|
||||||
# Get the error details for better debugging
|
# Get the error details for better debugging
|
||||||
error_text = await response.text()
|
error_text = await response.text()
|
||||||
logger.error(
|
logger.error(
|
||||||
f"Failed to terminate Twilio call {self._call_sid}: "
|
f"Failed to terminate Twilio call {call_sid}: "
|
||||||
f"Status {response.status}, Response: {error_text}"
|
f"Status {response.status}, Response: {error_text}"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user