当前位置:网站首页>微信公告号 图灵机器人实现智能回复

微信公告号 图灵机器人实现智能回复

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 = "您发送的是文本消息!";
			}

原网站

版权声明
本文为[种豆走天下]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_40708522/article/details/118392075