Send a phone call from AI agent, in an API call. Or, directly call the bot from the configured phone number!
Python
feat: Using Twilio instead of Azure to send SMS
Clémence Lesné4ee7b613bb29
diff --git a/README.md b/README.mdindex f64be8d..723c4e8 100644--- a/README.md+++ b/README.md@@ -118,6 +118,11 @@ eventgrid:resource_group: claim-ai-pocsubscription_id: xxxsystem_topic: claim-ai-poc++twilio:+ account_sid: xxx+ auth_token: xxx+ phone_number: "+19291234567"```Then run:diff --git a/helpers/config_models/root.py b/helpers/config_models/root.pyindex fc1acc4..ca5e390 100644--- a/helpers/config_models/root.py+++ b/helpers/config_models/root.py@@ -5,6 +5,7 @@ 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@@ -17,4 +18,5 @@ class RootModel(BaseModel):monitoring: MonitoringModelopenai: OpenAiModelresources: ResourcesModel+ twilio: TwilioModelworkflow: WorkflowModeldiff --git a/helpers/config_models/twilio.py b/helpers/config_models/twilio.pynew file mode 100644index 0000000..21bc044--- /dev/null+++ b/helpers/config_models/twilio.py@@ -0,0 +1,7 @@+from pydantic import BaseModel, SecretStr+++class TwilioModel(BaseModel):+ account_sid: str+ auth_token: SecretStr+ phone_number: strdiff --git a/main.py b/main.pyindex 98d4d88..b29a85f 100644--- a/main.py+++ b/main.py@@ -25,6 +25,9 @@ from helpers.config import CONFIGfrom helpers.logging import build_loggerfrom helpers.version import VERSIONfrom models.action import ActionModel, Indent as IndentAction+from twilio.rest import Client+from twilio.base.exceptions import TwilioRestException+from twilio.rest import Clientfrom models.call import (CallModel,MessageModel as CallMessageModel,@@ -69,7 +72,11 @@ call_automation_client = CallAutomationClient(CONFIG.communication_service.access_key.get_secret_value()),)-sms_client = SmsClient(credential=AZ_CREDENTIAL, endpoint=CONFIG.communication_service.endpoint)+# 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()+)db = sqlite3.connect(".local.sqlite", check_same_thread=False)EVENTS_DOMAIN = environ.get("EVENTS_DOMAIN").strip("/")@@ -721,14 +728,20 @@ async def handle_hangup(client: CallConnectionClient, call: CallModel) -> None:_logger.info(f"SMS report ({call.id}): {content}")try:- sms_client.send(- from_=CONFIG.communication_service.phone_number,- message=content,+ # 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,to=call.phone_number,)_logger.info(f"SMS report sent ({call.id})")- except Exception:- _logger.warn(f"SMS report error ({call.id})", exc_info=True)+ except TwilioRestException:+ _logger.warn(f"Twilio SMS error ({call.id})", exc_info=True)def audio_from_text(text: str) -> TextSource:diff --git a/requirements.txt b/requirements.txtindex 2b1f4ff..8931330 100644--- a/requirements.txt+++ b/requirements.txt@@ -7,4 +7,5 @@ fastapi==0.108.0openai==1.7.0python-dotenv==1.0.0pyyaml==6.0.1+twilio==8.11.0uvicorn==0.25.0