Skip to content

Commit

Permalink
Misc slack bot improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Weves committed Sep 7, 2023
1 parent 7972c8a commit ccbc69d
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions backend/danswer/bots/slack/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ def _build_custom_semantic_identifier(
truncated_blurb = UserIdReplacer.replace_tags_basic(truncated_blurb)
truncated_blurb = UserIdReplacer.replace_channels_basic(truncated_blurb)
truncated_blurb = UserIdReplacer.replace_special_mentions(truncated_blurb)
truncated_blurb = UserIdReplacer.replace_links(truncated_blurb)
# stop as soon as we see a newline, since these break the link
truncated_blurb = truncated_blurb.split("\n")[0]
if truncated_blurb:
return f"#{semantic_identifier}: {truncated_blurb}"
else:
Expand Down
18 changes: 17 additions & 1 deletion backend/danswer/bots/slack/handlers/handle_feedback.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
from slack_sdk import WebClient
from sqlalchemy.orm import Session

from danswer.configs.constants import QAFeedbackType
from danswer.db.engine import get_sqlalchemy_engine
from danswer.db.feedback import update_query_event_feedback


def handle_qa_feedback(query_id: int, feedback_type: QAFeedbackType) -> None:
def handle_qa_feedback(
query_id: int,
feedback_type: QAFeedbackType,
client: WebClient,
user_id_to_post_confirmation: str,
channel_id_to_post_confirmation: str,
thread_ts_to_post_confirmation: str,
) -> None:
engine = get_sqlalchemy_engine()
with Session(engine) as db_session:
update_query_event_feedback(
Expand All @@ -14,3 +22,11 @@ def handle_qa_feedback(query_id: int, feedback_type: QAFeedbackType) -> None:
user_id=None, # no "user" for Slack bot for now
db_session=db_session,
)

# post message to slack confirming that feedback was received
client.chat_postEphemeral(
channel=channel_id_to_post_confirmation,
user=user_id_to_post_confirmation,
thread_ts=thread_ts_to_post_confirmation,
text="Thanks for your feedback!",
)
2 changes: 2 additions & 0 deletions backend/danswer/bots/slack/handlers/handle_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ def _get_answer(question: QuestionRequest) -> QAResponse:
channel=channel,
blocks=blocks,
thread_ts=message_ts_to_respond_to,
# don't unfurl, since otherwise we will have 5+ previews which makes the message very long
unfurl=False,
)
except Exception:
logger.exception(
Expand Down
7 changes: 7 additions & 0 deletions backend/danswer/bots/slack/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,17 @@ def _process_slack_event(client: SocketModeClient, req: SocketModeRequest) -> No
return

block_id = cast(str, action.get("block_id"))
user_id = cast(str, req.payload["user"]["id"])
channel_id = cast(str, req.payload["container"]["channel_id"])
thread_ts = cast(str, req.payload["container"]["thread_ts"])
query_event_id = get_query_event_id_from_block_id(block_id)
handle_qa_feedback(
query_id=query_event_id,
feedback_type=feedback_type,
client=client.web_client,
user_id_to_post_confirmation=user_id,
channel_id_to_post_confirmation=channel_id,
thread_ts_to_post_confirmation=thread_ts,
)

logger.info(f"Successfully handled QA feedback for event: {query_event_id}")
Expand Down
3 changes: 3 additions & 0 deletions backend/danswer/bots/slack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def respond_in_thread(
text: str | None = None,
blocks: list[Block] | None = None,
metadata: Metadata | None = None,
unfurl: bool = True,
) -> None:
if not text and not blocks:
raise ValueError("One of `text` or `blocks` must be provided")
Expand All @@ -45,6 +46,8 @@ def respond_in_thread(
blocks=blocks,
thread_ts=thread_ts,
metadata=metadata,
unfurl_links=unfurl,
unfurl_media=unfurl,
)
if not response.get("ok"):
raise RuntimeError(f"Unable to post message: {response}")
Expand Down
15 changes: 15 additions & 0 deletions backend/danswer/connectors/slack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,18 @@ def replace_special_mentions(message: str) -> str:
message = message.replace("<!here>", "@here")
message = message.replace("<!everyone>", "@everyone")
return message

@staticmethod
def replace_links(message: str) -> str:
"""Replaces slack links e.g. `<URL>` -> `URL` and `<URL|DISPLAY>` -> `DISPLAY`"""
# Find user IDs in the message
possible_link_matches = re.findall(r"<(.*?)>", message)
for possible_link in possible_link_matches:
if possible_link[0] not in ["#", "@", "!"]:
link_display = (
possible_link
if "|" not in possible_link
else possible_link.split("|")[1]
)
message = message.replace(f"<{possible_link}>", link_display)
return message

1 comment on commit ccbc69d

@vercel
Copy link

@vercel vercel bot commented on ccbc69d Sep 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.