Python--微信智能聊天机器人

微信智能聊天机器人

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()
运行效果
运行结果
大家用手机微信扫码就可以登录微信智能机器人,此程序会把你的微信进行托管,当你的好友给你发信息时,图灵机器人会根据后台数据库所储备的信息进行一个自动的智能回复。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值