對接阿里雲語音驗證碼各種坑。
一. 阿里雲的資質審覈:
阿里雲使用語音服務要有語音資質。資質審覈分爲雲通信以及運營商審覈。
阿里的語音服務是聯通運營商,預計三天的時間,我的卡在阿里雲交給聯通那邊,審覈了半個月。所以要提前做資質審覈。
運營商審覈是第三方審覈,具體的審覈進度需要以第三方那邊爲主。
碰到節假日啊,工信部管控啊,都會延期。(由於語音服務行業管控嚴格,近期號碼實名資質申請暫不支持,目前工信部進行騷擾電話治理,運營商國慶前不開展新增資質審覈,國慶節後需我們再和運營商確定具體資質新增恢復時間,給您帶來不便敬請諒解 )
二. 阿里雲的語音服務開發:
現在,阿里雲短信服務和語音服務各有新舊版本。中間的jar包可能互有衝突,不同版本的jar,互有不同。建議短信服務爲舊版本時候,語音服務的也使用舊版。(血淋淋的淚啊!)
短信服務和語音服務都有新舊兩個版本;新舊版本是有些衝突,建議保留一個版本,或者將短信、語音項目分開實現
下面爲舊版阿里雲語音服務開發教程:
使用舊版的SDK請參考舊版的示例,下面這幾行代碼確認下:
語音服務 > 開發指南(舊版) > API文檔 > JAVA > 文本轉語音外呼API(SingleCallByTts)---JAVA https://help.aliyun.com/document_detail/55315.html?spm=a2c4g.11186623.6.623.37ef4163yfZJzQ
代碼:
引入pom文件:
<!-- 老版本的阿里jar --> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>3.3.1</version> </dependency>
<!--阿里雲語音驗證碼jar包--> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dyvmsapi</artifactId> <version>1.2.2</version> </dependency>
public SingleCallByTtsResponse sendCallSms(String mobile, String template_code, String param) throws ClientException { final String product = "Dyvmsapi"; //產品域名(接口地址固定,無需修改) final String domain = "dyvmsapi.aliyuncs.com"; //設置訪問超時時間 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化acsClient 暫時不支持多region IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); SingleCallByTtsRequest request = new SingleCallByTtsRequest(); //必填-被叫顯號,可在語音控制檯中找到所購買的顯號 request.setCalledShowNumber(calledShowNumber); //必填-被叫號碼 request.setCalledNumber(mobile); //必填-Tts模板ID request.setTtsCode(template_code); //可選-當模板中存在變量時需要設置此值{"code":123456} request.setTtsParam(param); //可選-音量 取值範圍 0--200 request.setVolume(100); //可選-播放次數 request.setPlayTimes(3); //可選-外部擴展字段,此ID將在回執消息中帶回給調用方 //request.setOutId("yourOutId"); //hint 此處可能會拋出異常,注意catch SingleCallByTtsResponse singleCallByTtsResponse = acsClient.getAcsResponse(request); if(singleCallByTtsResponse.getCode() != null && singleCallByTtsResponse.getCode().equals("OK")) { //請求成功 System.err.println("語音文本外呼---------------"); System.err.println("RequestId=" + singleCallByTtsResponse.getRequestId()); System.err.println("Code=" + singleCallByTtsResponse.getCode()); System.err.println("Message=" + singleCallByTtsResponse.getMessage()); System.err.println("CallId=" + singleCallByTtsResponse.getCallId()); } return singleCallByTtsResponse; }
上面是完整的,下面是備註的坑;
有種異常:SDK.InvalidRegionId : Can not find endpoint to access.
阿里的語音服務服務器在杭州,只能寫cn-hangzhou
下面是一個文檔找不到的一個DEMO(如果調不通,可以試一試這個):
public void SingleCallByTtsPhone(String caller,String phone) {
DefaultProfile profile = DefaultProfile.getProfile("default", "", "");
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setProtocol(ProtocolType.HTTP);
request.setMethod(MethodType.POST);
request.setDomain("dyvmsapi.aliyuncs.com");
request.setVersion("2017-05-25");
request.setAction("SingleCallByTts");
request.putQueryParameter("CalledShowNumber", caller);
request.putQueryParameter("CalledNumber", phone);
request.putQueryParameter("TtsCode", "");
request.putQueryParameter ("TtsParam", "{\"code\":\"1 2 3\"}");
// request.setTtsParam ("{\"code\":\"1 2 3\"}");
request.putQueryParameter("Volume", "200");
request.putQueryParameter("PlayTimes", "3");
request.putQueryParameter("OutId", "123");
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
} }
異常:Caused by: java.lang.ClassNotFoundException: com.aliyuncs.CommonRequest;
可以嘗試清理IDEA的緩存。清理緩存可以見我別的文章。
新版pom依賴:
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.0</version>
</dependency>
https://help.aliyun.com/document_detail/112459.html?spm=a2c4g.11186623.6.606.754749360f4YOs