# Demo示例
本节以对接第三方客服系统为例,说明服务调用的过程。
# 服务场景描述
智能问题服务通过调用receiveAPI、chatlog、userinfo完成对接第三方客服系统功能的调用,满足第三方客服系统获取用户信息,用户聊天记录,和用户交互的需求。
在以下场景下可以通过接口调用实现
- 用户请求专家坐席(人工客服)服务
- 专家接入用户请求后,获取用户相关信息
- 专家接入用户请求后,获取用户最近聊天信息
- 专家解决用户的问题,向用户发送文本,文件,图片等信息
- 用户和专家的会话结束后,第三方客服系统获取用户对本次专家服务的评价信息
# 服务调用流程
1.本服务调用依赖以下API:
第三方客服回调消息,消息支持文本,图片,文件等:receiveAPI
第三方客服系统获取机器人和用户之间的聊天记录:chatlog
第三方客服系统调用该接口获得用户相关的信息:userinfo
2.服务调用前置条件:
创建应用,获取appkey、appsecret;
获取服务调用token;
开通服务,scope授权通过;
以上步骤可参考前置准备
3.具体调用步骤
step1:获取token
public static String getAccessToken() {
Map res = null;
try {
String appkey = "111";//isv自己的appKey
String appSecret = "222";//isv自己的appSecret
String creds = String.format("%s:%s", appkey, appSecret);
String credential = null;
try {
credential = new String(Base64Utils.encode(creds.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
//需申请应用权限
String userName = URLEncoder.encode("333");//isv自己申请的账号
String passwd = URLEncoder.encode("444");//isv自己申请时的密码
//中台获取token
String params = "grant_type=password&username="+userName+"&password="+passwd ;
String url = "https://account.glodon.com/v3/api/oauth2/token";
res = HttpUtil.sendPost(url, params, "application/x-www-form-urlencoded", "Basic " + credential);
//解析返回参数 获取 AccessToken
if(res != null){
Map result=(Map)res.get("result");
Map data=(Map)result.get("data");
return data.get("access_token").toString();
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
step2:获取聊天记录
// openId意思为用户的id,可以通过机器人和第三方客服系统建立的握手消息获得该值;accessToken通过step1获得
String url = "https://apigate.glodon.com/chatbot-test/servlet/third/msg/chatlog/get?access_token="+accessToken+"&openId=2f508d5b-8b0d-46ab-ab0f-8e1baae5f9d8";
res = HttpUtil.sendGet(url, "Bearer " + accessToken);
1
2
3
4
2
3
4
step3:获取用户信息
// clientId意思为用户的id,可以通过机器人和第三方客服系统建立的握手消息获得该值;accessToken通过step1获得
url = "https://apigate.glodon.com/chatbot-test/servlet/user/info/get?access_token="+accessToken+"&&clientId=CA5E3D546794EE8F643A652A734E7C57";
res = HttpUtil.sendGet(url, "Bearer " + accessToken);
1
2
3
4
5
2
3
4
5
step4:异步发送数据给用户
//请按照文档传递body参数,此处给出的例子为评价消息;touser为用户的id,可以通过机器人和第三方客服系统建立的握手消息获得该值;accessToken通过step1获得,对于评价消息,msgtype固定为evaluate
JsonObject json = new JsonObject();
json.addProperty("touser", "561f7439-9ace-4ba7-8121-bbbb3e1e96a");
json.addProperty("msgtype","evaluate");
json.addProperty("url","http://gkf-test.glodon.com:22601/prod/satisfysPage.html?orgName=czc0zjf1ewvyzw&appName=gld600&suborgId=czc0zjf1ewvyzw&sessionID=wroogj2470&username=698d51a19d8a121ce581499d7b701668&env=prod&workgroupId=gxetest");
String url1 = "https://apigate.glodon.com/chatbot-test/servlet/third/msg/receive?access_token="+accessToken;
res = HttpUtil.sendPost(url1, json.toString(), "application/json", "Bearer " + accessToken);
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 演示代码
//step 1. 导入工具类
public class HttpUtil {
private static Gson gson = new Gson();
public static Map<String,Object> sendGet(String urlAddress,String token) {
String code="";
String result = "";
try {
// 创建URL对象
URL url = new URL(urlAddress);
// 打开连接 获取连接对象
URLConnection connection = url.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Authorization",token);
// 从连接对象中获取网络连接中的输入字节流对象
InputStream inputStream = connection.getInputStream();
// 将输入字节流包装成输入字符流对象,并进行字符编码
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
// 创建一个输入缓冲区对象,将字符流对象传入
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
// 定义一个字符串变量,用来接收输入缓冲区中的每一行字符串数据
String line;
// 创建一个可变字符串对象,用来装载缓冲区对象的数据,使用字符串追加的方式,将响应的所有数据都保存在该对象中
// 使用循环逐行读取输入缓冲区的数据,每次循环读入一行字符串数据赋值给line字符串变量,直到读取的行为空时标识内容读取结束循环
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
code = new Integer(((HttpURLConnection)connection).getResponseCode()).toString();
// 依次关闭打开的输入流
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
// 将可变字符串转换成String对象返回
} catch (IOException e) {
e.printStackTrace();
}
Map<String,Object> rmap = new HashMap<String, Object>();
rmap.put("code",code);
if("200".equals(code)) rmap.put("result",gson.fromJson(result.toString(),Map.class));
return rmap;
}
public static Map<String,Object> sendPost(String url, String param, String contentType, String token) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
String code = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
// System.out.println("---open connection");
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
conn.setRequestProperty("Content-Type", contentType);
conn.setRequestProperty("Authorization",token);
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// System.out.println("---start to read...");
code = new Integer(((HttpURLConnection)conn).getResponseCode()).toString();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
Map<String,Object> rmap = new HashMap<String, Object>();
rmap.put("code",code);
if("200".equals(code)) rmap.put("result",gson.fromJson(result,Map.class));
return rmap;
}
//step 2. 获取token
public static String getAccessToken() {
Map res = null;
try {
String appkey = "111";//isv自己的appKey
String appSecret = "222";//isv自己的appSecret
String creds = String.format("%s:%s", appkey, appSecret);
String credential = null;
try {
credential = new String(Base64Utils.encode(creds.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//需申请应用权限
String userName = URLEncoder.encode("333");//isv自己申请的账号
String passwd = URLEncoder.encode("444");//isv自己申请时的密码
//中台获取token
String params = "grant_type=password&username="+userName+"&password="+passwd ;
String url = "https://account.glodon.com/v3/api/oauth2/token";
res = HttpUtil.sendPost(url, params, "application/x-www-form-urlencoded", "Basic " + credential);
//解析返回参数 获取 AccessToken
if(res != null){
Map result=(Map)res.get("result");
Map data=(Map)result.get("data");
return data.get("access_token").toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//step 3. 接口的具体使用
public static void main(String[] args) throws Exception {
Map res = null;
try {
String accessToken = getAccessToken();
//1. 调用获取聊天记录接口
String url = "https://apigate.glodon.com/chatbot-test/servlet/third/msg/chatlog/get?access_token="+accessToken+"&openId=2f508d5b-8b0d-46ab-ab0f-8e1baae5f9d8";
res = HttpUtil.sendGet(url, "Bearer " + accessToken);
System.out.println("test1:"+res);
//2. 调用获取用户信息接口
String url1 = "https://apigate.glodon.com/chatbot-test/servlet/user/info/get?access_token="+accessToken+"&&clientId=CA5E3D546794EE8F643A652A734E7C57";
res = HttpUtil.sendGet(url1, "Bearer " + accessToken);
System.out.println("test2:"+res);
//3. 调用异步发送数据接口
//请按照文档传递body参数,此处给出的例子为评价消息
JsonObject json = new JsonObject();
json.addProperty("touser","561f7439-9ace-4ba7-8121-bbbb3e1e96a");
json.addProperty("msgtype","evaluate");
json.addProperty("url","http://gkf-test.glodon.com:22601/prod/satisfysPage.html?orgName=czc0zjf1ewvyzw&appName=gld600&suborgId=czc0zjf1ewvyzw&sessionID=wroogj2470&username=698d51a19d8a121ce581499d7b701668&env=prod&workgroupId=gxetest");
String url2 = "https://apigate.glodon.com/chatbot-test/servlet/third/msg/receive?access_token="+accessToken;
res = HttpUtil.sendPost(url2, json.toString(), "application/json", "Bearer " + accessToken);
System.out.println("test3:"+res);
} catch (Exception e) {
e.printStackTrace();
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180