Beispiel #1
0
async def _(session: CommandSession):
    if session.current_key:
        if session.current_arg_text.strip() in {'算了', '不删了', '取消'}:
            session.finish('好的')
            return

    if not session.current_key and session.current_arg.strip():
        # initial interaction, and there is an argument, we take it as the id
        session.current_key = 'id'

    if session.current_key == 'id':
        try:
            id_ = int(session.current_arg.strip())
            session.args['id'] = id_
        except ValueError:
            session.pause('ID 不正确,应只包含数字,请重新输入')
Beispiel #2
0
async def tuling(session: CommandSession):
    message = session.get('message', prompt='我已经准备好啦,来跟我聊天吧~')

    finish = message in ('结束', '拜拜', '再见')
    if finish:
        session.finish('拜拜啦,你忙吧,下次想聊天随时找我哦~')
        return

    # call tuling api
    reply = f'你说了:{message}'

    one_time = session.get_optional('one_time', False)
    if one_time:
        session.finish(reply)
    else:
        session.pause(reply)
Beispiel #3
0
async def _(session: CommandSession):
    text = session.current_arg_text.strip()

    if session.is_first_run and text:
        # first run, and there is an argument, we take it as the id
        session.current_key = 'id'

    if session.current_key == 'id':
        id_ = None
        try:
            # try parse the text message as an id
            id_ = int(text)
        except ValueError:
            # it's not directly a number
            if not session.is_first_run:
                # we are in and interactive session, do nlp

                # user may want to ask for all notes, check it
                match_score = await nlp.sentence_similarity(
                    session.current_arg_text.strip(), '现在有哪些呢?')
                if match_score > 0.70:
                    # we think it matches
                    await session.send_expr(expr.QUERYING_ALL)
                    # sleep to make conversation natural :)
                    await asyncio.sleep(1)
                    await call_command(session.bot,
                                       session.ctx, ('note', 'list'),
                                       check_perm=False,
                                       disable_interaction=True)

                    # pause the session and wait for further interaction
                    await session.pause()
                    return

                # user may also put the id in a natural sentence, check it
                m = re.search(r'\d+', text)
                if m:
                    possible_id = int(m.group(0))
                    match_score = await nlp.sentence_similarity(
                        session.current_arg_text.strip(), f'删掉笔记{possible_id}')
                    if match_score > 0.70:
                        # we think it matches
                        id_ = possible_id
        if id_ is not None:
            session.args['id'] = id_
        else:
            session.pause(render(expr.DEL_CANNOT_RECOGNIZE_ID))
Beispiel #4
0
async def tuling(session: CommandSession):
    message = session.get('message', prompt_expr=expr.I_AM_READY)

    ctx_id = context_id(session.ctx)
    if ctx_id in tuling_sessions:
        del tuling_sessions[ctx_id]

    tmp_msg = Message(message)
    text = tmp_msg.extract_plain_text()
    images = [s.data['url'] for s in tmp_msg
              if s.type == 'image' and 'url' in s.data]

    # call tuling api
    replies = await call_tuling_api(session, text, images)
    logger.debug(f'Got tuling\'s replies: {replies}')

    if replies:
        for reply in replies:
            await session.send(escape(reply))
            await asyncio.sleep(0.8)
    else:
        await session.send_expr(expr.I_DONT_UNDERSTAND)

    one_time = session.get_optional('one_time', False)
    if one_time:
        # tuling123 may opened a session, we should recognize the
        # situation that tuling123 want more information from the user.
        # for simplification, we only recognize named entities,
        # and since we will also check the user's input later,
        # here we can allow some ambiguity.
        ne_type = tuling_ne_type(replies, {
            'LOC': ('哪里', '哪儿', re.compile(r'哪\S城市'), '位置'),
            'TIME': ('什么时候',),
        })
        if ne_type:
            logger.debug(f'One time call, '
                         f'and there is a tuling session for {ne_type}')
            tuling_sessions[ctx_id] = ne_type
    else:
        session.pause()