這里我主要用了3個機器人,可以切換。
1.圖靈機器人 (傻的不行,一直在問別人問題,沒有限制)
http://www.tuling123.com
2.showApi上的圖靈機器人 (感覺最聰明,可以識別微信表情,英文,中文等,但是因為我是普通會員,每天1000次調用)
https://www.showapi.com/api/lookPoint/60
3.茉莉機器人 (功能豐富,但是只識別中文,不識別英文,微信表情)
http://www.itpk.cn/robot.php
代碼如下:
from itchat.content import * import itchat import json import requests from ShowapiRequest import ShowapiRequest import os import time import datetime import urllib import urllib.request # 調用圖靈機器人的api,根據聊天消息返回回復內容 def tuLing(info): appkey="************" #自己注冊 url="http://www.tuling123.com/openapi/api?key=%s&;info=%s"%(appkey,info) req=requests.get(url) content=req.text data=json.loads(content) answer=data['text'] return answer # 調用showApi的圖靈機器人,根據聊天消息返回回復內容 def showRobot(info="",nickName="",imgDir=""): my_appId='****' #自己注冊 my_appSecret='******************' #自己注冊 r = ShowapiRequest("http://route.showapi.com/60-27", my_appId, my_appSecret) r.addBodyPara("userid", nickName) if info !="": r.addBodyPara("info", info) else: r.addBodyPara("info", "你好") if imgDir !="" : r.addFilePara("img", imgDir) res = r.post() data=json.loads(res.text) return data['showapi_res_body']['text'] # 調用茉莉機器人是,根據聊天消息返回回復內容 # 指令: # 地區天氣 上海天氣 查詢上海的天氣預報(市級或縣級地區) # 天氣 天氣 根據訪問IP自動獲取天氣預報 # @tq您的ip地址 @tq8.8.8.8 查詢IP地址為8.8.8.8的天氣預報 # @ip您的ip地址 @ip8.8.8.8 查詢IP地址為8.8.8.8的位置和經緯度 # ip ip 查詢訪問者的IP地址詳情 # @qq要查詢的QQ號碼 @qq123456 查詢QQ號碼123456的信息 # @lol英雄聯盟大區--角色名稱 @lol艾歐尼亞--我要超神 查詢艾歐尼亞區我要超神的戰績信息 # @sfz身份證號碼 @sfz111111111111111111 查詢身份證號碼公民所在地、生日和性別 # @sjh手機號碼 @sjh18888888888 查詢手機號碼歸屬地、城市區號和卡的類型 # @cy四字成語 @cy一馬當先 【一馬當先】的成語接龍 # 笑話 笑話 隨機返回一個笑話 # 觀音靈簽 觀音靈簽 隨機抽取觀音靈簽 # 月老靈簽 月老靈簽 隨機抽取月老靈簽 # 財神爺靈簽 財神爺靈簽 隨機抽取財神爺靈簽 def moLi_Robot(question): Key='***************' #自己注冊 Secret='*******' #自己注冊 url='http://i.itpk.cn/api.php?question=%s&api_key=%s&api_secret=%s'%(question,Key,Secret) req=requests.get(url) content=req.text if question in ['笑話','觀音靈簽','月老靈簽','財神爺靈簽']: if content.startswith(u'\ufeff'): content = content.encode('utf8')[3:].decode('utf8') jsondata=json.loads(content) # 處理'笑話','觀音靈簽','月老靈簽','財神爺靈簽'數據 if question=='笑話': data='%s\n%s'%(jsondata['title'],jsondata['content']) elif question=='觀音靈簽': data='簽號:%s\n抽中:%s\n簽語:%s\n解簽:%s\n白話解簽:%s\n'%(jsondata['number1'],jsondata['haohua'],jsondata['qianyu'],jsondata['shiyi'],jsondata['jieqian'],) elif question=='月老靈簽': data='簽號:%s\n抽中:%s\n詩意:%s\n解簽:%s\n注釋:%s\n白話解簽:%s\n'%(jsondata['number1'],jsondata['haohua'],jsondata['shiyi'],jsondata['jieqian'],jsondata['zhushi'],jsondata['baihua']) else: # 財神爺靈簽 data='簽號:%s\n簽語:%s\n注釋:%s\n解簽:%s\n解說:%s\n含義:%s\n婚姻:%s\n事業:%s\n功名:%s\n失物:%s\n出外移居:%s\n六甲:%s\n求財:%s\n交易:%s\n疾病:%s\n訴訟:%s\n運途:%s\n謀事:%s\n生意:%s\n'%( jsondata['number1'],jsondata['qianyu'],jsondata['zhushi'],jsondata['jieqian'],jsondata['jieshuo'],jsondata['jieguo'],jsondata['hunyin'],jsondata['shiye'],jsondata['gongming'],jsondata['shiwu'],jsondata['cwyj'],jsondata['liujia'],jsondata['qiucai'],jsondata['jiaoyi'],jsondata['jibin'],jsondata['susong'],jsondata['yuntu'],jsondata['moushi'],jsondata['hhzsy'], ) elif question=='指令': data="""指令: 地區天氣 \t上海天氣\t 查詢上海的天氣預報(市級或縣級地區)\n 天氣 \t天氣\t 根據訪問IP自動獲取天氣預報\n @tq您的ip地址\t@tq8.8.8.8\t查詢IP地址為8.8.8.8的天氣預報\n @ip您的ip地址\t@ip8.8.8.8\t查詢IP地址為8.8.8.8的位置和經緯度\n ip\tip\t查詢訪問者的IP地址詳情\n @qq要查詢的QQ號碼\t@qq123456\t查詢QQ號碼123456的信息\n @lol英雄聯盟大區--角色名稱\t @lol艾歐尼亞--我要超神\t查詢艾歐尼亞區我要超神的戰績信息\n @sfz身份證號碼\t@sfz111111111111111111\t查詢身份證號碼公民所在地、生日和性別\n @sjh手機號碼 \t@sjh18888888888\t查詢手機號碼歸屬地、城市區號和卡的類型\n @cy四字成語\t@cy一馬當先\t【一馬當先】的成語接龍\n 笑話 \t笑話\t 隨機返回一個笑話\n 觀音靈簽 \t觀音靈簽\t 隨機抽取觀音靈簽\n 月老靈簽 \t月老靈簽\t 隨機抽取月老靈簽\n 財神爺靈簽\t財神爺靈簽\t隨機抽取財神爺靈簽\n """ else: data=content # return jsondata return data # 根據NickName[]查找UserName[] def findUserName(toNickName): u = [] for i in toNickName: try: u1 = itchat.search_friends(i)[0]['UserName'] u.append(u1) print('已將聯系人\t',i,'\t加入了聊天機器人的對象') except Exception as E: print(i,"你沒有此聯系人") print('------------------------------------') return u # 根據群的NickName[]查找UserName[] def findQunUserName(toQunNickName): u = [] for i in toQunNickName: try: u1 = itchat.search_chatrooms(i)[0]['UserName'] u.append(u1) print('已將群\t',i,'\t加入了聊天機器人的對象') except Exception as E: print(i,"你沒有此群") print('------------------------------------') return u if __name__ == '__main__': # 登錄發送 itchat.auto_login(hotReload=True,statusStorageDir='itchat.pkl') myUserName=itchat.get_friends(update=True)[0]['UserName'] myNickName=itchat.get_friends(update=True)[0]['NickName'] # 對指定的人當機器人 toSendUserNickName = ['sssss', 'ttttt', 'mmmmm','sssssssssssssssss'] toRobotUserName = findUserName(toSendUserNickName) # 對指定的群當機器人 toSendQunNickName = ['test','test2'] toRobotQunUserName = findQunUserName(toSendQunNickName) # 使用的機器人 RobotName=['tuLing','showRobot','moLi_Robot'] usingRobot=RobotName[2] #群機器人 @itchat.msg_register(TEXT,isGroupChat=True) def reply(msg): answer="" for i in toRobotQunUserName: if msg['FromUserName'] == i and msg['ActualUserName'] != myUserName : if usingRobot=='tuLing': answer = tuLing(msg['Text']) elif usingRobot=='showRobot': answer = showRobot(msg['Text']) else: answer = moLi_Robot(msg['Text']) # print(answer) itchat.send_msg(answer, msg['FromUserName']) return @itchat.msg_register([TEXT,PICTURE],isFriendChat=True) def replyText(msg): fromNum=0 #toSendUserNickName[fromNum]給我發送信息 toNum=0 #判斷我要給toSendUserNickName[toNum]發送信息 for i in toRobotUserName: # 判斷發送消息的人是你選定的人 if msg['FromUserName']==i : # 傳輸的文件類型為照片 if msg['Type']=='Picture': # 判斷使用 那個機器人 if usingRobot=='showRobot': # 下載照片 path =r'.\Info\Picture' try: os.makedirs(path) print("%s 文件夾創建成功" % path) except Exception as e: print("%s 文件夾創建失敗,可能已經存在此文件夾" % path) path += r'\%s' % msg['FileName'] msg['Text'](path) # 打印信息 an = showRobot(imgDir=msg['Text']()) print(datetime.datetime.now(), '\n', toSendUserNickName[fromNum], '給 我 :\n\t', msg['FileName'], '\t存儲在', path) elif usingRobot=='tuLing': an = tuLing('照片') else: an = moLi_Robot('照片') # 傳輸的是文字 else: # 判斷使用 那個機器人 if usingRobot=='tuLing': an = tuLing(msg['Text']) elif usingRobot=='showRobot': an = showRobot(msg['Text']) else: an= moLi_Robot(msg['Text']) print(datetime.datetime.now(),'\n',toSendUserNickName[fromNum],'給 我 :\n\t',msg['Text'],'\n') print(datetime.datetime.now(),'\n','我 給', toSendUserNickName[fromNum], ':\n\t', an,'\n') itchat.send_msg(an,i) break else: fromNum += 1 # 我自己給上面指定的人發送信息,不是機器人發 for j in toRobotUserName: if msg['ToUserName']==j: break else: toNum += 1 if msg['FromUserName']==myUserName: # 傳輸的文件類型為照片 if msg['Type'] == 'Picture': print(datetime.datetime.now(),'\n','我 給',toSendUserNickName[toNum],':\n\t',msg['FileName'],'\n') else: print(datetime.datetime.now(), '\n', '我 給', toSendUserNickName[toNum], ':\n\t', msg['Text'], '\n') return itchat.run()
需要注冊的機器人:
修改機器人:
修改對那幾個朋友使用機器人(這里是你微信對他們的備注):
修改對那幾個群使用機器人(這里是你微信上的群的名字):
如果是自己對指定的朋友聊天,聊天消息會體現在調試台上,如果是群回復則不會。
版權 作者:feiquan 出處:http://www.cnblogs.com/feiquan/ 版權聲明:本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。 大家寫文都不容易,請尊重勞動成果~ 這里謝謝大家啦(*/ω\*)