python:itchat操作微信自動回覆
阿新 • • 發佈:2018-11-19
該程式實現對特定好友的自動回覆,指定一位
安裝
pip install itchat
註冊一個圖靈官網賬號:http://www.tuling123.com/,免費的
註冊完成後需要用到你申請機器人的key
原始碼:
程式碼執行時會跳出二維碼介面,通過手機微信掃碼進行登入
import itchat import requests # 登入微信 itchat.auto_login(hotReload=True) # 獲取好友發的訊息 apiUrl="http://www.tuling123.com/openapi/api" # 根據發的訊息回覆 def get_info(message): data={ # 這是註冊機器人時的key 'key':'a40e72ec77e247a99ff79169b8d1e26e', 'info':message, 'userid':'wechat-robot' } try: r = requests.post(apiUrl, data=data).json() info=r['text'] # 印表機器人回覆的資訊 print("robot reply:%s"%info) return info except: return # 回覆給微信好友 @itchat.msg_register(itchat.content.TEXT) def auto_reply(msg): defaultReply='我知道了' # 搜尋微信好友 readFriend=itchat.search_friends(name='微信好友的備註') readFriendsName=readFriend[0]['UserName'] # 列印好友回覆的資訊 print("message:%s"%msg['Text']) reply=get_info(msg['Text']) if msg['FromUserName']==readFriendsName: itchat.send(reply,toUserName=readFriendsName) itchat.run()