Skip to content

Commit

Permalink
Refactor integration connection and fix usability problems with integ…
Browse files Browse the repository at this point in the history
…rations for new users

- Add `AppUser.first_name_possesive`
- Set `BotIntegration` streaming default to `True` and don't overwrite streaming=True in integration connect code
- Simplify run title generation in `BasePage`.
- Create `bot_integration_connect.py` for common integration methods.
- Streamline integration logic: use published run only & pass pr_id in the integration redirect url state
- Remove integration welcome screen for logged-in users - simply duplicate the published run for them
- Cleanup URL generation and fix redirects on the integrations page
- Add user nudges for unpublished changes + for non-owners
- Fix slack personal channel creation: Avoid setting slack_create_personal_channels=False after connect so re-connect force creates personal channels
  • Loading branch information
devxpy committed Aug 19, 2024
1 parent 8087c2b commit ad1501e
Show file tree
Hide file tree
Showing 12 changed files with 229 additions and 214 deletions.
14 changes: 14 additions & 0 deletions app_users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,20 @@ def first_name(self):
return str(self.phone_number)
return "Anon"

def first_name_possesive(self) -> str:
if self.display_name:
name = self.display_name.split(" ")[0]
elif self.email:
name = self.email.split("@")[0]
elif self.phone_number:
name = str(self.phone_number)
else:
return "My"
if name.endswith("s"):
return name + "'"
else:
return name + "'s"

@db_middleware
@transaction.atomic
def add_balance(
Expand Down
18 changes: 18 additions & 0 deletions bots/migrations/0080_alter_botintegration_streaming_enabled.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2024-08-19 16:46

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('bots', '0079_remove_botintegration_twilio_asr_language_and_more'),
]

operations = [
migrations.AlterField(
model_name='botintegration',
name='streaming_enabled',
field=models.BooleanField(default=True, help_text='If set, the bot will stream messages to the frontend (Slack, Web & Whatsapp only)'),
),
]
4 changes: 2 additions & 2 deletions bots/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,8 +698,8 @@ class BotIntegration(models.Model):
)

streaming_enabled = models.BooleanField(
default=False,
help_text="If set, the bot will stream messages to the frontend (Slack & Web only)",
default=True,
help_text="If set, the bot will stream messages to the frontend",
)

created_at = models.DateTimeField(auto_now_add=True)
Expand Down
9 changes: 1 addition & 8 deletions daras_ai_v2/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,6 @@ def _render_header(self):
and published_run.saved_run != current_run
and self.request
and self.request.user
and published_run.created_by == self.request.user
)

if can_user_edit_run and has_unpublished_changes:
Expand Down Expand Up @@ -589,13 +588,7 @@ def _render_publish_modal(
title = published_run.title or self.title
else:
recipe_title = self.get_root_published_run().title or self.title
if self.request.user.display_name:
username = self.request.user.display_name + "'s"
elif self.request.user.email:
username = self.request.user.email.split("@")[0] + "'s"
else:
username = "My"
title = f"{username} {recipe_title}"
title = f"{self.request.user.first_name_possesive()} {recipe_title}"
published_run_title = gui.text_input(
"##### Title",
key="published_run_title",
Expand Down
44 changes: 44 additions & 0 deletions daras_ai_v2/bot_integration_connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import json

from fastapi import HTTPException, Request

from bots.models import (
BotIntegration,
Platform,
PublishedRun,
)
from daras_ai_v2.base import RecipeTabs


def connect_bot_to_published_run(
bi: BotIntegration, published_run: PublishedRun | None
) -> str:
"""
Connect the bot integration to the provided saved and published runs.
Returns the redirect url to the integrations page for that bot integration.
"""

from daras_ai_v2.slack_bot import send_confirmation_msg
from recipes.VideoBots import VideoBotsPage

print(f"Connecting {bi} to {published_run}")

bi.published_run = published_run
bi.save(update_fields=["published_run"])

if bi.platform == Platform.SLACK:
send_confirmation_msg(bi)

return VideoBotsPage.app_url(
tab=RecipeTabs.integrations,
example_id=published_run.published_run_id,
path_params=dict(integration_id=bi.api_integration_id()),
)


def load_published_run_from_state(request: Request) -> PublishedRun:
pr_id = json.loads(request.query_params.get("state") or "{}").get("pr_id")
try:
return PublishedRun.objects.get(id=pr_id)
except PublishedRun.DoesNotExist:
raise HTTPException(status_code=404, detail="Published Run not found")
35 changes: 35 additions & 0 deletions daras_ai_v2/bot_integration_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,41 @@
from routers.root import RecipeTabs, chat_route, chat_lib_route


def integrations_welcome_screen(title: str):
with gui.center():
gui.markdown(f"#### {title}")

col1, col2, col3 = gui.columns(
3,
column_props=dict(
style=dict(
display="flex",
flexDirection="column",
alignItems="center",
textAlign="center",
maxWidth="300px",
),
),
style={"justifyContent": "center"},
)
with col1:
gui.html("🏃‍♀️", style={"fontSize": "4rem"})
gui.markdown(
"""
1. Fork & Save your Run
"""
)
gui.caption("Make changes, Submit & Save your perfect workflow")
with col2:
gui.image(icons.integrations_img, alt="Integrations", style={"height": "5rem"})
gui.markdown("2. Connect to Slack, Whatsapp or your App")
gui.caption("Or Facebook, Instagram and the web. Wherever your users chat.")
with col3:
gui.html("📈", style={"fontSize": "4rem"})
gui.markdown("3. Test, Analyze & Iterate")
gui.caption("Analyze your usage. Update your Saved Run to test changes.")


def general_integration_settings(bi: BotIntegration, current_user: AppUser):
if gui.session_state.get(f"_bi_reset_{bi.id}"):
gui.session_state[f"_bi_streaming_enabled_{bi.id}"] = (
Expand Down
2 changes: 2 additions & 0 deletions daras_ai_v2/icons.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@
"jcb": '<i class="fa-brands fa-cc-jcb"></i>',
"diners": '<i class="fa-brands fa-cc-diners-club"></i>',
}

integrations_img = "https://storage.googleapis.com/dara-c1b52.appspot.com/daras_ai/media/c3ba2392-d6b9-11ee-a67b-6ace8d8c9501/image.png"
Loading

0 comments on commit ad1501e

Please sign in to comment.