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: Use Communication Services for SMS instead of Twilio

Clémence Lesnée47133eae417
diff --git a/README.md b/README.md
index a9a67d7..beffb35 100644
--- a/README.md
+++ b/README.md
@@ -69,9 +69,7 @@ graph
agent(["Agent"])
api["Claim AI"]
- twilio["Twilio\n(external)"]
- api -- Send SMS report --> twilio
api -- Transfer to --> agent
api -. Send voice .-> user
user -- Call --> api
@@ -87,10 +85,10 @@ graph
user(["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 @@ graph
gpt["GPT-4 Turbo\n(Azure OpenAI)"]
end
- api -- Answer with text --> communication_service
+ api -- Answer with text --> communication_service_call
api -- Generate completion --> gpt
api -- Save claim --> db_claim
api -- 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_call
api -. 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-FR
conversation_timeout_hour: 72
-communication_service:
+communication_service_call:
access_key: xxx
endpoint: https://xxx.france.communication.azure.com
phone_number: "+33612345678"
@@ -160,11 +159,6 @@ eventgrid:
resource_group: claim-ai-poc
subscription_id: xxx
system_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.py
index 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 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
@@ -18,5 +17,4 @@ 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
deleted file mode 100644
index 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: PhoneNumber
diff --git a/main.py b/main.py
index 3a18fff..5c4764c 100644
--- a/main.py
+++ b/main.py
@@ -28,8 +28,6 @@ from helpers.version import VERSION
from models.action import ActionModel, Indent as IndentAction
from models.reminder import ReminderModel
from pydantic.json import pydantic_encoder
-from twilio.base.exceptions import TwilioRestException
-from twilio.rest import Client
from 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.txt
index 8a1c672..44b1942 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -9,5 +9,4 @@ phonenumbers==8.13.27
pydantic-extra-types==2.4.0
python-dotenv==1.0.0
pyyaml==6.0.1
-twilio==8.11.0
uvicorn==0.25.0