misskey2telegram/modules/post.py

80 lines
2.8 KiB
Python
Raw Permalink Normal View History

2022-12-24 08:31:16 +00:00
import contextlib
2022-12-24 16:10:54 +00:00
from os import remove
2022-12-25 09:40:41 +00:00
from pyrogram import Client, filters, ContinuePropagation
2022-12-24 08:31:16 +00:00
from pyrogram.types import Message, CallbackQuery
2022-12-22 14:18:43 +00:00
2022-12-24 08:31:16 +00:00
from defs.confirm import ReadySend, ready_send
2023-01-27 12:36:41 +00:00
from misskey_init import get_misskey_bot
from models.filters import timeline_filter
2022-12-22 14:18:43 +00:00
2023-01-27 12:36:41 +00:00
@Client.on_message(filters.incoming & timeline_filter & filters.text)
2022-12-22 14:18:43 +00:00
async def post_command(_: Client, message: Message):
"""
2023-07-03 14:39:52 +00:00
发送新贴或者回复
2022-12-22 14:18:43 +00:00
"""
2022-12-24 08:31:16 +00:00
note_id = None
if message.reply_to_message and message.reply_to_message.reply_markup:
with contextlib.suppress(IndexError, AttributeError):
url = message.reply_to_message.reply_markup.inline_keyboard[0][0].url
note_id = url.split("/")[-1]
text = message.text.strip()
2022-12-25 09:40:41 +00:00
if text.startswith("@"):
raise ContinuePropagation
2022-12-24 08:31:16 +00:00
need_send = ReadySend(text, note_id)
await need_send.confirm(message)
2023-01-27 12:36:41 +00:00
@Client.on_message(filters.incoming & timeline_filter & filters.photo)
2022-12-24 16:10:54 +00:00
async def post_photo_command(_: Client, message: Message):
"""
2023-07-03 14:39:52 +00:00
发送新贴或者回复
2022-12-24 16:10:54 +00:00
"""
note_id = None
if message.reply_to_message and message.reply_to_message.reply_markup:
with contextlib.suppress(IndexError, AttributeError):
url = message.reply_to_message.reply_markup.inline_keyboard[0][0].url
note_id = url.split("/")[-1]
text = message.caption.strip() if message.caption else ""
photo = await message.download()
try:
2023-01-27 12:36:41 +00:00
misskey_bot = get_misskey_bot(message.from_user.id)
2024-02-22 14:41:38 +00:00
file_ = await misskey_bot.core.api.drive.files.action.create(
2023-07-05 11:58:39 +00:00
photo,
is_sensitive=message.has_media_spoiler or False,
)
2022-12-24 16:10:54 +00:00
except Exception as e:
return await message.reply(f"上传文件失败:{e}", quote=True)
2023-07-05 11:58:39 +00:00
finally:
remove(photo)
2022-12-24 16:10:54 +00:00
need_send = ReadySend(text, note_id, [file_])
await need_send.confirm(message)
2023-01-27 12:36:41 +00:00
@Client.on_callback_query(filters.regex("^send$") & timeline_filter)
2022-12-24 08:31:16 +00:00
async def send_callback(_: Client, callback_query: CallbackQuery):
"""
2023-07-03 14:39:52 +00:00
发送
2022-12-24 08:31:16 +00:00
"""
2023-01-27 12:36:41 +00:00
msg = callback_query.message
if need_send := ready_send.get((msg.chat.id, msg.id), None):
await need_send.send(msg, callback_query.from_user.id)
2022-12-24 08:31:16 +00:00
return await callback_query.answer("发送成功")
2022-12-22 14:18:43 +00:00
else:
2022-12-24 08:31:16 +00:00
return await callback_query.answer("按钮已过期", show_alert=True)
2023-01-27 12:36:41 +00:00
@Client.on_callback_query(filters.regex("^delete$") & timeline_filter)
2022-12-24 08:31:16 +00:00
async def delete_callback(_: Client, callback_query: CallbackQuery):
"""
2023-07-03 14:39:52 +00:00
删除
2022-12-24 08:31:16 +00:00
"""
if not (need_send := callback_query.message):
return await callback_query.answer("按钮已过期", show_alert=True)
await need_send.delete()
2023-01-27 12:36:41 +00:00
msg = callback_query.message
if ready_send.get((msg.chat.id, msg.id), None):
del ready_send[(msg.chat.id, msg.id)]
2022-12-24 08:31:16 +00:00
return await callback_query.answer("已删除")