55from unittest .mock import AsyncMock , MagicMock , patch
66
77import pytest
8+ from telegram import Message
89
910import astrbot .api .message_components as Comp
1011from astrbot .api .platform import Group
@@ -495,6 +496,102 @@ async def test_telegram_reply_without_quote_text_uses_full_message(quote_text):
495496 assert reply .chain [0 ].text == original_text
496497
497498
499+ @pytest .mark .asyncio
500+ @pytest .mark .parametrize (
501+ ("text" , "entities" , "expected" , "names" ),
502+ [
503+ ("😀 @test_bot hello" , [(3 , 9 )], "😀 hello" , ["test_bot" ]),
504+ ("😀😀 @test_bot hello" , [(5 , 9 )], "😀😀 hello" , ["test_bot" ]),
505+ ("文字 @test_bot hello" , [(3 , 9 )], "文字 hello" , ["test_bot" ]),
506+ ("@test_bot 😀 hello" , [(0 , 9 )], " 😀 hello" , ["test_bot" ]),
507+ (
508+ "😀 @test_bot @alice @test_bot hello" ,
509+ [(3 , 9 ), (13 , 6 ), (20 , 9 )],
510+ "😀 @alice hello" ,
511+ ["test_bot" , "alice" , "test_bot" ],
512+ ),
513+ (
514+ "/help@test_bot 😀 @test_bot hello" ,
515+ [(18 , 9 )],
516+ "/help 😀 hello" ,
517+ ["test_bot" ],
518+ ),
519+ ],
520+ )
521+ async def test_telegram_mentions_use_original_utf16_offsets (
522+ text , entities , expected , names
523+ ):
524+ adapter = _load_telegram_adapter ()(
525+ make_platform_config ("telegram" ), {}, asyncio .Queue ()
526+ )
527+ adapter .client .username = "test_bot"
528+ sdk_message = Message .de_json (
529+ {
530+ "message_id" : 1 ,
531+ "date" : 0 ,
532+ "chat" : {"id" : - 100123 , "type" : "supergroup" },
533+ "text" : text ,
534+ "entities" : [
535+ {"type" : "mention" , "offset" : offset , "length" : length }
536+ for offset , length in entities
537+ ],
538+ },
539+ bot = None ,
540+ )
541+ update = create_mock_update (
542+ message_text = text , chat_type = "supergroup" , entities = sdk_message .entities
543+ )
544+ update .message .parse_entity = sdk_message .parse_entity
545+
546+ result = await adapter .convert_message (update , _build_context ())
547+
548+ assert result is not None
549+ assert result .message_str == expected
550+ assert [part .qq for part in result .message if isinstance (part , Comp .At )] == names
551+ assert any (
552+ isinstance (part , Comp .At ) and part .qq == result .self_id
553+ for part in result .message
554+ )
555+
556+
557+ @pytest .mark .asyncio
558+ @pytest .mark .parametrize ("media_type" , ["photo" , "document" , "video" ])
559+ async def test_telegram_media_caption_mention_uses_utf16_offsets (media_type ):
560+ adapter = _load_telegram_adapter ()(
561+ make_platform_config ("telegram" ), {}, asyncio .Queue ()
562+ )
563+ caption = "😀 @test_bot hello"
564+ sdk_message = Message .de_json (
565+ {
566+ "message_id" : 1 ,
567+ "date" : 0 ,
568+ "chat" : {"id" : - 100123 , "type" : "supergroup" },
569+ "caption" : caption ,
570+ "caption_entities" : [{"type" : "mention" , "offset" : 3 , "length" : 9 }],
571+ },
572+ bot = None ,
573+ )
574+ media = create_mock_file ()
575+ media .file_name = "example"
576+ update = create_mock_update (
577+ message_text = None ,
578+ chat_type = "supergroup" ,
579+ caption = caption ,
580+ caption_entities = sdk_message .caption_entities ,
581+ ** {media_type : [media ] if media_type == "photo" else media },
582+ )
583+ update .message .parse_caption_entity = sdk_message .parse_caption_entity
584+
585+ result = await adapter .convert_message (update , _build_context ())
586+
587+ assert result is not None
588+ assert result .message_str == caption
589+ assert any (
590+ isinstance (part , Comp .At ) and part .qq == result .self_id
591+ for part in result .message
592+ )
593+
594+
498595@pytest .mark .asyncio
499596async def test_telegram_document_caption_populates_message_text_and_plain ():
500597 TelegramPlatformAdapter = _load_telegram_adapter ()
@@ -512,6 +609,7 @@ async def test_telegram_document_caption_populates_message_text_and_plain():
512609 caption = "@alice 请总结这份文档" ,
513610 caption_entities = [mention ],
514611 )
612+ update .message .parse_caption_entity .return_value = "@alice"
515613
516614 result = await adapter .convert_message (update , _build_context ())
517615
0 commit comments