Envoi de messages vocaux - Amazon Pinpoint

Les traductions sont fournies par des outils de traduction automatique. En cas de conflit entre le contenu d'une traduction et celui de la version originale en anglais, la version anglaise prévaudra.

Envoi de messages vocaux

Vous pouvez utiliser l'API Amazon Pinpoint pour envoyer des messages vocaux à des numéros de téléphone spécifiques. Cette section contient des exemples de code complets que vous pouvez utiliser pour envoyer des messages vocaux via l'API Amazon Pinpoint SMS and Voice à l'aide d'un AWS SDK. Votre compte doit être en cours de production et vous devez disposer d'une identité d'origine active capable d'envoyer des messages vocaux.

Pour plus d'exemples de code sur les points de terminaison, les segments et les canaux, voir Exemples de code.

Java

Utilisez cet exemple pour envoyer un message vocal à l'aide du kit AWS SDK for Java. Cette procédure suppose que vous avez déjà installé et configuré le kit SDK pour Java. Pour plus d'informations, consultez Mise en route dans le Manuel du développeur AWS SDK for Java .

Cet exemple suppose que vous utilisez un fichier d'informations d'identification partagé pour spécifier la clé d'accès et la clé d'accès secrète d'un utilisateur existant. Pour plus d'informations, voir Configurer les AWS informations d'identification et la région pour le développement dans le Guide du AWS SDK for Java développeur.

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

Pour l'exemple complet du SDK, consultez SendVoiceMessage.java on. GitHub

JavaScript (Node.js)

Utilisez cet exemple pour envoyer un message vocal à l'aide du AWS SDK pour JavaScript dans Node.js. Cet exemple suppose que vous avez déjà installé et configuré le SDK pour JavaScript Node.js.

Cet exemple suppose que vous utilisez un fichier d'informations d'identification partagé pour spécifier la clé d'accès et la clé d'accès secrète d'un utilisateur existant. Pour plus d'informations, consultez la section Configuration des informations d'identification dans le AWS SDK pour JavaScript le guide du développeur Node.js.

"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

Utilisez cet exemple pour envoyer un message vocal à l'aide du kit AWS SDK for Python (Boto3). Cette procédure suppose que vous avez déjà installé et configuré le kit SDK pour Python (Boto3).

Cet exemple suppose que vous utilisez un fichier d'informations d'identification partagé pour spécifier la clé d'accès et la clé d'accès secrète d'un utilisateur existant. Pour plus d'informations, consultez Informations d'identité dans la Référence des API du kit SDK AWS pour Python (Boto).

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