場(chǎng)景集成方式
更新時(shí)間:
將外呼機(jī)器人的配置場(chǎng)景通過(guò)Iframe方式集成到用戶自己的系統(tǒng)上使用。
參考阿里云免登方案。
實(shí)踐步驟
創(chuàng)建RAM子用戶及授權(quán),如子賬號(hào)已存在,可跳過(guò)該步驟
1.1、為RAM用戶授權(quán),AliyunSTSAssumeRoleAccess
1.2、創(chuàng)建RAM角色
使用RAM主賬號(hào)登錄控制臺(tái),并創(chuàng)建RAM角色。或者通過(guò)RAM的API CreateRole創(chuàng)建RAM角色。
1.3、為角色授權(quán)
1.4、roleArn參數(shù)獲取
1.5 pom.xml
<dependencies>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-sts</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
</dependencies>
1.6、Code Sample
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.auth.sts.AssumeRoleRequest;
import com.aliyuncs.auth.sts.AssumeRoleResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.net.URISyntaxException;
public class Test {
private static final String SIGN_IN_DOMAIN = "http://signin.aliyun.com/federation";
private static String getRoleArn(String accountId, String roleName) {
return String.format("acs:ram::%s:role/%s", accountId, roleName);
}
/**
* 使用安全令牌獲取登錄令牌
* http://bestwisewords.com/document_detail/91913.html
*
* @param accesskeyId
* @param accessKeySecret
* @param securityToken
* @return
* @throws IOException
* @throws URISyntaxException
*/
private static String getSignInToken(String accesskeyId, String accessKeySecret, String securityToken)
throws IOException, URISyntaxException {
URIBuilder builder = new URIBuilder(SIGN_IN_DOMAIN);
builder.setParameter("Action", "GetSigninToken")
.setParameter("AccessKeyId", accesskeyId)
.setParameter("AccessKeySecret", accessKeySecret)
.setParameter("SecurityToken", securityToken)
.setParameter("TicketType", "mini");
HttpGet request = new HttpGet(builder.build());
CloseableHttpClient httpclient = HttpClients.createDefault();
try (CloseableHttpResponse response = httpclient.execute(request)) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String context = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = JSON.parseObject(context);
return jsonObject.getString("SigninToken");
} else {
System.out.println(response.getStatusLine());
}
}
return null;
}
private static String getChatbotLoginUrl(String pageUrl, String signInToken) throws URISyntaxException {
URIBuilder builder = new URIBuilder(SIGN_IN_DOMAIN);
builder.setParameter("Action", "Login");
// 登錄失效跳轉(zhuǎn)的地址,一般配置為自建WEB配置302跳轉(zhuǎn)的URL
builder.setParameter("LoginUrl", "https://signin.aliyun.com/login.htm");
// 實(shí)際訪問(wèn)Chatbot的頁(yè)面,比如對(duì)話工廠管理頁(yè),某個(gè)實(shí)例詳情等
builder.setParameter("Destination", pageUrl);
builder.setParameter("SigninToken", signInToken);
HttpGet request = new HttpGet(builder.build());
return request.getURI().toString();
}
/**
* 通過(guò)AssumeRole接口獲取用戶臨時(shí)身份
* 參考 http://bestwisewords.com/document_detail/28763.html
*
* @param accountId
* @param accessKeyId
* @param accessKeySecret
* @param ramRole
* @return
* @throws ClientException
*/
private static AssumeRoleResponse.Credentials assumeRole(String accountId, String accessKeyId,
String accessKeySecret, String ramRole)
throws ClientException {
String defaultRegion = "cn-hangzhou";
IClientProfile profile = DefaultProfile.getProfile(defaultRegion, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
AssumeRoleRequest request = new AssumeRoleRequest();
// 設(shè)置RAMArn, accountId為資源Owner的UID,即主賬號(hào)
request.setRoleArn(getRoleArn(accountId, ramRole));
// 用戶自定義參數(shù)。此參數(shù)用來(lái)區(qū)分不同的令牌,可用于用戶級(jí)別的訪問(wèn)審計(jì)。格式:^[a-zA-Z0-9\.@\-_]+$
request.setRoleSessionName("session-name");
// 指定的過(guò)期時(shí)間,單位為秒。過(guò)期時(shí)間范圍:900 ~ 3600,默認(rèn)值為 3600
request.setDurationSeconds(3600L);
AssumeRoleResponse response = client.getAcsResponse(request);
return response.getCredentials();
}
public static void main(String[] args) throws IOException, URISyntaxException {
try {
/*
* Step 0 準(zhǔn)備子賬號(hào)和權(quán)限授權(quán)
*/
//* accountId指主賬號(hào)ID
String accountId = "xxxx";
// 用來(lái)訪問(wèn)對(duì)話機(jī)器人產(chǎn)品的Role,可以按照需要添加AliyunOutboundbotFullAccess 權(quán)限
// 當(dāng)前ramRole為示例值,請(qǐng)使用真實(shí)值替代
String ramRole = "xxxx";
// 某個(gè)子賬號(hào)AK,SK, 要求需要有 AliyunSTSAssumeRoleAccess 權(quán)限
// 當(dāng)前accessKeyId、accessKeySecret為示例值,請(qǐng)使用真實(shí)值替代
String accessKeyId = "按需替換";
String accessKeySecret = "按需替換";
/*
* Step 1 通過(guò)AssumeRole接口獲取臨時(shí)AK, SK, SecurityToken
*/
AssumeRoleResponse.Credentials credentials = assumeRole(accountId, accessKeyId, accessKeySecret, ramRole);
System.out.println("Expiration: " + credentials.getExpiration());
System.out.println("Access Key Id: " + credentials.getAccessKeyId());
System.out.println("Access Key Secret: " + credentials.getAccessKeySecret());
System.out.println("Security Token: " + credentials.getSecurityToken());
/*
* Step 2 獲取SigninToken
*/
String signInToken = getSignInToken(credentials.getAccessKeyId(),
credentials.getAccessKeySecret(),
credentials.getSecurityToken());
System.out.println("Your SigninToken is: " + signInToken);
/*
* Step 4 構(gòu)造免登錄鏈接,https://outboundbot4service.console.aliyun.com是智能外呼虛商免登控制臺(tái)地址
*/
String pageUrl = "https://outboundbot4service.console.aliyun.com/#/outboundbot_script?instanceId=按需替換&nluServiceType=DialogStudio";
String accessUrl = getChatbotLoginUrl(pageUrl, signInToken);
System.out.println("Your PageUrl is : " + accessUrl);
} catch (ClientException e) {
System.out.println("Failed:");
System.out.println("Error code: " + e.getErrCode());
System.out.println("Error message: " + e.getErrMsg());
System.out.println("RequestId: " + e.getRequestId());
}
}
}
Step4中注意幾點(diǎn):
? instanceId的值需要按需替換。
1.7 前端集成
1. 上述生成的鏈接可直接在瀏覽器中打開(kāi)使用
2. 若需將鏈接以iframe的形式嵌入業(yè)務(wù)系統(tǒng),請(qǐng)開(kāi)放iframe以下兩個(gè)配置
allow="microphone *" allowfullscreen
注:iframe嵌套時(shí),外呼系統(tǒng)在請(qǐng)求接口時(shí),發(fā)現(xiàn)用戶登錄過(guò)期,會(huì)通過(guò)postMessage向主應(yīng)用發(fā)送消息,主應(yīng)用可根據(jù)自己的需求進(jìn)行刷新頁(yè)面處理或者跳轉(zhuǎn)登錄頁(yè)面處理。
示例代碼如下:
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Iframe Test</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<script>
// 外呼頁(yè)面調(diào)用接口返回登錄cookie失效會(huì)給父頁(yè)面postmessage,父頁(yè)面接受自己處理即可
function handleMessage(e) {
// 只接受外呼預(yù)發(fā)和線上域名,按需加,實(shí)際可去除預(yù)發(fā)域名
if (!['https://outboundbot4service.console.aliyun.com'].includes(e.origin)) {
return;
}
// e.data = {code: 'ConsoleNeedLogin', message: '登錄超時(shí),請(qǐng)重新登錄'}
if (e.data && e.data.code === 'ConsoleNeedLogin') {
// todo 頁(yè)面跳轉(zhuǎn)登錄或者刷新主站頁(yè)面
}
}
window.addEventListener('message', handleMessage);
</script>
</head>
<body>
<iframe src="http://signin.aliyun.com/federation?Action=Login&LoginUrl=https%3A%2F%2Fsignin.aliyun.com%2Flogin.htm&Destination=http%3A%2F%2Foutboundbot4service.console.aliyun.com%2F%23%2Foutboundbot_script%3FinstanceId%3D2a10e7f2-8f13-4a99-9f57-fea3b0f9c6f8%26nluServiceType%3DDialogStudio&SigninToken=svX6LAkjGGjnQTYHFNh2tdAMRoNeseZyFMuVWpZpceayC3wSTpvPidzMqcSpDxsUtwZCSUwEVNvJQpTuoozLV3R9BRyscrJQWedCxuguTRTrFBqvLFrVMkTD2ouWFb6KEJGS7vHbCUH5TkA6Y4LRa7Lyjsyz614LrpBkdkudWDZ4rGbXriQbyhCTJVn5LhXGwvyDprNcAPFAybfUzMLtQPgFtEhji9bU" width="100%" style="height: calc(100vh);" allow="microphone *" allowfullscreen></iframe>
</body>
</html>
文檔內(nèi)容是否對(duì)您有幫助?