import itchat, requests, json
def get_response(msg):#应答程序
url = 'http://www.tuling123.com/openapi/api/v2'#图灵机器人接口地址
send_msg = {#发送给图灵机器人的消息格式(json),参考api文档
"reqType": 0,#文本格式
"perception": {
"inputText": {
"text": msg #发送的文本内容
},
},
"userInfo": {
"apiKey": " ",#机器人apiKey
"userId": "363282"#标示用户
}
}
try:
r = requests.post(url, data = json.dumps(send_msg)).json()#把收到的微信消息发送给图灵机器人
return r['results'][0]['values']['text']#返回图灵机器人说的消息,我的微信号只是个搬运工
except:
return
@itchat.msg_register('Text')#定义消息类型为文本
def text_reply(msg):
return get_response(msg['Text'])
itchat.auto_login(True)#扫码登入微信,和登入网页版微信一样
itchat.run()#自动应答