Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!
Python
feat: Use Communication Services for SMS instead of Twilio
Clémence Lesnée47133eae417
diff --git a/README.md b/README.mdindex a9a67d7..beffb35 100644--- a/README.md+++ b/README.md@@ -69,9 +69,7 @@ graphagent(["Agent"])api["Claim AI"]- twilio["Twilio\n(external)"]- api -- Send SMS report --> twilioapi -- Transfer to --> agentapi -. Send voice .-> useruser -- Call --> api@@ -87,10 +85,10 @@ graphuser(["User"])agent(["Agent"])- twilio["Twilio\n(external)"]subgraph "Claim AI"- communication_service["Call gateway\n(Azure Communication Services)"]+ communication_service_call["Call gateway\n(Azure Communication Services)"]+ communication_service_sms["SMS gateway\n(Azure Communication Services)"]event_grid[("Broker\n(Azure Event Grid)")]api["API"]db_conversation[("Conversations")]@@ -98,20 +96,21 @@ graphgpt["GPT-4 Turbo\n(Azure OpenAI)"]end- api -- Answer with text --> communication_service+ api -- Answer with text --> communication_service_callapi -- Generate completion --> gptapi -- Save claim --> db_claimapi -- Save conversation --> db_conversation- api -- Send SMS report --> twilio- api -- Transfer to agent --> communication_service+ api -- Send SMS report --> communication_service_sms+ api -- Transfer to agent --> communication_service_callapi -. Watch .-> event_grid- communication_service -- Notifies --> event_grid- communication_service -- Transfer to --> agent+ communication_service_call -- Notifies --> event_grid+ communication_service_call -- Transfer to --> agent+ communication_service_call -. Send voice .-> user- communication_service -. Send voice .-> user+ communication_service_sms -- Send SMS --> user- user -- Call --> communication_service+ user -- Call --> communication_service_call```## Installation@@ -140,7 +139,7 @@ workflow:conversation_lang: fr-FRconversation_timeout_hour: 72-communication_service:+communication_service_call:access_key: xxxendpoint: https://xxx.france.communication.azure.comphone_number: "+33612345678"@@ -160,11 +159,6 @@ eventgrid:resource_group: claim-ai-pocsubscription_id: xxxsystem_topic: claim-ai-poc--twilio:- account_sid: xxx- auth_token: xxx- phone_number: "+19291234567"```If you want to use a Service Principal to authenticate to Azure, you can also add the following in a `.env` file:diff --git a/helpers/config_models/root.py b/helpers/config_models/root.pyindex ca5e390..fc1acc4 100644--- a/helpers/config_models/root.py+++ b/helpers/config_models/root.py@@ -5,7 +5,6 @@ from helpers.config_models.eventgrid import EventgridModelfrom helpers.config_models.monitoring import MonitoringModelfrom helpers.config_models.openai import OpenAiModelfrom helpers.config_models.resources import ResourcesModel-from helpers.config_models.twilio import TwilioModelfrom helpers.config_models.workflow import WorkflowModelfrom pydantic import BaseModel@@ -18,5 +17,4 @@ class RootModel(BaseModel):monitoring: MonitoringModelopenai: OpenAiModelresources: ResourcesModel- twilio: TwilioModelworkflow: WorkflowModeldiff --git a/helpers/config_models/twilio.py b/helpers/config_models/twilio.pydeleted file mode 100644index 0629a84..0000000--- a/helpers/config_models/twilio.py+++ /dev/null@@ -1,12 +0,0 @@-from pydantic import BaseModel, SecretStr-from pydantic_extra_types.phone_numbers import PhoneNumber---# E164 is standard accross all Microsoft services-PhoneNumber.phone_format = "E164"---class TwilioModel(BaseModel):- account_sid: str- auth_token: SecretStr- phone_number: PhoneNumberdiff --git a/main.py b/main.pyindex 3a18fff..5c4764c 100644--- a/main.py+++ b/main.py@@ -28,8 +28,6 @@ from helpers.version import VERSIONfrom models.action import ActionModel, Indent as IndentActionfrom models.reminder import ReminderModelfrom pydantic.json import pydantic_encoder-from twilio.base.exceptions import TwilioRestException-from twilio.rest import Clientfrom models.call import (CallModel,MessageModel as CallMessageModel,@@ -73,11 +71,7 @@ call_automation_client = CallAutomationClient(CONFIG.communication_service.access_key.get_secret_value()),)-# TODO: Wait for the legal approval to send SMS with Azure Communication Services-# sms_client = SmsClient(credential=AZ_CREDENTIAL, endpoint=CONFIG.communication_service.endpoint)-twilio_client = Client(- CONFIG.twilio.account_sid, CONFIG.twilio.auth_token.get_secret_value()-)+sms_client = SmsClient(credential=AZ_CREDENTIAL, endpoint=CONFIG.communication_service.endpoint)db = sqlite3.connect(".local.sqlite", check_same_thread=False)EVENTS_DOMAIN = environ.get("EVENTS_DOMAIN").strip("/")@@ -866,20 +860,20 @@ async def handle_hangup(client: CallConnectionClient, call: CallModel) -> None:_logger.info(f"SMS report ({call.id}): {content}")try:- # TODO: Wait for the legal approval to send SMS with Azure Communication Services- # sms_client.send(- # from_=CONFIG.communication_service.phone_number,- # message=content,- # to=call.phone_number,- # )- twilio_client.messages.create(- body=content,- from_=CONFIG.twilio.phone_number,+ responses = sms_client.send(+ from_=CONFIG.communication_service.phone_number,+ message=content,to=call.phone_number,)- _logger.info(f"SMS report sent ({call.id})")- except TwilioRestException:- _logger.warn(f"Twilio SMS error ({call.id})", exc_info=True)+ response = responses[0]++ if (response.successful):+ _logger.info(f"SMS report sent {response.message_id} to {response.to} ({call.id})")+ else:+ _logger.warn(f"Failed SMS to {response.to}, status {response.http_status_code}, error {response.error_message} ({call.id})")++ except Exception:+ _logger.warn(f"SMS error ({call.id})", exc_info=True)def audio_from_text(text: str) -> TextSource:diff --git a/requirements.txt b/requirements.txtindex 8a1c672..44b1942 100644--- a/requirements.txt+++ b/requirements.txt@@ -9,5 +9,4 @@ phonenumbers==8.13.27pydantic-extra-types==2.4.0python-dotenv==1.0.0pyyaml==6.0.1-twilio==8.11.0uvicorn==0.25.0