packagecom.ssx.robot.baidu;importnet.sf.json.JSONArray;importnet.sf.json.JSONObject;importjava.util.HashMap;importjava.util.Map;/*** 重要提示代码中所需工具类
* FileUtil,Base64Util,HttpUtil,GsonUtils请从
*https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72*https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2*https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3*https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3* 下载*/
public classUnitService {/*sessionid和msg用户的话*/
public static Maputterance(String session_id, String content) {
Map map = new HashMap();try{
String talkUrl= "https://aip.baidubce.com/rpc/2.0/unit/service/chat";//请求参数 scene_id记得修改//{"log_id":"UNITTEST_10000","version":"2.0","service_id":"S27823","skill_id":"","session_id":" ","request":{"query":"","user_id":"UNIT_DEV_11"}}
String params = "{"log_id":"UNITTEST_10000","version":"2.0","service_id":"S27823","session_id":"" +session_id+ " ","request":{"query":"" + content +
"","user_id":"UNIT_DEV_11"}}";
String accessToken=AuthService.getAuth();//获得返回的json数据
String str = HttpUtil.post(talkUrl, accessToken, "application/json", params);
System.out.println(str);
System.out.println("=========str已获得========");//获得result
JSONObject jsonobject =JSONObject.fromObject(str);
String result= jsonobject.getString("result");
System.out.println(result);
System.out.println("======result已获得======");//获得会话session_id
JSONObject jsonobject2 =JSONObject.fromObject(result);
session_id= jsonobject2.getString("session_id");
System.out.println("======sessionid:====" +session_id);
map.put("session_id", session_id);//获得动作列表
JSONArray response_list = jsonobject2.getJSONArray("response_list");//JSONArray response_list2 = JSONObject.fromObject(response_list);
System.out.println("===response_list===:" +response_list);if (response_list.size() > 0) {for (int i = 0; i < response_list.size(); i++) {
JSONObject test=response_list.getJSONObject(i);
JSONArray action_list= test.getJSONArray("action_list");
JSONObject action_list1= JSONObject.fromObject(action_list.get(0));
System.out.println("====循环action_list====" +action_list1);
String say= "";if (!action_list1.get("type").toString().equals("failure")) {
say= (String) action_list1.get("say");//机器bot返回的话术
map.put("say", say);//循环插入覆盖,但每次调用都不同所以依然是随机的
}
System.out.println("======say:====" +map.get("say"));
}
}
}catch(Exception e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnmap;
}public static voidmain(String[] args) {
utterance("", "你好");
}
}