Skip to content

Commit

Permalink
Merge pull request #20 from lgc2333/fix-telegram-local
Browse files Browse the repository at this point in the history
Feat: feat & fix for `image_fetch` function
  • Loading branch information
RF-Tar-Railt authored Oct 31, 2023
2 parents 0c32868 + ded64f3 commit affd1e2
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/nonebot_plugin_alconna/uniseg/tools.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import random
from pathlib import Path
from base64 import b64decode
from typing import TYPE_CHECKING, List, Type, Union, Literal, Optional, overload

from yarl import URL
from nonebot import get_bots
from nonebot.typing import T_State
from nonebot import get_bot as _get_bot
from nonebot.exception import ActionFailed
from nonebot.internal.driver.model import Request
from nonebot.internal.adapter import Bot, Event, Adapter

from .segment import Image


async def image_fetch(event: Event, bot: Bot, state: T_State, img: Image):
async def image_fetch(event: Event, bot: Bot, state: T_State, img: Image, **kwargs):
adapter_name = bot.adapter.get_name()
if adapter_name == "RedProtocol":
origin = img.origin
Expand All @@ -26,7 +28,7 @@ async def image_fetch(event: Event, bot: Bot, state: T_State, img: Image):
return await origin.download(bot)

if img.url: # mirai2, qqguild, kook, villa, feishu, minecraft, ding
req = Request("GET", img.url)
req = Request("GET", img.url, **kwargs)
resp = await bot.adapter.request(req)
return resp.content
if not img.id:
Expand All @@ -37,7 +39,7 @@ async def image_fetch(event: Event, bot: Bot, state: T_State, img: Image):

assert isinstance(bot, Bot)
url = (await bot.get_image(file=img.id))["data"]["url"]
req = Request("GET", url)
req = Request("GET", url, **kwargs)
resp = await bot.adapter.request(req)
return resp.content
if adapter_name == "OneBot V12":
Expand All @@ -49,16 +51,21 @@ async def image_fetch(event: Event, bot: Bot, state: T_State, img: Image):
return b64decode(resp) if isinstance(resp, str) else bytes(resp)
if adapter_name == "mirai2":
url = f"https://gchat.qpic.cn/gchatpic_new/0/0-0-" f"{img.id.replace('-', '').upper()}/0"
req = Request("GET", url)
req = Request("GET", url, **kwargs)
resp = await bot.adapter.request(req)
return resp.content
if adapter_name == "Telegram":
if TYPE_CHECKING:
from nonebot.adapters.telegram.bot import Bot

assert isinstance(bot, Bot)
url = URL(bot.bot_config.api_server) / "file" / f"bot{bot.bot_config.token}" / img.id
req = Request("GET", url)
res = await bot.get_file(file_id=img.id)
if not res.file_path:
raise ActionFailed("Telegram", "get file failed")
if (p := Path(res.file_path)).exists(): # telegram api local mode
return p.read_bytes()
url = URL(bot.bot_config.api_server) / "file" / f"bot{bot.bot_config.token}" / res.file_path
req = Request("GET", url, **kwargs)
resp = await bot.adapter.request(req)
return resp.content
if adapter_name == "ntchat":
Expand Down

0 comments on commit affd1e2

Please sign in to comment.