当前位置:网站首页>微信公告号 图灵机器人实现智能回复
微信公告号 图灵机器人实现智能回复
2022-06-29 15:29:00 【种豆走天下】
微信公告号 图灵机器人实现智能回复
使用五个图灵机器人实现循环,若第一个机器人回复次数使用完,则可以使用下一个机器人,五次机会使用完,则返回提示。
其他详细代码接上一节
package com.qfjy.project.weixin.api.tuling.dev;
import com.qfjy.project.weixin.api.tuling.bean.InputText;
import com.qfjy.project.weixin.api.tuling.bean.Perception;
import com.qfjy.project.weixin.api.tuling.bean.TulingBean;
import com.qfjy.project.weixin.api.tuling.bean.UserInfo;
import com.qfjy.project.weixin.util.WeixinUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/** * @Classname TeacherUtil * @Author guoweixin * @Description TODO 图灵机器人 代码功能提升 * @Date 2021/6/25 9:34 * @Created by Administrator */
public class TeacherUtil {
/**图灵API访问接口*/
private static String TULING_URL_POST="http://openapi.tuling123.com/openapi/api/v2";
/**用户唯一标识*/
private static final String TULING_USER_ID="java2101";
/** * ####TODO 生成Tuling 接口入参参数对象 * @param msg 发送文本消息 * @param apiKey 机器人的key */
private JSONObject getJsonObject(String msg,String apiKey){
TulingBean tulingBean=new TulingBean();
tulingBean.setReqType(0);
Perception perception=new Perception();
InputText inputText=new InputText();
inputText.setText(msg);
perception.setInputText(inputText);
tulingBean.setPerception(perception);
UserInfo userInfo=new UserInfo();
userInfo.setUserId(TULING_USER_ID);
userInfo.setApiKey(apiKey);
tulingBean.setUserInfo(userInfo);
JSONObject json1=JSONObject.fromObject(tulingBean);
return json1;
}
/** * ####TODO 向图灵服务器发送请求,得到响应结果 * @param json1 URL请求的入参参数 */
private String getResult(JSONObject json1){
// 1向服务器接口地址 发送POST请求
JSONObject jsonObject= WeixinUtil.httpRequest(TULING_URL_POST,"POST",json1.toString());
//2得到图灵服务器机器人返回的结果
JSONArray json2= (JSONArray) jsonObject.get("results");
JSONObject json3= (JSONObject) json2.get(0);
JSONObject json4= (JSONObject) json3.get("values");
String result= json4.getString("text");
return result;
}
/** * ####TODO 图灵智能聊天机器人核心方法 * @param msg * @param apiKey * @return */
public String sendMessage(String msg,String apiKey){
//1 图灵得到入参对象
JSONObject jsonObject= this.getJsonObject(msg,apiKey);
//2向服务器发送请求并得到结果
String result=this.getResult(jsonObject);
return result;
}
/** * @param msg 用户发送文本消息 * @return 图灵机器人智能回复内容 */
public String send(String msg){
if(TULING_APIKEYS_LOCK_FLAG==false){
return "今天太累了,明天我们再聊吧";
}
String result= this.sendMessage(msg,APIKEYS[APIKEYS_INDEX]);
if("请求次数超限制!".equals(result)){
APIKEYS_INDEX++;
if(APIKEYS_INDEX>=APIKEYS.length){
//恢复0
APIKEYS_INDEX=0;
//机器人全部不可用
TULING_APIKEYS_LOCK_FLAG=false;
return "今天太累了,明天我们再聊吧";
}
return this.send(msg);
}
return result;
}
/**机器人收集的所有key*/
private static String[] APIKEYS={
"fb5a78bb2e79482d8075acd90b13231d",
"acc513be8b5e4b26929247e83132f116",
"911ea1eef67843449750dc7f19fb3d8d"};
/**机器人 下标索引*/
private static int APIKEYS_INDEX=0;
/**机器人 全局锁 可用 true 不可用 false*/
public static boolean TULING_APIKEYS_LOCK_FLAG=true;
/**定时任务方法 凌晨点: TULING_APIKEYS_LOCK_FLAG=true; --》 true机器人全部可用 false全部不可用 数据库公共字段: status 0无效 1有效 判断机器人是否全部可用: 0不可用 1可用 生活中关是否开关: 0/1表示 车是否在开还是停: 0/1表示 * */
/** 1、图灵每个认证帐户 发了5个免费机器人。(机器人KEY) 2、一个机器人每天只能用100次智能回复功能。(过了12点,自动恢复了功能) 如果超过了100次的回复限制,机器人会默认返回: 请求次数超限制! 如何让你的机器人每天满足最大的功能回复? 1、收集多个机器人。 5个机器人。 2、如果所有的机器人都不能给个默认回复功能:“今天太累了,明天我们再聊吧“ 3、过了凌晨24点,所有的机器人又可以工作了。 */
}
在CoreService.java中:
1.导入对象
@Autowired
private TeacherUtil teacherUtil;
2.调用对象
// 文本消息
if (msgType.equals(MessageUtil.REQ_MESSAGE_TYPE_TEXT)) {
//respContent = tuLingUtil.getResult(content);
respContent = teacherUtil.getResult();
//respContent = hitokotoUtil.getResult();
//respContent = "您发送的是文本消息!";
}
边栏推荐
- 雷达天线简介
- LeetCode-1188. Designing finite blocking queues
- 14.ip protocol -bite
- 卷积神经网络中各层的作用
- PostgreSQL source code learning (25) -- transaction log ⑥ - wait for log writing to complete
- MCS: discrete random variable Poisson distribution
- Andorid Jetpack Hilt
- ImgUtil 图片处理工具类,文字提取,图片水印
- 明德扬XILINX-K7-325T/410T核心板数据手册
- Taro2.* 小程序配置分享微信朋友圈
猜你喜欢
postgresql源码学习(24)—— 事务日志⑤-日志写入WAL Buffer
three.js和高德地图结合引入obj格式模型-效果演示
89.(cesium篇)cesium聚合图(自定义图片)
Take another picture of cloud redis' improvement path
Flink SQL任务TaskManager内存设置
Northwestern Polytechnic University attacked by overseas e-mail
89.(cesium篇)cesium聚合图(自定义图片)
Cmake learning-2
Abnormal logic reasoning problem of Huawei software test written test [2] Huawei hot interview problem
Chapter IX app project test (4) test tools
随机推荐
Building SQL statements in Excel
极化SAR几种成像模式
MySQL开发规范.pdf
PostgreSQL source code learning (25) -- transaction log ⑥ - wait for log writing to complete
Development and application of NFT chain Games: Six noteworthy NFT trends in 2022
swift JSONSerialization
动作捕捉系统用于苹果采摘机器人
three.js和高德地图结合引入obj格式模型-效果演示
About sql+nosql: newsql database
Google software version experience cycle
近期工作总结
12.UDP协议-bite
Informatics Olympiad all in one 1002: output the second integer
Mysql database naming conventions PDF
中序和后序遍历构建二叉树[递归划分区间与回溯拼接子树+中后序和中前序的相似与不同]
File常用工具類, 流相關運用 (記錄)
Training mode of deep learning network
关于遥感图像解译的思考
Alibaba cloud experience Award: use polardb-x and Flink to build a large real-time data screen
Scroll, do you understand?