mirror of
https://github.com/danswer-ai/danswer.git
synced 2025-06-29 09:20:01 +02:00
Fix bug where slack bot would tag users / everyone
This commit is contained in:
@ -143,7 +143,39 @@ class UserIdReplacer:
|
||||
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
|
||||
|
@ -15,6 +15,7 @@ from danswer.configs.app_configs import DANSWER_BOT_NUM_RETRIES
|
||||
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
|
||||
@ -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:
|
||||
@ -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
|
||||
@ -226,6 +233,7 @@ def process_slack_event(client: SocketModeClient, req: SocketModeRequest) -> Non
|
||||
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,
|
||||
|
Reference in New Issue
Block a user