From e042e1c6cd8c7a844fb273474aa9a84d8385283c Mon Sep 17 00:00:00 2001 From: Hai Liang Wang Date: Tue, 11 Sep 2018 17:22:50 +0800 Subject: [PATCH] =?UTF-8?q?#74=20=E6=94=AF=E6=8C=81=E5=90=AF=E7=94=A8/?= =?UTF-8?q?=E7=A6=81=E7=94=A8=E5=AE=A2=E6=9C=8D=E6=9C=BA=E5=99=A8=E4=BA=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/rest/ApiChatbotController.java | 55 ++++++++++++++++++- 1 file changed, 52 insertions(+), 3 deletions(-) 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 9bf0947c..2aa6899f 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 @@ -106,6 +106,12 @@ public class ApiChatbotController extends Handler { case "update": json = update(j); break; + case "enable": + json = enable(j, true); + break; + case "disable": + json = enable(j, false); + break; default: json.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_2); json.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的操作。"); @@ -114,6 +120,49 @@ public class ApiChatbotController extends Handler { return new ResponseEntity(json.toString(), headers, HttpStatus.OK); } + /** + * Enable Chatbot + * + * @param j + * @return + */ + private JsonObject enable(JsonObject j, boolean isEnabled) { + JsonObject resp = new JsonObject(); + if ((!j.has("id")) || StringUtils.isBlank(j.get("id").getAsString())) { + 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; + } + + try { + if (c.getApi().exists(c.getChatbotID())) { + c.setEnabled(isEnabled); + chatbotRes.save(c); + 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_7); + resp.addProperty(RestUtils.RESP_KEY_ERROR, "智能问答引擎不存在该聊天机器人,未能正确设置。"); + } + } catch (ChatbotAPIRuntimeException e) { + resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_5); + resp.addProperty(RestUtils.RESP_KEY_DATA, "设置不成功,智能问答引擎服务异常。"); + } catch (MalformedURLException e) { + resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_6); + resp.addProperty(RestUtils.RESP_KEY_DATA, "设置不成功,智能问答引擎地址不合法。"); + } + return resp; + } + /** * 更新聊天机器人 * @@ -173,13 +222,13 @@ public class ApiChatbotController extends Handler { } } - if(StringUtils.isNotBlank(description)) + if (StringUtils.isNotBlank(description)) c.setDescription(description); - if(StringUtils.isNotBlank(fallback)) + if (StringUtils.isNotBlank(fallback)) c.setFallback(fallback); - if(StringUtils.isNotBlank(welcome)) + if (StringUtils.isNotBlank(welcome)) c.setWelcome(welcome); c.setUpdatetime(new Date());