Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 使用 parse_obj_as 解析 InputMedia 泛型 #62

Merged
merged 2 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion nonebot/adapters/telegram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
63 changes: 63 additions & 0 deletions tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
),
)
Loading