Skip to content

Commit

Permalink
almost done
Browse files Browse the repository at this point in the history
  • Loading branch information
hagen-danswer committed Nov 19, 2024
1 parent 41ea202 commit e73532b
Show file tree
Hide file tree
Showing 21 changed files with 238 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def upgrade() -> None:

session = Session(bind=op.get_bind())
new_slack_bot = SlackBot(
name="Slack App (Migrated)",
name="Slack Bot (Migrated)",
enabled=True,
bot_token=bot_token,
app_token=app_token,
Expand Down
8 changes: 4 additions & 4 deletions backend/danswer/danswerbot/slack/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,24 @@ def get_slack_channel_config_for_bot_and_channel(

def validate_channel_name(
db_session: Session,
slack_bot_id: int,
channel_name: str,
current_slack_channel_config_id: int | None,
) -> str:
"""Make sure that this channel_name does not exist in other Slack channel configs.
Returns a cleaned up channel name (e.g. '#' removed if present)"""
slack_bot_configs = fetch_slack_channel_configs(
db_session=db_session, slack_bot_id=slack_bot_id
db_session=db_session,
)
cleaned_channel_name = channel_name.lstrip("#").lower()
for slack_channel_config in slack_bot_configs:
if slack_channel_config.id == current_slack_channel_config_id:
continue

if cleaned_channel_name in slack_channel_config.channel_config["channel_name"]:
if cleaned_channel_name == slack_channel_config.channel_config["channel_name"]:
raise ValueError(
f"Channel name '{channel_name}' already exists in "
"another Slack channel config"
"another Slack channel config with in Slack Bot with name: "
f"{slack_channel_config.slack_bot.name}"
)

return cleaned_channel_name
Expand Down
27 changes: 15 additions & 12 deletions backend/danswer/db/slack_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def update_slack_bot(
) -> SlackBot:
slack_bot = db_session.scalar(select(SlackBot).where(SlackBot.id == slack_bot_id))
if slack_bot is None:
raise ValueError(f"Unable to find slack app with ID {slack_bot_id}")
raise ValueError(f"Unable to find Slack Bot with ID {slack_bot_id}")

# update the app
slack_bot.name = name
Expand All @@ -48,25 +48,28 @@ def update_slack_bot(
return slack_bot


def remove_slack_bot(
def fetch_slack_bot(
db_session: Session,
slack_bot_id: int,
) -> None:
) -> SlackBot:
slack_bot = db_session.scalar(select(SlackBot).where(SlackBot.id == slack_bot_id))
if slack_bot is None:
raise ValueError(f"Unable to find slack app with ID {slack_bot_id}")

db_session.delete(slack_bot)
db_session.commit()
raise ValueError(f"Unable to find Slack Bot with ID {slack_bot_id}")

return slack_bot

def fetch_slack_bot(db_session: Session, slack_bot_id: int) -> SlackBot:
slack_bot = db_session.scalar(select(SlackBot).where(SlackBot.id == slack_bot_id))

if slack_bot is None:
raise ValueError(f"Unable to find slack app with ID {slack_bot_id}")
def remove_slack_bot(
db_session: Session,
slack_bot_id: int,
) -> None:
slack_bot = fetch_slack_bot(
db_session=db_session,
slack_bot_id=slack_bot_id,
)

return slack_bot
db_session.delete(slack_bot)
db_session.commit()


def fetch_slack_bots(db_session: Session) -> Sequence[SlackBot]:
Expand Down
12 changes: 7 additions & 5 deletions backend/danswer/server/manage/slack_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ def _form_channel_config(
try:
cleaned_channel_name = validate_channel_name(
db_session=db_session,
slack_bot_id=slack_channel_config_creation_request.slack_bot_id,
channel_name=raw_channel_name,
current_slack_channel_config_id=current_slack_channel_config_id,
)
Expand Down Expand Up @@ -244,21 +243,24 @@ def patch_bot(
def delete_bot(
slack_bot_id: int,
db_session: Session = Depends(get_session),
user: User | None = Depends(current_admin_user),
_: User | None = Depends(current_admin_user),
) -> None:
remove_slack_bot(
db_session=db_session,
slack_bot_id=slack_bot_id,
)


@router.get("/admin/slack-app/bots/{bot_id}")
@router.get("/admin/slack-app/bots/{slack_bot_id}")
def get_bot_by_id(
bot_id: int,
slack_bot_id: int,
db_session: Session = Depends(get_session),
_: User | None = Depends(current_admin_user),
) -> SlackBot:
slack_bot_model = fetch_slack_bot(db_session=db_session, slack_bot_id=bot_id)
slack_bot_model = fetch_slack_bot(
db_session=db_session,
slack_bot_id=slack_bot_id,
)
return SlackBot.from_model(slack_bot_model)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export function SlackBotTable({ slackBots }: { slackBots: SlackBot[] }) {
{slackBotsForPage.map((slackBot) => {
return (
<ClickableTableRow
url={`/admin/bot/app/${slackBot.id}`}
url={`/admin/bots/bot/${slackBot.id}`}
key={slackBot.id}
className="hover:bg-muted cursor-pointer"
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { SlackBot } from "@/lib/types";
import { TextFormField } from "@/components/admin/connectors/Field";
import CardSection from "@/components/admin/CardSection";
import { Button } from "@/components/ui/button";
import { updateSlackApp, SlackAppCreationRequest } from "./app/new/lib";
import { updateSlackBot, SlackBotCreationRequest } from "./bot/new/lib";

interface SlackBotTokensFormProps {
onClose: () => void;
setPopup: (popupSpec: PopupSpec | null) => void;
Expand Down Expand Up @@ -42,9 +43,9 @@ export const SlackBotTokensForm = ({
}

formikHelpers.setSubmitting(true);
const response = await updateSlackApp(
const response = await updateSlackBot(
existingSlackApp?.id || 0,
values as SlackAppCreationRequest
values as SlackBotCreationRequest
);
formikHelpers.setSubmitting(false);
if (response.ok) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const SlackChannelConfigCreationForm = ({
}
formikHelpers.setSubmitting(false);
if (response.ok) {
router.push(`/admin/bot/app/${slack_bot_id}?u=${Date.now()}`);
router.push(`/admin/bots/bot/${slack_bot_id}?u=${Date.now()}`);
} else {
const responseJson = await response.json();
const errorMsg = responseJson.detail || responseJson.message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function SlackChannelConfigsTable({
<div className="flex gap-x-2">
<Link
className="cursor-pointer my-auto"
href={`/admin/bot/${slackChannelConfig.id}`}
href={`/admin/bots/${slackChannelConfig.id}`}
>
<EditIcon />
</Link>
Expand Down Expand Up @@ -132,7 +132,8 @@ export function SlackChannelConfigsTable({
colSpan={4}
className="text-center text-muted-foreground"
>
Add a new Slack bot configuration and it will show up here
Please add a New Slack Bot Configuration to begin chatting
with Danswer!
</TableCell>
</TableRow>
)}
Expand Down
File renamed without changes.
31 changes: 31 additions & 0 deletions web/src/app/admin/bots/bot/SlackBotCreationForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use client";

import { usePopup } from "@/components/admin/connectors/Popup";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { SlackTokensForm } from "./SlackTokensForm";

export const NewSlackBotForm = ({}: {}) => {
const [formValues] = useState({
name: "",
enabled: true,
bot_token: "",
app_token: "",
});
const { popup, setPopup } = usePopup();
const router = useRouter();

return (
<div>
{popup}
<div className="p-4">
<SlackTokensForm
isUpdate={false}
initialValues={formValues}
setPopup={setPopup}
router={router}
/>
</div>
</div>
);
};
Loading

0 comments on commit e73532b

Please sign in to comment.