python微信+requests+圖靈機器人制作聊天機器人
阿新 • • 發佈:2018-11-20
在使用過python微信之後,對於其功能和圖靈機器人做了一下整合,這樣在使用者訊息不包含關鍵字的時候,預設使用者處於聊天模式,可以進行一些輕鬆的對話,比如講講笑話,看看新聞,查查天氣一類的操作,在加上接入第三方翻譯介面,實現更加豐富的功能。這裡分享一下接入圖靈聊天機器人的程式碼:
#!/usr/bin/python # coding=utf-8 from wxpy import * import os import time import requests import json bot = Bot(cache_path=True) fs = bot.friends() gs = bot.groups() @bot.register(Friend, TEXT) def auto_reply_friend(msg): print msg m = msg.text friend = msg.sender if "@" not in m: j = dict( reqType=0, perception=dict( inputText=dict( text=m ) ), userInfo=dict( apiKey="***********", userId=fs.index(friend) ) ) r = requests.post("http://openapi.tuling123.com/openapi/api/v2",json=j) info = json.loads(r.text)["results"] for i in range(len(info)): c = info[i]["values"] d = c.keys() m = c[d[0]] friend.send(m) return try: r = requests.post("http://10.10.32.155:8081/uname/"+m) b = json.loads(r.text)["data"][u"使用者token:"] friend.send(b) except BaseException: friend.send(u"你輸入的賬號不存在!") embed()
歡迎有興趣的一起交流:群號:340964272