本文为您展示了语音通知功能的实现方法。您可以通过调用SingleCallByVoice发起语音文件类型的语音通知,调用SingleCallByTts发起文本转语音类型的语音通知。

功能介绍

语音通知可解决通知到达率延迟、关注度不高、阅读体验差等问题,并且您可以通过电话获得更全面的信息通知。您只需要提前设置通知模版内容,调用API就可以给用户推送语音通知,并可以设置播放音量、播放次数等。

准备工作

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

代码示例

本示例展示了如何调用SingleCallByVoice接口发送语音通知。接口参数详情,请参见SingleCallByVoiceSingleCallByTts

如果您需要进行调试,请参见使用OpenAPI Explorer
// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.demo;

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.teaopenapi.models.Config config = new com.aliyun.teaopenapi.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.SingleCallByVoiceRequest request = new com.aliyun.dyvmsapi20170525.models.SingleCallByVoiceRequest()
                .setCalledShowNumber("18850xxxx")
                .setCalledNumber("15750xxxx")
                .setVoiceCode("e271f3f2-e155-4366-a9f4-0fe55765b3ec.wav");
        com.aliyun.dyvmsapi20170525.models.SingleCallByVoiceResponse response = client.singleCallByVoice(request);
        com.aliyun.teaconsole.Client.log(response.body.callId);
    }
}
# -*- 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_openapi import models as open_api_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 = open_api_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.SingleCallByVoiceRequest(
            called_show_number='18850xxxx',
            called_number='15750xxxx',
            voice_code='e271f3f2-e155-4366-a9f4-0fe55765b3ec.wav'
        )
        response = client.single_call_by_voice(request)
        ConsoleClient.log(response.body.call_id)

    @staticmethod
    async def main_async(
        args: List[str],
    ) -> None:
        """
        @param args:
        @throws Exception
        """
        client = Client.create_client('accessKeyId', 'accessKeySecret', 'regionId')
        request = dyvmsapi_client_models.SingleCallByVoiceRequest(
            called_show_number='18850xxxx',
            called_number='15750xxxx',
            voice_code='e271f3f2-e155-4366-a9f4-0fe55765b3ec.wav'
        )
        response = await client.single_call_by_voice_async(request)
        ConsoleClient.log(response.body.call_id)


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/v2/client"
  openapi  "github.com/alibabacloud-go/darabonba-openapi/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 := &openapi.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.SingleCallByVoiceRequest{
    // 被叫显号,若您使用的模板为公共号池号码外呼模板,则该字段值必须为空;
    // 若您使用的模板为专属号码外呼模板,则必须传入已购买的号码,仅支持一个号码,您可以在语音服务控制台上查看已购买的号码。
    CalledShowNumber: tea.String("18850xxxx"),
    // 被叫号码。仅支持中国内地号码。一次请求仅支持一个被叫号。
    CalledNumber: tea.String("15750xxxx"),
    // 语音文件的语音ID。
    VoiceCode: tea.String("e271f3f2-e155-4366-a9f4-0fe55765b3ec.wav"),
  }
  response, _err := client.SingleCallByVoice(request)
  if _err != nil {
    return _err
  }

  console.Log(response.Body.CallId)
  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\Demo;

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

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dyvmsapi\V20170525\Models\SingleCallByVoiceRequest;
use AlibabaCloud\SDK\Dyvmsapi\V20170525\Models\SingleCallByVoiceResponse;

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 SingleCallByVoiceRequest([
            // 被叫显号,若您使用的模板为公共号池号码外呼模板,则该字段值必须为空;
            // 若您使用的模板为专属号码外呼模板,则必须传入已购买的号码,仅支持一个号码,您可以在语音服务控制台上查看已购买的号码。
            "calledShowNumber" => "18850xxxx",
            // 被叫号码。仅支持中国内地号码。一次请求仅支持一个被叫号。
            "calledNumber" => "15750xxxx",
            // 语音文件的语音ID。
            "voiceCode" => "e271f3f2-e155-4366-a9f4-0fe55765b3ec.wav"
        ]);
        $response = $client->singleCallByVoice($request);
        Console::log($response->body->callId);
    }
}
$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 Alibabacloud.Demo
{
    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.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.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.SingleCallByVoiceRequest request = new AlibabaCloud.SDK.Dyvmsapi20170525.Models.SingleCallByVoiceRequest
            {
                // 被叫显号,若您使用的模板为公共号池号码外呼模板,则该字段值必须为空;
                // 若您使用的模板为专属号码外呼模板,则必须传入已购买的号码,仅支持一个号码,您可以在语音服务控制台上查看已购买的号码。
                CalledShowNumber = "18850xxxx",
                // 被叫号码。仅支持中国内地号码。一次请求仅支持一个被叫号。
                CalledNumber = "15750xxxx",
                // 语音文件的语音ID。
                VoiceCode = "e271f3f2-e155-4366-a9f4-0fe55765b3ec.wav",
            };
            AlibabaCloud.SDK.Dyvmsapi20170525.Models.SingleCallByVoiceResponse response = client.SingleCallByVoice(request);
            AlibabaCloud.TeaConsole.Client.Log(response.Body.CallId);
        }


    }
}
// This file is auto-generated, don't edit it
import DyvmsapiClient, * as $DyvmsapiClient from '@alicloud/dyvmsapi20170525';
import OpenApi, * as $OpenApi from '@alicloud/openapi-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 $OpenApi.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.SingleCallByVoiceRequest({
      // 被叫显号,若您使用的模板为公共号池号码外呼模板,则该字段值必须为空;
      // 若您使用的模板为专属号码外呼模板,则必须传入已购买的号码,仅支持一个号码,您可以在语音服务控制台上查看已购买的号码。
      calledShowNumber: "18850xxxx",
      // 被叫号码。仅支持中国内地号码。一次请求仅支持一个被叫号。
      calledNumber: "15750xxxx",
      // 语音文件的语音ID。
      voiceCode: "e271f3f2-e155-4366-a9f4-0fe55765b3ec.wav",
    });
    let response = await client.singleCallByVoice(request);
    Console.log(response.body.callId);
  }

}

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

执行结果

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

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