From f5814aa2beb07c5f882a8fc8396f3c96b8e5494f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=89=E5=B9=BF?= Date: Mon, 26 Jun 2023 18:38:54 +0800 Subject: [PATCH] feat[jx-wiki]support select by tips --- src/plugins/jx3/wiki/__init__.py | 23 +++++++++++++++++++++++ src/plugins/jx3/wiki/api.py | 15 +++++++++++---- src/views/jx3/wiki/question.html | 16 ++++------------ tests/test_jx3/test_wiki/__init__.py | 14 +++++++++++++- 4 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/plugins/jx3/wiki/__init__.py b/src/plugins/jx3/wiki/__init__.py index d71bb233..20669a98 100644 --- a/src/plugins/jx3/wiki/__init__.py +++ b/src/plugins/jx3/wiki/__init__.py @@ -17,5 +17,28 @@ async def jx3_wiki(state: T_State, event: GroupMessageEvent, args: Message = Com return await jx3_cmd_wiki.finish('没有说出需要接引的问题哦') logger.info(f'start wiki {arg_keywords}') result = await get_guide(arg_keywords) + state['result'] = result img = await render_items(arg_keywords, result.to_dict()) return await jx3_cmd_wiki.send(ms.image(Path(img).as_uri())) + + +@jx3_cmd_wiki.got('reference') +async def jx3_next_ques(state: T_State, event: GroupMessageEvent, reference: Message = CommandArg()): + ''' + 提交相关项查询 + ''' + template = [Jx3Arg(Jx3ArgsType.default)] + [arg_reference] = get_args( + reference.extract_plain_text(), template_args=template) + if not arg_reference: + return + arg_cmd = arg_reference[0:2].lower() + result = state.get('result') + arg_index = get_number(arg_reference[2:]) + if arg_cmd == 'xg' or arg_cmd == '相关': # 相关问题 + arg_keyword = result.tip.results[arg_index] + elif arg_cmd == 'yy' or arg_cmd == '引用': # 引用词 + arg_keyword = result.question.relateds[arg_index] + else: + return + return await jx3_wiki(state, event, obMessage(arg_keyword)) diff --git a/src/plugins/jx3/wiki/api.py b/src/plugins/jx3/wiki/api.py index ca27605b..a913e740 100644 --- a/src/plugins/jx3/wiki/api.py +++ b/src/plugins/jx3/wiki/api.py @@ -50,12 +50,16 @@ def __init__(self, data: dict) -> None: self.results = None return self.results = [x.get('answerContent') for x in self.items] + # 当没有回答时返回该列表 + self.confirm_list = [x.get('confirmList') for x in self.items] + self.relateds = [] # 相关问题 def to_dict(self): return { 'results': self.results, 'items': self.items, 'relateds': self.relateds, + 'confirm_list': self.confirm_list } @@ -72,7 +76,8 @@ def to_dict(self): class Jx3Guide: - API_host = 'https://chatrobot.xoyo.com/chatbot/' + API_web_host = 'https://chatrobot.xoyo.com/' + API_host = f'{API_web_host}chatbot/' # 初始化 API_init = 'web/init/{channel}?sysNum={channel}&sourceId={source}&lang=zh_CN&_={timestamp}' # 获取提示 @@ -143,6 +148,8 @@ async def handle_single_res(self, res: str): src = img.attrs.get('src') if not src: continue + if not src.startswith(Jx3Guide.API_web_host): + src = f'{Jx3Guide.API_web_host}{src}' img_data = await self.session.get(src) if not img_data.status_code == 200: img.attrs['src'] = '' # 错误 @@ -180,10 +187,10 @@ async def handle_single_res(self, res: str): return [result, relateds] async def handle_answer(self, res: QuesResponse): - pass - r = [await self.handle_single_res(x) for x in res.results] + r = [await self.handle_single_res(x) for x in res.results if x] res.results = [[x for x in paras[0] if x] for paras in r if paras] - res.relateds = extensions.flat([[x for x in paras[1] if x] for paras in r if paras]) + res.relateds = extensions.flat( + [[x for x in paras[1] if x] for paras in r if paras]) # logger.debug(f'answers handled:{res.results},{res.relateds}') async def run_async(self): diff --git a/src/views/jx3/wiki/question.html b/src/views/jx3/wiki/question.html index b710c690..057e9d63 100644 --- a/src/views/jx3/wiki/question.html +++ b/src/views/jx3/wiki/question.html @@ -150,7 +150,8 @@ padding: 0.5rem; border-radius: 0.8rem; font-size: 1rem; - max-width: 15rem; + max-width: 20rem; + line-height: 1.1rem; } .related-banner { @@ -170,17 +171,8 @@ formatTime, } const all_references = computed(() => { - const items = params_data.question.results - const result = [] - items.map(item => { // 最外层 - item.map(paras => { // 各段落 - paras.map(p => { // 各段 - if (!p.find) return // 检查是否是数组 - if (p[0] === 'RELA') result.push(p[1]) // 如果是关联项则加入 - }) - }) - }) - return result + const items = params_data.question + return items.relateds }) const reference_dict = computed(() => { // 将相关项目转为字典 diff --git a/tests/test_jx3/test_wiki/__init__.py b/tests/test_jx3/test_wiki/__init__.py index a9c96c6a..4ae4f30c 100644 --- a/tests/test_jx3/test_wiki/__init__.py +++ b/tests/test_jx3/test_wiki/__init__.py @@ -3,6 +3,7 @@ import src.plugins.jx3 from src.plugins.jx3 import wiki + def test_question(): mc = MessageCallback() wiki.jx3_cmd_wiki = mc @@ -11,7 +12,18 @@ def test_question(): state = {} event = SFGroupMessageEvent() - mc.tag = '五行石' + mc.tag = '端午节活动' + task = func(state, event, obMessage(mc.tag)) + asyncio.run(task) + mc.check_counter() + + func = src.plugins.jx3.jx3_next_ques + mc.tag = 'xg0' # 相关项 + task = func(state, event, obMessage(mc.tag)) + asyncio.run(task) + mc.check_counter() + + mc.tag = 'yy0' # 引用项 task = func(state, event, obMessage(mc.tag)) asyncio.run(task) mc.check_counter()