您的位置:首页 > 编程语言 > Python开发

python实现微信机器人自动回复

2017-11-12 21:14 926 查看
import requests
import itchat #这是一个用于微信回复的库

KEY = '8edce3ce905a4c1dbb965e6b35c3834d' #这个key可以直接拿来用

# 向api发送请求
def get_response(msg):
apiUrl = 'http://www.tuling123.com/openapi/api'
data = {
'key'  : KEY,
'info'  : msg,
'userid' : 'pth-robot',
}
try:
r = requests.post(apiUrl, data=data).json()
return r.get('text')
except:
return

# 注册方法
\@itchat.msg_register(itchat.content.TEXT)
def tuling_reply(msg):
# 为了保证在图灵Key出现问题的时候仍旧可以回复,这里设置一个默认回复
defaultReply = 'I received: ' + msg['Text']
# 如果图灵Key出现问题,那么reply将会是None
reply = get_response(msg['Text'])
# a or b的意思是,如果a有内容,那么返回a,否则返回b
return reply or defaultReply

# 为了让修改程序不用多次扫码,使用热启动
itchat.auto_login(hotReload=True)
itchat.run()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python api 机器人 微信