From 7b360eb9ceb9d118df5b4093a4a6d8be5615357b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Sun, 31 Jul 2022 15:38:31 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=B7=BB=E5=8A=A0=E8=A7=92?= =?UTF-8?q?=E8=89=B2=E6=94=BB=E7=95=A5=E6=9F=A5=E8=AF=A2=E6=8F=92=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/strategy.py | 63 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 plugins/strategy.py diff --git a/plugins/strategy.py b/plugins/strategy.py new file mode 100644 index 00000000..2e430bdd --- /dev/null +++ b/plugins/strategy.py @@ -0,0 +1,63 @@ +from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup +from telegram.constants import ChatAction, ParseMode +from telegram.ext import filters, ConversationHandler, CommandHandler, MessageHandler, CallbackContext + +from app.game import GameStrategyService +from logger import Log +from plugins.base import BasePlugins +from utils.app.inject import inject +from utils.bot import get_all_args +from utils.decorators.error import error_callable +from utils.decorators.restricts import restricts +from utils.helpers import url_to_file +from utils.plugins.manager import listener_plugins_class + + +@listener_plugins_class() +class Strategy(BasePlugins): + """角色攻略查询""" + + KEYBOARD = [[InlineKeyboardButton(text="查看角色攻略列表并查询", switch_inline_query_current_chat="查看角色攻略列表并查询")]] + + @inject + def __init__(self, game_strategy_service: GameStrategyService = None): + self.game_strategy_service = game_strategy_service + + @classmethod + def create_handlers(cls) -> list: + strategy = cls() + return [ + CommandHandler("strategy", strategy.command_start, block=False), + MessageHandler(filters.Regex("^角色攻略查询(.*)"), strategy.command_start, block=False), + ] + + @error_callable + @restricts(return_data=ConversationHandler.END) + async def command_start(self, update: Update, context: CallbackContext) -> None: + message = update.message + user = update.effective_user + args = get_all_args(context) + if len(args) >= 1: + character_name = args[0] + else: + reply_message = await message.reply_text("请回复你要查询的攻略的角色名", + reply_markup=InlineKeyboardMarkup(self.KEYBOARD)) + if filters.ChatType.GROUPS.filter(reply_message): + self._add_delete_message_job(context, message.chat_id, message.message_id) + self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id) + return + url = await self.game_strategy_service.get_strategy(character_name) + if url == "": + reply_message = await message.reply_text(f"没有找到 {character_name} 的攻略", + reply_markup=InlineKeyboardMarkup(self.KEYBOARD)) + if filters.ChatType.GROUPS.filter(reply_message): + self._add_delete_message_job(context, message.chat_id, message.message_id) + self._add_delete_message_job(context, reply_message.chat_id, reply_message.message_id) + return + Log.info(f"用户 {user.full_name}[{user.id}] 查询角色攻略命令请求 || 参数 {character_name}") + await message.reply_chat_action(ChatAction.UPLOAD_PHOTO) + file_path = await url_to_file(url, "") + caption = "Form 米游社 西风驿站 " \ + f"查看 [原图]({url})" + await message.reply_photo(photo=open(file_path, "rb"), caption=caption, filename=f"{character_name}.png", + allow_sending_without_reply=True, parse_mode=ParseMode.MARKDOWN_V2)