From 32030cf630abc7afa0f9443712c9a6aabb35ecef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B4=9B=E6=B0=B4=E5=B1=85=E5=AE=A4?= Date: Sat, 6 Aug 2022 17:07:47 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=B7=BB=E5=8A=A0=20`inline=5Fquer?= =?UTF-8?q?y`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugins/inline.py | 81 +++++++++++++++++++++++++++++++++++++++ utils/plugins/register.py | 8 +++- 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 plugins/inline.py diff --git a/plugins/inline.py b/plugins/inline.py new file mode 100644 index 0000000..aa9d912 --- /dev/null +++ b/plugins/inline.py @@ -0,0 +1,81 @@ +from typing import cast +from uuid import uuid4 + +from telegram import InlineQueryResultArticle, InputTextMessageContent, Update, InlineQuery +from telegram.constants import ParseMode +from telegram.error import BadRequest +from telegram.ext import CallbackContext + +from apps.wiki import WikiService +from logger import Log +from utils.apps.inject import inject + + +class Inline: + """Inline模块""" + + @inject + def __init__(self, wiki_service: WikiService): + self.wiki_service = wiki_service + + async def inline_query(self, update: Update, _: CallbackContext) -> None: + user = update.effective_user + ilq = cast(InlineQuery, update.inline_query) + query = ilq.query + switch_pm_text = "需要帮助嘛?" + results_list = [] + args = query.split(" ") + if args[0] == "": + pass + else: + if "查看武器列表并查询" == args[0]: + weapons_list = await self.wiki_service.get_weapons_name_list() + for weapons_name in weapons_list: + results_list.append( + InlineQueryResultArticle( + id=str(uuid4()), + title=weapons_name, + description=f"查看武器列表并查询 {weapons_name}", + input_message_content=InputTextMessageContent(f"武器查询{weapons_name}", + parse_mode=ParseMode.MARKDOWN_V2) + )) + elif "查看角色攻略列表并查询" == args[0]: + characters_list = await self.wiki_service.get_characters_name_list() + for role_name in characters_list: + results_list.append( + InlineQueryResultArticle( + id=str(uuid4()), + title=role_name, + description=f"查看角色攻略列表并查询 {role_name}", + input_message_content=InputTextMessageContent(f"角色攻略查询{role_name}", + parse_mode=ParseMode.MARKDOWN_V2) + )) + + if len(results_list) == 0: + results_list.append( + InlineQueryResultArticle( + id=str(uuid4()), + title="好像找不到问题呢", + description="这个问题我也不知道,因为我就是个应急食品。", + input_message_content=InputTextMessageContent("这个问题我也不知道,因为我就是个应急食品。"), + )) + try: + await ilq.answer( + results=results_list, + switch_pm_text=switch_pm_text, + switch_pm_parameter="inline_message", + cache_time=0, + auto_pagination=True, + ) + except BadRequest as exc: + if "Query is too old" in exc.message: # 过时请求全部忽略 + Log.warning(f"用户 {user.full_name}[{user.id}] inline_query请求过时") + return + if "can't parse entities" not in exc.message: + raise exc + Log.warning("inline_query发生BadRequest错误", exc_info=exc) + await ilq.answer( + results=[], + switch_pm_text="糟糕,发生错误了。", + switch_pm_parameter="inline_message", + ) diff --git a/utils/plugins/register.py b/utils/plugins/register.py index cb9b4e4..5484aa0 100644 --- a/utils/plugins/register.py +++ b/utils/plugins/register.py @@ -1,9 +1,10 @@ from typing import Optional -from telegram.ext import CommandHandler, MessageHandler, filters, CallbackQueryHandler, Application +from telegram.ext import CommandHandler, MessageHandler, filters, CallbackQueryHandler, Application, InlineQueryHandler from logger import Log from plugins.errorhandler import error_handler +from plugins.inline import Inline from plugins.start import start, ping, reply_keyboard_remove, unknown_command from utils.plugins.manager import PluginsManager @@ -42,13 +43,16 @@ def register_plugin_handlers(application: Application): Log.info("正在加载内置插件") + inline = Inline() + add_handler(start, command="start") add_handler(ping, command="ping") # 调试功能 add_handler(reply_keyboard_remove, command="reply_keyboard_remove") + application.add_handler(InlineQueryHandler(inline.inline_query, block=False)) application.add_handler(MessageHandler(filters.COMMAND & filters.ChatType.PRIVATE, unknown_command)) application.add_error_handler(error_handler, block=False) - Log.info("插件加载成功") \ No newline at end of file + Log.info("插件加载成功")