Skip to content

Commit

Permalink
Merge pull request #260 from serfend/JxWiki
Browse files Browse the repository at this point in the history
feat[jx-wiki]support select by tips
  • Loading branch information
Serfend authored Jun 26, 2023
2 parents 7c32814 + f5814aa commit 37b70fc
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 17 deletions.
23 changes: 23 additions & 0 deletions src/plugins/jx3/wiki/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
15 changes: 11 additions & 4 deletions src/plugins/jx3/wiki/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
}


Expand All @@ -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}'
# 获取提示
Expand Down Expand Up @@ -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'] = '' # 错误
Expand Down Expand Up @@ -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):
Expand Down
16 changes: 4 additions & 12 deletions src/views/jx3/wiki/question.html
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(() => {
// 将相关项目转为字典
Expand Down
14 changes: 13 additions & 1 deletion tests/test_jx3/test_wiki/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import src.plugins.jx3
from src.plugins.jx3 import wiki


def test_question():
mc = MessageCallback()
wiki.jx3_cmd_wiki = mc
Expand All @@ -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()

0 comments on commit 37b70fc

Please sign in to comment.