Skip to content

Commit 45b775a

Browse files
committed
Prevent bot from mis-recognizing itself on symphony
Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
1 parent d524aca commit 45b775a

3 files changed

Lines changed: 73 additions & 1 deletion

File tree

csp_bot/bot.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,15 @@ def is_msg_to_bot(self, msg: Message) -> Tuple[bool, str, str, List[Tag]]:
588588
# if the bot is not tagged, first argument is false so ignore second arg
589589
bot_name = self._configs[msg.backend].bot_name
590590
bot_tag_to_find = self._get_bot_tag(msg.backend)
591-
bot_tag = f"@{bot_tag_to_find}" in text
591+
592+
# Check if bot is tagged - use exact entity matching to avoid
593+
# "@CCRT Bot" matching inside "@CCRT Bot Dev"
594+
if msg.backend == "symphony":
595+
# For Symphony: entities contain the exact tagged names like "@CCRT Bot Dev"
596+
bot_tag = f"@{bot_tag_to_find}" in entities
597+
else:
598+
# For Slack/Discord: entities contain user IDs, use substring match on text
599+
bot_tag = f"@{bot_tag_to_find}" in text
592600

593601
if bot_tag:
594602
return True, msg.channel, text, entities

csp_bot/tests/conftest.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,32 @@ def side_effect(name):
7676
)
7777

7878
yield bot
79+
80+
81+
@pytest.fixture(scope="function")
82+
def bot_with_symphony():
83+
"""Bot fixture with Symphony config for testing Symphony-specific behavior."""
84+
# Create a minimal bot config without Symphony (we'll mock it)
85+
bot_config = BotConfig()
86+
bot = Bot(config=bot_config)
87+
88+
# Create a mock symphony config with just the bot_name we need
89+
mock_symphony_config = MagicMock()
90+
mock_symphony_config.bot_name = "Cubist Bot" # Shorter name to test against "Cubist Bot Dev"
91+
92+
# Mock the symphony adapter
93+
mock_symphony_adapter = MagicMock()
94+
bot._adapters["symphony"] = mock_symphony_adapter
95+
bot._configs["symphony"] = mock_symphony_config
96+
97+
# Load default commands
98+
bot.load_commands(
99+
[
100+
HelpCommandModel(),
101+
EchoCommandModel(),
102+
StatusCommandModel(),
103+
ScheduleCommandModel(),
104+
]
105+
)
106+
107+
yield bot

csp_bot/tests/test_bot.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,41 @@ def test_message_not_to_bot_discord(self, bot: Bot):
205205
is_to_bot, channel, text, entities = bot.is_msg_to_bot(msg)
206206
assert is_to_bot is False
207207

208+
def test_symphony_similar_bot_names_no_false_match(self, bot_with_symphony: Bot):
209+
"""Test that '@Cubist Bot' does not match when '@Cubist Bot Dev' is tagged in Symphony.
210+
211+
This ensures exact entity matching is used, preventing substring matches
212+
where a bot with a shorter name incorrectly receives messages meant for
213+
a bot with a longer, similar name.
214+
"""
215+
# Bot is configured with name "Cubist Bot", but message tags "Cubist Bot Dev"
216+
msg = Message(
217+
user="John Doe",
218+
user_id="123456789",
219+
msg='<div data-format="PresentationML"><span class="entity" data-entity-id="0">@Cubist Bot Dev</span> /help</div>',
220+
channel="Test Room",
221+
tags=["987654321"],
222+
backend="symphony",
223+
)
224+
is_to_bot, channel, text, entities = bot_with_symphony.is_msg_to_bot(msg)
225+
# Should NOT match because "@Cubist Bot" != "@Cubist Bot Dev"
226+
assert is_to_bot is False
227+
228+
def test_symphony_exact_bot_name_matches(self, bot_with_symphony: Bot):
229+
"""Test that exact bot name match works correctly in Symphony."""
230+
# Bot is configured with name "Cubist Bot", message also tags "Cubist Bot"
231+
msg = Message(
232+
user="John Doe",
233+
user_id="123456789",
234+
msg='<div data-format="PresentationML"><span class="entity" data-entity-id="0">@Cubist Bot</span> /help</div>',
235+
channel="Test Room",
236+
tags=["987654321"],
237+
backend="symphony",
238+
)
239+
is_to_bot, channel, text, entities = bot_with_symphony.is_msg_to_bot(msg)
240+
# Should match because "@Cubist Bot" == "@Cubist Bot"
241+
assert is_to_bot is True
242+
208243

209244
class TestBot:
210245
"""Tests for Bot command extraction and execution."""

0 commit comments

Comments
 (0)