微信模組的引用
阿新 • • 發佈:2018-11-09
API聊天機器人
import requests
import itchat
itchat.auto_login()
##時刻監控好友傳送的文字訊息,並給予一個回覆
@itchat.msg_register(itchat.content.TEXT,isFriendChat=True)
def text_reply(msg):
##獲取好友傳送的訊息內容
content = msg['Content']
##將好友的訊息傳送給機器人處理,處理結果iuo就是返回給好友的資訊
returnContent = get_tuling_reponse(content)
return returnContent
def get_tuling_reponse(_info):
print(_info)
#圖靈機器人的網址
api_url = "http://www.tuling123.com/openapi/api"
data = {
'key':'077cbff1b4534d44bb2896ea37b5c47d',
'info':_info,
'userid':'wechat-robot'
}
#傳送資料到指定網址,獲取網址返回的資料(字元資料型別)
res = requests.post(api_url,data).json()
print(res['text' ])
return res['text']
itchat.run()
結果:
給指定使用者傳送訊息
import itchat
itchat.auto_login()
info = itchat.get_friends('李佳')
info_lijia = info[0]['UserName']
itchat.send('hello',toUsername = info_lijia)
通過手機控制電腦
import itchat
import os
@itchat.msg_register(itchat.content.TEXT,isFriendChat=True)
def text_reply(msg):
print(msg)
if msg['ToUserName'] == 'filehelper':
command = msg['Content']
print(command)
if os.system(command) == 0:
res = os.popen(command).read()
result = '命令執行成功,執行結果:' + res
itchat.send(result,'filehelper')
else:
result = '命令%s執行失敗,請重試' %(command)
itchat.send(result,'filehelper')
if __name__ == "__main__":
itchat.auto_login(hotReload=True)
itchat.run()
結果: