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

fix: Enable callback timeout for calls only, not API, nor SMS

Clémence Lesné5c0c0e137de5
diff --git a/app/main.py b/app/main.py
index df4fb3d..9d7ac5e 100644
--- a/app/main.py
+++ b/app/main.py
@@ -363,6 +363,7 @@ async def call_get(call_id_or_phone_number: str) -> CallGetModel:
# Second, try to get by phone number
phone_number = PhoneNumber(call_id_or_phone_number)
call = await _db.call_search_one(
+ callback_timeout=False,
phone_number=phone_number,
scheduler=scheduler,
)
@@ -498,6 +499,7 @@ async def sms_event(
async with get_scheduler() as scheduler:
# Get call
call = await _db.call_search_one(
+ callback_timeout=False,
phone_number=phone_number,
scheduler=scheduler,
)
@@ -987,6 +989,7 @@ async def twilio_sms_post(
async with get_scheduler() as scheduler:
# Get call
call = await _db.call_search_one(
+ callback_timeout=False,
phone_number=From,
scheduler=scheduler,
)
diff --git a/app/persistence/cosmos_db.py b/app/persistence/cosmos_db.py
index 08688bb..576dfdd 100644
--- a/app/persistence/cosmos_db.py
+++ b/app/persistence/cosmos_db.py
@@ -243,6 +243,7 @@ class CosmosDbStore(IStore):
self,
phone_number: str,
scheduler: Scheduler,
+ callback_timeout: bool = True,
) -> CallStateModel | None:
logger.debug("Loading last call for %s", phone_number)
@@ -255,6 +256,11 @@ class CosmosDbStore(IStore):
except ValidationError:
logger.debug("Parsing error", exc_info=True)
+ # Filter by timeout if needed
+ extra_where = ""
+ if callback_timeout:
+ extra_where = f"AND c.created_at >= DATETIMEADD('hh', -{await callback_timeout_hour(scheduler)}, GETCURRENTDATETIME())"
+
# Try live
call = None
try:
@@ -262,7 +268,7 @@ class CosmosDbStore(IStore):
async with self._use_client() as db:
items = db.query_items(
max_item_count=1,
- query=f"SELECT * FROM c WHERE (STRINGEQUALS(c.initiate.phone_number, @phone_number, true) OR STRINGEQUALS(c.claim.policyholder_phone, @phone_number, true)) AND c.created_at >= DATETIMEADD('hh', -{await callback_timeout_hour(scheduler)}, GETCURRENTDATETIME()) ORDER BY c.created_at DESC",
+ query=f"SELECT * FROM c WHERE (STRINGEQUALS(c.initiate.phone_number, @phone_number, true) OR STRINGEQUALS(c.claim.policyholder_phone, @phone_number, true)) {extra_where} ORDER BY c.created_at DESC",
parameters=[
{
"name": "@phone_number",
diff --git a/app/persistence/istore.py b/app/persistence/istore.py
index 960f4ae..3cfe792 100644
--- a/app/persistence/istore.py
+++ b/app/persistence/istore.py
@@ -54,6 +54,7 @@ class IStore(ABC):
self,
phone_number: str,
scheduler: Scheduler,
+ callback_timeout: bool = True,
) -> CallStateModel | None:
pass