PagerMaid_Plugins_Pyro/tgbg/main.py

36 lines
1.5 KiB
Python
Raw Normal View History

from urllib.parse import urlparse
from pyrogram.raw.functions.account import GetWallPaper
from pyrogram.raw.types import InputWallPaperSlug, WallPaper
from pyrogram.types.messages_and_media.document import Document
from pagermaid.listener import listener
2022-09-01 08:28:48 +00:00
from pagermaid.enums import Client, Message
2023-07-01 12:18:58 +00:00
@listener(command="tgbg", description="解析 Telegram 聊天窗口背景图", parameters="t.me/bg/xxx")
2022-09-01 08:28:48 +00:00
async def tg_bg(client: Client, message: Message):
argument = message.obtain_message()
if url := urlparse(argument):
if path := url.path:
if url.hostname == "t.me" and path.startswith("/bg/"):
slug = path[4:]
try:
2023-07-01 12:18:58 +00:00
bg: WallPaper = await client.invoke(
GetWallPaper(wallpaper=InputWallPaperSlug(slug=slug))
)
except Exception as e:
return await message.edit(f"获取失败: {str(e)}")
if bg.document:
2023-07-01 12:18:58 +00:00
bg_doc = Document._parse(
client, document=bg.document, file_name="bg.jpg"
) # noqa
2022-11-21 11:38:51 +00:00
await client.send_document(
message.chat.id,
bg_doc.file_id,
file_name="bg.jpg",
2024-02-04 07:56:06 +00:00
message_thread_id=message.message_thread_id,
2022-11-21 11:38:51 +00:00
)
return await message.safe_delete()
await message.edit("获取失败,请检查 URL")