Azure Communication Call Automation Client Library for Python

raw JSON →
1.5.0 verified Mon Apr 27 auth: no python

Microsoft Azure Communication Call Automation Client Library for Python, version 1.5.0. Provides APIs to manage and automate voice calls, including answering, connecting, and playing audio. Released monthly with the Azure SDK for Python.

pip install azure-communication-callautomation
error AttributeError: 'CallAutomationClient' object has no attribute 'create_call'
cause Incorrect method name or trying to use sync client with async method. The correct method is 'create_call' on sync client.
fix
Use 'client.create_call(call_invite=...)' for sync. For async, use async client and 'await client.create_call(...)'.
error HttpResponseError: (BadRequest) The callback URI is invalid.
cause The callback URL provided is not HTTPS or not publicly accessible.
fix
Ensure the callback URL starts with 'https://' and is reachable from the internet.
error ValueError: The connection string is not a valid Azure Communication Services connection string
cause The connection string format is incorrect or missing.
fix
Use a connection string from Azure portal, typically 'endpoint=https://...;accesskey=...'. Alternatively, use token credential.
breaking In version 1.5.0, the 'AnswerCallOptions' constructor changed: 'callback_url' is now required, and 'operation_context' is deprecated. Use 'options' parameter in answer_call.
fix Provide callback_url when creating AnswerCallOptions. Do not pass operation_context.
gotcha The 'CallAutomationClient' requires a valid 'callback_url' for all operations that receive events (answer, createCall, etc.). The callback URL must be publicly accessible HTTPS endpoint. Localhost will not work.
fix Use a publicly accessible HTTPS endpoint. For local development, use tools like ngrok.
deprecated Using 'PlayAudioOptions' with 'loop' parameter is deprecated in 1.5.0. Use 'PlayOptions' with 'loop' parameter instead.
fix Replace PlayAudioOptions with PlayOptions and set 'loop' accordingly.
gotcha The 'CallConnection' object is only valid for the duration of the call. Once the call is disconnected, any method call on it will raise an exception.
fix Store call connection id and recreate CallConnection if needed. Listen for call disconnect events to clean up.

Initializes CallAutomationClient and answers an incoming call. Requires a connection string or token credential. Callback URL must be HTTPS.

import os
from azure.communication.callautomation import CallAutomationClient, AnswerCallOptions, CallInvite, PhoneNumberIdentifier

connection_string = os.environ.get('COMMUNICATION_SERVICES_CONNECTION_STRING', '')
client = CallAutomationClient.from_connection_string(connection_string)

# Example: answer an incoming call
incoming_call_context = "incomingCallContextValue"  # from event
answer_options = AnswerCallOptions(callback_url="https://example.com/callback")
result = client.answer_call(incoming_call_context=incoming_call_context, options=answer_options)
print(f"Call answered: {result.call_connection_id}")