然后我们启动 Web 服务器 , 去公众号后台发消息发图片测试 , 如果成功的话立马就会回复你相同的文字和图片 。
微信提供了一个在线测试的平台 , 可以很方便的进行开发中的各种测试 。
接入图灵机器人首先我们去图灵机器人官网注册一个账号 。然后在后台创建一个机器人 。
然后我们根据图灵机器人接入文档的使用说明:
- 编码方式:调用调用图灵API的各个环节的编码方式均为 UTF-8
- 接口地址:http://openapi.tuling123.com/openapi/api/v2
- 请求方式: HTTP POST
- 请求参数:参数格式为 json
{ "reqType":0, "perception": { "inputText": { "text": "附近的酒店" }, "inputImage": { "url": "imageUrl" }, "selfInfo": { "location": { "city": "北京", "province": "北京", "street": "信息路" } } }, "userInfo": { "apiKey": "", "userId": "" }}输出参数示例:{ "intent": { "code": 10005, "intentName": "", "actionName": "", "parameters": { "nearby_place": "酒店" } }, "results": [ { "groupType": 1, "resultType": "url", "values": { "url": "http://m.elong.com/hotel/0101/nlist/#indate=2016-12-10&outdate=2016-12-11&keywords=%E4%BF%A1%E6%81%AF%E8%B7%AF" } }, { "groupType": 1, "resultType": "text", "values": { "text": "亲 , 已帮你找到相关酒店信息" } } ]}其中 apiKey 是可以在我们创建的机器人的参数中找到 , userId 是用户唯一标识 。好了 , 下面来编写我们的代码 。我们增加一个 tuling.py 文件来接入图灵接口 。
tuling.py
import jsonimport urllibapiKey = '从你创建的机器人获得'tulingUrl = "http://openapi.tuling123.com/openapi/api/v2"# content 是接收的消息 , userId 是用户唯一标识def tulingReply(content, userId): requestData = { "reqType": 0, "perception": { "inputText": { "text": content }, "selfInfo": { "location": { "city": "北京" } } }, "userInfo": { "apiKey": apiKey, "userId": userId } } requestData = json.dumps(requestData).encode('utf8') http_post = urllib.request.Request( tulingUrl, data=https://www.isolves.com/it/cxkf/yy/Python/2020-09-25/requestData, headers={'content-type': 'application/json'}) response = urllib.request.urlopen(http_post) response_str = response.read().decode('utf8') response_dic = json.loads(response_str) results_code = response_dic['intent']['code'] # 免费版每天有固定次数 , 如果超过之后会返回4003错误码 if results_code == 4003: results_text = "4003:%s" % response_dic['results'][0]['values']['text'] else: results_text = response_dic['results'][0]['values']['text'] return results_text
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Python爬虫案例:爬取微信公众号文章
- 小程序云开发支持公众号网页开发了
- 机器学习概念和经典算法,我用大白话给你讲清楚了!入门必看
- 一行代码让你的python运行速度提高100倍
- 新手如何快速推广微信公众号
- 道光为什么不传位给恭亲王 恭亲王奕欣和光绪什么关系
- 西汉东方朔 东方朔给汉武帝写了一篇文章
- 教你学会网易云JS逆向,爬来的歌打包发给女友邮箱可好?
- 详解Hbase底层的数据结构——LSMT
- 在30分钟内创建你的深度学习服务器
