diff --git a/nonebot/adapters/telegram/bot.py b/nonebot/adapters/telegram/bot.py index 8185d00..a08a52a 100644 --- a/nonebot/adapters/telegram/bot.py +++ b/nonebot/adapters/telegram/bot.py @@ -237,7 +237,15 @@ async def send_to( if len(files) > 1: # 多个文件 medias = [ - InputMedia(type=file.type, media=file.data["file"]) for file in files + parse_obj_as( + InputMedia, + { + "type": file.type, + "media": file.data.pop("file"), + **file.get("data", {}), + }, + ) + for file in files ] try: diff --git a/tests/test_bot.py b/tests/test_bot.py index f3a585c..00d15a7 100644 --- a/tests/test_bot.py +++ b/tests/test_bot.py @@ -34,3 +34,66 @@ async def test_check_tome(app: App): bot.username = "test" bot._check_tome(event) assert event._tome + + +@pytest.mark.asyncio +async def test_send_to(app: App): + from nonebot.adapters.telegram.bot import Bot + from nonebot.adapters.telegram.model import Chat + from nonebot.adapters.telegram.message import File, Message + from nonebot.adapters.telegram.model import Message as ReturnMessage + from nonebot.adapters.telegram.model import InputMediaAudio, InputMediaPhoto + + Chat.update_forward_refs() + + with (Path(__file__).parent / "updates.json").open("r", encoding="utf8") as f: + test_updates = json.load(f) + test_update = test_updates[0] + test_update["message"]["chat"]["type"] = "group" + test_update["message"]["text"] = "@test" + test_update["message"]["entities"] = [{"type": "mention", "offset": 0, "length": 5}] + event = Event.parse_event(test_update) + assert isinstance(event, GroupMessageEvent) + + async with app.test_api() as ctx: + bot = Bot( + ctx.create_adapter(base=Adapter), + Bot.get_bot_id_by_token(bot_config.token), + config=bot_config, + ) + bot.username = "test" + ctx.should_call_api( + "send_media_group", + { + "chat_id": 1234567890, + "message_thread_id": None, + "media": [ + InputMediaPhoto( + media="test.jpg", + has_spoiler=True, + ), + InputMediaAudio( + media="test.ogg", + ), + ], + "reply_parameters": None, + "disable_notification": None, + "protect_content": None, + "reply_to_message_id": None, + "allow_sending_without_reply": None, + }, + [ + ReturnMessage( + message_id=1, date=111, chat=Chat(id=1, type="group") + ).dict(), + ReturnMessage( + message_id=2, date=222, chat=Chat(id=2, type="group") + ).dict(), + ], + ) + await bot.send_to( + chat_id=1234567890, + message=Message( + [File.photo("test.jpg", has_spoiler=True), File.audio("test.ogg")] + ), + )