本节以对接第三方客服系统为例,说明服务调用的过程。
智能问题服务通过调用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();
}
//需申请应用权限
String userName = URLEncoder.encode("333");//isv自己申请的账号
String passwd = URLEncoder.encode("444");//isv自己申请时的密码
//中台获取token
String params = "grant_type=password&username="+userName+"&password="+passwd ;
String url = "http://account.hsifue.cn/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;
}
step2:获取聊天记录
// openId意思为用户的id,可以通过机器人和第三方客服系统建立的握手消息获得该值;accessToken通过step1获得
String url = "http://apigate.hsifue.cn/chatbot-test/servlet/third/msg/chatlog/get?access_token="+accessToken+"&openId=2f508d5b-8b0d-46ab-ab0f-8e1baae5f9d8";
res = HttpUtil.sendGet(url, "Bearer " + accessToken);
step3:获取用户信息
// clientId意思为用户的id,可以通过机器人和第三方客服系统建立的握手消息获得该值;accessToken通过step1获得
url = "http://apigate.hsifue.cn/chatbot-test/servlet/user/info/get?access_token="+accessToken+"&&clientId=CA5E3D546794EE8F643A652A734E7C57";
res = HttpUtil.sendGet(url, "Bearer " + accessToken);
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.hsifue.cn:22601/prod/satisfysPage.html?orgName=czc0zjf1ewvyzw&appName=gld600&suborgId=czc0zjf1ewvyzw&sessionID=wroogj2470&username=698d51a19d8a121ce581499d7b701668&env=prod&workgroupId=gxetest");
String url1 = "http://apigate.hsifue.cn/chatbot-test/servlet/third/msg/receive?access_token="+accessToken;
res = HttpUtil.sendPost(url1, json.toString(), "application/json", "Bearer " + accessToken);
//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 = "http://account.hsifue.cn/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 = "http://apigate.hsifue.cn/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 = "http://apigate.hsifue.cn/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.hsifue.cn:22601/prod/satisfysPage.html?orgName=czc0zjf1ewvyzw&appName=gld600&suborgId=czc0zjf1ewvyzw&sessionID=wroogj2470&username=698d51a19d8a121ce581499d7b701668&env=prod&workgroupId=gxetest");
String url2 = "http://apigate.hsifue.cn/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();
}
}
}