本文为您展示了如何调用IvrCall接口实现交互式语音应答。

功能介绍

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

准备工作

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

代码示例

本示例展示了如何调用IvrCall进行语音交互。接口详情,请参见IvrCall

如果您需要进行调试,请参见使用OpenAPI Explorer

// This file is auto-generated, don't edit it. Thanks.
package com.aliyun;

import com.aliyun.tea.*;


public class Client {

    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @param regionId
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dyvmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret, String regionId) throws Exception {
        com.aliyun.tearpc.models.Config config = new com.aliyun.tearpc.models.Config();
        // 您的AccessKey ID
        config.accessKeyId = accessKeyId;
        // 您的AccessKey Secret
        config.accessKeySecret = accessKeySecret;
        // 您的可用区ID
        config.regionId = regionId;
        return new com.aliyun.dyvmsapi20170525.Client(config);
    }

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {
        com.aliyun.dyvmsapi20170525.Client client = Client.createClient("accessKeyId", "accessKeySecret", "regionId");
        com.aliyun.dyvmsapi20170525.models.IvrCallRequest request = new com.aliyun.dyvmsapi20170525.models.IvrCallRequest()
                .setCalledShowNumber("05715621xx")
                .setCalledNumber("1500000xx")
                .setStartCode("TTS_713900000")
                .setStartTtsParams("{'product':'aliyun','code':'123'}");
        com.aliyun.dyvmsapi20170525.models.IvrCallResponse response = client.ivrCall(request);
        com.aliyun.teaconsole.Client.log(response.message);
    }
}
# -*- coding: utf-8 -*-
# This file is auto-generated, don't edit it. Thanks.
import sys

from typing import List

from alibabacloud_dyvmsapi20170525.client import Client as DyvmsapiClientClient
from alibabacloud_tea_rpc import models as rpc_models
from alibabacloud_dyvmsapi20170525 import models as dyvmsapi_client_models
from alibabacloud_tea_console.client import Client as ConsoleClient


class Client:
    def __init__(self):
        pass

    @staticmethod
    def create_client(
        access_key_id: str,
        access_key_secret: str,
        region_id: str,
    ) -> DyvmsapiClientClient:
        """
        使用AK&SK初始化账号Client
        @param access_key_id:
        @param access_key_secret:
        @param region_id:
        @return: Client
        @throws Exception
        """
        config = rpc_models.Config()
        # 您的AccessKey ID
        config.access_key_id = access_key_id
        # 您的AccessKey Secret
        config.access_key_secret = access_key_secret
        # 您的可用区ID
        config.region_id = region_id
        return DyvmsapiClientClient(config)

    @staticmethod
    def main(
        args: List[str],
    ) -> None:
        """
        @param args:
        @throws Exception
        """
        client = Client.create_client('accessKeyId', 'accessKeySecret', 'regionId')
        request = dyvmsapi_client_models.IvrCallRequest(
            called_show_number='05715621xx',
            called_number='1500000xx',
            start_code='TTS_713900000',
            start_tts_params="{'product':'aliyun','code':'123'}"
        )
        response = client.ivr_call(request)
        ConsoleClient.log(response.message)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        """
        @param args:
        @throws Exception
        """
        client = Client.create_client('accessKeyId', 'accessKeySecret', 'regionId')
        request = dyvmsapi_client_models.IvrCallRequest(
            called_show_number='05715621xx',
            called_number='1500000xx',
            start_code='TTS_713900000',
            start_tts_params="{'product':'aliyun','code':'123'}"
        )
        response = await client.ivr_call_async(request)
        ConsoleClient.log(response.message)


if __name__ == '__main__':
    Client.main(sys.argv[1:])
// This file is auto-generated, don't edit it. Thanks.
package main

import (
  "os"
  dyvmsapiclient  "github.com/alibabacloud-go/dyvmsapi-20170525/client"
  rpc  "github.com/alibabacloud-go/tea-rpc/client"
  console  "github.com/alibabacloud-go/tea-console/client"
  "github.com/alibabacloud-go/tea/tea"
)


/**
 * 使用AK&SK初始化账号Client
 * @param accessKeyId
 * @param accessKeySecret
 * @param regionId
 * @return Client
 * @throws Exception
 */
func CreateClient (accessKeyId *string, accessKeySecret *string, regionId *string) (_result *dyvmsapiclient.Client, _err error) {
  config := &rpc.Config{}
  // 您的AccessKey ID
  config.AccessKeyId = accessKeyId
  // 您的AccessKey Secret
  config.AccessKeySecret = accessKeySecret
  // 您的可用区ID
  config.RegionId = regionId
  _result = &dyvmsapiclient.Client{}
  _result, _err = dyvmsapiclient.NewClient(config)
  return _result, _err
}

/**
 * @param args
 * @throws Exception
 */
func _main (args []*string) (_err error) {
  client, _err := CreateClient(tea.String("accessKeyId"), tea.String("accessKeySecret"), tea.String("regionId"))
  if _err != nil {
    return _err
  }

  request := &dyvmsapiclient.IvrCallRequest{
    // 主叫号码。仅支持中国大陆号码。单次请求仅支持一个号码。
    CalledShowNumber: tea.String("05715621xx"),
    // 被叫号码。仅支持中国大陆号码。单次请求仅支持一个号码。
    CalledNumber: tea.String("1500000xx"),
    // 呼叫开始时播放的提示音。
    // 语音文件:语音ID。可以在文本转语音模板页面查看语音ID。
    // 文本转语音模板:模板ID。可以在文本转语音模板页面查看模板ID。
    StartCode: tea.String("TTS_713900000"),
    // 文本转语音模板内的变量参数,格式为JSON。
    // 当StartCode为文本转语音模板(TTS)、且模板中带变量的情况下此参数为必选。
    StartTtsParams: tea.String("{'product':'aliyun','code':'123'}"),
  }
  response, _err := client.IvrCall(request)
  if _err != nil {
    return _err
  }

  console.Log(response.Message)
  return _err
}


func main() {
  err := _main(tea.StringSlice(os.Args))
  if err != nil {
    panic(err)
  }
}
<?php

// This file is auto-generated, don't edit it. Thanks.
namespace Alibabacloud\Dyvmsapi20170525;

use AlibabaCloud\SDK\Dyvmsapi\V20170525\Dyvmsapi;
use AlibabaCloud\Tea\Console\Console;

use AlibabaCloud\Tea\Rpc\Rpc\Config;
use AlibabaCloud\SDK\Dyvmsapi\V20170525\Models\IvrCallRequest;
use AlibabaCloud\SDK\Dyvmsapi\V20170525\Models\IvrCallResponse;

class Client {

    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @param string $regionId
     * @return Dyvmsapi Client
     */
    public static function createClient($accessKeyId, $accessKeySecret, $regionId){
        $config = new Config([]);
        // 您的AccessKey ID
        $config->accessKeyId = $accessKeyId;
        // 您的AccessKey Secret
        $config->accessKeySecret = $accessKeySecret;
        // 您的可用区ID
        $config->regionId = $regionId;
        return new Dyvmsapi($config);
    }

    /**
     * @param string[] $args
     * @return void
     */
    public static function main($args){
        $client = self::createClient("accessKeyId", "accessKeySecret", "regionId");
        $request = new IvrCallRequest([
            // 主叫号码。仅支持中国大陆号码。单次请求仅支持一个号码。
            "calledShowNumber" => "05715621xx",
            // 被叫号码。仅支持中国大陆号码。单次请求仅支持一个号码。
            "calledNumber" => "1500000xx",
            // 呼叫开始时播放的提示音。
            // 语音文件:语音ID。可以在文本转语音模板页面查看语音ID。
            // 文本转语音模板:模板ID。可以在文本转语音模板页面查看模板ID。
            "startCode" => "TTS_713900000",
            // 文本转语音模板内的变量参数,格式为JSON。
            // 当StartCode为文本转语音模板(TTS)、且模板中带变量的情况下此参数为必选。
            "startTtsParams" => "{'product':'aliyun','code':'123'}"
        ]);
        $response = $client->ivrCall($request);
        Console::log($response->message);
    }
}
$path = __DIR__ . \DIRECTORY_SEPARATOR . '..' . \DIRECTORY_SEPARATOR . 'vendor' . \DIRECTORY_SEPARATOR . 'autoload.php';
if (file_exists($path)) {
    require_once $path;
}
Client::main(array_slice($argv, 1));
// This file is auto-generated, don't edit it. Thanks.

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

using Tea;
using Tea.Utils;


namespace com.aliyun
{
    public class Client 
    {

        /**
         * 使用AK&SK初始化账号Client
         * @param accessKeyId
         * @param accessKeySecret
         * @param regionId
         * @return Client
         * @throws Exception
         */
        public static AlibabaCloud.SDK.Dyvmsapi20170525.Client CreateClient(string accessKeyId, string accessKeySecret, string regionId)
        {
            AlibabaCloud.RPCClient.Models.Config config = new AlibabaCloud.RPCClient.Models.Config();
            // 您的AccessKey ID
            config.AccessKeyId = accessKeyId;
            // 您的AccessKey Secret
            config.AccessKeySecret = accessKeySecret;
            // 您的可用区ID
            config.RegionId = regionId;
            return new AlibabaCloud.SDK.Dyvmsapi20170525.Client(config);
        }

        /**
         * @param args
         * @throws Exception
         */
        public static void Main(string[] args)
        {
            AlibabaCloud.SDK.Dyvmsapi20170525.Client client = CreateClient("accessKeyId", "accessKeySecret", "regionId");
            AlibabaCloud.SDK.Dyvmsapi20170525.Models.IvrCallRequest request = new AlibabaCloud.SDK.Dyvmsapi20170525.Models.IvrCallRequest
            {
                // 主叫号码。仅支持中国大陆号码。单次请求仅支持一个号码。
                CalledShowNumber = "05715621xx",
                // 被叫号码。仅支持中国大陆号码。单次请求仅支持一个号码。
                CalledNumber = "1500000xx",
                // 呼叫开始时播放的提示音。
                // 语音文件:语音ID。可以在文本转语音模板页面查看语音ID。
                // 文本转语音模板:模板ID。可以在文本转语音模板页面查看模板ID。
                StartCode = "TTS_713900000",
                // 文本转语音模板内的变量参数,格式为JSON。
                // 当StartCode为文本转语音模板(TTS)、且模板中带变量的情况下此参数为必选。
                StartTtsParams = "{'product':'aliyun','code':'123'}",
            };
            AlibabaCloud.SDK.Dyvmsapi20170525.Models.IvrCallResponse response = client.IvrCall(request);
            AlibabaCloud.TeaConsole.Client.Log(response.Message);
        }


    }
}
// This file is auto-generated, don't edit it
import DyvmsapiClient, * as $DyvmsapiClient from '@alicloud/dyvmsapi20170525';
import RPC, * as $RPC from '@alicloud/rpc-client';
import Console from '@alicloud/tea-console';
import * as $tea from '@alicloud/tea-typescript';


export default class Client {

  /**
   * 使用AK&SK初始化账号Client
   * @param accessKeyId
   * @param accessKeySecret
   * @param regionId
   * @return Client
   * @throws Exception
   */
  static createClient(accessKeyId: string, accessKeySecret: string, regionId: string): DyvmsapiClient {
    let config = new $RPC.Config({ });
    // 您的AccessKey ID
    config.accessKeyId = accessKeyId;
    // 您的AccessKey Secret
    config.accessKeySecret = accessKeySecret;
    // 您的可用区ID
    config.regionId = regionId;
    return new DyvmsapiClient(config);
  }

  /**
   * @param args
   * @throws Exception
   */
  static async main(args: string[]): Promise<void> {
    let client = Client.createClient("accessKeyId", "accessKeySecret", "regionId");
    let request = new $DyvmsapiClient.IvrCallRequest({
      // 主叫号码。仅支持中国大陆号码。单次请求仅支持一个号码。
      calledShowNumber: "05715621xx",
      // 被叫号码。仅支持中国大陆号码。单次请求仅支持一个号码。
      calledNumber: "1500000xx",
      // 呼叫开始时播放的提示音。
      // 语音文件:语音ID。可以在文本转语音模板页面查看语音ID。
      // 文本转语音模板:模板ID。可以在文本转语音模板页面查看模板ID。
      startCode: "TTS_713900000",
      // 文本转语音模板内的变量参数,格式为JSON。
      // 当StartCode为文本转语音模板(TTS)、且模板中带变量的情况下此参数为必选。
      startTtsParams: "{'product':'aliyun','code':'123'}",
    });
    let response = await client.ivrCall(request);
    Console.log(response.message);
  }

}

Client.main(process.argv.slice(2));

执行结果

正常返回结果如下所示:
{
  "Message": "OK",
  "RequestId": "4ABF7FB6-449C-4624-8CC6-9AAE36845****",
  "Code": "OK",
  "CallId": "1187209624900105518****"
}

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