From bcc070e13e1442040e70071701cbeeaefd68c9a0 Mon Sep 17 00:00:00 2001 From: Dev Aggarwal Date: Mon, 22 Jul 2024 02:23:36 +0530 Subject: [PATCH] pass more context info like whatsapp phone number from bot integration to saved run --- bots/models.py | 5 ++++ daras_ai_v2/bots.py | 44 +++++++++++++++++++++++++++++++++++ functions/recipe_functions.py | 16 +++++-------- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/bots/models.py b/bots/models.py index 9831858ec..2523d179d 100644 --- a/bots/models.py +++ b/bots/models.py @@ -1159,6 +1159,11 @@ def d30(self): def msgs_for_llm_context(self): return self.messages.all().as_llm_context(reset_at=self.reset_at) + def api_integration_id(self) -> str: + from routers.bots_api import api_hashids + + return api_hashids.encode(self.id) + class MessageQuerySet(models.QuerySet): def to_df(self, tz=pytz.timezone(settings.TIME_ZONE)) -> "pd.DataFrame": diff --git a/daras_ai_v2/bots.py b/daras_ai_v2/bots.py index 7774d7343..e00805385 100644 --- a/daras_ai_v2/bots.py +++ b/daras_ai_v2/bots.py @@ -377,6 +377,7 @@ def _process_and_send_msg( input_images=input_images, input_documents=input_documents, messages=saved_msgs, + variables=build_run_vars(bot.convo, bot.user_msg_id), ) if bot.user_language: body["user_language"] = bot.user_language @@ -491,6 +492,49 @@ def _process_and_send_msg( ) +def build_run_vars(convo: Conversation, user_msg_id: str): + from routers.bots_api import MSG_ID_PREFIX + + bi = convo.bot_integration + if bi.platform == Platform.WEB: + user_msg_id = user_msg_id.lstrip(MSG_ID_PREFIX) + variables = dict( + platform=Platform(bi.platform).name, + integration_id=bi.api_integration_id(), + integration_name=bi.name, + conversation_id=convo.api_integration_id(), + user_message_id=user_msg_id, + ) + match bi.platform: + case Platform.FACEBOOK: + variables["user_fb_page_name"] = convo.fb_page_name + variables["bot_fb_page_name"] = bi.fb_page_name + case Platform.INSTAGRAM: + variables["user_ig_username"] = convo.ig_username + variables["bot_ig_username "] = bi.ig_username + case Platform.WHATSAPP: + variables["user_wa_phone_number"] = ( + convo.wa_phone_number and convo.wa_phone_number.as_international + ) + variables["bot_wa_phone_number"] = ( + bi.wa_phone_number and bi.wa_phone_number.as_international + ) + case Platform.SLACK: + variables["slack_user_name"] = convo.slack_user_name + variables["slack_channel_name"] = convo.slack_channel_name + variables["slack_team_name"] = bi.slack_team_name + case Platform.WEB: + variables["web_user_id"] = convo.web_user_id + case Platform.TWILIO: + variables["user_twilio_phone_number"] = ( + convo.twilio_phone_number and convo.twilio_phone_number.as_international + ) + variables["bot_twilio_phone_number"] = ( + bi.twilio_phone_number and bi.twilio_phone_number.as_international + ) + return variables + + def _save_msgs( bot: BotInterface, input_images: list[str] | None, diff --git a/functions/recipe_functions.py b/functions/recipe_functions.py index ae34ca54f..a4b4e0183 100644 --- a/functions/recipe_functions.py +++ b/functions/recipe_functions.py @@ -36,6 +36,11 @@ def call_recipe_functions( if not functions: return variables = state.setdefault("variables", {}) + fn_vars = dict( + web_url=saved_run.get_app_url(), + request=json.loads(request.json(exclude_unset=True, exclude={"variables"})), + response={k: v for k, v in state.items() if k in response_model.__fields__}, + ) yield f"Running {trigger.name} hooks..." @@ -46,16 +51,7 @@ def call_recipe_functions( current_user=current_user, parent_pr=pr, request_body=dict( - variables=sr.state.get("variables", {}) - | variables - | dict( - request=json.loads( - request.json(exclude_unset=True, exclude={"variables"}) - ), - response={ - k: v for k, v in state.items() if k in response_model.__fields__ - }, - ), + variables=sr.state.get("variables", {}) | variables | fn_vars, ), )