Skip to content

Commit d524aca

Browse files
committed
Fix tagging logic to resolve slack tag issues
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
1 parent 0de1790 commit d524aca

2 files changed

Lines changed: 32 additions & 16 deletions

File tree

csp_bot/bot.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -384,14 +384,18 @@ def bot_commands_from_command_string(self, tokens: List[str], message: Message,
384384

385385
# entity detection
386386
if entity_map and token in entity_map:
387-
# symphony and slack handle tagging opposite
388-
if message.backend == "symphony":
389-
name, id = entity_map[token]
390-
elif message.backend == "slack":
391-
id, name = entity_map[token]
392-
elif message.backend == "discord":
393-
name, id = entity_map[token]
394-
target_tags.append(User(name=name, id=id, backend=message.backend))
387+
# entity_map stores (entity_text, tag_value) where:
388+
# - For Slack: entity_text is "@USER_ID", tag_value is display name
389+
# - For Symphony/Discord: entity_text is "@DisplayName", tag_value is user ID
390+
entity_text, tag_value = entity_map[token]
391+
if message.backend == "slack":
392+
# For Slack: entity_text has the user ID (with @ prefix), tag_value is display name
393+
# Strip @ from entity_text to get clean user ID matching source.id format
394+
user_id = entity_text[1:] if entity_text.startswith("@") else entity_text
395+
target_tags.append(User(name=tag_value, id=user_id, backend=message.backend))
396+
else:
397+
# Symphony/Discord: entity_text is display name, tag_value is user ID
398+
target_tags.append(User(name=entity_text, id=tag_value, backend=message.backend))
395399
continue
396400

397401
command_args.append(token)
@@ -427,6 +431,7 @@ def extract_bot_commands(self, message: Message, channel: str, text: str, entiti
427431
if message.backend == "symphony":
428432
text = text.replace(entity, entity_placeholder, 1)
429433
elif message.backend == "slack":
434+
# NOTE: don't replace the @ in front of entity, to match other frameworks
430435
text = text.replace(f"<{entity}>", entity_placeholder, 1)
431436
elif message.backend == "discord":
432437
text = text.replace(f"<{entity}>", entity_placeholder, 1)
@@ -436,9 +441,19 @@ def extract_bot_commands(self, message: Message, channel: str, text: str, entiti
436441

437442
tokens = next(reader(StringIO(text), delimiter=" ", quotechar='"', skipinitialspace=True))
438443

439-
if tokens[0] in entity_map and entity_map[tokens[0]][0] == f"@{self._get_bot_tag(message.backend)}":
440-
# remove initial bot directive
441-
tokens = tokens[1:]
444+
# Check if the first token is the bot being tagged - if so, remove it
445+
if tokens[0] in entity_map:
446+
entity_text, tag_value = entity_map[tokens[0]]
447+
bot_tag = self._get_bot_tag(message.backend)
448+
if message.backend == "slack":
449+
# Slack: entity_text is "@USER_ID", strip @ and compare with bot_tag (user ID)
450+
entity_user_id = entity_text[1:] if entity_text.startswith("@") else entity_text
451+
if entity_user_id == bot_tag:
452+
tokens = tokens[1:]
453+
else:
454+
# Symphony/Discord: entity_text contains @name
455+
if entity_text == f"@{bot_tag}":
456+
tokens = tokens[1:]
442457

443458
ret = self.bot_commands_from_command_string(tokens, message, channel, entity_map)
444459
if not ret:

csp_bot/tests/test_bot.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,11 @@ def test_command_with_entity_slack(self, bot: Bot):
398398
user_id="U123",
399399
msg="/echo <@U456>",
400400
channel="general",
401-
tags=["U456"],
401+
tags=["Bob Smith"], # Slack tags contain display names, not IDs
402402
backend="slack",
403403
)
404-
entity_map = {"ENTITY_0": ("@U456", "U456")}
404+
# For Slack: entity_text is "@USER_ID", tag_value is display name
405+
entity_map = {"ENTITY_0": ("@U456", "Bob Smith")}
405406
result = bot.bot_commands_from_command_string(
406407
tokens=["/echo", "ENTITY_0"],
407408
message=msg,
@@ -412,9 +413,9 @@ def test_command_with_entity_slack(self, bot: Bot):
412413
command, args, target_channel, target_tags = result
413414
assert command == "echo"
414415
assert len(target_tags) == 1
415-
# The Tag stores the full name with @ prefix as the ID for Slack
416-
assert target_tags[0].id == "@U456"
417-
assert target_tags[0].name == "U456"
416+
# For Slack: id comes from entity_text (stripped @), name comes from tag_value
417+
assert target_tags[0].id == "U456"
418+
assert target_tags[0].name == "Bob Smith"
418419

419420
def test_command_with_entity_symphony(self, bot: Bot):
420421
"""Test parsing command with entity mentions for Symphony."""

0 commit comments

Comments
 (0)