微信智能聊天机器人
import requests
import itchat
import time
from threading import Timer
图灵机器人编号
如果大家感兴趣的可以登图灵官方网站申请一个自己的图灵小机器人
KEY = ‘8f7fcbf19bab471e8067e2783e95b924’
def get_response(msg):
apiUrl = 'http://www.tuling123.com/openapi/api'
data = {
'key' : KEY,
'info' : msg,
'userid' : 'wechat-robot',
}
try:
r = requests.post(apiUrl, data=data).json()
return r.get('text')
except:
return
def isMsgFromMyself(msgFromUserName):
global myName
return myName == msgFromUserName
@itchat.msg_register(itchat.content.TEXT)
def tuling_reply(msg):
global autoReplyFlag, timerSet, noReply, t
print(msg[‘Text’])
if isMsgFromMyself(msg[‘FromUserName’]):
print(“Replied!!”)
autoReplyFlag = False
noReply = False
try:
t.cancel()
print(“Timer Canceled”)
timerSet = False
except:
pass
return None
if autoReplyFlag:
defaultReply = 'I received: ' + msg['Text']
reply = get_response(msg['Text'])
return reply or defaultReply
else:
noReply = True
if not timerSet:
print("Timer setting")
t = Timer(3, sendBusyStatus, [msg['FromUserName']])
t.start()
timerSet = True
def sendBusyStatus(UserName):
global noReply, autoReplyFlag, timerSet
print(“Timer Working!”)
if noReply:
itchat.send(“我的主人不知道在干嘛!让我先陪你聊一会”, UserName)
autoReplyFlag = True
timerSet = False
itchat.auto_login()
autoReplyFlag, timerSet, noReply = False, False, False
t = 0
myName = itchat.get_friends(update=True)[0][‘UserName’]
itchat.run()
运行效果
大家用手机微信扫码就可以登录微信智能机器人,此程序会把你的微信进行托管,当你的好友给你发信息时,图灵机器人会根据后台数据库所储备的信息进行一个自动的智能回复。