安裝itchat
pip install itchat關于itchat API的講解,這里比較清楚詳細,我這寫作水平也講不清楚,大家看這兒吧主要講講我的代碼,和實現過程、效果需要的庫import reimport timeimport itchatfrom itchat.content import *下面的代碼,僅僅實現了記錄文本信息@itchat.msg_register([TEXT, PICTURE, MAP, CARD, NOTE, SHARING, RECORDING, ATTACHMENT, VIDEO])def text_reply(msg):@itchat.msg_register([TEXT, PICTURE, MAP, CARD, NOTE, SHARING, RECORDING, ATTACHMENT, VIDEO])關于消息類型,詳細的可以參考上面給出的鏈接
在這里,除了好友邀請和無用消息都進行了注冊登錄itchat.auto_login() 登錄的是你個人微信的web端,然后會彈出一個二維碼,用微信掃一掃即可登錄。itchat.run()自動運行,檢測消息然后對上面代碼進行擴充。造血造肉。
@itchat.msg_register([TEXT, PICTURE, MAP, CARD, NOTE, SHARING, RECORDING, ATTACHMENT, VIDEO])def text_reply(msg): if msg['Type'] == 'Text': reply_content = msg['Text'] elif msg['Type'] == 'Picture': reply_content = r"圖片: " + msg['FileName'] elif msg['Type'] == 'Card': reply_content = r" " + msg['RecommendInfo']['NickName'] + r" 的名片" elif msg['Type'] == 'Map': x, y, location = re.search("<location x=/"(.*?)/" y=/"(.*?)/".*label=/"(.*?)/".*", msg['OriContent']).group(1, 2, 3) if location is None: reply_content = r"位置: 緯度->" + x.__str__() + " 經度->" + y.__str__() else: reply_content = r"位置: " + location elif msg['Type'] == 'Note': reply_content = r"通知" elif msg['Type'] == 'Sharing': reply_content = r"分享" elif msg['Type'] == 'Recording': reply_content = r"語音" elif msg['Type'] == 'Attachment': reply_content = r"文件: " + msg['FileName'] elif msg['Type'] == 'Video': reply_content = r"視頻: " + msg['FileName'] else: reply_content = r"消息" friend = itchat.search_friends(userName=msg['FromUserName']) itchat.send(r"Friend:%s -- %s " r"Time:%s " r" Message:%s" % (friend['NickName'], friend['RemarkName'], time.ctime(), reply_content), toUserName='filehelper') itchat.send(r"我已經收到你在【%s】發送的消息【%s】稍后回復。--微信助手(Python版)" % (time.ctime(), reply_content), toUserName=msg['FromUserName'])itchat.auto_login()itchat.run()開頭的各種if else是為了判斷消息類型進行各種不同的回復然后是消息的發送函數send(message, toUserName)發送的消息和發送對象防撤回功能是原理是將消息備份發送到文件助手toUserName='filehelper',由于是接入微信API,讀取消息會比手機端快一些。
自動回復功能是將發送對象改為給你發送消息的好友,大家可以再自定義其他消息回復規則實現個性化回復,針對不同好友的回復。
其他功能還在思考中...過幾天試試通過手機端發送消息到文件助手,電腦端處理消息,解析命令而執行命令。敬請指導、關注注:如果不知道msg還有哪些參數,可以通過print(msg)來查看詳細信息,真的特別詳細。喜歡點個贊再走唄(/≧▽≦)/~┴┴
新聞熱點
疑難解答