音声メッセージの送信 - Amazon Pinpoint

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

音声メッセージの送信

Amazon Pinpoint API を使用して、特定の電話番号に音声メッセージを送信できます。このセクションでは、 AWS SDK を使用して Amazon Pinpoint SMS および音声 API を介して音声メッセージを送信するために使用できる完全なコード例を示します。アカウントは本番環境に存在し、音声メッセージを送信できるアクティブな発信元 ID を持っている必要があります。

エンドポイント、セグメント、チャネルのコード例については、「コード例」を参照してください。

Java

この例を使用して、AWS SDK for Java を使用して音声メッセージを送信します。この例は、SDK for Java がすでにインストールされ、設定されていることを前提としています。詳細については、『 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 の完全な例については、「」のSendVoiceMessage「.java」を参照してくださいGitHub

JavaScript (Node.js)

この例を使用して、 AWS SDK for JavaScript in Node.js を使用して音声メッセージを送信します。この例では、Node.js JavaScript で の SDK を既にインストールして設定していることを前提としています。

この例では、共有認証情報ファイルを使用して既存のユーザーのアクセスキーとシークレットアクセスキーを指定することを前提としています。詳細については、「 SDK for Node.js https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials.htmlデベロッパーガイド」の「認証情報の設定」を参照してください。 AWS JavaScript

"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)を使用して音声メッセージを送信します。この例は、SDK for Python (Boto3) がすでにインストールされ、設定されていることを前提としています。

この例では、共有認証情報ファイルを使用して既存のユーザーのアクセスキーとシークレットアクセスキーを指定することを前提としています。詳細については、『AWS SDK for Python (Boto3) API Reference』の「認証情報」を参照してください。

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()