def text_reply(msg): logger.debug(json.dumps(msg)) msg_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(msg.createTime)) msg_from = msg['ActualNickName'] msg_to = msg['User']['NickName'] msg_text = msg['Text'] wx_record = WxRecord(is_group='1', msg_type=msg.type, msg_time=msg_time, msg_from=msg_from, msg_to=msg_to, msg_text=msg_text) wx_record.save() q.put( WebMessage({ 'is_group': '1', 'msg_type': msg.type, 'msg_time': msg_time, 'msg_from': msg_from, 'msg_to': msg_to, 'msg_text': msg_text })) logger.debug( "save to db type:%s time:%s from:%-15s to: %-15s content:%s" % ('2', msg_time, msg_from, msg_to, msg_text))
def download_files(msg): msg.download(os.path.join(wx_files_dir, msg.fileName)) msg_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(msg.createTime)) msg_from = itchat.search_friends( userName=msg['FromUserName'])['NickName'] msg_to = itchat.search_friends(userName=msg['ToUserName'])['NickName'] msg_text = os.path.join(wx_files_dir, msg.fileName) wx_record = WxRecord(is_group='1', msg_type=msg.type, msg_time=msg_time, msg_from=msg_from, msg_to=msg_to, msg_text=msg_text) wx_record.save() q.put( WebMessage({ 'is_group': '1', 'msg_type': msg.type, 'msg_time': msg_time, 'msg_from': msg_from, 'msg_to': msg_to, 'msg_text': msg_text })) logger.debug( "save to db type:%s time:%s from:%-15s to: %-15s content:%s" % (msg.type, msg_time, msg_from, msg_to, msg_text))
def text_reply(msg): logger.debug(json.dumps(msg)) # q_msg = WebMessage('text', msg) # q.put(q_msg) msg_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(msg.createTime)) msg_from = itchat.search_friends( userName=msg['FromUserName'])['NickName'] msg_to = itchat.search_friends(userName=msg['ToUserName'])['NickName'] msg_text = msg['Text'] wx_record = WxRecord(is_group='0', msg_type=msg.type, msg_time=msg_time, msg_from=msg_from, msg_to=msg_to, msg_text=msg_text) wx_record.save() q.put( WebMessage({ 'is_group': '0', 'msg_type': msg.type, 'msg_time': msg_time, 'msg_from': msg_from, 'msg_to': msg_to, 'msg_text': msg_text })) logger.debug( "save to db type:%s time:%s from:%-15s to: %-15s content:%s" % (msg.type, msg_time, msg_from, msg_to, msg_text))
def login(): if load_login(): global if_login if_login = True logger.info('loan login status success') return '200' logger.info('begin to login ...') status = itchat.check_login(qruuid) logger.info('check login status' + status) if status == '200': if_login = True logger.info('check login, status success') elif status == '201': logger.info('check login, need confirm') return status elif status == '408': logger.info('check login, qrCode timeout') get_qr() return status # 获取登陆人信息 user_info = itchat.web_init() logger.info('Login successfully as %s' % user_info['User']['NickName']) # 手机web微信登陆状态显示 itchat.show_mobile_login() logger.info('show mobile login') # 获取最新近聊列表 itchat.get_contact(update=True) logger.info('get contact complete') # 获取最新好友列表 itchat.get_friends(update=True) logger.info('get friends complete') # 获取最新群聊列表 chat_rooms = itchat.get_chatrooms(update=True) logger.info('get chatRooms complete') # 更新群聊详细信息(人员列表) for chat_room in chat_rooms: logger.debug(json.dumps(chat_room)) itchat.update_chatroom(userName=chat_room['UserName']) logger.info('update chatRooms members complete') # 保存登陆状态 itchat.dump_login_status(fileDir=login_status_dir) logger.info('save the login success to %s' % login_status_dir) # 启动心跳连接 itchat.start_receiving() logger.info('start receiving and heartbeat') class WebMessage(object): def __init__(self, _msg): self._msg = _msg def get_msg(self): return self._msg # 消息注册,好友文本消息 @itchat.msg_register(TEXT) def text_reply(msg): logger.debug(json.dumps(msg)) # q_msg = WebMessage('text', msg) # q.put(q_msg) msg_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(msg.createTime)) msg_from = itchat.search_friends( userName=msg['FromUserName'])['NickName'] msg_to = itchat.search_friends(userName=msg['ToUserName'])['NickName'] msg_text = msg['Text'] wx_record = WxRecord(is_group='0', msg_type=msg.type, msg_time=msg_time, msg_from=msg_from, msg_to=msg_to, msg_text=msg_text) wx_record.save() q.put( WebMessage({ 'is_group': '0', 'msg_type': msg.type, 'msg_time': msg_time, 'msg_from': msg_from, 'msg_to': msg_to, 'msg_text': msg_text })) logger.debug( "save to db type:%s time:%s from:%-15s to: %-15s content:%s" % (msg.type, msg_time, msg_from, msg_to, msg_text)) # 消息注册,好友图片/音频/视频/文件消息 @itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO]) def download_files(msg): msg.download(os.path.join(wx_files_dir, msg.fileName)) msg_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(msg.createTime)) msg_from = itchat.search_friends( userName=msg['FromUserName'])['NickName'] msg_to = itchat.search_friends(userName=msg['ToUserName'])['NickName'] msg_text = os.path.join(wx_files_dir, msg.fileName) wx_record = WxRecord(is_group='0', msg_type=msg.type, msg_time=msg_time, msg_from=msg_from, msg_to=msg_to, msg_text=msg_text) wx_record.save() q.put( WebMessage({ 'is_group': '0', 'msg_type': msg.type, 'msg_time': msg_time, 'msg_from': msg_from, 'msg_to': msg_to, 'msg_text': msg_text })) logger.debug( "save to db type:%s time:%s from:%-15s to: %-15s content:%s" % (msg.type, msg_time, msg_from, msg_to, msg_text)) # 消息注册,群文本消息 @itchat.msg_register(TEXT, isGroupChat=True) def text_reply(msg): logger.debug(json.dumps(msg)) msg_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(msg.createTime)) msg_from = msg['ActualNickName'] msg_to = msg['User']['NickName'] msg_text = msg['Text'] wx_record = WxRecord(is_group='1', msg_type=msg.type, msg_time=msg_time, msg_from=msg_from, msg_to=msg_to, msg_text=msg_text) wx_record.save() q.put( WebMessage({ 'is_group': '1', 'msg_type': msg.type, 'msg_time': msg_time, 'msg_from': msg_from, 'msg_to': msg_to, 'msg_text': msg_text })) logger.debug( "save to db type:%s time:%s from:%-15s to: %-15s content:%s" % ('2', msg_time, msg_from, msg_to, msg_text)) # 消息注册,群图片/音频/视频/文件消息 @itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO], isGroupChat=True) def download_files(msg): msg.download(os.path.join(wx_files_dir, msg.fileName)) msg_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(msg.createTime)) msg_from = itchat.search_friends( userName=msg['FromUserName'])['NickName'] msg_to = itchat.search_friends(userName=msg['ToUserName'])['NickName'] msg_text = os.path.join(wx_files_dir, msg.fileName) wx_record = WxRecord(is_group='1', msg_type=msg.type, msg_time=msg_time, msg_from=msg_from, msg_to=msg_to, msg_text=msg_text) wx_record.save() q.put( WebMessage({ 'is_group': '1', 'msg_type': msg.type, 'msg_time': msg_time, 'msg_from': msg_from, 'msg_to': msg_to, 'msg_text': msg_text })) logger.debug( "save to db type:%s time:%s from:%-15s to: %-15s content:%s" % (msg.type, msg_time, msg_from, msg_to, msg_text)) # 新建线程跑任务 def new_thread(): itchat.run() threading.Thread(target=new_thread).start() logger.info("聊天记录同步中 ... ") return status