今天写个好玩的~图灵机器人
这个参考了http://www.jianshu.com/p/5d4de51f5375
这篇文章
用的库是之前爬取微信好友头像的那个库itchat,这个库可以很方便的获取微信里的一些信息
首先要到图灵机器人网站去注册开通一个机器人,图灵机器人也是可以直接接入微信公众号的
图灵机器人接入微信其实就是我们发送一个请求到图灵机器人,然后返回给我们一个信息给我,调用接口地址是它官网的API
http://doc.tuling123.com/openapi2/263611
就是发送一个post请求而已,这个是2.0版本的接口,1.0是get请求的
参考API可以知道请求数据格式:
{
"reqType":1,
"perception": {
"inputText": {
"text": "附近的酒店"
},
"inputImage": {
"url": "imageUrl"
},
"selfInfo": {
"location": {
"city": "北京",
"latitude": "39.45492",
"longitude": "119.239293",
"nearest_poi_name": "上地环岛南",
"province": "北京",
"street": "信息路"
}
}
},
"userInfo": {
"apiKey": "",
"userId": ""
}
}
如果只要发送文字,只要perception中的inputText和 userInfo就欧科了
apikey是在图灵机器人官网获取,userId自己定义一个即可
userId = '123456'
inputText = {'text': text}
key = 'your apiKey'
userInfo = {'apiKey': key, 'userId': userId}
perception = {'inputText': inputText}
data = {'perception': perception, 'userInfo': userInfo}
然后用requests发送一个post请求
url = 'http://openapi.tuling123.com/openapi/api/v2'
response = requests.post(url=url, data=json.dumps(data))
response.encoding = 'utf-8'
result = response.json()
answer = result['results'][0]['values']['text']
这个answer就是机器人返回给我们的
然后我们用itchat发送到微信好友,就可以实现和机器人聊天了
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
myself = itchat.get_friends(update=True)[0]['NickName']
content = msg['Content']
friend = msg['User']['NickName']
answer = get_answer(msg['Text'])
itchat.send(answer, msg['FromUserName'])
如果发送群里,也差不多,这需要换成小组的ID
@itchat.msg_register(itchat.content.TEXT, isGroupChat=True)
def group_text_reply(msg):
group_name = msg['User']['NickName'] # 获取群聊名称
group = ['群聊测试', 'itchat'] # 设置聊天的群
group_info = itchat.search_chatrooms(name=name)
item = group_info[0]['UserName']
if group_name in group:
itchat.send(get_answer(msg['Text']), item)
如下是两个机器人间的对话 哈哈哈
最后惯例,贴下完整代码
#!usr/bin/env python3
# -*- coding:utf-8-*-
import itchat
import json
import requests
def get_data(text):
userId = '123456'
inputText = {'text': text}
key = 'your apiKey'
userInfo = {'apiKey': key, 'userId': userId}
perception = {'inputText': inputText}
data = {'perception': perception, 'userInfo': userInfo}
return data
def get_answer(text):
data = get_data(text)
url = 'http://openapi.tuling123.com/openapi/api/v2'
response = requests.post(url=url, data=json.dumps(data))
response.encoding = 'utf-8'
result = response.json()
answer = result['results'][0]['values']['text']
return answer
# 回复好友
@itchat.msg_register(itchat.content.TEXT)
def text_reply(msg):
myself = itchat.get_friends(update=True)[0]['NickName']
content = msg['Content']
friend = msg['User']['NickName']
# 给特定的人的回复,并且自己发的 不回复
if friend != myself and friend!= 'FRIEND':
print('%s: %s' % (friend, content))
answer = get_answer(msg['Text'])
itchat.send(answer, msg['FromUserName'])
print('我:%s' % answer)
else:
itchat.send('你是猪', msg['FromUserName'])
# 获得群聊ID
def group_id(name):
df = itchat.search_chatrooms(name=name)
return df[0]['UserName']
# 发送群聊
@itchat.msg_register(itchat.content.TEXT, isGroupChat=True)
def group_text_reply(msg):
group_name = msg['User']['NickName']
group = ['群聊测试', 'itchat']
igroup_info = itchat.search_chatrooms(name=name)
item = group_info[0]['UserName']
if group_name in group:
itchat.send(get_answer(msg['Text']), item)
itchat.auto_login(hotReload=True)
itchat.run()