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 17:03:30 +08:00
parent dfb1e0b12c
commit 9bf6681a62
10 changed files with 54 additions and 38 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.chatopera.chatbot</groupId> <groupId>com.chatopera.chatbot</groupId>
<artifactId>sdk</artifactId> <artifactId>sdk</artifactId>
<version>1.0.3</version> <version>1.1.0</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>sdk</name> <name>sdk</name>
@ -20,16 +20,6 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>

View File

@ -18,15 +18,12 @@ package com.chatopera.chatbot;
import com.mashape.unirest.http.exceptions.UnirestException; import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.HashMap; import java.util.HashMap;
public class ChatbotAPI { public class ChatbotAPI {
private static final Logger logger = LoggerFactory.getLogger(ChatbotAPI.class);
private String schema; private String schema;
private String hostname; private String hostname;
private int port; private int port;
@ -179,6 +176,7 @@ public class ChatbotAPI {
* @throws ChatbotAPIRuntimeException * @throws ChatbotAPIRuntimeException
*/ */
public boolean updateByChatbotID(final String chatbotID, public boolean updateByChatbotID(final String chatbotID,
final String name,
final String description, final String description,
final String fallback, final String fallback,
final String welcome) throws ChatbotAPIRuntimeException { final String welcome) throws ChatbotAPIRuntimeException {
@ -192,6 +190,8 @@ public class ChatbotAPI {
body.put("fallback", fallback); body.put("fallback", fallback);
if (StringUtils.isNotBlank(welcome)) if (StringUtils.isNotBlank(welcome))
body.put("welcome", welcome); body.put("welcome", welcome);
if (StringUtils.isNotBlank(name))
body.put("name", name);
try { try {
JSONObject result = RestAPI.put(this.baseUrl + "/chatbot/" + chatbotID, body, null); JSONObject result = RestAPI.put(this.baseUrl + "/chatbot/" + chatbotID, body, null);
@ -275,8 +275,6 @@ public class ChatbotAPI {
body.put("textMessage", textMessage); body.put("textMessage", textMessage);
body.put("isDebug", debug); body.put("isDebug", debug);
logger.info("conversation body {}", body);
try { try {
JSONObject resp = RestAPI.post(this.getBaseUrl() + "/chatbot/" + chatbotID + "/conversation/query", body); JSONObject resp = RestAPI.post(this.getBaseUrl() + "/chatbot/" + chatbotID + "/conversation/query", body);
return resp; return resp;

View File

@ -22,8 +22,6 @@ import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.GetRequest; import com.mashape.unirest.request.GetRequest;
import com.mashape.unirest.request.HttpRequestWithBody; import com.mashape.unirest.request.HttpRequestWithBody;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap; import java.util.HashMap;
@ -31,7 +29,6 @@ import java.util.HashMap;
* RestAPI接口 * RestAPI接口
*/ */
public class RestAPI { public class RestAPI {
private static final Logger logger = LoggerFactory.getLogger(RestAPI.class);
/** /**
* patch headers * patch headers
@ -66,7 +63,6 @@ public class RestAPI {
*/ */
public static JSONObject post(final String url, final HashMap<String, Object> body, final HashMap<String, Object> query, HashMap<String, String> headers) throws UnirestException { public static JSONObject post(final String url, final HashMap<String, Object> body, final HashMap<String, Object> query, HashMap<String, String> headers) throws UnirestException {
HttpRequestWithBody request = Unirest.post(url); HttpRequestWithBody request = Unirest.post(url);
logger.info("post body {}", body.toString());
x(headers); x(headers);
HttpResponse<JsonNode> resp = request HttpResponse<JsonNode> resp = request
.headers(headers) .headers(headers)

View File

@ -19,8 +19,6 @@ import junit.framework.Test;
import junit.framework.TestCase; import junit.framework.TestCase;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@ -29,7 +27,6 @@ import java.net.MalformedURLException;
*/ */
public class ChatbotAPITest public class ChatbotAPITest
extends TestCase { extends TestCase {
private static final Logger logger = LoggerFactory.getLogger(ChatbotAPITest.class);
private ChatbotAPI cb; private ChatbotAPI cb;
/** /**
@ -62,7 +59,7 @@ public class ChatbotAPITest
public void testGetChatbot() { public void testGetChatbot() {
try { try {
JSONObject resp = this.cb.getChatbot("co_bot_1"); JSONObject resp = this.cb.getChatbot("co_bot_1");
logger.info("[testGetChatbot] {}", resp.toString()); System.out.println("[testGetChatbot] " + resp.toString());
} catch (ChatbotAPIRuntimeException e) { } catch (ChatbotAPIRuntimeException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -71,7 +68,7 @@ public class ChatbotAPITest
public void testGetChatbots() { public void testGetChatbots() {
try { try {
JSONObject resp = this.cb.getChatbots("name chatbotID", null, 0, 10); JSONObject resp = this.cb.getChatbots("name chatbotID", null, 0, 10);
logger.info("[testGetChatbots] resp {}", resp.toString()); System.out.println("[testGetChatbots] resp " + resp.toString());
} catch (ChatbotAPIRuntimeException e) { } catch (ChatbotAPIRuntimeException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -80,7 +77,7 @@ public class ChatbotAPITest
public void testConversation() { public void testConversation() {
try { try {
JSONObject resp = this.cb.conversation("co_bot_1", "sdktest", "华夏春松在哪里", false); JSONObject resp = this.cb.conversation("co_bot_1", "sdktest", "华夏春松在哪里", false);
logger.info("[testConversation] resp {}", resp.toString()); System.out.println("[testConversation] resp " + resp.toString());
} catch (ChatbotAPIRuntimeException e) { } catch (ChatbotAPIRuntimeException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -89,7 +86,7 @@ public class ChatbotAPITest
public void testFaq() { public void testFaq() {
try { try {
JSONObject resp = this.cb.faq("co_bot_1", "sdktest", "华夏春松在哪里", false); JSONObject resp = this.cb.faq("co_bot_1", "sdktest", "华夏春松在哪里", false);
logger.info("[testFaq] resp {}", resp.toString()); System.out.print("[testFaq] resp " + resp.toString());
} catch (ChatbotAPIRuntimeException e) { } catch (ChatbotAPIRuntimeException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -98,7 +95,7 @@ public class ChatbotAPITest
public void testParseUrl() { public void testParseUrl() {
try { try {
ChatbotAPI c = new ChatbotAPI("https://local:8000/"); ChatbotAPI c = new ChatbotAPI("https://local:8000/");
logger.info("chatbot baseUrl {}", c.getBaseUrl()); System.out.println("chatbot baseUrl " + c.getBaseUrl());
assertEquals("https://local:8000/api/v1", c.getBaseUrl()); assertEquals("https://local:8000/api/v1", c.getBaseUrl());
} catch (ChatbotAPIRuntimeException e) { } catch (ChatbotAPIRuntimeException e) {
e.printStackTrace(); e.printStackTrace();
@ -124,7 +121,6 @@ public class ChatbotAPITest
"我不了解。", "我不了解。",
"小云机器人", "小云机器人",
"你好,我是小云。"); "你好,我是小云。");
logger.info("[testCreateBot] {}", j);
} catch (ChatbotAPIRuntimeException e) { } catch (ChatbotAPIRuntimeException e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

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

View File

@ -32,7 +32,7 @@ public class ChatbotUtils {
* @return * @return
*/ */
public static String resolveChatbotIDWithSnsid(String snsid, String clientId) { public static String resolveChatbotIDWithSnsid(String snsid, String clientId) {
return clientId + "_" + snsid; return (clientId + "_" + snsid).toLowerCase();
} }
/** /**
@ -42,6 +42,6 @@ public class ChatbotUtils {
* @return * @return
*/ */
public static String resolveSnsidWithChatbotID(String chatbotID, String clientId) { public static String resolveSnsidWithChatbotID(String chatbotID, String clientId) {
return StringUtils.remove(chatbotID, clientId + "_"); return StringUtils.remove(chatbotID, clientId.toLowerCase() + "_");
} }
} }

View File

@ -50,7 +50,7 @@ import com.chatopera.cc.webim.web.model.Secret;
@Controller @Controller
@RequestMapping("/admin/im") @RequestMapping("/admin/im")
public class SNSAccountIMController extends Handler{ public class SNSAccountIMController extends Handler{
@Autowired @Autowired
private SNSAccountRepository snsAccountRes; private SNSAccountRepository snsAccountRes;
@ -84,7 +84,7 @@ public class SNSAccountIMController extends Handler{
@Menu(type = "admin" , subtype = "weixin") @Menu(type = "admin" , subtype = "weixin")
public ModelAndView save(HttpServletRequest request ,@Valid SNSAccount snsAccount) throws NoSuchAlgorithmException { public ModelAndView save(HttpServletRequest request ,@Valid SNSAccount snsAccount) throws NoSuchAlgorithmException {
if(!StringUtils.isBlank(snsAccount.getBaseURL())){ if(!StringUtils.isBlank(snsAccount.getBaseURL())){
snsAccount.setSnsid(Base62.encode(snsAccount.getBaseURL())); snsAccount.setSnsid(Base62.encode(snsAccount.getBaseURL()).toLowerCase());
int count = snsAccountRes.countBySnsidAndOrgi(snsAccount.getSnsid() , super.getOrgi(request)) ; int count = snsAccountRes.countBySnsidAndOrgi(snsAccount.getSnsid() , super.getOrgi(request)) ;
if(count == 0){ if(count == 0){
snsAccount.setOrgi(super.getOrgi(request)); snsAccount.setOrgi(super.getOrgi(request));

View File

@ -148,12 +148,13 @@ public class ApiChatbotController extends Handler {
String description = j.has("description") ? j.get("description").getAsString() : null; String description = j.has("description") ? j.get("description").getAsString() : null;
String fallback = j.has("fallback") ? j.get("fallback").getAsString() : null; String fallback = j.has("fallback") ? j.get("fallback").getAsString() : null;
String welcome = j.has("welcome") ? j.get("welcome").getAsString() : null; String welcome = j.has("welcome") ? j.get("welcome").getAsString() : null;
String name = j.has("name") ? j.get("name").getAsString() : null;
if (StringUtils.isNotBlank(description) || if (StringUtils.isNotBlank(description) ||
StringUtils.isNotBlank(fallback) || StringUtils.isNotBlank(fallback) ||
StringUtils.isNotBlank(welcome)) { StringUtils.isNotBlank(welcome)) {
try { try {
if (c.getApi().updateByChatbotID(c.getChatbotID(), description, fallback, welcome)) { 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_RC, RestUtils.RESP_RC_SUCC);
resp.addProperty(RestUtils.RESP_KEY_DATA, "更新成功。"); resp.addProperty(RestUtils.RESP_KEY_DATA, "更新成功。");
} else { } else {

View File

@ -33,6 +33,8 @@ import org.hibernate.annotations.GenericGenerator;
@org.hibernate.annotations.Proxy(lazy = false) @org.hibernate.annotations.Proxy(lazy = false)
public class SNSAccount{ public class SNSAccount{
private String id ; private String id ;
@Column(unique=true)
private String snsid ; //表示 SNSAccount private String snsid ; //表示 SNSAccount
private String name ; private String name ;
private String code ; private String code ;

View File

@ -533,7 +533,7 @@
"header": [ "header": [
{ {
"key": "authorization", "key": "authorization",
"value": "00bf99785103475c896435ef7216ebd1" "value": "b641622ea4c54f1e9e45520e7fed266b"
}, },
{ {
"key": "Content-Type", "key": "Content-Type",
@ -542,7 +542,7 @@
], ],
"body": { "body": {
"mode": "raw", "mode": "raw",
"raw": "{\n \"ops\": \"create\",\n \"primaryLanguage\": \"zh_CN\",\n \"snsid\": \"104EAc\",\n \"name\": \"小C\",\n \"baseUrl\": \"http://lhc-dev:8003\",\n \"description\": \"描述\",\n \"fallback\": \"我不理解。\",\n \"welcome\": \"你好\",\n \"workmode\": \"客服机器人优先\"\n}" "raw": "{\n \"ops\": \"create\",\n \"primaryLanguage\": \"zh_CN\",\n \"snsid\": \"104eac\",\n \"name\": \"小C\",\n \"baseUrl\": \"http://lhc-dev:8003\",\n \"description\": \"描述\",\n \"fallback\": \"我不理解。\",\n \"welcome\": \"你好\",\n \"workmode\": \"客服机器人优先\"\n}"
}, },
"url": { "url": {
"raw": "http://{{IP}}:{{PORT}}/api/chatbot", "raw": "http://{{IP}}:{{PORT}}/api/chatbot",
@ -566,7 +566,7 @@
"header": [ "header": [
{ {
"key": "authorization", "key": "authorization",
"value": "8b9567161da54400b994f141d119100b" "value": "869c0e6dfd44421cabea7934d6fde218"
}, },
{ {
"key": "Content-Type", "key": "Content-Type",
@ -591,6 +591,39 @@
} }
}, },
"response": [] "response": []
},
{
"name": "机器人客服:更新",
"request": {
"method": "POST",
"header": [
{
"key": "authorization",
"value": "b641622ea4c54f1e9e45520e7fed266b"
},
{
"key": "Content-Type",
"value": "application/json"
}
],
"body": {
"mode": "raw",
"raw": "{\n \"ops\": \"update\",\n \"id\": \"54509c828fcd4d0e9709a975a04bf190\",\n \"workmode\": \"\",\n \"enabled\": false,\n \"description\": \"描述\",\n \"fallback\": \"我不知道。\",\n \"welcome\": \"你好!\",\n \"name\": \"cc\"\n}"
},
"url": {
"raw": "http://{{IP}}:{{PORT}}/api/chatbot",
"protocol": "http",
"host": [
"{{IP}}"
],
"port": "{{PORT}}",
"path": [
"api",
"chatbot"
]
}
},
"response": []
} }
], ],
"event": [ "event": [