Skip to content

Commit

Permalink
fix chat urls without trailing slashes,
Browse files Browse the repository at this point in the history
clearer embed code for developers to be able to modify the config
  • Loading branch information
devxpy committed Aug 19, 2024
1 parent b66e728 commit 8b9df6a
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
17 changes: 13 additions & 4 deletions daras_ai_v2/bot_integration_widgets.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import json
from itertools import zip_longest
from textwrap import dedent

import gooey_gui as gui
from django.core.exceptions import ValidationError
from django.db import transaction
from django.utils.text import slugify
from furl import furl

import gooey_gui as gui
from app_users.models import AppUser
from bots.models import BotIntegration, BotIntegrationAnalysisRun, Platform
from daras_ai_v2 import settings, icons
Expand Down Expand Up @@ -311,19 +312,27 @@ def get_bot_test_link(bi: BotIntegration) -> str | None:
return None


def get_web_widget_embed_code(bi: BotIntegration) -> str:
def get_web_widget_embed_code(bi: BotIntegration, *, config: dict = None) -> str:
lib_src = get_app_route_url(
chat_lib_route,
path_params=dict(
integration_id=bi.api_integration_id(),
integration_name=slugify(bi.name) or "untitled",
),
).rstrip("/")
if config is None:
config = {}
return dedent(
f"""
"""
<div id="gooey-embed"></div>
<script async defer onload="GooeyEmbed.mount()" src="{lib_src}"></script>
<script>
function onLoadGooeyEmbed() {
GooeyEmbed.mount(%(config_json)s);
}
</script>
<script async defer onload="onLoadGooeyEmbed()" src="%(lib_src)s"></script>
"""
% dict(config_json=json.dumps(config), lib_src=lib_src)
).strip()


Expand Down
2 changes: 2 additions & 0 deletions routers/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ def chat_route(
request: Request, integration_id: str = None, integration_name: str = None
):
from routers.bots_api import api_hashids
from daras_ai_v2.bot_integration_widgets import get_web_widget_embed_code

try:
bi = BotIntegration.objects.get(id=api_hashids.decode(integration_id)[0])
Expand All @@ -514,6 +515,7 @@ def chat_route(
{
"request": request,
"bi": bi,
"embed_code": get_web_widget_embed_code(bi, config=dict(mode="fullscreen")),
"meta": raw_build_meta_tags(
url=get_og_url_path(request),
title=f"Chat with {bi.name}",
Expand Down
6 changes: 1 addition & 5 deletions templates/chat_fullscreen.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
{% endfor %}
</head>
<body>
<div id="gooey-embed"></div>
<script src="lib.js"></script>
<script>
GooeyEmbed.mount({ mode: "fullscreen"});
</script>
{{ embed_code | safe }}
</body>
</html>

0 comments on commit 8b9df6a

Please sign in to comment.