本章节为您介绍了阿里云语音服务语音IVR的功能介绍和实现方法,您可以通过调用IvrCall实现交互式语音应答。

功能介绍

语音IVR是指通过调用语音呼叫的API,从运营商网络向指定号码发起一通呼叫,呼叫被接起后,播放一段指定音频,用户根据音频引导,通过手机按键信息返回意图,语音平台通通过消息回执返回按键信息给企业业务系统。

实现方法

在实现调用API实现语音IVR功能前,您需要完成以下操作。

  • 购买语音号码并审核通过语音模板或语音文件,具体操作请参见快速入门
  • 获取AccessKey信息,详情请参见获取AccessKey
  • 完成本地SDK环境搭建,详情请参见Java SDK

本章节调用接口详情,请参见IvrCall

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

public class IvrCall {
    public static void main(String[] args) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dyvmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("IvrCall");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("CalledShowNumber", "05715621xx");
        request.putQueryParameter("CalledNumber", "1500000xx");
        request.putQueryParameter("StartCode", "TTS_713900000");
        request.putQueryParameter("StartTtsParams", "{\"product\":\"aliyun\",\"code\":\"123\"}");
        request.putQueryParameter("MenuKeyMap.1.Key", "1");
        request.putQueryParameter("MenuKeyMap.1.Code", "9a9d7222-670f-40b0-a3af.wav");
        request.putQueryParameter("MenuKeyMap.2.Key", "2");
        request.putQueryParameter("MenuKeyMap.2.Code", "TTS_71390000");
        request.putQueryParameter("MenuKeyMap.2.TtsParams", "{\"product\":\"aliyun\",\"code\":\"123\"}");
        request.putQueryParameter("PlayTimes", "3");
        request.putQueryParameter("ByeTtsParams", "{\"product\":\"aliyun\",\"code\":\"123\"}");
        request.putQueryParameter("Timeout", "3000");
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

正常返回结果如下所示:

{
  "Message": "OK",
  "RequestId": "4ABF7FB6-449C-4624-8CC6-9AAE368453AA",
  "Code": "OK",
  "CallId": "118720962490^10551812xx"
}

调用SDK返回错误码,详情请参见错误码列表