2022-07-26 10:07:31 +00:00
|
|
|
|
from telegram import Update
|
|
|
|
|
from telegram.constants import ChatAction
|
|
|
|
|
from telegram.error import BadRequest
|
|
|
|
|
from telegram.ext import CommandHandler, CallbackContext
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from core.bot import bot
|
|
|
|
|
from core.plugin import Plugin, handler
|
|
|
|
|
from core.template import TemplateService
|
2022-07-26 10:07:31 +00:00
|
|
|
|
from utils.decorators.error import error_callable
|
|
|
|
|
from utils.decorators.restricts import restricts
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from utils.log import logger
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
class HelpPlugin(Plugin):
|
2022-09-02 08:27:54 +00:00
|
|
|
|
def __init__(self, template_service: TemplateService = None):
|
2022-07-26 10:07:31 +00:00
|
|
|
|
self.file_id = None
|
2022-09-08 01:08:37 +00:00
|
|
|
|
self.help_png = None
|
|
|
|
|
if template_service is None:
|
|
|
|
|
raise ModuleNotFoundError
|
|
|
|
|
self.template_service = template_service
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
|
@handler(CommandHandler, command="help", block=False)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
@error_callable
|
|
|
|
|
@restricts()
|
2022-09-08 01:08:37 +00:00
|
|
|
|
async def start(self, update: Update, _: CallbackContext):
|
2022-07-26 10:07:31 +00:00
|
|
|
|
user = update.effective_user
|
2022-09-08 01:08:37 +00:00
|
|
|
|
message = update.effective_message
|
|
|
|
|
logger.info(f"用户 {user.full_name}[{user.id}] 发出help命令")
|
|
|
|
|
if self.file_id is None or bot.config.debug:
|
2022-07-26 10:07:31 +00:00
|
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
2022-10-11 12:38:53 +00:00
|
|
|
|
help_png = await self.template_service.render("bot/help", "help.html", {}, {"width": 1280, "height": 900})
|
2022-07-26 10:07:31 +00:00
|
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
|
|
|
|
reply_photo = await message.reply_photo(help_png, filename="help.png", allow_sending_without_reply=True)
|
|
|
|
|
photo = reply_photo.photo[0]
|
|
|
|
|
self.file_id = photo.file_id
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
|
|
|
|
await message.reply_photo(self.file_id, allow_sending_without_reply=True)
|
|
|
|
|
except BadRequest as error:
|
|
|
|
|
self.file_id = None
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.error("发送图片失败,尝试清空已经保存的file_id,错误信息为", error)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
await message.reply_text("发送图片失败", allow_sending_without_reply=True)
|