Skip to content

Commit

Permalink
pass more context info like whatsapp phone number from bot integratio…
Browse files Browse the repository at this point in the history
…n to saved run
  • Loading branch information
devxpy committed Jul 21, 2024
1 parent 29e44c8 commit bcc070e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
5 changes: 5 additions & 0 deletions bots/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
44 changes: 44 additions & 0 deletions daras_ai_v2/bots.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 6 additions & 10 deletions functions/recipe_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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..."

Expand All @@ -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,
),
)

Expand Down

0 comments on commit bcc070e

Please sign in to comment.