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: Data validation for call contexts

Clémence Lesnécbaa2ed4b525
diff --git a/function_app.py b/function_app.py
index 3f81c52..40a40a9 100644
--- a/function_app.py
+++ b/function_app.py
@@ -810,12 +810,27 @@ async def twilio_sms_post(
)
-def _str_to_contexts(contexts: Optional[str]) -> Optional[set[CallContextEnum]]:
- return (
- {CallContextEnum(context) for context in json.loads(contexts)}
- if contexts
- else None
- )
+def _str_to_contexts(value: Optional[str]) -> Optional[set[CallContextEnum]]:
+ """
+ Convert a string to a set of contexts.
+
+ The string is a JSON array of strings.
+
+ Returns a set of `CallContextEnum` or None.
+ """
+ if not value:
+ return None
+ try:
+ contexts = json.loads(value)
+ except (json.JSONDecodeError, TypeError):
+ return None
+ res = set()
+ for context in contexts:
+ try:
+ res.add(CallContextEnum(context))
+ except ValueError:
+ logger.warning("Unknown context %s, skipping", context)
+ return res or None
def _validation_error(