feat: add amd callback

This commit is contained in:
Sabiha Khan 2026-03-03 20:12:04 +05:30
parent aed5a782fb
commit fb08f56524
11 changed files with 308 additions and 137 deletions

View file

@ -352,3 +352,17 @@ class TelephonyProvider(ABC):
True if provider supports call transfers, False otherwise
"""
pass
@abstractmethod
async def hang_up(self, call_id: str, **kwargs: Any) -> Dict[str, Any]:
"""
Terminate an active call.
Args:
call_id: Provider-specific call identifier
**kwargs: Provider-specific additional parameters
Returns:
Dict containing hangup response (format varies by provider)
"""
pass

View file

@ -418,3 +418,8 @@ class ARIProvider(TelephonyProvider):
f"&app={self.app_name}"
f"&subscribeAll=true"
)
async def hang_up(self, call_id: str, **kwargs: Any) -> Dict[str, Any]:
"""Terminate an ARI call."""
# TODO: Implement ARI call termination
return {"status": "not_implemented"}

View file

@ -106,6 +106,16 @@ class CloudonixProvider(TelephonyProvider):
"caller-id": from_number, # Required field
}
# Enable and process AMD
data["machineDetection"] = "DetectMessageEnd"
data["asyncAmd"] = True
data["machineDetectionTimeout"] = 30
data["machineDetectionSpeechThreshold"] = 2500
data["machineDetectionSpeechEndThreshold"] = 6000
data["machineDetectionSilenceTimeout"] = 2500
data["asyncAmdStatusCallback"] = f"{backend_endpoint}/api/v1/telephony/cloudonix/amd-callback/{workflow_run_id}"
data["asyncAmdStatusCallbackMethod"]= "POST"
# TODO: Cloudonix status callbacks are spammy, so commenting it out. Can send it to
# some persistent logging system instead of transcational database.
# Add status callback if workflow_run_id provided
@ -682,6 +692,42 @@ class CloudonixProvider(TelephonyProvider):
return Response(content=twiml, media_type="application/xml"), "application/xml"
async def hang_up(self, call_id: str, **kwargs: Any) -> Dict[str, Any]:
# Construct the DELETE session endpoint
# Using "self" as customer-id as per Cloudonix documentation
endpoint = (
f"{self.base_url}/customers/self/domains/{self.domain_id}/sessions/{call_id}"
)
# Prepare headers with Bearer token authentication
headers = {
"Authorization": f"Bearer {self.bearer_token}",
"Content-Type": "application/json",
}
logger.info(f"Terminating Cloudonix call {call_id} via DELETE {endpoint}")
# Make the DELETE request to terminate the session
async with aiohttp.ClientSession() as session:
async with session.delete(endpoint, headers=headers) as response:
status = response.status
response_text = await response.text()
if status in (200, 204, 404):
# 200/204: Success
# 404: Session already terminated (acceptable)
logger.info(
f"Successfully terminated Cloudonix session {call_id} "
f"(HTTP {status}), Response: {response_text}"
)
else:
logger.warning(
f"Unexpected response terminating Cloudonix session {call_id}: "
f"HTTP {status}, Response: {response_text}"
)
return {"status": "success"}
# ======== CALL TRANSFER METHODS ========
async def transfer_call(

View file

@ -587,3 +587,8 @@ class TwilioProvider(TelephonyProvider):
True - Twilio provider supports call transfers
"""
return True
async def hang_up(self, call_id: str, **kwargs: Any) -> Dict[str, Any]:
"""Terminate a Twilio call."""
# TODO: Implement Twilio call termination
return {"status": "not_implemented"}

View file

@ -560,3 +560,8 @@ class VobizProvider(TelephonyProvider):
False - Vobiz provider does not support call transfers
"""
return False
async def hang_up(self, call_id: str, **kwargs: Any) -> Dict[str, Any]:
"""Terminate a Vobiz call."""
# TODO: Implement Vobiz call termination
return {"status": "not_implemented"}

View file

@ -511,3 +511,8 @@ class VonageProvider(TelephonyProvider):
False - Vonage provider does not support call transfers
"""
return False
async def hang_up(self, call_id: str, **kwargs: Any) -> Dict[str, Any]:
"""Terminate a Vonage call."""
# TODO: Implement Vonage call termination
return {"status": "not_implemented"}