Python曾经对我说:“时间不多了,赶紧用Python吧”。于是看到了一个基于python的微信开源库:itchat。玩了一天,做了一个程序,收集私聊撤回的信息,发给个人微信文件传输助手,包括:who:whosentwhen:什么时候发消息what:什么信息which:what信息类型,包括:文字,图片,语音,视频,分享,位置,附件...01代码实现#-*-encoding:utf-8-*-importosimportreimportshutilimporttimeimportitchatfromitchat.contentimport*#描述:文字,语音,视频,图片、位置、名片、分享、附件可以撤回#{msg_id:(msg_from,msg_to,msg_time,msg_time_rec,msg_type,msg_content,msg_share_url)}msg_dict={}#文件存放临时目录rev_tmp_dir="/home/alic/revDir/"ifnotos.path.exists(rev_tmp_dir):os.mkdir(rev_tmp_dir)#表情有问题|接受信息和接受note的msg_id不一致巧合解决方法face_bug=None#将接收到的消息存入字典,当有新消息时,清理字典中的超时消息|不接受没有的信息召回功能msg):globalface_bug#获取本地时间戳并格式化本地时间戳e:2017-04-2121:30:08msg_time_rec=time.strftime("%Y-%m-%d%H:%M:%S",time.localtime())#messageIDmsg_id=msg['MsgId']#消息时间msg_time=msg['CreateTime']#发件人的昵称|这里也可以用RemarkName来备注 但是你或者没有备注的人是Nonemsg_from=(itchat.search_friends(userName=msg['FromUserName']))["NickName"]#消息内容msg_content=None#分享链接msg_share_url=Noneifmsg['Type']=='Text'\ormsg['Type']=='Friends':msg_content=msg['Text']elifmsg['Type']=='录音'\ormsg['类型']=='附件'\ormsg['类型']=='视频'\ormsg['类型']=='图片':msg_content=r""+msg['FileName']#保存文件msg['Text'](rev_tmp_dir+msg['FileName'])elifmsg['Type']=='Card':msg_content=msg['RecommendInfo']['NickName']+r"的名片"elifmsg['Type']=='Map':x,y,location=re.search(""+x.__str__()+"longitude->"+y.__str__()else:msg_content=r""+locationelifmsg['Type']=='Sharing':msg_content=msg['Text']msg_share_url=msg['Url']face_bug=msg_content#更新字典msg_dict.update({msg_id:{“消息_from":msg_from,"msg_time":msg_time,"msg_time_rec":msg_time_rec,"msg_type":msg["Type"],"msg_content":msg_content,"msg_share_url":msg_share_url}})#接收笔记通知消息,判断是否接收撤销并执行相应的操作@itchat.msg_register([NOTE])defsend_msg_helper(msg):globalface_bugifre.search(r"\<\!\[CDATA\[.*Amessagewaswithdrawn\]\]\>",msg['Content'])isnotNone:#获取消息的idold_msg_id=re.search("\(.*?)\<\/msgid\>",msg['Content']).group(1)old_msg=msg_dict.get(old_msg_id,{})iflen(old_msg_id)<11:itchat.send_file(rev_tmp_dir+face_bug,toUserName='filehelper')os.remove(rev_tmp_dir+face_bug)else:msg_body="告诉你一个秘密~"+"\n"\+old_msg.get('msg_from')+"收回"+old_msg.get("msg_type")+"消息"+"\n"\+old_msg.get('msg_time_rec')+"\n"\+"撤回了什么?"+"\n"\+r""+old_msg.get('msg_content')#如果有分享链接ifold_msg['msg_type']=="Sharing":msg_body+="\nThisisthelink?"+old_msg.get('msg_share_url')#发送取款信息到文件助手itchat.send(msg_body,toUserName='filehelper')#如果你有一个文件,还必须发回文件=="附件":file='@fil@%s'%(rev_tmp_dir+old_msg['msg_content'])itchat.send(msg=file,toUserName='filehelper')os.remove(rev_tmp_dir+old_msg['msg_content'])#删除字典中的旧消息msg_dict.pop(old_msg_id)if__name__=='__main__':itchat.auto_login(hotReload=True,enableCmdQR=2)itchat.run()这个程序可以直接在终端运行,直接在终端扫码登录成功,也可以在window系统打包运行(注意修改路径,建议使用相对路径)?~pythonwx.pyGettinguuidofQRcode.DownloadingQRcode.请扫描二维码登录。请按确认键哟urphone.Loadingthecontact,thismaytakealittlewhile.[3;JLoginsuccessfullyasAlicFengStartautoreplying.这都是编程逻辑的小问题。我还是记录下itchat微信,一个开源库。一、简介itchat是一个开源的微信个人账号接口,使用python调用微信变得非常简单。简单的是用itchat代码搭建一个基于微信的即时通讯,更好的是方便在其他平台扩展个人微信更多的通讯功能。2.安装pip3安装itchat3。itchat-Helloworld只需三行代码即可向文件传输助手发送消息。importitchatitchat.auto_login(hotReload=True)itchat.send('HelloAlicFeng',toUserName='filehelper')4.查看客户端学习最重要的是API手册:Githubforitchat:https://github.com/liduanwei/ItChat中文API:http://itchat.readthedocs.io/zh/latest/