async def _(session: CommandSession): if session.current_key == 'message': text = session.current_arg_text.strip() if ('拜拜' in text or '再见' in text) and len(text) <= 4: session.finish(render(expr.BYE_BYE)) return session.args[session.current_key] = session.current_arg
async def parse_args(session: CommandSession, parser: ArgumentParser, argv: Optional[List[str]], help_msg: str) -> Namespace: if not argv: await session.send(help_msg) else: try: return parser.parse_args(argv) except ParserExit as e: if e.status == 0: # --help await session.send(help_msg) else: await session.send('参数不足或不正确,请使用 --help 参数查询使用帮助') session.finish() # this will stop the command session
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 不正确,应只包含数字,请重新输入')
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)
async def handle_cancellation(session: CommandSession) -> None: """ Handle cancellation instructions. If the current arg text is a cancellation instruction, the command session will be made finished, so that the command will no long be run. This function is present for manually calling through the process of a command's args parser, which means the args parser can do something before handle cancellation. On contrary, @allow_cancellation decorator will handle cancellation at the very beginning of the args parser. :param session: command session to handle """ if session.is_first_run: return # we are in an interactive session, waiting for user's input # handle possible cancellation # use current_arg_text, we don't mind losing rich text parts of message text = session.current_arg_text.strip() small_sentences = re.split(r'\W+', text) logger.debug(f'Split small sentences: {small_sentences}') should_cancel = False new_ctx_message = None for i, sentence in enumerate(filter(lambda x: x.strip(), small_sentences)): if await is_cancellation(sentence): logger.debug(f'Sentence "{sentence}" is a cancellation, continue') should_cancel = True continue # this small sentence is not a cancellation, we split before this new_ctx_message = ','.join(small_sentences[i:]).strip() break if should_cancel: if not new_ctx_message: session.finish(render( session.bot.config.SESSION_CANCEL_EXPRESSION)) else: session.switch(new_ctx_message)