Skip to content

Commit

Permalink
Fix bug where slack bot would tag users / everyone
Browse files Browse the repository at this point in the history
  • Loading branch information
Weves committed Aug 22, 2023
1 parent e5352b6 commit a4b93c4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
34 changes: 33 additions & 1 deletion backend/danswer/connectors/slack/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,39 @@ def replace_user_ids_with_names(self, message: str) -> str:
message = message.replace(f"<@{user_id}>", f"@{user_name}")
except Exception:
logger.exception(
f"Unable to replace user ID with username for user_id '{user_id}"
f"Unable to replace user ID with username for user_id '{user_id}'"
)

return message

@staticmethod
def replace_tags_basic(message: str) -> str:
"""Simply replaces all tags with `@<USER_ID>` in order to prevent us from
tagging users in Slack when we don't want to"""
# Find user IDs in the message
user_ids = re.findall("<@(.*?)>", message)
for user_id in user_ids:
message = message.replace(f"<@{user_id}>", f"@{user_id}")
return message

@staticmethod
def replace_channels_basic(message: str) -> str:
"""Simply replaces all channel mentions with `#<CHANNEL_ID>` in order
to make a message work as part of a link"""
# Find user IDs in the message
channel_matches = re.findall("<#(.*?)\|(.*?)>", message)
for channel_id, channel_name in channel_matches:
message = message.replace(
f"<#{channel_id}|{channel_name}>", f"#{channel_name}"
)
return message

@staticmethod
def replace_special_mentions(message: str) -> str:
"""Simply replaces @channel, @here, and @everyone so we don't tag
a bunch of people in Slack when we don't want to"""
# Find user IDs in the message
message = message.replace("<!channel>", "@channel")
message = message.replace("<!here>", "@here")
message = message.replace("<!everyone>", "@everyone")
return message
8 changes: 8 additions & 0 deletions backend/danswer/listeners/slack_listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from danswer.configs.app_configs import QDRANT_DEFAULT_COLLECTION
from danswer.configs.constants import DocumentSource
from danswer.connectors.slack.utils import make_slack_api_rate_limited
from danswer.connectors.slack.utils import UserIdReplacer
from danswer.direct_qa.answer_question import answer_question
from danswer.direct_qa.interfaces import DanswerQuote
from danswer.server.models import QAResponse
Expand Down Expand Up @@ -65,6 +66,11 @@ def _build_custom_semantic_identifier(
truncated_blurb = (
f"{blurb[:_MAX_BLURB_LEN]}..." if len(blurb) > _MAX_BLURB_LEN else blurb
)
# NOTE: removing tags so that we don't accidentally tag users in Slack +
# so that it can be used as part of a <link|text> link
truncated_blurb = UserIdReplacer.replace_tags_basic(truncated_blurb)
truncated_blurb = UserIdReplacer.replace_channels_basic(truncated_blurb)
truncated_blurb = UserIdReplacer.replace_special_mentions(truncated_blurb)
if truncated_blurb:
return f"#{semantic_identifier}: {truncated_blurb}"
else:
Expand Down Expand Up @@ -118,6 +124,7 @@ def _process_documents(
blurb=d.blurb,
source=d.source_type,
)

top_document_lines.append(f"- <{d.link}|{custom_semantic_identifier}>")
if len(top_document_lines) >= num_docs_to_display:
break
Expand Down Expand Up @@ -226,6 +233,7 @@ def _respond_in_thread(
text: str,
thread_ts: str,
) -> None:
logger.info(f"Trying to send message: {text}")
slack_call = make_slack_api_rate_limited(client.web_client.chat_postMessage)
response = slack_call(
channel=channel,
Expand Down

0 comments on commit a4b93c4

Please sign in to comment.