mirror of
https://github.com/chatopera/cosin.git
synced 2025-08-01 16:38:02 +08:00
Closed #138 支持接入bot.chatopera.com
This commit is contained in:
parent
c9e00be7ca
commit
f501933c10
@ -310,9 +310,34 @@
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.chatopera.chatbot</groupId>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpclient</artifactId>
|
||||
<version>4.3.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpasyncclient</artifactId>
|
||||
<version>4.0.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents</groupId>
|
||||
<artifactId>httpmime</artifactId>
|
||||
<version>4.3.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.json</groupId>
|
||||
<artifactId>json</artifactId>
|
||||
<version>20140107</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mashape.unirest</groupId>
|
||||
<artifactId>unirest-java</artifactId>
|
||||
<version>1.4.9</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.chatopera.bot</groupId>
|
||||
<artifactId>sdk</artifactId>
|
||||
<version>1.1.0</version>
|
||||
<version>1.0.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
|
@ -15,17 +15,16 @@
|
||||
*/
|
||||
package com.chatopera.cc.app.handler.api.rest;
|
||||
|
||||
import com.chatopera.cc.util.Menu;
|
||||
import com.chatopera.bot.exception.ChatbotException;
|
||||
import com.chatopera.cc.app.basic.MainUtils;
|
||||
import com.chatopera.cc.exception.CallOutRecordException;
|
||||
import com.chatopera.cc.app.model.*;
|
||||
import com.chatopera.cc.app.persistence.repository.*;
|
||||
import com.chatopera.cc.util.OnlineUserUtils;
|
||||
import com.chatopera.cc.app.im.util.ChatbotUtils;
|
||||
import com.chatopera.cc.app.handler.Handler;
|
||||
import com.chatopera.cc.app.handler.api.request.RestUtils;
|
||||
import com.chatopera.chatbot.ChatbotAPI;
|
||||
import com.chatopera.chatbot.ChatbotAPIRuntimeException;
|
||||
import com.chatopera.cc.app.im.util.ChatbotUtils;
|
||||
import com.chatopera.cc.app.model.*;
|
||||
import com.chatopera.cc.app.persistence.repository.*;
|
||||
import com.chatopera.cc.exception.CallOutRecordException;
|
||||
import com.chatopera.cc.util.Menu;
|
||||
import com.chatopera.cc.util.OnlineUserUtils;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
@ -55,6 +54,7 @@ import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/chatbot")
|
||||
@Api(value = "聊天机器人", description = "请求聊天机器人服务")
|
||||
@ -182,7 +182,8 @@ public class ApiChatbotController extends Handler {
|
||||
}
|
||||
|
||||
try {
|
||||
if (c.getApi().exists(c.getChatbotID())) {
|
||||
com.chatopera.bot.sdk.Chatbot bot = new com.chatopera.bot.sdk.Chatbot(c.getClientId(), c.getSecret());
|
||||
if (bot.exists()) {
|
||||
c.setEnabled(isEnabled);
|
||||
chatbotRes.save(c);
|
||||
|
||||
@ -198,12 +199,12 @@ public class ApiChatbotController extends Handler {
|
||||
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, "设置不成功,智能问答引擎地址不合法。");
|
||||
} catch (ChatbotException e) {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_5);
|
||||
resp.addProperty(RestUtils.RESP_KEY_DATA, "设置不成功,智能问答引擎服务异常。");
|
||||
}
|
||||
return resp;
|
||||
}
|
||||
@ -231,6 +232,23 @@ public class ApiChatbotController extends Handler {
|
||||
return resp;
|
||||
}
|
||||
|
||||
// update clientId and secret
|
||||
if (j.has("clientId")) {
|
||||
c.setClientId(j.get("clientId").getAsString());
|
||||
} else {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_3);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的参数,未传入【clientId】。");
|
||||
return resp;
|
||||
}
|
||||
|
||||
if (j.has("secret")) {
|
||||
c.setSecret(j.get("secret").getAsString());
|
||||
} else {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_3);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的参数,未传入【secret】。");
|
||||
return resp;
|
||||
}
|
||||
|
||||
// 更新访客网站配置
|
||||
CousultInvite invite = OnlineUserUtils.cousult(c.getSnsAccountIdentifier(), c.getOrgi(), consultInviteRes);
|
||||
|
||||
@ -239,55 +257,37 @@ public class ApiChatbotController extends Handler {
|
||||
invite.setAifirst(StringUtils.equals(ChatbotUtils.CHATBOT_FIRST, c.getWorkmode()));
|
||||
}
|
||||
|
||||
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;
|
||||
String name = j.has("name") ? j.get("name").getAsString() : null;
|
||||
|
||||
|
||||
if (j.has("enabled")) {
|
||||
boolean enabled = j.get("enabled").getAsBoolean();
|
||||
c.setEnabled(enabled);
|
||||
invite.setAi(enabled);
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(description) ||
|
||||
StringUtils.isNotBlank(fallback) ||
|
||||
StringUtils.isNotBlank(welcome)) {
|
||||
try {
|
||||
if (c.getApi().updateByChatbotID(c.getChatbotID(), name, 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) {
|
||||
try {
|
||||
com.chatopera.bot.sdk.Chatbot bot = new com.chatopera.bot.sdk.Chatbot(c.getClientId(), c.getSecret());
|
||||
if (bot.exists()) {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_SUCC);
|
||||
resp.addProperty(RestUtils.RESP_KEY_DATA, "更新成功。");
|
||||
JSONObject botDetails = bot.details();
|
||||
c.setDescription(botDetails.getJSONObject("data").getString("description"));
|
||||
c.setFallback(botDetails.getJSONObject("data").getString("fallback"));
|
||||
c.setWelcome(botDetails.getJSONObject("data").getString("welcome"));
|
||||
invite.setAisuccesstip(botDetails.getJSONObject("data").getString("welcome"));
|
||||
c.setName(botDetails.getJSONObject("data").getString("name"));
|
||||
invite.setAiname(c.getName());
|
||||
} else {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_6);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "更新智能问答引擎失败。" + e.toString());
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "Chatopera开发者平台提示:该机器人不存在,请先创建机器人, 登录 https://bot.chatopera.com");
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(description))
|
||||
c.setDescription(description);
|
||||
|
||||
if (StringUtils.isNotBlank(fallback))
|
||||
c.setFallback(fallback);
|
||||
|
||||
if (StringUtils.isNotBlank(welcome)) {
|
||||
c.setWelcome(welcome);
|
||||
invite.setAisuccesstip(welcome);
|
||||
}
|
||||
|
||||
if (StringUtils.isNotBlank(name)) {
|
||||
c.setName(name);
|
||||
invite.setAiname(name);
|
||||
} catch (ChatbotException e) {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_5);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "Chatopera开发者平台提示:无法访问该机器人,请确认【1】该服务器可以访问互联网,【2】该聊天机器人已经创建,【3】clientId和Secret正确设置。");
|
||||
return resp;
|
||||
} catch (MalformedURLException e) {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_7);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "更新智能问答引擎失败。" + e.toString());
|
||||
return resp;
|
||||
}
|
||||
|
||||
c.setUpdatetime(new Date());
|
||||
@ -337,7 +337,7 @@ public class ApiChatbotController extends Handler {
|
||||
|
||||
// SNSAccount
|
||||
SNSAccount snsAccount = snsAccountRes.findBySnsidAndOrgi(c.getSnsAccountIdentifier(), orgi);
|
||||
if(snsAccount == null){
|
||||
if (snsAccount == null) {
|
||||
chatbotRes.delete(c); // 删除不存在snsAccount的机器人
|
||||
continue; // 忽略不存在snsAccount的机器人
|
||||
}
|
||||
@ -395,37 +395,21 @@ public class ApiChatbotController extends Handler {
|
||||
return resp;
|
||||
}
|
||||
|
||||
try {
|
||||
if (c.getApi().deleteByChatbotID(c.getChatbotID())) {
|
||||
// 更新访客网站配置
|
||||
CousultInvite invite = OnlineUserUtils.cousult(c.getSnsAccountIdentifier(), c.getOrgi(), consultInviteRes);
|
||||
if (invite != null) {
|
||||
invite.setAi(false);
|
||||
invite.setAiname(null);
|
||||
invite.setAisuccesstip(null);
|
||||
invite.setAifirst(false);
|
||||
invite.setAiid(null);
|
||||
consultInviteRes.save(invite);
|
||||
OnlineUserUtils.cacheCousult(invite);
|
||||
}
|
||||
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;
|
||||
// 更新访客网站配置
|
||||
CousultInvite invite = OnlineUserUtils.cousult(c.getSnsAccountIdentifier(), c.getOrgi(), consultInviteRes);
|
||||
if (invite != null) {
|
||||
invite.setAi(false);
|
||||
invite.setAiname(null);
|
||||
invite.setAisuccesstip(null);
|
||||
invite.setAifirst(false);
|
||||
invite.setAiid(null);
|
||||
consultInviteRes.save(invite);
|
||||
OnlineUserUtils.cacheCousult(invite);
|
||||
}
|
||||
chatbotRes.delete(c);
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_SUCC);
|
||||
resp.addProperty(RestUtils.RESP_KEY_DATA, "删除成功。");
|
||||
return resp;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -439,39 +423,26 @@ public class ApiChatbotController extends Handler {
|
||||
*/
|
||||
private JsonObject create(JsonObject j, String creater, String organ, String orgi) {
|
||||
JsonObject resp = new JsonObject();
|
||||
String baseUrl = null;
|
||||
String chatbotID = null;
|
||||
String name = null;
|
||||
String description = null;
|
||||
String fallback = null;
|
||||
String welcome = null;
|
||||
String primaryLanguage = null;
|
||||
String baseUrl = "https://bot.chatopera.com";
|
||||
String snsid = null;
|
||||
String workmode = null;
|
||||
String clientId = null;
|
||||
String secret = null;
|
||||
|
||||
// 验证数据: 必须字段
|
||||
if ((!j.has("baseUrl")) || StringUtils.isBlank(j.get("baseUrl").getAsString())) {
|
||||
if ((!j.has("clientId")) || StringUtils.isBlank(j.get("clientId").getAsString())) {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_3);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的参数,未传入【baseUrl】。");
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的参数,未传入【clientId】。");
|
||||
return resp;
|
||||
} else {
|
||||
baseUrl = j.get("baseUrl").getAsString();
|
||||
clientId = j.get("clientId").getAsString();
|
||||
}
|
||||
|
||||
if ((!j.has("name")) || StringUtils.isBlank(j.get("name").getAsString())) {
|
||||
if ((!j.has("secret")) || StringUtils.isBlank(j.get("secret").getAsString())) {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_3);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的参数,未传入【name】。");
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的参数,未传入【secret】。");
|
||||
return resp;
|
||||
} else {
|
||||
name = j.get("name").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;
|
||||
} else {
|
||||
primaryLanguage = j.get("primaryLanguage").getAsString();
|
||||
secret = j.get("secret").getAsString();
|
||||
}
|
||||
|
||||
if (!(j.has("workmode") && ChatbotUtils.VALID_WORKMODELS.contains(j.get("workmode").getAsString()))) {
|
||||
@ -502,48 +473,28 @@ public class ApiChatbotController extends Handler {
|
||||
}
|
||||
}
|
||||
|
||||
chatbotID = ChatbotUtils.resolveChatbotIDWithSnsid(snsid, clientId);
|
||||
if (chatbotRes.existsByChatbotIDAndOrgi(chatbotID, orgi)) {
|
||||
if (chatbotRes.existsByClientIdAndOrgi(clientId, orgi)) {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_3);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的参数,数据库中存在该聊天机器人。");
|
||||
return resp;
|
||||
}
|
||||
|
||||
if ((!j.has("fallback")) || StringUtils.isBlank(j.get("fallback").getAsString())) {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_3);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的参数,未传入【fallback】。");
|
||||
return resp;
|
||||
} else {
|
||||
fallback = j.get("fallback").getAsString();
|
||||
}
|
||||
|
||||
// 可选字段
|
||||
if (j.has("description"))
|
||||
description = j.get("description").getAsString();
|
||||
|
||||
if (j.has("welcome"))
|
||||
welcome = j.get("welcome").getAsString();
|
||||
|
||||
try {
|
||||
ChatbotAPI capi = new ChatbotAPI(baseUrl);
|
||||
JSONObject result = capi.createBot(chatbotID,
|
||||
name,
|
||||
primaryLanguage,
|
||||
fallback,
|
||||
description,
|
||||
welcome);
|
||||
com.chatopera.bot.sdk.Chatbot bot = new com.chatopera.bot.sdk.Chatbot(clientId, secret);
|
||||
|
||||
if (result.getInt("rc") == 0) {
|
||||
if (bot.exists()) { // 该机器人存在,clientId 和 Secret配对成功
|
||||
// 创建成功
|
||||
Chatbot c = new Chatbot();
|
||||
JSONObject botDetails = bot.details();
|
||||
c.setId(MainUtils.getUUID());
|
||||
c.setBaseUrl(capi.getBaseUrl());
|
||||
c.setChatbotID(chatbotID);
|
||||
c.setDescription(description);
|
||||
c.setFallback(fallback);
|
||||
c.setPrimaryLanguage(primaryLanguage);
|
||||
c.setName(name);
|
||||
c.setWelcome(result.getJSONObject("data").getString("welcome"));
|
||||
c.setClientId(clientId);
|
||||
c.setSecret(secret);
|
||||
c.setBaseUrl(baseUrl);
|
||||
c.setDescription(botDetails.getJSONObject("data").getString("description"));
|
||||
c.setFallback(botDetails.getJSONObject("data").getString("fallback"));
|
||||
c.setPrimaryLanguage(botDetails.getJSONObject("data").getString("primaryLanguage"));
|
||||
c.setName(botDetails.getJSONObject("data").getString("name"));
|
||||
c.setWelcome(botDetails.getJSONObject("data").getString("welcome"));
|
||||
c.setCreater(creater);
|
||||
c.setOrgan(organ);
|
||||
c.setOrgi(orgi);
|
||||
@ -577,16 +528,16 @@ public class ApiChatbotController extends Handler {
|
||||
} else {
|
||||
// 创建失败
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_6);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "创建失败,失败原因 [" + result.getString("error") + "]");
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "Chatopera开发者平台提示:该机器人不存在,请先创建机器人, 登录 https://bot.chatopera.com");
|
||||
return resp;
|
||||
}
|
||||
} catch (ChatbotAPIRuntimeException e) {
|
||||
} catch (ChatbotException e) {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_5);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "智能问答引擎服务异常,该机器人【chatbotID】已经存在或服务不能访问到,请联系 [info@chatopera.com] 获得支持。");
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "Chatopera开发者平台提示:无法访问该机器人,请确认【1】该服务器可以访问互联网,【2】该聊天机器人已经创建,【3】clientId和Secret正确设置。");
|
||||
return resp;
|
||||
} catch (MalformedURLException e) {
|
||||
resp.addProperty(RestUtils.RESP_KEY_RC, RestUtils.RESP_RC_FAIL_4);
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "不合法的智能问答引擎服务URL。");
|
||||
resp.addProperty(RestUtils.RESP_KEY_ERROR, "Chatopera开发者平台提示:不合法的聊天机器人服务URL。");
|
||||
return resp;
|
||||
}
|
||||
}
|
||||
|
@ -44,16 +44,6 @@ public class ChatbotUtils {
|
||||
|
||||
private static ChatMessageRepository chatMessageRes;
|
||||
|
||||
/**
|
||||
* 使用snsid得到ChatbotID
|
||||
*
|
||||
* @param snsid
|
||||
* @return
|
||||
*/
|
||||
public static String resolveChatbotIDWithSnsid(String snsid, String clientId) {
|
||||
return (clientId + "_" + snsid).toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用chatbotID得到snsid
|
||||
*
|
||||
|
@ -15,12 +15,9 @@
|
||||
*/
|
||||
package com.chatopera.cc.app.model;
|
||||
|
||||
import com.chatopera.chatbot.ChatbotAPI;
|
||||
import com.chatopera.chatbot.ChatbotAPIRuntimeException;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.Date;
|
||||
|
||||
@Entity
|
||||
@ -31,9 +28,6 @@ public class Chatbot {
|
||||
private String id;
|
||||
private String name;
|
||||
|
||||
@Column(unique=true)
|
||||
private String chatbotID;
|
||||
|
||||
private String description;
|
||||
private String primaryLanguage;
|
||||
private String fallback;
|
||||
@ -44,14 +38,15 @@ public class Chatbot {
|
||||
private String creater; // 创建者
|
||||
private String channel; // 渠道类型
|
||||
|
||||
@Column(unique=true)
|
||||
@Column(unique = true)
|
||||
private String snsAccountIdentifier; // 渠道唯一标识
|
||||
private boolean enabled; // 当前是否被启用
|
||||
private String workmode; // 工作模式, 机器人优先还是人工客服优先
|
||||
|
||||
private ChatbotAPI api;
|
||||
private Date createtime;
|
||||
private Date updatetime;
|
||||
private String clientId;
|
||||
private String secret;
|
||||
|
||||
|
||||
@Id
|
||||
@ -74,14 +69,6 @@ public class Chatbot {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getChatbotID() {
|
||||
return chatbotID;
|
||||
}
|
||||
|
||||
public void setChatbotID(String chatbotID) {
|
||||
this.chatbotID = chatbotID;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
@ -178,14 +165,6 @@ public class Chatbot {
|
||||
this.workmode = workmode;
|
||||
}
|
||||
|
||||
@Transient
|
||||
public ChatbotAPI getApi() throws MalformedURLException, ChatbotAPIRuntimeException {
|
||||
if(api == null){
|
||||
api = new ChatbotAPI(this.baseUrl);
|
||||
}
|
||||
return api;
|
||||
}
|
||||
|
||||
public Date getCreatetime() {
|
||||
return createtime;
|
||||
}
|
||||
@ -201,6 +180,22 @@ public class Chatbot {
|
||||
public void setUpdatetime(Date updatetime) {
|
||||
this.updatetime = updatetime;
|
||||
}
|
||||
|
||||
public String getClientId() {
|
||||
return clientId;
|
||||
}
|
||||
|
||||
public void setClientId(String clientId) {
|
||||
this.clientId = clientId;
|
||||
}
|
||||
|
||||
public String getSecret() {
|
||||
return secret;
|
||||
}
|
||||
|
||||
public void setSecret(String secret) {
|
||||
this.secret = secret;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -11,7 +11,7 @@ import java.util.List;
|
||||
|
||||
public abstract interface ChatbotRepository extends JpaRepository<Chatbot, String> {
|
||||
|
||||
public abstract boolean existsByChatbotIDAndOrgi(String chatbotID, String orgi);
|
||||
public abstract boolean existsByClientIdAndOrgi(String clientId, String orgi);
|
||||
|
||||
public abstract boolean existsBySnsAccountIdentifierAndOrgi(String snsid, String orgi);
|
||||
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
package com.chatopera.cc.concurrent.chatbot;
|
||||
|
||||
import com.chatopera.bot.exception.ChatbotException;
|
||||
import com.chatopera.cc.app.basic.MainContext;
|
||||
import com.chatopera.cc.app.cache.CacheHelper;
|
||||
import com.chatopera.cc.app.handler.api.request.RestUtils;
|
||||
@ -26,7 +27,6 @@ import com.chatopera.cc.app.persistence.repository.AgentUserRepository;
|
||||
import com.chatopera.cc.app.persistence.repository.ChatbotRepository;
|
||||
import com.chatopera.cc.concurrent.user.UserDataEvent;
|
||||
import com.chatopera.cc.util.Constants;
|
||||
import com.chatopera.chatbot.ChatbotAPIRuntimeException;
|
||||
import com.lmax.disruptor.EventHandler;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
@ -57,15 +57,15 @@ public class ChatbotEventHandler implements EventHandler<UserDataEvent> {
|
||||
CacheHelper.getAgentUserCacheBean().put(userid, agentUser, orgi);
|
||||
}
|
||||
|
||||
private void chat(final ChatbotEvent payload) throws MalformedURLException, ChatbotAPIRuntimeException {
|
||||
private void chat(final ChatbotEvent payload) throws MalformedURLException, ChatbotException {
|
||||
ChatMessage request = (ChatMessage) payload.getData();
|
||||
Chatbot c = getChatbotRes()
|
||||
.findOne(request.getAiid());
|
||||
|
||||
logger.info("[chatbot disruptor] chat request baseUrl {}, chatbotID {}, fromUserId {}, textMessage {}", c.getBaseUrl(), c.getChatbotID(), request.getUserid(), request.getMessage());
|
||||
logger.info("[chatbot disruptor] chat request baseUrl {}, chatbot {}, fromUserId {}, textMessage {}", c.getBaseUrl(), c.getName(), request.getUserid(), request.getMessage());
|
||||
// Get response from Conversational Engine.
|
||||
JSONObject result = c.getApi()
|
||||
.conversation(c.getChatbotID(), request.getUserid(), request.getMessage(), false);
|
||||
com.chatopera.bot.sdk.Chatbot bot = new com.chatopera.bot.sdk.Chatbot(c.getClientId(), c.getSecret());
|
||||
JSONObject result = bot.conversation(request.getUserid(), request.getMessage());
|
||||
|
||||
// parse response
|
||||
logger.info("[chatbot disruptor] chat response {}", result.toString());
|
||||
|
@ -28,41 +28,17 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">智能问答引擎地址</label>
|
||||
<label class="layui-form-label">ClientId</label>
|
||||
<div class="layui-input-inline">
|
||||
<input type="text" name="baseUrl" required lay-verify="required" placeholder="联系info@chatopera.com,获得智能问答引擎地址"
|
||||
autocomplete="off" class="layui-input" value="${bot.baseUrl}" <#if id!=null>disabled</#if>>
|
||||
</div>
|
||||
<label class="layui-form-label"></label>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">名称</label>
|
||||
<div class="layui-input-inline">
|
||||
<input type="text" name="name" required lay-verify="required" placeholder="请输入名称" autocomplete="off"
|
||||
class="layui-input" value="${bot.name}">
|
||||
<input type="text" name="clientId" required lay-verify="required" placeholder="请输入ClientId" autocomplete="off"
|
||||
class="layui-input" value="${bot.clientId}">
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">语言</label>
|
||||
<label class="layui-form-label">Secret</label>
|
||||
<div class="layui-input-inline">
|
||||
<select name="primaryLanguage" lay-verify="required">
|
||||
<option value="zh_CN" <#if bot.primaryLanguage=="zh_CN">selected="selected"</#if>>中文</option>
|
||||
<option value="en_US" <#if bot.primaryLanguage=="en_US">selected="selected"</#if>>英文</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">问候语</label>
|
||||
<div class="layui-input-inline">
|
||||
<input type="text" name="welcome" required lay-verify="required" placeholder="请输入问候语" autocomplete="off"
|
||||
class="layui-input" value="${bot.welcome}">
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">兜底回复</label>
|
||||
<div class="layui-input-inline">
|
||||
<input type="text" name="fallback" required lay-verify="required" autocomplete="off" value="${bot.fallback}"
|
||||
class="layui-input">
|
||||
<input type="text" name="secret" required lay-verify="required" placeholder="请输入Secret" autocomplete="off"
|
||||
class="layui-input" value="${bot.secret}">
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
@ -74,13 +50,6 @@
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">描述</label>
|
||||
<div class="layui-input-inline">
|
||||
<input type="text" name="description" required lay-verify="required" autocomplete="off" value="${bot.description}"
|
||||
class="layui-input">
|
||||
</div>
|
||||
</div>
|
||||
<div class="layui-form-item">
|
||||
<div class="layui-input-inline">
|
||||
<button class="layui-btn" lay-submit lay-filter="save">保存</button>
|
||||
|
@ -12,11 +12,15 @@
|
||||
<div class="col-lg-12">
|
||||
<h1>机器人列表
|
||||
<div style="float: right;" class="ukefu-bt-text-content">
|
||||
<button class="layui-btn layui-btn-samll green" onclick="showCreate()">新建机器人</button>
|
||||
<button class="layui-btn layui-btn-mini layui-btn-radius" onclick="openChatbotBotPlatform()"><i class="layui-icon"></i>机器人开发者平台</button>
|
||||
</div>
|
||||
</h1>
|
||||
</div>
|
||||
</div>
|
||||
<button class="layui-btn layui-btn-mini green" onclick="showCreate()">
|
||||
<i class="layui-icon"></i>
|
||||
集成机器人
|
||||
</button>
|
||||
<div class="row">
|
||||
<div class="col-lg-12">
|
||||
<table class="layui-table" lay-skin="line">
|
||||
@ -159,13 +163,17 @@
|
||||
|
||||
function showCreate() {
|
||||
layer.open({
|
||||
title: '新建机器人',
|
||||
title: '集成机器人',
|
||||
type: 2,
|
||||
area: ['800px', '450px'],
|
||||
content: 'edit.html'
|
||||
})
|
||||
}
|
||||
|
||||
function openChatbotBotPlatform(){
|
||||
window.open("https://bot.chatopera.com", "_blank");
|
||||
}
|
||||
|
||||
function init() {
|
||||
query(1, 50);
|
||||
}
|
||||
|
@ -3374,12 +3374,13 @@ DROP TABLE IF EXISTS `cs_chatbot`;
|
||||
CREATE TABLE `cs_chatbot` (
|
||||
`ID` varchar(32) NOT NULL COMMENT '主键ID',
|
||||
`base_url` varchar(255) NOT NULL COMMENT '基础URL',
|
||||
`client_id` varchar(32) NOT NULL COMMENT 'Client Id',
|
||||
`secret` varchar(32) NOT NULL COMMENT 'Client Secret',
|
||||
`creater` varchar(32) NOT NULL COMMENT '创建人',
|
||||
`orgi` varchar(255) NOT NULL COMMENT '租户ID',
|
||||
`organ` varchar(32) NOT NULL COMMENT '部门ID',
|
||||
`createtime` datetime NOT NULL COMMENT '创建时间',
|
||||
`updatetime` datetime NOT NULL COMMENT '更新时间',
|
||||
`chatbotid` varchar(255) NOT NULL COMMENT '聊天机器人ID',
|
||||
`name` varchar(255) NOT NULL COMMENT '聊天机器人名字',
|
||||
`description` varchar(255) NOT NULL COMMENT '描述',
|
||||
`primary_language` varchar(20) NOT NULL COMMENT '首选语言',
|
||||
@ -3390,8 +3391,7 @@ CREATE TABLE `cs_chatbot` (
|
||||
`enabled` tinyint(1) DEFAULT '0' COMMENT '是否开启',
|
||||
`workmode` varchar(32) NOT NULL COMMENT '工作模式',
|
||||
PRIMARY KEY (`ID`) USING BTREE,
|
||||
UNIQUE KEY `snsid` (`sns_account_identifier`,`orgi`) USING BTREE COMMENT '按照渠道标识唯一',
|
||||
UNIQUE KEY `chatbotID` (`chatbotid`,`orgi`) USING BTREE COMMENT '按照ChatbotID唯一'
|
||||
UNIQUE KEY `snsid` (`sns_account_identifier`,`orgi`) USING BTREE COMMENT '按照渠道标识唯一'
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT COMMENT='机器人客服表';
|
||||
|
||||
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user