Send voice messages - Amazon Pinpoint

Send voice messages

You can use the Amazon Pinpoint API to send voice messages to specific phone numbers. This section contains complete code examples that you can use to send voice messages through the Amazon Pinpoint SMS and Voice API by using an AWS SDK.

Java

Use this example to send a voice message by using the AWS SDK for Java. This example assumes that you've already installed and configured the SDK for Java. For more information, see Getting started in the AWS SDK for Java Developer Guide.

This example assumes that you're using a shared credentials file to specify the Access Key and Secret Access Key for an existing IAM user. For more information, see Set up AWS credentials and Region for development in the AWS SDK for Java Developer Guide.

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;
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); } }

For the full SDK example, see SendVoiceMessage.java on GitHub.

JavaScript (Node.js)

Use this example to send a voice message by using the AWS SDK for JavaScript in Node.js. This example assumes that you've already installed and configured the SDK for JavaScript in Node.js.

This example assumes that you're using a shared credentials file to specify the Access Key and Secret Access Key for an existing IAM user. For more information, see Setting credentials in the AWS SDK for JavaScript in Node.js Developer Guide.

'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

Use this example to send a voice message by using the AWS SDK for Python (Boto3). This example assumes that you've already installed and configured the SDK for Python (Boto3).

This example assumes that you're using a shared credentials file to specify the Access Key and Secret Access Key for an existing IAM user. For more information, see Credentials in the AWS SDK for Python (Boto3) API Reference.

import boto3 from botocore.exceptions import ClientError # 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/ 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. originationNumber = "+12065550110" # The recipient's phone number. For best results, you should specify the phone # number in E.164 format. 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 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 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. ssmlMessage = ("<speak>" "This is a test message sent from <emphasis>Amazon Pinpoint</emphasis> " "using the <break strength='weak'/>AWS SDK for Python. " "<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. callerId = "+12065550199" # The configuration set that you want to use to send the message. configurationSet = "ConfigSet" # Create a new SMS and Voice client and specify an AWS Region. client = boto3.client('sms-voice',region_name=region) try: response = client.send_voice_message( DestinationPhoneNumber = destinationNumber, OriginationPhoneNumber = originationNumber, CallerId = callerId, ConfigurationSetName = configurationSet, Content={ 'SSMLMessage':{ 'LanguageCode': languageCode, 'VoiceId': voiceId, 'Text': ssmlMessage } } ) # Display an error message if something goes wrong. except ClientError as e: print(e.response['Error']['Message']) # If the message is sent successfully, show the message ID. else: print("Message sent!"), print("Message ID: " + response['MessageId'])