Example #1
0
 def handle(self,
            msg,
            msg_type,
            sender_type,
            background=False,
            from_self=False):
     if from_self:
         return
     ItChatWrapper.send_msg(
         self.replay_text(msg['Text'], msg['FromUserName']),
         msg['FromUserName'])
Example #2
0
def create_bot(root_path='.') -> WxBot:
    """创建一个默认机器人"""
    info("Creating default bot.")
    info("Loading Configuration...")
    config = BotConfig(root_path)
    config.from_pyfile('wbot/config.dist.py', silent=True)
    config.from_pyfile('config.py', silent=True)
    config.from_env(silent=True)
    bot = WxBot(config)
    ext.init_ext(bot)
    modules.init_bot(bot)
    wrapper = ItChatWrapper(bot)
    wrapper.bind()
    return bot
Example #3
0
def get_chatroom_name_by_username(username: str) -> str:
    """将群的UserName转化成群昵称

    例如:PY Learning 的群 UserName 为 @@abcdef1234567890
    转化后输出 PY Learning
    """
    return ItChatWrapper.search_chatrooms(user_name=username).get('NickName')
Example #4
0
 def handle(self,
            msg,
            msg_type,
            sender_type,
            background=False,
            from_self=False):
     if background:
         if from_self:
             return
         username = msg['ActualNickName']
         to_user_name = msg['FromUserName']
         if username not in self.log_set:
             debug('%s Logged' % username)
             ItChatWrapper.send_msg('恭喜 @%s 冒泡成功!' % username, to_user_name)
             self.log_set.add(username)
             self.save_log()
Example #5
0
    def resend(self, msg, msg_type, sender_type):
        """重发消息"""
        if is_recall_message(msg, msg_type):
            key = gen_key(msg, msg_type)
            content = self.redis.get('WXBOTCNTN' + key).decode('utf-8')
            typ = self.redis.get('WXBOTTYPE' + key).decode('utf-8')
            username = msg['NickName'] if sender_type is SenderType.Friends else msg['ActualNickName']
            to_user_name = msg['FromUserName']
            if content:
                if typ == 'Text':
                    ItChatWrapper.send_msg("{username} 撤回消息:\n\n{content}".format(username=username, content=content),
                                           to_user_name)
                elif typ in ("Picture", "Video", "Recording", "Attachment"):
                    cn_type = {'Picture': '图片', 'Video': '视频', 'Recording': '录音', 'Attachment': '附件'}[typ]
                    send_meth = {'Picture': ItChatWrapper.send_image, 'Video': ItChatWrapper.send_video,
                                 'Recording': ItChatWrapper.send_file, 'Attachment': ItChatWrapper.send_file}[typ]

                    content_path = os.path.join(self.cache_dir, content)
                    if os.path.isfile(content_path):
                        ItChatWrapper.send_msg("{username} 撤回{typ}:".format(username=username, typ=cn_type),
                                               to_user_name)
                        send_meth(content_path, to_user_name)
                    else:
                        ItChatWrapper.send_msg(
                            "{username} 撤回一个{typ},但是文件不存在无法恢复。".format(username=username, typ=cn_type), to_user_name)
                elif typ == "Sharing":
                    ItChatWrapper.send_msg(
                        "{username} 撤回分享链接:{content}".format(username=username, content=content), to_user_name)
                elif typ == "Map":
                    ItChatWrapper.send_msg(
                        "{username} 撤回地图位置:{content}".format(username=username, content=content), to_user_name)
                elif typ == 'Card':
                    ItChatWrapper.send_msg(
                        "{username} 撤回名片分享:{content}".format(username=username, content=content), to_user_name)
                else:
                    pass