com.hn.robot.tuling.TuLingChatRobot Maven / Gradle / Ivy
package com.hn.robot.tuling;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.hn.robot.exception.ChatRobotException;
import java.util.List;
/**
* 图灵机器人
*
*
* 图灵机器人官网
* 文档地址
* 注意: 如果返回4001(加密方式错误), 请关闭你获取apikey下方的密钥 就可正常运行
*
*/
public class TuLingChatRobot {
private static final Log log = LogFactory.get();
private static final String URL = "http://openapi.tuling123.com/openapi/api/v2";
public static List talk(TuLingMsg tuLingMsg) {
String result = HttpUtil.post(URL, JSONUtil.toJsonStr(tuLingMsg));
JSONObject jsonObject = JSONUtil.parseObj(result);
Integer code = jsonObject.getJSONObject("intent")
.getInt("code");
String errMsg = TuLingErrorMsgEnum.findMsgByCode(code);
log.info("图灵机器人返回code: {}",code);
if(StrUtil.isNotBlank(errMsg)){
log.error("图灵机器人异常:{}",errMsg);
throw new ChatRobotException("图灵机器人异常:"+errMsg);
}
JSONArray results = jsonObject.getJSONArray("results");
return JSONUtil.toList(results, TuLingResult.class);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy