使用 Amazon Pinpoint 发送语音消息 - Amazon Pinpoint

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 Amazon Pinpoint 发送语音消息

您可以使用 Amazon Pinpoint API 向特定电话号码发送语音消息。本节包含完整的代码示例,您可以使用这些示例通过 Amazon Pinpoint SMS 发送语音消息,使用语音发送语音API消息。 AWS SDK您的帐户必须处于生产状态,并且您拥有可以发送语音消息的有效发起身份。

有关端点、区段和渠道的更多代码示例,请参阅代码示例

Java

参照此示例,使用 AWS SDK for Java 发送语音消息。此示例假设您已经安装并配置了 Java 版。SDK有关更多信息,请参阅《 AWS SDK for Java 开发人员指南》中的入门

此示例假定您正在使用共享凭证文件来指定现有用户的访问密钥和秘密访问密钥。有关更多信息,请参阅《AWS SDK for Java 开发人员指南》中的设置 AWS 证书和开发区域

import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpointsmsvoice.PinpointSmsVoiceClient; import software.amazon.awssdk.services.pinpointsmsvoice.model.SSMLMessageType; import software.amazon.awssdk.services.pinpointsmsvoice.model.VoiceMessageContent; import software.amazon.awssdk.services.pinpointsmsvoice.model.SendVoiceMessageRequest; import software.amazon.awssdk.services.pinpointsmsvoice.model.PinpointSmsVoiceException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpointsmsvoice.PinpointSmsVoiceClient; import software.amazon.awssdk.services.pinpointsmsvoice.model.SSMLMessageType; import software.amazon.awssdk.services.pinpointsmsvoice.model.VoiceMessageContent; import software.amazon.awssdk.services.pinpointsmsvoice.model.SendVoiceMessageRequest; import software.amazon.awssdk.services.pinpointsmsvoice.model.PinpointSmsVoiceException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class SendVoiceMessage { // The Amazon Polly voice that you want to use to send the message. For a list // of voices, see https://docs.aws.amazon.com/polly/latest/dg/voicelist.html static final String voiceName = "Matthew"; // The language to use when sending the message. For a list of supported // languages, see // https://docs.aws.amazon.com/polly/latest/dg/SupportedLanguage.html static final String languageCode = "en-US"; // The content of the message. This example uses SSML to customize and control // certain aspects of the message, such as by adding pauses and changing // phonation. The message can't contain any line breaks. static final String ssmlMessage = "<speak>This is a test message sent from " + "<emphasis>Amazon Pinpoint</emphasis> " + "using the <break strength='weak'/>AWS " + "SDK for Java. " + "<amazon:effect phonation='soft'>Thank " + "you for listening.</amazon:effect></speak>"; public static void main(String[] args) { final String usage = """ Usage: <originationNumber> <destinationNumber>\s Where: originationNumber - The phone number or short code that you specify has to be associated with your Amazon Pinpoint account. For best results, specify long codes in E.164 format (for example, +1-555-555-5654). destinationNumber - The recipient's phone number. For best results, you should specify the phone number in E.164 format (for example, +1-555-555-5654).\s """; if (args.length != 2) { System.out.println(usage); System.exit(1); } String originationNumber = args[0]; String destinationNumber = args[1]; System.out.println("Sending a voice message"); // Set the content type to application/json. List<String> listVal = new ArrayList<>(); listVal.add("application/json"); Map<String, List<String>> values = new HashMap<>(); values.put("Content-Type", listVal); ClientOverrideConfiguration config2 = ClientOverrideConfiguration.builder() .headers(values) .build(); PinpointSmsVoiceClient client = PinpointSmsVoiceClient.builder() .overrideConfiguration(config2) .region(Region.US_EAST_1) .build(); sendVoiceMsg(client, originationNumber, destinationNumber); client.close(); } public static void sendVoiceMsg(PinpointSmsVoiceClient client, String originationNumber, String destinationNumber) { try { SSMLMessageType ssmlMessageType = SSMLMessageType.builder() .languageCode(languageCode) .text(ssmlMessage) .voiceId(voiceName) .build(); VoiceMessageContent content = VoiceMessageContent.builder() .ssmlMessage(ssmlMessageType) .build(); SendVoiceMessageRequest voiceMessageRequest = SendVoiceMessageRequest.builder() .destinationPhoneNumber(destinationNumber) .originationPhoneNumber(originationNumber) .content(content) .build(); client.sendVoiceMessage(voiceMessageRequest); System.out.println("The message was sent successfully."); } catch (PinpointSmsVoiceException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

有关完整示SDK例,请参阅上GitHubSendVoiceMessage.java。

JavaScript (Node.js)

使用此示例 JavaScript 在 Node.js 中使用 for 发送语音消息。 AWS SDK此示例假设您已经 JavaScript 在 Node.js 中安装并配置了。SDK

此示例假定您正在使用共享凭证文件来指定现有用户的访问密钥和秘密访问密钥。有关更多信息,请参阅 Node.js 开发人员指南 JavaScript 中的设置凭证。AWS SDK

"use strict"; var AWS = require("aws-sdk"); // The AWS Region that you want to use to send the voice message. For a list of // AWS Regions where the Amazon Pinpoint SMS and Voice API is available, see // https://docs.aws.amazon.com/pinpoint-sms-voice/latest/APIReference/ var aws_region = "us-east-1"; // The phone number that the message is sent from. The phone number that you // specify has to be associated with your Amazon Pinpoint account. For best results, you // should specify the phone number in E.164 format. var originationNumber = "+12065550110"; // The recipient's phone number. For best results, you should specify the phone // number in E.164 format. var destinationNumber = "+12065550142"; // The language to use when sending the message. For a list of supported // languages, see https://docs.aws.amazon.com/polly/latest/dg/SupportedLanguage.html var languageCode = "en-US"; // The Amazon Polly voice that you want to use to send the message. For a list // of voices, see https://docs.aws.amazon.com/polly/latest/dg/voicelist.html var voiceId = "Matthew"; // The content of the message. This example uses SSML to customize and control // certain aspects of the message, such as the volume or the speech rate. // The message can't contain any line breaks. var ssmlMessage = "<speak>" + "This is a test message sent from <emphasis>Amazon Pinpoint</emphasis> " + "using the <break strength='weak'/>AWS SDK for JavaScript in Node.js. " + "<amazon:effect phonation='soft'>Thank you for listening." + "</amazon:effect>" + "</speak>"; // The phone number that you want to appear on the recipient's device. The phone // number that you specify has to be associated with your Amazon Pinpoint account. var callerId = "+12065550199"; // The configuration set that you want to use to send the message. var configurationSet = "ConfigSet"; // Specify that you're using a shared credentials file, and optionally specify // the profile that you want to use. var credentials = new AWS.SharedIniFileCredentials({ profile: "default" }); AWS.config.credentials = credentials; // Specify the region. AWS.config.update({ region: aws_region }); //Create a new Pinpoint object. var pinpointsmsvoice = new AWS.PinpointSMSVoice(); var params = { CallerId: callerId, ConfigurationSetName: configurationSet, Content: { SSMLMessage: { LanguageCode: languageCode, Text: ssmlMessage, VoiceId: voiceId, }, }, DestinationPhoneNumber: destinationNumber, OriginationPhoneNumber: originationNumber, }; //Try to send the message. pinpointsmsvoice.sendVoiceMessage(params, function (err, data) { // If something goes wrong, print an error message. if (err) { console.log(err.message); // Otherwise, show the unique ID for the message. } else { console.log("Message sent! Message ID: " + data["MessageId"]); } });
Python

参照此示例,使用 AWS SDK for Python (Boto3)发送语音消息。此示例假设你已经安装并配置了 Python (Boto3) 的。SDK

此示例假定您正在使用共享凭证文件来指定现有用户的访问密钥和秘密访问密钥。有关更多信息,请参阅 Python (Boto3) API 参考中的凭证。AWS SDK

import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def send_voice_message( sms_voice_client, origination_number, caller_id, destination_number, language_code, voice_id, ssml_message, ): """ Sends a voice message using speech synthesis provided by Amazon Polly. :param sms_voice_client: A Boto3 PinpointSMSVoice client. :param origination_number: The phone number that the message is sent from. The phone number must be associated with your Amazon Pinpoint account and be in E.164 format. :param caller_id: The phone number that you want to appear on the recipient's device. The phone number must be associated with your Amazon Pinpoint account and be in E.164 format. :param destination_number: The recipient's phone number. Specify the phone number in E.164 format. :param language_code: The language to use when sending the message. :param voice_id: The Amazon Polly voice that you want to use to send the message. :param ssml_message: The content of the message. This example uses SSML to control certain aspects of the message, such as the volume and the speech rate. The message must not contain line breaks. :return: The ID of the message. """ try: response = sms_voice_client.send_voice_message( DestinationPhoneNumber=destination_number, OriginationPhoneNumber=origination_number, CallerId=caller_id, Content={ "SSMLMessage": { "LanguageCode": language_code, "VoiceId": voice_id, "Text": ssml_message, } }, ) except ClientError: logger.exception( "Couldn't send message from %s to %s.", origination_number, destination_number, ) raise else: return response["MessageId"] def main(): origination_number = "+12065550110" caller_id = "+12065550199" destination_number = "+12065550142" language_code = "en-US" voice_id = "Matthew" ssml_message = ( "<speak>" "This is a test message sent from <emphasis>Amazon Pinpoint</emphasis> " "using the <break strength='weak'/>AWS SDK for Python (Boto3). " "<amazon:effect phonation='soft'>Thank you for listening." "</amazon:effect>" "</speak>" ) print(f"Sending voice message from {origination_number} to {destination_number}.") message_id = send_voice_message( boto3.client("pinpoint-sms-voice"), origination_number, caller_id, destination_number, language_code, voice_id, ssml_message, ) print(f"Message sent!\nMessage ID: {message_id}") if __name__ == "__main__": main()