misskey2telegram/modules/react.py

44 lines
1.7 KiB
Python
Raw Normal View History

2022-12-24 13:26:52 +00:00
from mipac.errors import NoSuchNoteError, AlreadyReactedError
2022-12-24 08:31:16 +00:00
from pyrogram import Client, filters
from pyrogram.types import CallbackQuery
2023-01-27 12:36:41 +00:00
from misskey_init import get_misskey_bot
from models.filters import timeline_filter
2022-12-24 08:31:16 +00:00
# react:note_id:reaction
2023-01-27 12:36:41 +00:00
@Client.on_callback_query(filters.regex(r"^react:(\w+):(\w+)$") & timeline_filter)
2022-12-24 08:31:16 +00:00
async def renote_callback(_: Client, callback_query: CallbackQuery):
note_id = callback_query.matches[0].group(1)
match callback_query.matches[0].group(2):
case "love":
reaction = "❤️"
case _:
reaction = "❤️"
try:
2023-01-27 12:36:41 +00:00
misskey_bot = get_misskey_bot(callback_query.from_user.id)
2022-12-25 15:41:51 +00:00
await misskey_bot.core.api.note.reaction.action.add(
2022-12-24 08:31:16 +00:00
reaction=reaction,
note_id=note_id,
)
2022-12-24 13:06:37 +00:00
except NoSuchNoteError:
await callback_query.answer("该嘟文不存在", show_alert=True)
2022-12-24 13:26:52 +00:00
except AlreadyReactedError:
try:
2023-01-27 12:36:41 +00:00
misskey_bot = get_misskey_bot(callback_query.from_user.id)
2022-12-25 15:41:51 +00:00
await misskey_bot.core.api.note.reaction.action.remove(
2022-12-24 13:26:52 +00:00
note_id=note_id,
)
await callback_query.answer("取消表态成功", show_alert=True)
except Exception as e:
if callback_query.message:
await callback_query.message.reply(f"取消表态失败:{e}", quote=True)
await callback_query.answer("取消表态失败", show_alert=True)
return
2022-12-24 08:31:16 +00:00
except Exception as e:
if callback_query.message:
await callback_query.message.reply(f"表态失败:{e}", quote=True)
await callback_query.answer("表态失败", show_alert=True)
else:
await callback_query.answer("表态成功", show_alert=True)