https://blog.csdn.net/u012315529/article/details/86501772
itchat 接口說明文檔
2019年01月16日 08:53:20?混元大仙?閱讀數(shù) 2191
sudo pip install itchat
itchat.auto_login()這種方法將會通過微信掃描二維碼登錄,但是這種登錄的方式確實短時間的登錄未斑,并不會保留登錄的狀態(tài)吕嘀,也就是下次登錄時還是需要掃描二維碼,如果加上hotReload==True,那么就會保留登錄的狀態(tài)栗菜,至少在后面的幾次登錄過程中不會再次掃描二維碼,該參數(shù)生成一個靜態(tài)文件itchat.pkl用于存儲登錄狀態(tài)
這里主要使用的是灰調(diào)函數(shù)的方法,登錄完成后的方法需要賦值在loginCallback?中退出后的方法,需要賦值在exitCallback?中.若不設(shè)置loginCallback?的值, 將會自動刪除二維碼圖片并清空命令行顯示.
importitchat, time
deflc():
print("Finash Login!")
defec():
print("exit")
itchat.auto_login(loginCallback=lc, exitCallback=ec)
time.sleep()
itchat.logout()#強制退出登錄? ?
send(msg="Text Message", toUserName=None)
參數(shù):
msg: 文本消息內(nèi)容
@fil@path_to_file: 發(fā)送文件
@img@path_to_img: 發(fā)送圖片
@vid@path_to_video: 發(fā)送視頻
toUserName: 發(fā)送對象, 如果留空, 將發(fā)送給自己.
返回值
TrueorFalse
實例代碼
# coding-utf-8
import itchat
itchat.auto_login()
itchat.send("Hello World!")
ithcat.send("@fil@%s"%'/tmp/test.text')
ithcat.send("@img@%s"%'/tmp/test.png')
ithcat.send("@vid@%s"%'/tmp/test.mkv')
send_msg(msg='Text Message', toUserName=None),其中的的msg是要發(fā)送的文本,toUserName是發(fā)送對象, 如果留空, 將發(fā)送給自己,返回值為True或者False
實例代碼
importitchat
itchat.auto_login()
itchat.send_msg("hello world.")
send_file(fileDir, toUserName=None)fileDir是文件路徑, 當文件不存在時, 將打印無此文件的提醒搔啊,返回值為True或者False
實例代碼
mport itchat
itchat.auto_login()
itchat.send_file("/tmp/test.txt")
send_image(fileDir, toUserName=None)參數(shù)同上
實例代碼
importitchat
itchat.auto_login()
itchat.send_img("/tmp/test.txt")
send_video(fileDir, toUserName=None)參數(shù)同上
實例代碼
importitchat
itchat.auto_login()
itchat.send_video("/tmp/test.txt")
itchat將根據(jù)接受到的消息類型尋找對應的已注冊的方法.如果一個消息類型沒有對應的注冊方法, 該消息將會被舍棄.在運行過程中也可以動態(tài)注冊方法, 注冊方式與結(jié)果不變.
不帶具體對象注冊, 將注冊為普通消息的回復方法.
importitchat
fromitchat.contentimport*
@itchat.msg_register(TEXT)? #這里的TEXT表示如果有人發(fā)送文本消息,那么就會調(diào)用下面的方法
defsimple_reply(msg):
#這個是向發(fā)送者發(fā)送消息
itchat.send_msg('已經(jīng)收到了文本消息北戏,消息內(nèi)容為%s'%msg['Text'],toUserName=msg['FromUserName'])
return"T reveived: %s"% msg["Text"]#返回的給對方的消息负芋,msg["Text"]表示消息的內(nèi)容
帶對象參數(shù)注冊, 對應消息對象將調(diào)用該方法,其中isFriendChat表示好友之間嗜愈,isGroupChat表示群聊旧蛾,isMapChat表示公眾號
importitchat
fromitchat.contentimport*
@itchat.msg_register(TEXT, isFriendChat=True, isGroupChat=True,isMpChat=True)
deftext_reply(msg):
msg.user.send("%s : %s"% (mst.type, msg.text))
消息類型向注冊方法傳入的msg包含微信返回的字典的所有內(nèi)容.itchat增加Text,Type(也就是參數(shù)) 鍵值, 方便操作.
itcaht.content?中包含所有的消息類型參數(shù), 如下表
參數(shù)l類型Text 鍵值
TEXT文本文本內(nèi)容(文字消息)
MAP地圖位置文本(位置分享)
CARD名片推薦人字典(推薦人的名片)
SHARING分享分享名稱(分享的音樂或者文章等)
PICTURE??????? 下載方法?圖片/表情
RECORDING語音下載方法
ATTACHMENT附件下載方法
VIDEO小視頻下載方法
FRIENDS好友邀請?zhí)砑雍糜阉鑵?shù)
SYSTEM系統(tǒng)消息更新內(nèi)容的用戶或群聊的UserName組成的列表
NOTE通知通知文本(消息撤回等)
itchat的附件下載方法存儲在msg的Text鍵中.發(fā)送的文件名(圖片給出的默認文件名), 都存儲在msg的FileName鍵中.下載方法, 接受一個可用的位置參數(shù)(包括文件名), 并將文件響應的存儲.注意:下載的文件存儲在指定的文件中,直接將路徑與FileName連接即可芝硬,如msg["Text"]('/tmp/weichat'+msg['FileName'])
@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO])
defdownload_files(msg):
#msg.download(msg['FileName'])? #這個同樣是下載文件的方式
msg['Text'](msg['FileName'])#下載文件
#將下載的文件發(fā)送給發(fā)送者
itchat.send('@%s@%s'% ('img'ifmsg['Type'] =='Picture'else'fil', msg["FileName"]), msg["FromUserName"])
增加了三個鍵值蚜点,如下:
isAt判斷是否 @ 本號
ActualNickName: 實際NickName(昵稱)
Content: 實際Content
測試程序
importitcaht
fromitchat.contentimportTEXT
@itchat.msg_register(TEXT, isGroupChat=True)
deftext_reply(msg):
if(msg.isAt):#判斷是否有人@自己
#如果有人@自己,就發(fā)一個消息告訴對方我已經(jīng)收到了信息
itchat.send_msg("我已經(jīng)收到了來自{0}的消息拌阴,實際內(nèi)容為{1}".format(msg['ActualNickName'],msg['Text']),toUserName=msg['FromUserName'])
itchat.auto_login()
itchat.run()
總的來說就是后面注冊同種類型的消息會覆蓋之前注冊的消息,詳情見文檔https://itchat.readthedocs.io/zh/latest/
注意:所有的消息內(nèi)容都是可以用鍵值對來訪問的奶镶,如msg["FromUserName]就是查看發(fā)送者迟赃,itchat.search_friends(userName=msg['FromUserName'])['NickName']查看的是當發(fā)送者昵稱
一般的消息都遵循以下的內(nèi)容:
{
"FromUserName":"",
"ToUserName":"",
"Content":"",
"StatusNotifyUserName":"",
"ImgWidth":0,
"PlayLength":0,
"RecommendInfo": {},
"StatusNotifyCode":0,
"NewMsgId":"",
"Status":0,
"VoiceLength":0,
"ForwardFlag":0,
"AppMsgType":0,
"Ticket":"",
"AppInfo": {},
"Url":"",
"ImgStatus":0,
"MsgType":0,
"ImgHeight":0,
"MediaId":"",
"MsgId":"",
"FileName":"",
"HasProductId":0,
"FileSize":"",
"CreateTime":0,
"SubMsgType":0
}
MsgType:51
? ? FromUserName: 自己ID
? ? ToUserName: 自己ID
? ? StatusNotifyUserName: 最近聯(lián)系的聯(lián)系人ID
? ? Content:
? ? ? ? ? ? ? ? ? ? # 最近聯(lián)系的聯(lián)系人
? ? ? ? ? ? ? ? ? ? filehelper,xxx@chatroom,wxid_xxx,xxx,...
? ? ? ? ? ? ? ? ? ? ? ? # 朋友圈
? ? ? ? ? ? ? ? ? ? ? ? ? ? MomentsUnreadMsgStatus
? ? ? ? ? ? ? ? ? ? ? ? ? ? 1454502365
? ? ? ? ? ? ? ? # 未讀的功能賬號消息,群發(fā)助手厂镇,漂流瓶等
MsgType: 1
? ? FromUserName: 發(fā)送方ID
? ? ToUserName: 接收方ID
? ? Content: 消息內(nèi)容
itchat增加了Text鍵, 鍵值為 下載該圖片的方法.
MsgType:3
? ? FromUserName: 發(fā)送方ID
? ? ToUserName: 接收方ID
? ? MsgId: 用于獲取圖片纤壁,用于表示每一條消息
? ? Content:
拓展:如果想要得到Content中的具體內(nèi)容可以使用正則表達式匹配出來
*itchat增加了Text鍵, 鍵值為 下載該視頻的方法.*
MsgType:62
? ? FromUserName: 發(fā)送方ID
? ? ToUserName: 接收方ID
? ? MsgId: 用于獲取小視頻
? ? Content:
itchat增加了Text鍵, 鍵值為 該地點的文本形式.
MsgType: 1
? ? FromUserName: 發(fā)送方ID
? ? ToUserName: 接收方ID
? ? Content: http://weixin.qq.com/cgi-bin/redirectforward?args=xxx
OriContent:<?xml version="1.0"?>
itchat增加了Text鍵, 鍵值為 該調(diào)用add_friend需要的屬性.
MsgType:42
? ? FromUserName: 發(fā)送方ID
? ? ToUserName: 接收方ID
? ? Content:
<?xml version="1.0"?>
? ? RecommendInfo:
? ? ? ? {
"UserName":"xxx",# ID,這里的是昵稱
"Province":"xxx",
"City":"xxx",
"Scene":17,
"QQNum":0,
"Content":"",
"Alias":"xxx",# 微信號
"OpCode":0,
"Signature":"",
"Ticket":"",
"Sex":0,# 1:男, 2:女
"NickName":"xxx",# 昵稱
"AttrStatus":4293221,
"VerifyFlag":0
? ? ? ? }
下面是添加好友的測試代碼
@itchat.msg_register(itchat.content.CARD,isFriendChat=True)
defsimply(msg):
printmsg['Text']
printmsg['Content']
itchat.add_friend(userName=msg['Text']['UserName'])#添加推薦的好友
printmsg['RecommendInfo']
printmsg['RecommendInfo']['UserName']
*itchat增加了Text鍵,鍵值為下載該語音文件的方法,下載下來的是MP3的格式
MsgType:34
? ? FromUserName: 發(fā)送方ID
? ? ToUserName: 接收方ID
? ? MsgId: 用于獲取語音
? ? Content:
下載方法:msg['Text'](msg['FileName'])
itchat添加了Text鍵捺信,鍵值為下載該圖片表情的方法酌媒。注意:本人親測對于一些微信商店提供的表情是不能下載成功的,這里的自帶的表情emoji是屬于TEXT類別的,因此如果將其注冊為PICTURE消息類型的話是不可以監(jiān)測到的
MsgType:47
? ? FromUserName: 發(fā)送方ID
? ? ToUserName: 接收方ID
? ? Content:
主要針對的是分享的文章等等
MsgType:49
AppMsgType:5
? ? FromUserName: 發(fā)送方ID
? ? ToUserName: 接收方ID
? ? Url: 鏈接地址
? ? FileName: 鏈接標題
? ? Content:
5
? ? ? ? ? ? ? ? ...
主要針對的是音樂
MsgType: 49
? ? AppMsgType: 3
? ? FromUserName: 發(fā)送方ID
? ? ToUserName: 接收方ID
? ? Url: 鏈接地址
? ? FileName: 音樂名
? ? AppInfo: # 分享鏈接的應用
? ? ? ? {
? ? ? ? ? ? Type: 0,
? ? ? ? ? ? AppID: wx485a97c844086dc9
? ? ? ? }
? ? Content:
3
0
0
? ? ? ? ? ? ? ? ? ? http://ws.stream.qqmusic.qq.com/C100003i9hMt1bgui0.m4a?vkey=6867EF99F3684&guid=ffffffffc104ea2964a111cf3ff3edaf&fromtag=46
? ? ? ? ? ? ? ? ? ? http://ws.stream.qqmusic.qq.com/C100003i9hMt1bgui0.m4a?vkey=6867EF99F3684&guid=ffffffffc104ea2964a111cf3ff3edaf&fromtag=46
0
? ? ? ? ? ? ? ? ? ? http://imgcache.qq.com/music/photo/album/63/180_albumpic_143163_0.jpg
0
29
搖一搖搜歌
itchat增加了三個群聊相關(guān)的鍵值:
isAt: 判斷是否 @ 本號
ActualNickName: 實際NickName
Content: 實際Content
MsgType:1
FromUserName:@@xxx
ToUserName:@xxx
Content:
@xxx:<br/>xxx
MsgType: 49
? ? AppMsgType: 2001
? ? FromUserName: 發(fā)送方ID
? ? ToUserName: 接收方ID
? ? Content: 未知
MsgType:10000
? ? FromUserName: 發(fā)送方ID
? ? ToUserName: 自己ID
? ? Content:
"你已添加了 xxx 迄靠,現(xiàn)在可以開始聊天了秒咨。"
"如果陌生人主動添加你為朋友,請謹慎核實對方身份掌挚。"
"收到紅包雨席,請在手機上查看"
tchat為三種賬號都提供了 整體獲取方法與搜索方法.
get_friends
itchat.get_friends()返回完整的好友列表
每個好友為一個字典, 其中第一項為本人的賬號信息;
傳入?update=True, 將更新好友列表并返回,get_friends(update=True)
search_friends
itchat.get_friends()?? 好友搜索,有以下四種方式
僅獲取自己的用戶信息
# 獲取自己的用戶信息吠式,返回自己的屬性字典
itchat.search_friends()
獲取特定UserName的用戶信息
# 獲取特定UserName的用戶信息
itchat.search_friends(userName='@abcdefg1234567')
## 獲取發(fā)送信息的好友的詳細信息
@itchat.msg_register(itchat.content.TEXT,isFriendChat=True)
defreply(msg):
printmsg['FromUserName']
printitchat.search_friends(userName=msg['FromUserName'])#詳細信息
printitchat.search_friends(userName=msg['FromUserName'])['NickName']#獲取昵稱
獲取備注,微信號, 昵稱中的任何一項等于name鍵值的用戶. (可以與下一項配置使用.)
比如在我的微信中有一個備注為autolife的人陡厘,我可以使用這個方法搜索出詳細的信息
# 獲取任何一項等于name鍵值的用戶
itchat.search_friends(name='autolife')
獲取備注,微信號, 昵稱分別等于相應鍵值的用戶. (可以與上一項配置使用.)
# 獲取分別對應相應鍵值的用戶
itchat.search_friends(wechatAccount='littlecodersh')
# 三抽米、四項功能可以一同使用
itchat.search_friends(name='LittleCoder機器人', wechatAccount='littlecodersh')
update_friend
主要用于好友更新
特定用戶: 傳入用戶UserName, 返回指定用戶的最新信息.
用戶列表: 傳入UserName組成的列表, 返回用戶最新信息組成的列表
memberList = itchat.update_friend('@abcdefg1234567')
get_mps
將返回完整的工作號列表
每個公眾號為一個字典,
傳入update=True將更新公眾號列表, 并返回.
search_mps
獲取特定UserName的公眾號
# 獲取特定UserName的公眾號,返回值為一個字典
itchat.search_mps(userName='@abcdefg1234567')
獲取名字中還有特定字符的公眾號.
# 獲取名字中含有特定字符的公眾號糙置,返回值為一個字典的列表
itchat.search_mps(name='LittleCoder')
當兩項都是勇士, 將僅返回特定UserName的公眾號.
get_chatrooms: 返回完整的群聊列表.
search_chatrooms: 群聊搜索.
update_chatroom: 獲取群聊用戶列表或更新該群聊.
群聊在首次獲取中不會獲取群聊的用戶列表, 所以需要調(diào)用該命令才能獲取群聊成員.
傳入群聊的UserName, 返回特定群聊的詳細信息.
傳入UserName組成的列表, 返回指定用戶的最新信息組成的列表.
memberList = itchat.update_chatroom('@@abcdefg1234567', detailedMember=True)
創(chuàng)建群聊,增加/刪除群聊用戶:
由于之前通過群聊檢測是否被好友拉黑的程序, 目前這三個方法都被嚴格限制了使用頻率.
刪除群聊需要本賬號為管理員, 否則無效.
將用戶加入群聊有直接加入與發(fā)送邀請, 通過useInvitation設(shè)置.
超過 40 人的群聊無法使用直接加入的加入方式.
memberList = itchat.get_frients()[1:]
# 創(chuàng)建群聊, topic 鍵值為群聊名稱.
chatroomUserName = itchat.create_chatroom(memberList,"test chatroom")
# 刪除群聊內(nèi)的用戶
itchat.delete_member_from_chatroom(chatroomUserName, memberList[0])
# 增加用戶進入群聊.
itchat.add_member_into_chatroom(chatroomUserName, memberList[0], useInvitation=False)
itchat.add_friend
itchat.new_instance
itchat.add_member_into_chatroom
itchat.originInstance
itchat.auto_login
itchat.returnvalues
itchat.check_login
itchat.run
itchat.components
itchat.search_chatrooms
itchat.config
itchat.search_friends
itchat.configured_reply
itchat.search_mps
itchat.content
itchat.send
itchat.core
itchat.send_file
itchat.Core
itchat.send_image
itchat.create_chatroom
itchat.send_msg
itchat.delete_member_from_chatroom
itchat.send_raw_msg
itchat.dump_login_status
itchat.send_video
itchat.get_chatrooms
itchat.set_alias
itchat.get_contact
itchat.set_chatroom_name
itchat.get_friends
itchat.set_logging
itchat.get_head_img
itchat.set_pinned
itchat.get_mps
itchat.show_mobile_login
itchat.get_msg
itchat.start_receiving
itchat.get_QR
itchat.storage
itchat.get_QRuuid
itchat.update_chatroom
itchat.instanceList
itchat.update_friend
itchat.load_login_status
itchat.upload_file
itchat.log
itchat.utils
itchat.login
itchat.VERSION
itchat.logout
itchat.web_init
itchat.msg_register