Inviare messaggi vocali con Amazon Pinpoint - Amazon Pinpoint

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Inviare messaggi vocali con Amazon Pinpoint

Puoi utilizzare Amazon Pinpoint API per inviare messaggi vocali a numeri di telefono specifici. Questa sezione contiene esempi di codice completi che puoi utilizzare per inviare messaggi vocali tramite Amazon Pinpoint SMS e Voice API utilizzando un. AWS SDK Il tuo account deve essere in produzione e disporre di un'identità di origine attiva in grado di inviare messaggi vocali.

Per altri esempi di codice su endpoint, segmenti e canali, consulta Esempi di codice.

Java

Utilizza questo esempio per l'invio di un messaggio vocale tramite l’AWS SDK for Java. Questo esempio presuppone che tu abbia già installato e configurato il SDK file per Java. Per ulteriori informazioni, consulta l'argomento relativo alle nozioni di base nella Guida per gli sviluppatori di AWS SDK for Java .

Questo esempio presuppone che tu stia utilizzando un file di credenziali condivise per specificare la chiave di accesso e la chiave di accesso segreta per un utente esistente. Per ulteriori informazioni, consulta Configurare AWS le credenziali e la regione per lo sviluppo nella Guida per gli AWS SDK for Java sviluppatori.

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

Per l'SDKesempio completo, consulta SendVoiceMessage.java on. GitHub

JavaScript (Node.js)

Utilizzate questo esempio per inviare un messaggio vocale utilizzando il modulo contenuto AWS SDK JavaScript in Node.js. L'esempio presuppone che il modulo in Node.js sia già stato installato e configurato. SDK JavaScript

Questo esempio presuppone che tu stia utilizzando un file di credenziali condivise per specificare la chiave di accesso e la chiave di accesso segreta per un utente esistente. Per ulteriori informazioni, vedere Impostazione delle credenziali nel modulo JavaScript in Node.js Developer Guide.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

Utilizza questo esempio per l'invio di un messaggio vocale tramite l’ AWS SDK for Python (Boto3). Questo esempio presuppone che tu abbia già installato e configurato SDK for Python (Boto3).

Questo esempio presuppone che tu stia utilizzando un file di credenziali condivise per specificare la chiave di accesso e la chiave di accesso segreta per un utente esistente. Per ulteriori informazioni, vedete Credenziali nel riferimento AWS SDKfor Python (APIBoto3).

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