From bc5fbbbb9641cface6a772138870d1476670185a Mon Sep 17 00:00:00 2001 From: RF-Tar-Railt Date: Sun, 24 Nov 2024 15:59:12 +0800 Subject: [PATCH] :bug: fix telegram file build resolve #79 --- src/nonebot_plugin_alconna/__init__.py | 2 +- src/nonebot_plugin_alconna/uniseg/__init__.py | 2 +- .../uniseg/adapters/telegram/builder.py | 18 +++++++++++++----- src/nonebot_plugin_alconna/uniseg/tools.py | 4 ++++ 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/nonebot_plugin_alconna/__init__.py b/src/nonebot_plugin_alconna/__init__.py index 7d0029a..af38107 100644 --- a/src/nonebot_plugin_alconna/__init__.py +++ b/src/nonebot_plugin_alconna/__init__.py @@ -137,7 +137,7 @@ from .uniseg import SupportAdapterModule as SupportAdapterModule from .extension import add_global_extension as add_global_extension -__version__ = "0.54.0" +__version__ = "0.54.1" __plugin_meta__ = PluginMetadata( name="Alconna 插件", diff --git a/src/nonebot_plugin_alconna/uniseg/__init__.py b/src/nonebot_plugin_alconna/uniseg/__init__.py index 782ea55..5c7a585 100644 --- a/src/nonebot_plugin_alconna/uniseg/__init__.py +++ b/src/nonebot_plugin_alconna/uniseg/__init__.py @@ -58,7 +58,7 @@ from .constraint import SupportAdapterModule as SupportAdapterModule from .adapters import alter_get_builder, alter_get_fetcher, alter_get_exporter -__version__ = "0.54.0" +__version__ = "0.54.1" __plugin_meta__ = PluginMetadata( name="Universal Segment 插件", diff --git a/src/nonebot_plugin_alconna/uniseg/adapters/telegram/builder.py b/src/nonebot_plugin_alconna/uniseg/adapters/telegram/builder.py index 6ce7765..df45326 100644 --- a/src/nonebot_plugin_alconna/uniseg/adapters/telegram/builder.py +++ b/src/nonebot_plugin_alconna/uniseg/adapters/telegram/builder.py @@ -1,9 +1,9 @@ from typing import TYPE_CHECKING from nonebot.adapters import Bot, Event -from nonebot.adapters.telegram.message import Entity from nonebot.adapters.telegram.event import MessageEvent from nonebot.adapters.telegram.message import File as FileSegment +from nonebot.adapters.telegram.message import Entity, UnCombinFile from nonebot.adapters.telegram.message import Reply as ReplySegment from nonebot_plugin_alconna.uniseg.constraint import SupportAdapter @@ -34,19 +34,27 @@ def photo(self, seg: FileSegment): @build("video", "animation") def video(self, seg: FileSegment): - return Video(id=seg.data["file_id"]) + return Video(id=seg.data["file"]) + + @build("sticker") + def sticker(self, seg: UnCombinFile): + return Image(id=seg.data["file"]) + + @build("video_note") + def video_note(self, seg: UnCombinFile): + return Video(id=seg.data["file"]) @build("voice") def voice(self, seg: FileSegment): - return Voice(id=seg.data["file_id"]) + return Voice(id=seg.data["file"]) @build("audio") def audio(self, seg: FileSegment): - return Audio(id=seg.data["file_id"]) + return Audio(id=seg.data["file"]) @build("document") def document(self, seg: FileSegment): - return File(seg.data["file_id"], name=seg.data["file_name"]) + return File(seg.data["file"], name=seg.data.get("file_name", "file. bin")) @build("reply") def reply(self, seg: ReplySegment): diff --git a/src/nonebot_plugin_alconna/uniseg/tools.py b/src/nonebot_plugin_alconna/uniseg/tools.py index 1f71b80..6006b03 100644 --- a/src/nonebot_plugin_alconna/uniseg/tools.py +++ b/src/nonebot_plugin_alconna/uniseg/tools.py @@ -27,6 +27,10 @@ async def reply_fetch(event: Event, bot: Bot): async def image_fetch(event: Event, bot: Bot, state: T_State, img: Image, **kwargs) -> Optional[bytes]: + if img.raw: + return img.raw_bytes + if img.path: + return Path(img.path).read_bytes() adapter_name = bot.adapter.get_name() if adapter_name == "RedProtocol": origin = img.origin