当前位置:网站首页>微信公告号 图灵机器人实现智能回复
微信公告号 图灵机器人实现智能回复
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 = "您发送的是文本消息!";
}
边栏推荐
猜你喜欢

14.IP协议-bite

Basic composition of radar

Review of digital image processing

Pytorch two-dimensional multi-channel convolution operation method

14.ip protocol -bite

12.UDP协议-bite

12.udp protocol -bite

MCS: discrete random variables - geometric distribution

three.js和高德地图结合引入obj格式模型-效果演示

GWD:基于高斯Wasserstein距离的旋转目标检测 | ICML 2021
随机推荐
在shop工程中,实现一个菜单(增删改查)
从雷达回波中可获取的信息
Several imaging modes of polarimetric SAR
Material dynamic self illumination
卷积神经网络中各层的作用
GWD: rotating target detection based on Gaussian Wasserstein distance | ICML 2021
数字图像处理复习
GWD:基于高斯Wasserstein距离的旋转目标检测 | ICML 2021
复数卷积神经网络:CV-CNN
京东健康回应拟以 3.554 亿美元收购京东资产:与宠物健康产品品类相关
message from server: “Host ‘xxxxxx‘ is blocked because of many connection errors; unblock with ‘m
Flink SQL task taskmanager memory settings
13.TCP-bite
MySQL定时整库备份&滚动删除指定日期前的备份数据
MCS: discrete random variable - Hyper geometric distribution
14.ip protocol -bite
Taro 小程序开启wxml代码压缩
Unity C basic review 28 - delegation with return (p449)
Complex convolutional neural network: cv-cnn
Implementing redis distributed locks using custom annotations