python图灵机器人教程_Python-微信图灵机器人

今天写个好玩的~图灵机器人

这个参考了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)

如下是两个机器人间的对话 哈哈哈

image.png

最后惯例,贴下完整代码

#!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()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值