API https://itchat.readthedocs.io/zh/latest/api/php
教程 https://www.yahboom.com/build.html?id=1999&cid=257html
项目地址 https://github.com/revotu/ItChatpython
更多使用 http://www.javashuo.com/article/p-hduqjifz-c.htmlgit
重要文件传输 : 视频 图片 语音 文字github
http://www.javashuo.com/article/p-xjyngefc-c.htmlshell
音乐交互bootstrap
https://itchat.readthedocs.io/zh/latest/tutorial/tutorial2/api
说明: 这玩意至关于网页版微信,因此登录了会挤掉电脑端。手机也会提示网页微信已经登录,手机通知关闭,可是手机仍然能够接收消息(因此和手机不冲突喽??)。微信
首先使用pip安装itchat:app
Windows用户能够直接打开cmd输入:
pip install itchat
Mac用户因为没有对anaconda的python配置环境变量,须要先在终端输入:
curl https://bootstrap.pypa.io/get-pip.py | python3
树莓派版本无需下载pip,直接下载itchat输入:
pip install itchat
pip命令和pip3的区别是:pip3能够直接下载适合python3 使用的package。对于树莓派,咱们能够根据spyder中python的版本使用pip2或pip3下载itchat:
pip2 install itchat
https://blog.csdn.net/lockelk/article/details/85296818
能够先查看是否已安装及安装的版本
pip list
解决:
先查看wxpy的安装路径
pip show itchat
而后将路径添加
python 进入>>>
>>>import sys
>>>sys.path.append("/home/pi/.local/lib/python2.7/site-packages")
其中括号内的为上一步实际查出的安装路径。
>>>sys.path 查看已安装的
#!/usr/bin/env python2 # -*- coding: utf-8 -*- import sys sys.path.append("/home/pi/.local/lib/python2.7/site-packages") import itchat #登录 扫描图片二维码,一次登录永久记录 itchat.auto_login(hotReload=True) #给文件出书助手,发送一条消息 itchat.send(u'你好', 'filehelper') #给指定用户发消息 users=itchat.search_friends("育铃") userName= users[0]['UserName'] print(userName) itchat.send('你好,姚宝贝蛋',toUserName=userName) #程序不停运行 itchat.run()
#!/usr/bin/env python2 # -*- coding: utf-8 -*- import sys sys.path.append("F:/dongdong/0tool/python/Anaconda3/Lib/site-packages") import itchat itchat.auto_login(hotReload=True)
https://blog.csdn.net/Lynn_coder/article/details/79436539
import itchat itchat.auto_login() itchat.send('Hello, filehelper', toUserName='filehelper')
这段代码意思是给filehelper发送一个hello,filehelper就是文件助手。
那么咱们想给指定的人发消息,并非把filehelper改掉这么简单
users=itchat.search_friends("老王") userName= users[0]['UserName'] print(userName) itchat.send('你好老王',toUserName=userName)
若是咱们想给老王发消息,就先使用itchat.search方法,会把全部备注名为老王的联系人全都找出来。
以后咱们选取第一个(若是你的联系人列表里只有一个老王,那么就只会搜出来一个)
users[0]取到的是一个联系人对象,他里面有个key叫UserName,它就是真正的用户的username
以后咱们再使用itchat.send方法,就能够成功向老王发送消息了
完整例程
#!/usr/bin/env python2 # -*- coding: utf-8 -*- import sys sys.path.append("F:/dongdong/0tool/python/Anaconda3/Lib/site-packages") import itchat #注册程序打开和关闭函数 def lc(): print('finish login') itchat.send(u'设备连线', 'filehelper') def ec(): itchat.send(u'设备离线', 'filehelper') # 注册打印出来消息的来源者函数 @itchat.msg_register(itchat.content.TEXT) def _(msg): # equals to print(msg['FromUserName']) print(msg.fromUserName) #登录 图片二维码扫描 一次登录以后不用从新扫描 itchat.auto_login(loginCallback=lc, exitCallback=ec,hotReload=True) #给文件出书助手,发送一条消息 itchat.send(u'你好', 'filehelper') users=itchat.search_friends("育铃") userName= users[0]['UserName'] print(userName) itchat.send('你好,姚宝贝蛋',toUserName=userName) #程序不停运行 itchat.run() #程序中止 #itchat.logout()
这个图片发送很奇怪,我开始死活发不出去,后来把图片搞小了一点发出去了,后来再用原来分辨率,又能够了,难道是当时我网速慢???
#!/usr/bin/env python2 # -*- coding: utf-8 -*- import sys sys.path.append("F:/dongdong/0tool/python/Anaconda3/Lib/site-packages") import itchat #注册程序打开和关闭函数 def lc(): print('finish login') itchat.send(u'设备连线', 'filehelper') def ec(): itchat.send(u'设备离线', 'filehelper') # 注册打印出来消息的来源者函数 @itchat.msg_register(itchat.content.TEXT) def _(msg): # equals to print(msg['FromUserName']) print(msg.fromUserName) #登录 图片二维码扫描 一次登录以后不用从新扫描 itchat.auto_login(loginCallback=lc, exitCallback=ec,hotReload=True) #给文件出书助手,发送一条消息 itchat.send(u'你好', 'filehelper') #找到指定用户 users=itchat.search_friends("育铃")#微信备注 userName= users[0]['UserName'] print(userName) itchat.send('哇,姚宝贝蛋',toUserName=userName) f="F:/dongdong/1.jpg" #图片地址 try: itchat.send_image(f,toUserName=userName) #若是是其余文件能够直接send_file print("success") except: print("fail") #程序不停运行 itchat.run() #程序中止 #itchat.logout()
消息分类:
图片或表情(PICTURE)、录制(RECORDING)、附件(ATTACHMENT)、小视频(VIDEO)、文本(TEXT),地图(MAP),名片(CARD),通知(NOTE),分享(SHARING),好友邀请(FRIENDS)、语音(RECORDING)、系统消息(SYSTEM)
http://www.javashuo.com/article/p-bpfmwsyb-p.html
图片或表情(PICTURE)、录制(RECORDING)、附件(ATTACHMENT)、小视频(VIDEO)、文本(TEXT),地图(MAP),名片(CARD),通知(NOTE),分享(SHARING),好友邀请(FRIENDS)、语音(RECORDING)、系统消息(SYSTEM)
https://github.com/BossBobxuan/WeChatToCommand/blob/master/chatToCommand.py
本项目的具体需求是:树莓派启动微信服务和OpenCV服务,OpenCV对摄像头实时视频监控,当检测到人脸后后拍照,将拍摄到的照片经过微信发送给用户的我的好友。
http://www.javashuo.com/article/p-xjyngefc-c.html
https://www.jianshu.com/p/c8b164b840db
使用 user_info = itchat.search_chatrooms(name='XXX') 查找群的时候,有时会遇到找不到的状况,须要在群设置选择-保存到通信录,才能被查找到
https://itchat.readthedocs.io/zh/latest/tutorial/tutorial2/
https://blog.csdn.net/weixin_33998125/article/details/85077561
https://www.alphayan.cn/index.php/2018/03/17/itchat_01/
https://itchat.readthedocs.io/zh/latest/tutorial/tutorial0/#_5
https://blog.csdn.net/xiaoxianerqq/article/details/81428054
https://blog.csdn.net/xiaoxianerqq/article/details/81428054
1,itchat:这个是主要的工具,用于链接微信我的帐号接口。如下是一些相关的知识点网站。
2,xlrd:这个是用来读Excel文件的工具。
3,apscheduler:这个是用来定时调度时间的工具。
本项目中,对于微信的操做,咱们须要用到的API是itchat。 itchat是一个微信对于python封装的api,它的功能很是强大,咱们可使用它和python对本身微信的不少信息进行爬取。
######################################### 程序说明 ######################################## # 功能: # 集合了接收消息处理,主动发送消息处理 # 使用: # 1运行后,第一次扫描二维码登录,能够图片形式,也能够命令行形式显示二维码 # 2注册接收消息和文件处理函数 # 2-1 我的用户或者群给本身发的消息,图片,文件,视频,保存等机制 # 2-2 本身经过文件助手给本身发的消息 # 3主动给别人发消息 # 3-1 本身给普通用户或者群发消息,各类文件,图片,视频 # 3-2 本身给文件助手发送消息,至关于给本身发消息,用来控制设备,发送文件给设备等 # ########################################################################################### #!/usr/bin/env python2 # -*- coding: utf-8 -*- import sys sys.path.append("F:/dongdong/0tool/python/Anaconda3/Lib/site-packages") import itchat #各种型消息的注册 from itchat.content import * from datetime import datetime #from itchat.content import PICTURE, RECORDING, ATTACHMENT, VIDEO,TEXT ######################################### 消息发送函数 ######################################## #1-1注册程序打开和关闭函数 def lc(): print('finish login') itchat.send(u'设备连线', 'filehelper') def ec(): itchat.send(u'设备离线', 'filehelper') #1-2给普通用户发送图片 def send_pic(picpath,toUserName): try: itchat.send_image(picpath,toUserName) #第一种通用发送方式 若是是其余文件能够直接send_file #itchat.send('@img@%s' % picpath, toUserName) #第二种指定类型发送方式 print("success_jpg") except: print("fail_jpg") #1-3给普通用户发送文件 def send_file(filepath,toUserName): try: #itchat.send('@fil@%s' % './filex/tt.txt', toUserName=msg['FromUserName']) itchat.send_file(filepath, toUserName) # txt若是是空的就不会发送 print("success_file") except: print("fail_file") #1-4给普通用户发送视频 def send_video(videopath,toUserName): try: #itchat.send('@vid@%s' % './filex/videox.mp4', toUserName=msg['FromUserName']) itchat.send_video(videopath, toUserName) print("success_video") except: print("fail_video") #1-5给群发消息 def SentChatRoomsMsg(chatroomName ,context): itchat.get_chatrooms(update=True) chatrooms = itchat.search_chatrooms(name=chatroomName) #有时会遇到找不到的状况,须要在该群设置选择-保存到通信录,才能被查找到 if len(chatrooms)==0 : print('没有找到群聊:' + chatroomName) # exit(0) else: print(chatrooms[0]['UserName'])#输出群聊标识符 itchat.send_msg(context, toUserName=chatrooms[0]['UserName'])#发送消息 ######################################### 消息处理回调函数 ######################################## # 消息分类: # 图片或表情(PICTURE)、录制(RECORDING)、附件(ATTACHMENT)、小视频(VIDEO)、文本(TEXT) # 地图(MAP),名片(CARD),通知(NOTE),分享(SHARING),好友邀请(FRIENDS)、语音(RECORDING)、系统消息(SYSTEM) # 1-3 文本消息接收和处理 # 1本身发给文件助手的 # 2别人发给己的 #@itchat.msg_register(itchat.content.TEXT,isGroupChat=True) # 2-1只针对群消息 #@itchat.msg_register(itchat.content.TEXT) # 2-2自针对普通用户消息 @itchat.msg_register(itchat.content.TEXT, isFriendChat=True, isGroupChat=True, isMpChat=True) # 2-3针对全部人消息 def text_reply(msg): #if msg['ToUserName'] != 'filehelper': return # 过滤掉不是文件助手的消息 #print(msg) # 打印消息 因此的信息 #print(msg["Text"]) # 收到的消息是什么 #print(msg["ToUserName"]) # 发给谁的 rebacktxt_msg= "收到:"+msg["Text"] #判断是不是@本人 #if msg["Text"].find(usermsgx["RemarkName"])== '育铃': if msg["ToUserName"].find("filehelper")==0: # 若是是给文件助手发的 itchat.send(rebacktxt_msg, 'filehelper') return rebacktxt_msg # 回复除了本身之外人的消息 #1-4文件下载---存在同级filex目录下 #1别人发给本身的消息 #2本身发给文件助手的 @itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO]) def download_files(msg): filedpx="./filex/"+msg["FileName"] # 获得文件路径+名字 目录须要手动建立 msg.download(filedpx) rebackfile_msg="收到文件类型"+msg['Type']+" 保存地址为:filex/"+msg.fileName if msg["ToUserName"].find("filehelper")==0: # 1若是是给文件助手发的,发个提醒 itchat.send(rebackfile_msg, 'filehelper') return rebackfile_msg # 2若是是别人发给本身的,直接回复对方 ######################################### 主函数函数 ######################################## #1登录 itchat.auto_login(hotReload=True,loginCallback=lc, exitCallback=ec) #图片二维码扫描 登录以后一段时间内不用从新扫描,注册打开和结束回调函数 #2-1找到本身的名字 usermsgx=itchat.search_friends() #获得当前用户的信息 首位是本身 #print(usermsgx) #个人微信ID #print(usermsgx["NickName"]) #个人备注名字 #2-2找到昵称为“”的用户 users=itchat.search_friends("育铃") #按照昵称查找 #itchat.search_friends(userName='@abcdefg1234567') #按照ID查找 userName= users[0]['UserName'] #print(userName) #3-1-1给文件助手,发送一条消息 itchat.send(u'你好文件助手,这是一条文本测试消息', 'filehelper') #3-1-2给我的用户,发送一条消息 #itchat.send('东小东: 这是一个来自树莓派的测试',toUserName=userName) #3-3给我的用户, 发送图片 pic_file_name='./filex/1.txt' #图片相对路径地址 #send_pic("F:/dongdong/1.jpg",'filehelper') #图片绝对路径地址 #3-4发送文件 #send_file('./filex/1.txt','filehelper') #3-5发送视频 #send_video('./filex/1.mp4','filehelper') #3-6群名向群里发送内容 #SentChatRoomsMsg('温县独霸', '你好') #程序保持运行 itchat.run() #程序中止 #itchat.logout()