misskey2telegram/defs/notice.py

86 lines
2.9 KiB
Python
Raw Normal View History

2023-07-03 14:22:02 +00:00
from json import load
2023-01-24 07:23:25 +00:00
from mipac.models.notification import NotificationFollow, NotificationFollowRequest, NotificationAchievement
2022-12-26 09:48:13 +00:00
from mipac.models.lite.user import LiteUser
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from init import bot
user_followed_template = """<b>有人关注了你!</b> <a href="{0}">{1}</a>"""
follow_request_template = """<b>有人申请关注你!</b> <a href="{0}">{1}</a>"""
follow_request_accept_template = """<b>有人同意了你的关注申请!</b> <a href="{0}">{1}</a>"""
2023-01-24 07:23:25 +00:00
achievement_template = """<b>你获得了新成就!</b> <b>{0}</b>{1} {2}"""
2023-07-03 14:22:02 +00:00
with open("gen/achievement.json", "r", encoding="utf-8") as f:
achievement_map = load(f)
2022-12-26 09:48:13 +00:00
def gen_user_link_button(user: LiteUser):
return [
InlineKeyboardButton(
text="Link",
url=user.api.action.get_profile_link(),
),
]
2023-01-27 12:36:41 +00:00
async def send_user_followed(chat_id: int, notice: NotificationFollow, topic_id: int):
2022-12-26 09:48:13 +00:00
await bot.send_message(
2023-01-27 12:36:41 +00:00
chat_id,
2022-12-26 09:48:13 +00:00
user_followed_template.format(
notice.user.api.action.get_profile_link(),
notice.user.username,
),
2023-01-27 12:36:41 +00:00
reply_to_message_id=topic_id,
2022-12-26 09:48:13 +00:00
reply_markup=InlineKeyboardMarkup([gen_user_link_button(notice.user)]),
)
2023-01-27 12:36:41 +00:00
async def send_follow_request(chat_id: int, notice: NotificationFollowRequest, topic_id: int):
2022-12-26 09:48:13 +00:00
await bot.send_message(
2023-01-27 12:36:41 +00:00
chat_id,
2022-12-26 09:48:13 +00:00
follow_request_template.format(
notice.user.api.action.get_profile_link(),
notice.user.username,
),
2023-01-27 12:36:41 +00:00
reply_to_message_id=topic_id,
2022-12-26 09:48:13 +00:00
reply_markup=InlineKeyboardMarkup(
[
gen_user_link_button(notice.user),
[
InlineKeyboardButton(
text="Accept",
callback_data=f"request_accept:{notice.user.id}",
),
InlineKeyboardButton(
text="Reject",
callback_data=f"request_reject:{notice.user.id}",
),
]
],
),
)
2023-01-27 12:36:41 +00:00
async def send_follow_request_accept(chat_id: int, notice: NotificationFollowRequest, topic_id: int):
2022-12-26 09:48:13 +00:00
await bot.send_message(
2023-01-27 12:36:41 +00:00
chat_id,
2022-12-26 09:48:13 +00:00
follow_request_accept_template.format(
notice.user.api.action.get_profile_link(),
notice.user.username,
),
2023-01-27 12:36:41 +00:00
reply_to_message_id=topic_id,
2022-12-26 09:48:13 +00:00
reply_markup=InlineKeyboardMarkup([gen_user_link_button(notice.user)]),
)
2023-01-24 07:23:25 +00:00
2023-01-27 12:36:41 +00:00
async def send_achievement_earned(chat_id: int, notice: NotificationAchievement, topic_id: int):
2023-01-24 07:23:25 +00:00
name, desc, note = achievement_map.get(notice.achievement, ("", "", ""))
await bot.send_message(
2023-01-27 12:36:41 +00:00
chat_id,
2023-01-24 07:23:25 +00:00
achievement_template.format(
name,
desc,
f"- {note}" if note else "",
),
2023-01-27 12:36:41 +00:00
reply_to_message_id=topic_id,
2023-01-24 07:23:25 +00:00
)