mirror of
https://github.com/chatopera/cosin.git
synced 2025-08-01 16:38:02 +08:00
#74 支持聊天机器人更新接口
This commit is contained in:
parent
c1c73d01d3
commit
dfb1e0b12c
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.chatopera.chatbot</groupId>
|
||||
<artifactId>sdk</artifactId>
|
||||
<version>1.0.2</version>
|
||||
<version>1.0.3</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>sdk</name>
|
||||
|
@ -168,8 +168,48 @@ public class ChatbotAPI {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新聊天机器人
|
||||
*
|
||||
* @param chatbotID
|
||||
* @param description
|
||||
* @param fallback
|
||||
* @param welcome
|
||||
* @return
|
||||
* @throws ChatbotAPIRuntimeException
|
||||
*/
|
||||
public boolean updateByChatbotID(final String chatbotID,
|
||||
final String description,
|
||||
final String fallback,
|
||||
final String welcome) throws ChatbotAPIRuntimeException {
|
||||
if (StringUtils.isBlank(chatbotID))
|
||||
throw new ChatbotAPIRuntimeException("不合法的参数,【chatbotID】不能为空。");
|
||||
|
||||
HashMap<String, Object> body = new HashMap<String, Object>();
|
||||
if (StringUtils.isNotBlank(description))
|
||||
body.put("description", description);
|
||||
if (StringUtils.isNotBlank(fallback))
|
||||
body.put("fallback", fallback);
|
||||
if (StringUtils.isNotBlank(welcome))
|
||||
body.put("welcome", welcome);
|
||||
|
||||
try {
|
||||
JSONObject result = RestAPI.put(this.baseUrl + "/chatbot/" + chatbotID, body, null);
|
||||
if (result.getInt("rc") == 0) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (UnirestException e) {
|
||||
throw new ChatbotAPIRuntimeException(e.toString());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 删除聊天机器人
|
||||
*
|
||||
* @param chatbotID
|
||||
* @return
|
||||
* @throws ChatbotAPIRuntimeException
|
||||
@ -179,7 +219,7 @@ public class ChatbotAPI {
|
||||
throw new ChatbotAPIRuntimeException("聊天机器人ID不能为空。");
|
||||
try {
|
||||
JSONObject result = RestAPI.delete(this.getBaseUrl() + "/chatbot/" + chatbotID, null);
|
||||
if(result.getInt("rc") == 0)
|
||||
if (result.getInt("rc") == 0)
|
||||
return true;
|
||||
return false;
|
||||
} catch (UnirestException e) {
|
||||
@ -245,6 +285,29 @@ public class ChatbotAPI {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 意图识别
|
||||
* @param chatbotID
|
||||
* @param clientId
|
||||
* @param textMessage
|
||||
* @return
|
||||
* @throws UnirestException
|
||||
*/
|
||||
public JSONObject intent(final String chatbotID, final String clientId, final String textMessage) throws ChatbotAPIRuntimeException {
|
||||
if(StringUtils.isBlank(chatbotID) || StringUtils.isBlank(clientId) || StringUtils.isBlank(textMessage))
|
||||
throw new ChatbotAPIRuntimeException("参数不合法,不能为空。");
|
||||
|
||||
HashMap<String, Object> body = new HashMap<String, Object>();
|
||||
body.put("clientId", clientId);
|
||||
body.put("query", textMessage);
|
||||
try {
|
||||
JSONObject result = RestAPI.post(this.baseUrl + "/chatbot/" + chatbotID, body);
|
||||
return result;
|
||||
} catch (UnirestException e) {
|
||||
throw new ChatbotAPIRuntimeException(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检索知识库
|
||||
*
|
||||
|
@ -114,7 +114,11 @@ public class RestAPI {
|
||||
|
||||
public static JSONObject delete(final String url, HashMap<String, String> headers) throws UnirestException {
|
||||
x(headers);
|
||||
return Unirest.delete(url).headers(headers).asJson().getBody().getObject();
|
||||
return Unirest.delete(url).headers(headers).asJson().getBody().getObject();
|
||||
}
|
||||
|
||||
public static JSONObject put(final String url, HashMap<String, Object> body, HashMap<String, String> headers) throws UnirestException {
|
||||
x(headers);
|
||||
return Unirest.put(url).headers(headers).fields(body).asJson().getBody().getObject();
|
||||
}
|
||||
}
|
||||
|
@ -312,7 +312,7 @@
|
||||
<dependency>
|
||||
<groupId>com.chatopera.chatbot</groupId>
|
||||
<artifactId>sdk</artifactId>
|
||||
<version>1.0.2</version>
|
||||
<version>1.0.3</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
|
@ -103,6 +103,9 @@ public class ApiChatbotController extends Handler {
|
||||
case "fetch":
|
||||
json = fetch(j, curruser.getId(), curruser.isSuperuser(), curruser.getMyorgans(), curruser.getOrgi(), super.getP(request), super.getPs(request));
|
||||
break;
|
||||
case "update":
|
||||
json = update(j);
|
||||
break;
|
||||
default:
|
||||
json.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_2);
|
||||
json.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的操作。");
|
||||
@ -111,6 +114,79 @@ public class ApiChatbotController extends Handler {
|
||||
return new ResponseEntity<String>(json.toString(), headers, HttpStatus.OK);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新聊天机器人
|
||||
*
|
||||
* @param j
|
||||
* @return
|
||||
*/
|
||||
private JsonObject update(JsonObject j) {
|
||||
JsonObject resp = new JsonObject();
|
||||
if (!j.has("id")) {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_3);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "非法参数,id不能为空。");
|
||||
return resp;
|
||||
}
|
||||
final String id = j.get("id").getAsString();
|
||||
|
||||
Chatbot c = chatbotRes.findOne(id);
|
||||
|
||||
if (c == null) {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_4);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "该聊天机器人不存在。");
|
||||
return resp;
|
||||
}
|
||||
|
||||
if (j.has("workmode") && ChatbotUtils.VALID_WORKMODELS.contains(j.get("workmode").getAsString())) {
|
||||
c.setWorkmode(j.get("workmode").getAsString());
|
||||
}
|
||||
|
||||
if (j.has("enabled")) {
|
||||
c.setEnabled(j.get("enabled").getAsBoolean());
|
||||
}
|
||||
|
||||
String description = j.has("description") ? j.get("description").getAsString() : null;
|
||||
String fallback = j.has("fallback") ? j.get("fallback").getAsString() : null;
|
||||
String welcome = j.has("welcome") ? j.get("welcome").getAsString() : null;
|
||||
|
||||
if (StringUtils.isNotBlank(description) ||
|
||||
StringUtils.isNotBlank(fallback) ||
|
||||
StringUtils.isNotBlank(welcome)) {
|
||||
try {
|
||||
if (c.getApi().updateByChatbotID(c.getChatbotID(), description, fallback, welcome)) {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_SUCC);
|
||||
resp.addProperty(RestUtils.RESP_KEY_DATA, "更新成功。");
|
||||
} else {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_5);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "更新失败。");
|
||||
return resp;
|
||||
}
|
||||
} catch (ChatbotAPIRuntimeException e) {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_5);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "更新智能问答引擎失败。" + e.toString());
|
||||
return resp;
|
||||
} catch (MalformedURLException e) {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_6);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "更新智能问答引擎失败。" + e.toString());
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
|
||||
if(StringUtils.isNotBlank(description))
|
||||
c.setDescription(description);
|
||||
|
||||
if(StringUtils.isNotBlank(fallback))
|
||||
c.setFallback(fallback);
|
||||
|
||||
if(StringUtils.isNotBlank(welcome))
|
||||
c.setWelcome(welcome);
|
||||
|
||||
c.setUpdatetime(new Date());
|
||||
chatbotRes.save(c);
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取聊天机器人列表
|
||||
*
|
||||
@ -132,7 +208,7 @@ public class ApiChatbotController extends Handler {
|
||||
return resp;
|
||||
}
|
||||
|
||||
Page<Chatbot> records = chatbotRes.findByOrgans( myorgans != null? new ArrayList<String>(myorgans) : null, new PageRequest(p, ps, Sort.Direction.DESC, new String[]{"createtime"}));
|
||||
Page<Chatbot> records = chatbotRes.findByOrgans(myorgans != null ? new ArrayList<String>(myorgans) : null, new PageRequest(p, ps, Sort.Direction.DESC, new String[]{"createtime"}));
|
||||
|
||||
JsonArray ja = new JsonArray();
|
||||
for (Chatbot c : records) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user