Example #1
0
    def __init__(self,
                 cache_path=None,
                 console_qr=False,
                 qr_path=None,
                 qr_callback=None,
                 login_callback=None,
                 logout_callback=None):
        """
        :param cache_path:
            * 设置当前会话的缓存路径,并开启缓存功能;为 `None` (默认) 则不开启缓存功能。
            * 开启缓存后可在短时间内避免重复扫码,缓存失效时会重新要求登陆。
            * 设为 `True` 时,使用默认的缓存路径 'wxpy.pkl'。
        :param console_qr:
            * 在终端中显示登陆二维码,需要安装 pillow 模块 (`pip3 install pillow`)。
            * 可为整数(int),表示二维码单元格的宽度,通常为 2 (当被设为 `True` 时,也将在内部当作 2)。
            * 也可为负数,表示以反色显示二维码,适用于浅底深字的命令行界面。
            * 例如: 在大部分 Linux 系统中可设为 `True` 或 2,而在 macOS Terminal 的默认白底配色中,应设为 -2。
        :param qr_path: 保存二维码的路径
        :param qr_callback: 获得二维码后的回调,接收参数: uuid, status, qrcode
        :param login_callback: 登陆成功后的回调,接收参数同上
        :param logout_callback: 登出时的回调,接收参数同上
        """

        self.core = itchat.Core()
        itchat.instanceList.append(self)

        if cache_path is True:
            cache_path = 'wxpy.pkl'

        if console_qr is True:
            console_qr = 2

        self.core.auto_login(hotReload=bool(cache_path),
                             statusStorageDir=cache_path,
                             enableCmdQR=console_qr,
                             picDir=qr_path,
                             qrCallback=qr_callback,
                             loginCallback=login_callback,
                             exitCallback=logout_callback)

        self.message_configs = MessageConfigs(self)
        self.messages = Messages(bot=self)

        self.file_helper = Chat(wrap_user_name('filehelper'), self)

        self.self = Chat(self.core.loginInfo['User'], self)
        self.self.bot = self

        self.cache_path = cache_path
Example #2
0
    def _get_chat_by_user_name(self, user_name):
        """
        通过 user_name 找到对应的聊天对象

        :param user_name: user_name
        :return: 找到的对应聊天对象
        """
        def match_in_chats(_chats):
            for c in _chats:
                if c.user_name == user_name:
                    return c

        _chat = None

        if user_name.startswith('@@'):
            _chat = match_in_chats(self.bot.groups())
        elif user_name:
            _chat = match_in_chats(self.bot.friends())
            if _chat is None:
                _chat = match_in_chats(self.bot.mps())

        if _chat is None:
            _chat = Chat(wrap_user_name(user_name), self.bot)

        return _chat