Skip to content

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.md
index f64be8d..723c4e8 100644
--- a/README.md
+++ b/README.md
@@ -118,6 +118,11 @@ eventgrid:
resource_group: claim-ai-poc
subscription_id: xxx
system_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.py
index 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 EventgridModel
from helpers.config_models.monitoring import MonitoringModel
from helpers.config_models.openai import OpenAiModel
from helpers.config_models.resources import ResourcesModel
+from helpers.config_models.twilio import TwilioModel
from helpers.config_models.workflow import WorkflowModel
from pydantic import BaseModel
@@ -17,4 +18,5 @@ class RootModel(BaseModel):
monitoring: MonitoringModel
openai: OpenAiModel
resources: ResourcesModel
+ twilio: TwilioModel
workflow: WorkflowModel
diff --git a/helpers/config_models/twilio.py b/helpers/config_models/twilio.py
new file mode 100644
index 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: str
diff --git a/main.py b/main.py
index 98d4d88..b29a85f 100644
--- a/main.py
+++ b/main.py
@@ -25,6 +25,9 @@ from helpers.config import CONFIG
from helpers.logging import build_logger
from helpers.version import VERSION
from models.action import ActionModel, Indent as IndentAction
+from twilio.rest import Client
+from twilio.base.exceptions import TwilioRestException
+from twilio.rest import Client
from 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.txt
index 2b1f4ff..8931330 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -7,4 +7,5 @@ fastapi==0.108.0
openai==1.7.0
python-dotenv==1.0.0
pyyaml==6.0.1
+twilio==8.11.0
uvicorn==0.25.0