Esempio n. 1
0
 def __init__(self, bot: NoneBot, ctx: Dict[str, Any], msg: str):
     super().__init__(bot, ctx)
     self.msg = msg
     tmp_msg = Message(msg)
     self.msg_text = tmp_msg.extract_plain_text()
     self.msg_images = [s.data['url'] for s in tmp_msg
                        if s.type == 'image' and 'url' in s.data]
Esempio n. 2
0
def extract_text(arg: Message_T) -> str:
    """
    提取消息中的纯文本部分(使用空格合并纯文本消息段)。

    参数:
        arg (nonebot.typing.Message_T):
    """
    arg_as_msg = Message(arg)
    return arg_as_msg.extract_plain_text()
Esempio n. 3
0
 def __init__(self, bot: NoneBot, event: CQEvent, msg: str):
     super().__init__(bot, event)
     self.msg: str = msg
     """以字符串形式表示的消息内容,已去除开头的 @ 和机器人称呼,可能存在 CQ 码。"""
     tmp_msg = Message(msg)
     self.msg_text: str = tmp_msg.extract_plain_text()
     """消息内容的纯文本部分,已去除所有 CQ 码/非 `text` 类型的消息段。各纯文本消息段之间使用空格连接。"""
     self.msg_images: List[str] = [
         s.data['url'] for s in tmp_msg
         if s.type == 'image' and 'url' in s.data
     ]
     """消息内容中所有图片的 URL 的列表,如果消息中没有图片,则为 `[]`。"""
Esempio n. 4
0
    def refresh(self, ctx: Dict[str, Any], *, current_arg: str = '') -> None:
        """
        Refill the session with a new message context.

        :param ctx: new message context
        :param current_arg: new command argument as a string
        """
        self.ctx = ctx
        self.current_arg = current_arg
        current_arg_as_msg = Message(current_arg)
        self.current_arg_text = current_arg_as_msg.extract_plain_text()
        self.current_arg_images = [s.data['url'] for s in current_arg_as_msg
                                   if s.type == 'image' and 'url' in s.data]
Esempio n. 5
0
async def tuling(session: CommandSession):
    message = session.get('message', prompt=__(e.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(__(e.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()
Esempio n. 6
0
def _extract_text(arg: Message_T) -> str:
    """Extract all plain text segments from a message-like object."""
    arg_as_msg = Message(arg)
    return arg_as_msg.extract_plain_text()