Skip to content

Commit e2015eb

Browse files
authored
MentionRemoved fix (#2216)
1 parent bd5662a commit e2015eb

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

libraries/botbuilder-core/botbuilder/core/turn_context.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,13 @@ def remove_mention_text(activity: Activity, identifier: str) -> str:
396396
mentions = TurnContext.get_mentions(activity)
397397
for mention in mentions:
398398
if mention.additional_properties["mentioned"]["id"] == identifier:
399+
replace_text = (
400+
mention.additional_properties.get("text")
401+
or mention.additional_properties.get("mentioned")["name"]
402+
)
399403
mention_name_match = re.match(
400404
r"<at(.*)>(.*?)<\/at>",
401-
escape(mention.additional_properties["text"]),
405+
escape(replace_text),
402406
re.IGNORECASE,
403407
)
404408
if mention_name_match:

libraries/botbuilder-core/tests/test_turn_context.py

+42
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,48 @@ def test_should_remove_at_mention_with_regex_characters(self):
350350
assert text == " test activity"
351351
assert activity.text == " test activity"
352352

353+
def test_should_remove_custom_mention_from_activity(self):
354+
activity = Activity(
355+
text="Hallo",
356+
text_format="plain",
357+
type="message",
358+
timestamp="2025-03-11T14:16:47.0093935Z",
359+
id="1741702606984",
360+
channel_id="msteams",
361+
service_url="https://smba.trafficmanager.net/emea/REDACTED/",
362+
from_property=ChannelAccount(
363+
id="29:1J-K4xVh-sLpdwQ-R5GkOZ_TB0W3ec_37p710aH8qe8bITA0zxdgIGc9l-MdDdkdE_jasSfNOeWXyyL1nsrHtBQ",
364+
name="",
365+
aad_object_id="REDACTED",
366+
),
367+
conversation=ConversationAccount(
368+
is_group=True,
369+
conversation_type="groupChat",
370+
tenant_id="REDACTED",
371+
372+
),
373+
recipient=ChannelAccount(
374+
id="28:c5d5fb56-a1a4-4467-a7a3-1b37905498a0", name="Azure AI Agent"
375+
),
376+
entities=[
377+
Entity().deserialize(
378+
Mention(
379+
type="mention",
380+
mentioned=ChannelAccount(
381+
id="28:c5d5fb56-a1a4-4467-a7a3-1b37905498a0",
382+
name="Custom Agent",
383+
),
384+
).serialize()
385+
)
386+
],
387+
channel_data={"tenant": {"id": "REDACTED"}, "productContext": "COPILOT"},
388+
)
389+
390+
text = TurnContext.remove_mention_text(activity, activity.recipient.id)
391+
392+
assert text == "Hallo"
393+
assert activity.text == "Hallo"
394+
353395
async def test_should_send_a_trace_activity(self):
354396
context = TurnContext(SimpleAdapter(), ACTIVITY)
355397
called = False

0 commit comments

Comments
 (0)