兩個代碼,一個是把python當微信操作,可以查看自己的好友信息,羣信息等。還可以定時發送文本,文件等。效果就和你本人操作一樣,沒差別。
還有一個是連接你的微信和圖靈機器人,別人和你說話可以自動回覆(非常愚蠢)。
itchat.search_friends(name=r’ ‘) 是找到某好友
itchat.get_chatrooms(update=True)是獲得所有羣。同類。
#coding=utf8
import itchat
itchat.auto_login(hotReload=True) #熱啓動你的微信
#itchat.run()
rooms=itchat.get_chatrooms(update=True)
for i in range(len(rooms)):
print(rooms[i]) #查看你多有的羣
room = itchat.search_friends(name=r' ') #這裏輸入你好友的名字或備註。
print(room)
userName = room[0]['UserName']
f="C:\文件/lh.jpg" #圖片地址
try:
itchat.send_image(f,toUserName=userName) 如果是其他文件可以直接send_file
print("success")
except:
print("fail")
二、首先去圖靈機器人註冊賬號,查看api接口和key:
#coding=utf8
import requests
import itchat
def get_response(msg):
apiUrl = 'http://www.tuling123.com/openapi/api'
data = {
'key' : '', #這裏自行輸入key
'info' : msg,
'userid' : '175007', #這是我的賬號
}
try:
r = requests.post(apiUrl, data=data).json()
return r.get('text')
except:
return "呵呵" #出問題就回復“呵呵”
@itchat.msg_register(itchat.content.TEXT)
def tuling_reply(msg):
defaultReply = 'I received: ' + msg['Text'] #一個默認回覆
reply = get_response(msg['Text'])
return reply or defaultReply
itchat.auto_login(hotReload=True) #熱啓動,不需要多次掃碼登錄
itchat.run()