From dfb1e0b12c28be432eb2c2d3a1961dd360ba0ab6 Mon Sep 17 00:00:00 2001 From: Hai Liang Wang Date: Tue, 11 Sep 2018 10:06:57 +0800 Subject: [PATCH] =?UTF-8?q?#74=20=E6=94=AF=E6=8C=81=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E6=9C=BA=E5=99=A8=E4=BA=BA=E6=9B=B4=E6=96=B0=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cc-chatbot/app/pom.xml | 2 +- .../com/chatopera/chatbot/ChatbotAPI.java | 65 +++++++++++++++- .../java/com/chatopera/chatbot/RestAPI.java | 6 +- contact-center/app/pom.xml | 2 +- .../api/rest/ApiChatbotController.java | 78 ++++++++++++++++++- 5 files changed, 148 insertions(+), 5 deletions(-) diff --git a/cc-chatbot/app/pom.xml b/cc-chatbot/app/pom.xml index 91e19eea..64cc275d 100644 --- a/cc-chatbot/app/pom.xml +++ b/cc-chatbot/app/pom.xml @@ -6,7 +6,7 @@ com.chatopera.chatbot sdk - 1.0.2 + 1.0.3 jar sdk diff --git a/cc-chatbot/app/src/main/java/com/chatopera/chatbot/ChatbotAPI.java b/cc-chatbot/app/src/main/java/com/chatopera/chatbot/ChatbotAPI.java index 3dd3054c..2c1d5494 100644 --- a/cc-chatbot/app/src/main/java/com/chatopera/chatbot/ChatbotAPI.java +++ b/cc-chatbot/app/src/main/java/com/chatopera/chatbot/ChatbotAPI.java @@ -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 body = new HashMap(); + 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 body = new HashMap(); + 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()); + } + } + /** * 检索知识库 * diff --git a/cc-chatbot/app/src/main/java/com/chatopera/chatbot/RestAPI.java b/cc-chatbot/app/src/main/java/com/chatopera/chatbot/RestAPI.java index 3df96ade..d85a693c 100644 --- a/cc-chatbot/app/src/main/java/com/chatopera/chatbot/RestAPI.java +++ b/cc-chatbot/app/src/main/java/com/chatopera/chatbot/RestAPI.java @@ -114,7 +114,11 @@ public class RestAPI { public static JSONObject delete(final String url, HashMap 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 body, HashMap headers) throws UnirestException { + x(headers); + return Unirest.put(url).headers(headers).fields(body).asJson().getBody().getObject(); + } } diff --git a/contact-center/app/pom.xml b/contact-center/app/pom.xml index de222920..8c5efd09 100644 --- a/contact-center/app/pom.xml +++ b/contact-center/app/pom.xml @@ -312,7 +312,7 @@ com.chatopera.chatbot sdk - 1.0.2 + 1.0.3 diff --git a/contact-center/app/src/main/java/com/chatopera/cc/webim/web/handler/api/rest/ApiChatbotController.java b/contact-center/app/src/main/java/com/chatopera/cc/webim/web/handler/api/rest/ApiChatbotController.java index d828757e..12c8d2c3 100644 --- a/contact-center/app/src/main/java/com/chatopera/cc/webim/web/handler/api/rest/ApiChatbotController.java +++ b/contact-center/app/src/main/java/com/chatopera/cc/webim/web/handler/api/rest/ApiChatbotController.java @@ -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(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 records = chatbotRes.findByOrgans( myorgans != null? new ArrayList(myorgans) : null, new PageRequest(p, ps, Sort.Direction.DESC, new String[]{"createtime"})); + Page records = chatbotRes.findByOrgans(myorgans != null ? new ArrayList(myorgans) : null, new PageRequest(p, ps, Sort.Direction.DESC, new String[]{"createtime"})); JsonArray ja = new JsonArray(); for (Chatbot c : records) {