Skip to content

Commit 3861730

Browse files
authored
fix(telegram): match bot mentions case-insensitively (#10399)
1 parent 42972e9 commit 3861730

2 files changed

Lines changed: 65 additions & 3 deletions

File tree

‎astrbot/core/platform/sources/telegram/tg_adapter.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,10 @@ def _apply_caption() -> None:
473473
for entity in update.message.caption_entities:
474474
if entity.type == "mention":
475475
name = update.message.parse_caption_entity(entity)[1:]
476-
message.message.append(Comp.At(qq=name, name=name))
476+
mention = Comp.At(qq=name, name=name)
477+
if name.lower() == message.self_id.lower():
478+
mention.qq = message.self_id
479+
message.message.append(mention)
477480

478481
message = AstrBotMessage()
479482
message.session_id = str(update.message.chat.id)
@@ -604,10 +607,12 @@ def _apply_caption() -> None:
604607
for entity in update.message.entities:
605608
if entity.type == "mention":
606609
name = update.message.parse_entity(entity)[1:]
607-
message.message.append(Comp.At(qq=name, name=name))
608-
if name.lower() == context.bot.username.lower():
610+
mention = Comp.At(qq=name, name=name)
611+
if name.lower() == message.self_id.lower():
612+
mention.qq = message.self_id
609613
text_parts.append(text_utf16[last_end : entity.offset * 2])
610614
last_end = (entity.offset + entity.length) * 2
615+
message.message.append(mention)
611616
text_parts.append(text_utf16[last_end:])
612617
plain_text = b"".join(text_parts).decode("utf-16-le")
613618

‎tests/test_telegram_adapter.py‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,63 @@ async def test_telegram_mentions_use_original_utf16_offsets(
554554
)
555555

556556

557+
@pytest.mark.asyncio
558+
@pytest.mark.parametrize("in_caption", [False, True], ids=["text", "photo-caption"])
559+
@pytest.mark.parametrize(
560+
"name", ["ExampleBot", "examplebot", "EXAMPLEBOT", "eXaMpLeBoT", "OtherBot"]
561+
)
562+
async def test_telegram_self_mention_is_case_insensitive(in_caption, name):
563+
"""Normalize self-mention IDs while preserving the original display name.
564+
565+
Args:
566+
in_caption: Whether the mention appears in a photo caption.
567+
name: Username spelling used in the incoming mention.
568+
"""
569+
adapter = _load_telegram_adapter()(
570+
make_platform_config("telegram"), {}, asyncio.Queue()
571+
)
572+
context = _build_context()
573+
context.bot.username = "ExampleBot"
574+
text = f"😀 @{name} hello"
575+
sdk_message = Message.de_json(
576+
{
577+
"message_id": 1,
578+
"date": 0,
579+
"chat": {"id": -100123, "type": "supergroup"},
580+
"caption" if in_caption else "text": text,
581+
"caption_entities" if in_caption else "entities": [
582+
{"type": "mention", "offset": 3, "length": len(name) + 1}
583+
],
584+
},
585+
bot=None,
586+
)
587+
if in_caption:
588+
update = create_mock_update(
589+
message_text=None,
590+
chat_type="supergroup",
591+
photo=[create_mock_file()],
592+
caption=text,
593+
caption_entities=sdk_message.caption_entities,
594+
)
595+
update.message.parse_caption_entity = sdk_message.parse_caption_entity
596+
else:
597+
update = create_mock_update(
598+
message_text=text, chat_type="supergroup", entities=sdk_message.entities
599+
)
600+
update.message.parse_entity = sdk_message.parse_entity
601+
602+
result = await adapter.convert_message(update, context)
603+
604+
assert result is not None
605+
mentions = [part for part in result.message if isinstance(part, Comp.At)]
606+
assert len(mentions) == 1
607+
is_self = name != "OtherBot"
608+
assert mentions[0].qq == ("ExampleBot" if is_self else name)
609+
assert mentions[0].name == name
610+
assert (mentions[0].qq == result.self_id) is is_self
611+
assert result.message_str == (text if in_caption or not is_self else "😀 hello")
612+
613+
557614
@pytest.mark.asyncio
558615
@pytest.mark.parametrize("media_type", ["photo", "document", "video"])
559616
async def test_telegram_media_caption_mention_uses_utf16_offsets(media_type):

0 commit comments

Comments
 (0)