如今不单只QQ拥有机器人自动回复功能,现在就连大家日夜离不开的微信也有了这种功能,那我们这么才能做到像QQ机器人那样的呢?
啊哈!这就涉及到如今最火的编程语言python啦!
我们可以运用python中的requests库和itchat库就可以实现其功能啦!
首先我们要申请一个图灵机器人APIKEY
图灵机器人是一个中文语境下的对话机器人,可以使用免费版,一个账号最多可以创建5个机器人,一个机器人日调用接口数5000次。
建议用PyCharm运行。
1.安装wxpy
2.安装pip install itchat
3.安装pip installrequests
4.用手机微信扫一扫二维码登录即可
具体代码解释如下:
# coding=utf8
importrequests
importitchat
KEY ='这里写你的KEY'# key
defget_response(msg):
apiUrl ='http://www.tuling123.com/openapi/api'
data = {
'key': KEY,
'info': msg,#这是我们发出去的信息
'userid':'Aidas',#这里你想改上面都行
}
try:
r = requests.post(apiUrl,data=data).json()#我们通过如下命令发送一个post请求
returnr.get('text')
except:
return
@itchat.msg_register(itchat.content.TEXT)#itchat装饰器
deftuling_reply(msg):##定义消息回复函数
defaultReply ='I received: '+ msg['Text']
reply = get_response(msg['Text'])
returnreplyordefaultReply
itchat.auto_login(hotReload=True)#微信扫码登录
itchat.run()#运行itchat
运行结果如下:
领取专属 10元无门槛券
私享最新 技术干货