1
0
mirror of https://github.com/chatopera/cosin.git synced 2025-08-01 16:38:02 +08:00

#74 支持删除机器人客服

This commit is contained in:
Hai Liang Wang 2018-09-11 01:00:14 +08:00
parent 2a7e6eb828
commit cfaf42ccc1
6 changed files with 131 additions and 33 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.chatopera.chatbot</groupId>
<artifactId>sdk</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
<packaging>jar</packaging>
<name>sdk</name>

View File

@ -138,12 +138,13 @@ public class ChatbotAPI {
/**
* 创建聊天机器人
* @param chatbotID 聊天机器人标识[a-zA-Z0-9-]组成字母开头
* @param name 拟人化的名字
*
* @param chatbotID 聊天机器人标识[a-zA-Z0-9-]组成字母开头
* @param name 拟人化的名字
* @param primaryLanguage 首选语言支持 [zh_CN|en_US]
* @param fallback 兜底回复
* @param description 描述
* @param welcome 欢迎语
* @param fallback 兜底回复
* @param description 描述
* @param welcome 欢迎语
* @return
*/
public JSONObject createBot(final String chatbotID,
@ -167,6 +168,24 @@ public class ChatbotAPI {
}
}
/**
* 删除聊天机器人
* @param chatbotID
* @return
* @throws ChatbotAPIRuntimeException
*/
public boolean deleteByChatbotID(final String chatbotID) throws ChatbotAPIRuntimeException {
if (StringUtils.isBlank(chatbotID))
throw new ChatbotAPIRuntimeException("聊天机器人ID不能为空。");
try {
JSONObject result = RestAPI.delete(this.getBaseUrl() + "/chatbot/" + chatbotID, null);
if(result.getInt("rc") == 0)
return true;
return false;
} catch (UnirestException e) {
throw new ChatbotAPIRuntimeException(e.toString());
}
}
/**
* 获取聊天机器人详情

View File

@ -111,4 +111,10 @@ public class RestAPI {
public static JSONObject get(final String url, HashMap<String, Object> queryString) throws UnirestException {
return get(url, queryString, null);
}
public static JSONObject delete(final String url, HashMap<String, String> headers) throws UnirestException {
x(headers);
return Unirest.delete(url).headers(headers).asJson().getBody().getObject();
}
}

View File

@ -312,7 +312,7 @@
<dependency>
<groupId>com.chatopera.chatbot</groupId>
<artifactId>sdk</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</dependency>
</dependencies>
<build>

View File

@ -0,0 +1,47 @@
/*
* Copyright (C) 2018 Chatopera Inc, <https://www.chatopera.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.chatopera.cc.webim.util.chatbot;
import org.apache.commons.lang.StringUtils;
import java.util.Arrays;
import java.util.HashSet;
public class ChatbotUtils {
public static final HashSet<String> VALID_LANGS = new HashSet<String>(Arrays.asList(new String[]{"zh_CN", "en_US"}));
public static final HashSet<String> VALID_WORKMODELS = new HashSet<String>(Arrays.asList(new String[]{"客服机器人优先", "人工客服优先"}));
public static final String SNS_TYPE_WEBIM = "webim";
/**
* 使用snsid得到ChatbotID
*
* @param snsid
* @return
*/
public static String resolveChatbotIDWithSnsid(String snsid, String clientId) {
return clientId + "_" + snsid;
}
/**
* 使用chatbotID得到snsid
*
* @param chatbotID
* @return
*/
public static String resolveSnsidWithChatbotID(String chatbotID, String clientId) {
return StringUtils.remove(chatbotID, clientId + "_");
}
}

View File

@ -20,6 +20,7 @@ import com.chatopera.cc.util.UKTools;
import com.chatopera.cc.util.exception.CallOutRecordException;
import com.chatopera.cc.webim.service.repository.ChatbotRepository;
import com.chatopera.cc.webim.service.repository.SNSAccountRepository;
import com.chatopera.cc.webim.util.chatbot.ChatbotUtils;
import com.chatopera.cc.webim.web.handler.Handler;
import com.chatopera.cc.webim.web.handler.api.request.RestUtils;
import com.chatopera.cc.webim.web.model.Chatbot;
@ -46,18 +47,13 @@ import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.net.MalformedURLException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
@RestController
@RequestMapping("/api/chatbot")
@Api(value = "聊天机器人", description = "请求聊天机器人服务")
public class ApiChatbotController extends Handler {
private final static Logger logger = LoggerFactory.getLogger(ApiChatbotController.class);
private final HashSet<String> VALID_LANGS = new HashSet<String>(Arrays.asList(new String[]{"zh_CN", "en_US"}));
private final HashSet<String> VALID_WORKMODELS = new HashSet<String>(Arrays.asList(new String[]{"客服机器人优先", "人工客服优先"}));
private final String SNS_TYPE_WEBIM = "webim";
@Value("${license.client.id}")
private String clientId;
@ -86,6 +82,9 @@ public class ApiChatbotController extends Handler {
case "create":
json = create(j, curruser.getId(), curruser.getOrgan(), curruser.getOrgi());
break;
case "delete":
json = delete(j, curruser.getId(), curruser.getOrgan(), curruser.getOrgi());
break;
default:
json.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_2);
json.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的操作。");
@ -95,24 +94,49 @@ public class ApiChatbotController extends Handler {
}
/**
* 使用snsid得到ChatbotID
*
* @param snsid
* 删除聊天机器人
* @param j
* @param uid
* @param organ
* @param orgi
* @return
*/
private String resolveChatbotIDWithSnsid(String snsid) {
return clientId + "_" + snsid;
}
private JsonObject delete(JsonObject j, String uid, String organ, String orgi) {
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_3);
resp.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的参数,不存在该聊天机器人。");
return resp;
}
/**
* 使用chatbotID得到snsid
*
* @param chatbotID
* @return
*/
private String resolveSnsidWithChatbotID(String chatbotID) {
return StringUtils.remove(chatbotID, clientId + "_");
try {
if (c.getApi().deleteByChatbotID(c.getChatbotID())) {
chatbotRes.delete(c);
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_SUCC);
resp.addProperty(RestUtils.RESP_KEY_DATA, "删除成功。");
return resp;
} else {
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_6);
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_4);
resp.addProperty(RestUtils.RESP_KEY_ERROR, "该聊天机器人地址错误。");
return resp;
}
}
/**
@ -153,7 +177,7 @@ public class ApiChatbotController extends Handler {
name = j.get("name").getAsString();
}
if (!(j.has("primaryLanguage") && VALID_LANGS.contains(j.get("primaryLanguage").getAsString()))) {
if (!(j.has("primaryLanguage") && ChatbotUtils.VALID_LANGS.contains(j.get("primaryLanguage").getAsString()))) {
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_3);
resp.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的参数未传入有效【primaryLanguage】。");
return resp;
@ -161,7 +185,7 @@ public class ApiChatbotController extends Handler {
primaryLanguage = j.get("primaryLanguage").getAsString();
}
if (!(j.has("workmode") && VALID_WORKMODELS.contains(j.get("workmode").getAsString()))) {
if (!(j.has("workmode") && ChatbotUtils.VALID_WORKMODELS.contains(j.get("workmode").getAsString()))) {
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_3);
resp.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的参数未传入有效【workmode】。");
return resp;
@ -176,7 +200,7 @@ public class ApiChatbotController extends Handler {
} else {
snsid = j.get("snsid").getAsString();
// #TODO 仅支持webim
if (!snsAccountRes.existsBySnsidAndSnstypeAndOrgi(snsid, SNS_TYPE_WEBIM, orgi)) {
if (!snsAccountRes.existsBySnsidAndSnstypeAndOrgi(snsid, ChatbotUtils.SNS_TYPE_WEBIM, orgi)) {
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_3);
resp.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的参数未传入有效【snsid】。");
return resp;
@ -189,7 +213,7 @@ public class ApiChatbotController extends Handler {
}
}
chatbotID = resolveChatbotIDWithSnsid(snsid);
chatbotID = ChatbotUtils.resolveChatbotIDWithSnsid(snsid, clientId);
if (chatbotRes.existsByChatbotIDAndOrgi(chatbotID, orgi)) {
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_3);
resp.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的参数,数据库中存在该聊天机器人。");
@ -229,15 +253,15 @@ public class ApiChatbotController extends Handler {
c.setDescription(description);
c.setFallback(fallback);
c.setPrimaryLanguage(primaryLanguage);
c.setWelcome(welcome);
c.setName(name);
c.setWelcome(result.getJSONObject("data").getString("welcome"));
// 默认不开启
c.setEnabled(false);
c.setCreater(creater);
c.setOrgan(organ);
c.setOrgi(orgi);
c.setChannel(SNS_TYPE_WEBIM);
c.setChannel(ChatbotUtils.SNS_TYPE_WEBIM);
c.setSnsAccountIdentifier(snsid);
Date dt = new Date();
c.setCreatetime(dt);
@ -246,8 +270,10 @@ public class ApiChatbotController extends Handler {
chatbotRes.save(c);
JsonObject data = new JsonObject();
data.addProperty("id", c.getId());
resp.add(RestUtils.RESP_KEY_DATA, data);
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_SUCC);
resp.addProperty(RestUtils.RESP_KEY_DATA, "创建成功。");
return resp;
} else {
// 创建失败