Invio di messaggi e-mail e di testo con Amazon Pinpoint tramite AWS SDK - AWSEsempi di codice SDK

Sono disponibili altri esempi AWS SDK nel repository AWSDoc SDK Examples. GitHub

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à.

Invio di messaggi e-mail e di testo con Amazon Pinpoint tramite AWS SDK

Gli esempi di codice seguenti mostrano come inviare messaggi e-mail e di testo con Amazon Pinpoint.

CLI
AWS CLI

Invio di un messaggio SMS utilizzando l'endpoint di un'applicazione

L'esempio send-messages seguente invia un messaggio diretto per un'applicazione con un endpoint.

aws pinpoint send-messages \ --application-id 611e3e3cdd47474c9c1399a505665b91 \ --message-request file://myfile.json \ --region us-west-2

Contenuto di myfile.json.

{ "MessageConfiguration": { "SMSMessage": { "Body": "hello, how are you?" } }, "Endpoints": { "testendpoint": {} } }

Output:

{ "MessageResponse": { "ApplicationId": "611e3e3cdd47474c9c1399a505665b91", "EndpointResult": { "testendpoint": { "Address": "+12345678900", "DeliveryStatus": "SUCCESSFUL", "MessageId": "itnuqhai5alf1n6ahv3udc05n7hhddr6gb3lq6g0", "StatusCode": 200, "StatusMessage": "MessageId: itnuqhai5alf1n6ahv3udc05n7hhddr6gb3lq6g0" } }, "RequestId": "c7e23264-04b2-4a46-b800-d24923f74753" } }

Per ulteriori informazioni, consulta Canale SMS di Amazon Pinpoint nella Guida per l'utente di Amazon Pinpoint.

  • Per i dettagli sull'API, consulta Command SendMessagesReferenceAWS CLI.

Java
SDK per Java 2.x
Nota

C'è altro su GitHub. Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

Invia un messaggio e-mail.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.AddressConfiguration; import software.amazon.awssdk.services.pinpoint.model.ChannelType; import software.amazon.awssdk.services.pinpoint.model.SimpleEmailPart; import software.amazon.awssdk.services.pinpoint.model.SimpleEmail; import software.amazon.awssdk.services.pinpoint.model.EmailMessage; import software.amazon.awssdk.services.pinpoint.model.DirectMessageConfiguration; import software.amazon.awssdk.services.pinpoint.model.MessageRequest; import software.amazon.awssdk.services.pinpoint.model.SendMessagesRequest; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import software.amazon.awssdk.services.pinpointemail.PinpointEmailClient; import software.amazon.awssdk.services.pinpointemail.model.Body; import software.amazon.awssdk.services.pinpointemail.model.Content; import software.amazon.awssdk.services.pinpointemail.model.Destination; import software.amazon.awssdk.services.pinpointemail.model.EmailContent; import software.amazon.awssdk.services.pinpointemail.model.Message; import software.amazon.awssdk.services.pinpointemail.model.SendEmailRequest; import java.util.HashMap; 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 SendEmailMessage { // The character encoding the you want to use for the subject line and // message body of the email. public static String charset = "UTF-8"; // The body of the email for recipients whose email clients support HTML content. static final String body = """ Amazon Pinpoint test (AWS SDK for Java 2.x) This email was sent through the Amazon Pinpoint Email API using the AWS SDK for Java 2.x """; public static void main(String[] args) { final String usage = """ Usage: <subject> <appId> <senderAddress> <toAddress> Where: subject - The email subject to use. senderAddress - The from address. This address has to be verified in Amazon Pinpoint in the region you're using to send email\s toAddress - The to address. This address has to be verified in Amazon Pinpoint in the region you're using to send email\s """; if (args.length != 3) { System.out.println(usage); System.exit(1); } String subject = args[0]; String senderAddress = args[1]; String toAddress = args[2]; System.out.println("Sending a message"); PinpointEmailClient pinpoint = PinpointEmailClient.builder() .region(Region.US_EAST_1) .build(); sendEmail(pinpoint, subject, senderAddress, toAddress); System.out.println("Email was sent"); pinpoint.close(); } public static void sendEmail(PinpointEmailClient pinpointEmailClient, String subject, String senderAddress, String toAddress) { try { Content content = Content.builder() .data(body) .build(); Body messageBody = Body.builder() .text(content) .build(); Message message = Message.builder() .body(messageBody) .subject(Content.builder().data(subject).build()) .build(); Destination destination = Destination.builder() .toAddresses(toAddress) .build(); EmailContent emailContent = EmailContent.builder() .simple(message) .build(); SendEmailRequest sendEmailRequest = SendEmailRequest.builder() .fromEmailAddress(senderAddress) .destination(destination) .content(emailContent) .build(); pinpointEmailClient.sendEmail(sendEmailRequest); System.out.println("Message Sent"); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

Invio di un messaggio e-mail con valori CC.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import software.amazon.awssdk.services.pinpointemail.PinpointEmailClient; import software.amazon.awssdk.services.pinpointemail.model.Body; import software.amazon.awssdk.services.pinpointemail.model.Content; import software.amazon.awssdk.services.pinpointemail.model.Destination; import software.amazon.awssdk.services.pinpointemail.model.EmailContent; import software.amazon.awssdk.services.pinpointemail.model.Message; import software.amazon.awssdk.services.pinpointemail.model.SendEmailRequest; import java.util.ArrayList; /** * 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 SendEmailMessageCC { // The body of the email. static final String body = """ Amazon Pinpoint test (AWS SDK for Java 2.x) This email was sent through the Amazon Pinpoint Email API using the AWS SDK for Java 2.x """; public static void main(String[] args) { final String usage = """ Usage: <subject> <senderAddress> <toAddress> <ccAddress> Where: subject - The email subject to use. senderAddress - The from address. This address has to be verified in Amazon Pinpoint in the region you're using to send email\s toAddress - The to address. This address has to be verified in Amazon Pinpoint in the region you're using to send email\s ccAddress - The CC address. """; if (args.length != 4) { System.out.println(usage); System.exit(1); } String subject = args[0]; String senderAddress = args[1]; String toAddress = args[2]; String ccAddress = args[3]; System.out.println("Sending a message"); PinpointEmailClient pinpoint = PinpointEmailClient.builder() .region(Region.US_EAST_1) .build(); ArrayList<String> ccList = new ArrayList<>(); ccList.add(ccAddress); sendEmail(pinpoint, subject, senderAddress, toAddress, ccList); pinpoint.close(); } public static void sendEmail(PinpointEmailClient pinpointEmailClient, String subject, String senderAddress, String toAddress, ArrayList<String> ccAddresses) { try { Content content = Content.builder() .data(body) .build(); Body messageBody = Body.builder() .text(content) .build(); Message message = Message.builder() .body(messageBody) .subject(Content.builder().data(subject).build()) .build(); Destination destination = Destination.builder() .toAddresses(toAddress) .ccAddresses(ccAddresses) .build(); EmailContent emailContent = EmailContent.builder() .simple(message) .build(); SendEmailRequest sendEmailRequest = SendEmailRequest.builder() .fromEmailAddress(senderAddress) .destination(destination) .content(emailContent) .build(); pinpointEmailClient.sendEmail(sendEmailRequest); System.out.println("Message Sent"); } catch (PinpointException e) { // Handle exception e.printStackTrace(); } } }

Invia un messaggio SMS.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.DirectMessageConfiguration; import software.amazon.awssdk.services.pinpoint.model.SMSMessage; import software.amazon.awssdk.services.pinpoint.model.AddressConfiguration; import software.amazon.awssdk.services.pinpoint.model.ChannelType; import software.amazon.awssdk.services.pinpoint.model.MessageRequest; import software.amazon.awssdk.services.pinpoint.model.SendMessagesRequest; import software.amazon.awssdk.services.pinpoint.model.SendMessagesResponse; import software.amazon.awssdk.services.pinpoint.model.MessageResponse; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import java.util.HashMap; 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 SendMessage { // The type of SMS message that you want to send. If you plan to send // time-sensitive content, specify TRANSACTIONAL. If you plan to send // marketing-related content, specify PROMOTIONAL. public static String messageType = "TRANSACTIONAL"; // The registered keyword associated with the originating short code. public static String registeredKeyword = "myKeyword"; // The sender ID to use when sending the message. Support for sender ID // varies by country or region. For more information, see // https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-countries.html public static String senderId = "MySenderID"; public static void main(String[] args) { final String usage = """ Usage: <message> <appId> <originationNumber> <destinationNumber>\s Where: message - The body of the message to send. appId - The Amazon Pinpoint project/application ID to use when you send this message. 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 != 4) { System.out.println(usage); System.exit(1); } String message = args[0]; String appId = args[1]; String originationNumber = args[2]; String destinationNumber = args[3]; System.out.println("Sending a message"); PinpointClient pinpoint = PinpointClient.builder() .region(Region.US_EAST_1) .build(); sendSMSMessage(pinpoint, message, appId, originationNumber, destinationNumber); pinpoint.close(); } public static void sendSMSMessage(PinpointClient pinpoint, String message, String appId, String originationNumber, String destinationNumber) { try { Map<String, AddressConfiguration> addressMap = new HashMap<String, AddressConfiguration>(); AddressConfiguration addConfig = AddressConfiguration.builder() .channelType(ChannelType.SMS) .build(); addressMap.put(destinationNumber, addConfig); SMSMessage smsMessage = SMSMessage.builder() .body(message) .messageType(messageType) .originationNumber(originationNumber) .senderId(senderId) .keyword(registeredKeyword) .build(); // Create a DirectMessageConfiguration object. DirectMessageConfiguration direct = DirectMessageConfiguration.builder() .smsMessage(smsMessage) .build(); MessageRequest msgReq = MessageRequest.builder() .addresses(addressMap) .messageConfiguration(direct) .build(); // create a SendMessagesRequest object SendMessagesRequest request = SendMessagesRequest.builder() .applicationId(appId) .messageRequest(msgReq) .build(); SendMessagesResponse response = pinpoint.sendMessages(request); MessageResponse msg1 = response.messageResponse(); Map map1 = msg1.result(); // Write out the result of sendMessage. map1.forEach((k, v) -> System.out.println((k + ":" + v))); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }

Invia messaggi SMS in batch.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.pinpoint.PinpointClient; import software.amazon.awssdk.services.pinpoint.model.DirectMessageConfiguration; import software.amazon.awssdk.services.pinpoint.model.SMSMessage; import software.amazon.awssdk.services.pinpoint.model.AddressConfiguration; import software.amazon.awssdk.services.pinpoint.model.ChannelType; import software.amazon.awssdk.services.pinpoint.model.MessageRequest; import software.amazon.awssdk.services.pinpoint.model.SendMessagesRequest; import software.amazon.awssdk.services.pinpoint.model.SendMessagesResponse; import software.amazon.awssdk.services.pinpoint.model.MessageResponse; import software.amazon.awssdk.services.pinpoint.model.PinpointException; import java.util.HashMap; 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 SendMessageBatch { // The type of SMS message that you want to send. If you plan to send // time-sensitive content, specify TRANSACTIONAL. If you plan to send // marketing-related content, specify PROMOTIONAL. public static String messageType = "TRANSACTIONAL"; // The registered keyword associated with the originating short code. public static String registeredKeyword = "myKeyword"; // The sender ID to use when sending the message. Support for sender ID // varies by country or region. For more information, see // https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-countries.html public static String senderId = "MySenderID"; public static void main(String[] args) { final String usage = """ Usage: <message> <appId> <originationNumber> <destinationNumber> <destinationNumber1>\s Where: message - The body of the message to send. appId - The Amazon Pinpoint project/application ID to use when you send this message. 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). destinationNumber1 - The second 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 != 5) { System.out.println(usage); System.exit(1); } String message = args[0]; String appId = args[1]; String originationNumber = args[2]; String destinationNumber = args[3]; String destinationNumber1 = args[4]; System.out.println("Sending a message"); PinpointClient pinpoint = PinpointClient.builder() .region(Region.US_EAST_1) .build(); sendSMSMessage(pinpoint, message, appId, originationNumber, destinationNumber, destinationNumber1); pinpoint.close(); } public static void sendSMSMessage(PinpointClient pinpoint, String message, String appId, String originationNumber, String destinationNumber, String destinationNumber1) { try { Map<String, AddressConfiguration> addressMap = new HashMap<String, AddressConfiguration>(); AddressConfiguration addConfig = AddressConfiguration.builder() .channelType(ChannelType.SMS) .build(); // Add an entry to the Map object for each number to whom you want to send a // message. addressMap.put(destinationNumber, addConfig); addressMap.put(destinationNumber1, addConfig); SMSMessage smsMessage = SMSMessage.builder() .body(message) .messageType(messageType) .originationNumber(originationNumber) .senderId(senderId) .keyword(registeredKeyword) .build(); // Create a DirectMessageConfiguration object. DirectMessageConfiguration direct = DirectMessageConfiguration.builder() .smsMessage(smsMessage) .build(); MessageRequest msgReq = MessageRequest.builder() .addresses(addressMap) .messageConfiguration(direct) .build(); // Create a SendMessagesRequest object. SendMessagesRequest request = SendMessagesRequest.builder() .applicationId(appId) .messageRequest(msgReq) .build(); SendMessagesResponse response = pinpoint.sendMessages(request); MessageResponse msg1 = response.messageResponse(); Map map1 = msg1.result(); // Write out the result of sendMessage. map1.forEach((k, v) -> System.out.println((k + ":" + v))); } catch (PinpointException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
  • Per i dettagli sull'API, consulta la SendMessagessezione AWS SDK for Java 2.xAPI Reference.

JavaScript
SDK per JavaScript (v3)
Nota

C'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l’esecuzione nel Repository di esempi di codice AWS.

Creare il client in un modulo separato ed esportarlo.

import { PinpointClient } from "@aws-sdk/client-pinpoint"; // Set the AWS Region. const REGION = "us-east-1"; //Set the MediaConvert Service Object const pinClient = new PinpointClient({ region: REGION }); export { pinClient };

Invia un messaggio e-mail.

// Import required AWS SDK clients and commands for Node.js import { SendMessagesCommand } from "@aws-sdk/client-pinpoint"; import { pinClient } from "./libs/pinClient.js"; // The FromAddress must be verified in SES. const fromAddress = "FROM_ADDRESS"; const toAddress = "TO_ADDRESS"; const projectId = "PINPOINT_PROJECT_ID"; // The subject line of the email. var subject = "Amazon Pinpoint Test (AWS SDK for JavaScript in Node.js)"; // The email body for recipients with non-HTML email clients. var body_text = `Amazon Pinpoint Test (SDK for JavaScript in Node.js) ---------------------------------------------------- This email was sent with Amazon Pinpoint using the AWS SDK for JavaScript in Node.js. For more information, see https://aws.amazon.com/sdk-for-node-js/`; // The body of the email for recipients whose email clients support HTML content. var body_html = `<html> <head></head> <body> <h1>Amazon Pinpoint Test (SDK for JavaScript in Node.js)</h1> <p>This email was sent with <a href='https://aws.amazon.com/pinpoint/'>the Amazon Pinpoint Email API</a> using the <a href='https://aws.amazon.com/sdk-for-node-js/'> AWS SDK for JavaScript in Node.js</a>.</p> </body> </html>`; // The character encoding for the subject line and message body of the email. var charset = "UTF-8"; const params = { ApplicationId: projectId, MessageRequest: { Addresses: { [toAddress]: { ChannelType: "EMAIL", }, }, MessageConfiguration: { EmailMessage: { FromAddress: fromAddress, SimpleEmail: { Subject: { Charset: charset, Data: subject, }, HtmlPart: { Charset: charset, Data: body_html, }, TextPart: { Charset: charset, Data: body_text, }, }, }, }, }, }; const run = async () => { try { const data = await pinClient.send(new SendMessagesCommand(params)); const { MessageResponse: { Result }, } = data; const recipientResult = Result[toAddress]; if (recipientResult.StatusCode !== 200) { throw new Error(recipientResult.StatusMessage); } else { console.log(recipientResult.MessageId); } } catch (err) { console.log(err.message); } }; run();

Invia un messaggio SMS.

// Import required AWS SDK clients and commands for Node.js import { SendMessagesCommand } from "@aws-sdk/client-pinpoint"; import { pinClient } from "./libs/pinClient.js"; ("use strict"); /* The phone number or short code to send the message from. 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. */ const originationNumber = "SENDER_NUMBER"; //e.g., +1XXXXXXXXXX // The recipient's phone number. For best results, you should specify the phone number in E.164 format. const destinationNumber = "RECEIVER_NUMBER"; //e.g., +1XXXXXXXXXX // The content of the SMS message. const message = "This message was sent through Amazon Pinpoint " + "using the AWS SDK for JavaScript in Node.js. Reply STOP to " + "opt out."; /*The Amazon Pinpoint project/application ID to use when you send this message. Make sure that the SMS channel is enabled for the project or application that you choose.*/ const projectId = "PINPOINT_PROJECT_ID"; //e.g., XXXXXXXX66e4e9986478cXXXXXXXXX /* The type of SMS message that you want to send. If you plan to send time-sensitive content, specify TRANSACTIONAL. If you plan to send marketing-related content, specify PROMOTIONAL.*/ var messageType = "TRANSACTIONAL"; // The registered keyword associated with the originating short code. var registeredKeyword = "myKeyword"; /* The sender ID to use when sending the message. Support for sender ID // varies by country or region. For more information, see https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-countries.html.*/ var senderId = "MySenderID"; // Specify the parameters to pass to the API. var params = { ApplicationId: projectId, MessageRequest: { Addresses: { [destinationNumber]: { ChannelType: "SMS", }, }, MessageConfiguration: { SMSMessage: { Body: message, Keyword: registeredKeyword, MessageType: messageType, OriginationNumber: originationNumber, SenderId: senderId, }, }, }, }; const run = async () => { try { const data = await pinClient.send(new SendMessagesCommand(params)); return data; // For unit tests. console.log( "Message sent! " + data["MessageResponse"]["Result"][destinationNumber]["StatusMessage"] ); } catch (err) { console.log(err); } }; run();
  • Per i dettagli sull'API, consulta la SendMessagessezione AWS SDK for JavaScriptAPI Reference.

SDK per JavaScript (v2)
Nota

C'è altro da fare. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

Invia un messaggio e-mail.

"use strict"; const AWS = require("aws-sdk"); // The AWS Region that you want to use to send the email. For a list of // AWS Regions where the Amazon Pinpoint API is available, see // https://docs.aws.amazon.com/pinpoint/latest/apireference/ const aws_region = "us-west-2"; // The "From" address. This address has to be verified in Amazon Pinpoint // in the region that you use to send email. const senderAddress = "sender@example.com"; // The address on the "To" line. If your Amazon Pinpoint account is in // the sandbox, this address also has to be verified. var toAddress = "recipient@example.com"; // The Amazon Pinpoint project/application ID to use when you send this message. // Make sure that the SMS channel is enabled for the project or application // that you choose. const appId = "ce796be37f32f178af652b26eexample"; // The subject line of the email. var subject = "Amazon Pinpoint (AWS SDK for JavaScript in Node.js)"; // The email body for recipients with non-HTML email clients. var body_text = `Amazon Pinpoint Test (SDK for JavaScript in Node.js) ---------------------------------------------------- This email was sent with Amazon Pinpoint using the AWS SDK for JavaScript in Node.js. For more information, see https:\/\/aws.amazon.com/sdk-for-node-js/`; // The body of the email for recipients whose email clients support HTML content. var body_html = `<html> <head></head> <body> <h1>Amazon Pinpoint Test (SDK for JavaScript in Node.js)</h1> <p>This email was sent with <a href='https://aws.amazon.com/pinpoint/'>the Amazon Pinpoint API</a> using the <a href='https://aws.amazon.com/sdk-for-node-js/'> AWS SDK for JavaScript in Node.js</a>.</p> </body> </html>`; // The character encoding the you want to use for the subject line and // message body of the email. var charset = "UTF-8"; // Specify that you're using a shared credentials file. 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 pinpoint = new AWS.Pinpoint(); // Specify the parameters to pass to the API. var params = { ApplicationId: appId, MessageRequest: { Addresses: { [toAddress]: { ChannelType: "EMAIL", }, }, MessageConfiguration: { EmailMessage: { FromAddress: senderAddress, SimpleEmail: { Subject: { Charset: charset, Data: subject, }, HtmlPart: { Charset: charset, Data: body_html, }, TextPart: { Charset: charset, Data: body_text, }, }, }, }, }, }; //Try to send the email. pinpoint.sendMessages(params, function (err, data) { // If something goes wrong, print an error message. if (err) { console.log(err.message); } else { console.log( "Email sent! Message ID: ", data["MessageResponse"]["Result"][toAddress]["MessageId"] ); } });

Invia un messaggio SMS.

"use strict"; var AWS = require("aws-sdk"); // The AWS Region that you want to use to send the message. For a list of // AWS Regions where the Amazon Pinpoint API is available, see // https://docs.aws.amazon.com/pinpoint/latest/apireference/. var aws_region = "us-east-1"; // The phone number or short code to send the message from. 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. var originationNumber = "+12065550199"; // The recipient's phone number. For best results, you should specify the // phone number in E.164 format. var destinationNumber = "+14255550142"; // The content of the SMS message. var message = "This message was sent through Amazon Pinpoint " + "using the AWS SDK for JavaScript in Node.js. Reply STOP to " + "opt out."; // The Amazon Pinpoint project/application ID to use when you send this message. // Make sure that the SMS channel is enabled for the project or application // that you choose. var applicationId = "ce796be37f32f178af652b26eexample"; // The type of SMS message that you want to send. If you plan to send // time-sensitive content, specify TRANSACTIONAL. If you plan to send // marketing-related content, specify PROMOTIONAL. var messageType = "TRANSACTIONAL"; // The registered keyword associated with the originating short code. var registeredKeyword = "myKeyword"; // The sender ID to use when sending the message. Support for sender ID // varies by country or region. For more information, see // https://docs.aws.amazon.com/pinpoint/latest/userguide/channels-sms-countries.html var senderId = "MySenderID"; // 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 pinpoint = new AWS.Pinpoint(); // Specify the parameters to pass to the API. var params = { ApplicationId: applicationId, MessageRequest: { Addresses: { [destinationNumber]: { ChannelType: "SMS", }, }, MessageConfiguration: { SMSMessage: { Body: message, Keyword: registeredKeyword, MessageType: messageType, OriginationNumber: originationNumber, SenderId: senderId, }, }, }, }; //Try to send the message. pinpoint.sendMessages(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! " + data["MessageResponse"]["Result"][destinationNumber]["StatusMessage"] ); } });
  • Per i dettagli sull'API, consulta la SendMessagessezione AWS SDK for JavaScriptAPI Reference.

Kotlin
SDK per Kotlin
Nota

C'è altro su GitHub. Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

/** Before running this Kotlin 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-kotlin/latest/developer-guide/setup.html */ val body: String = """ Amazon Pinpoint test (AWS SDK for Kotlin) This email was sent through the Amazon Pinpoint Email API using the AWS SDK for Kotlin. """.trimIndent() suspend fun main(args: Array<String>) { val usage = """ Usage: <subject> <appId> <senderAddress> <toAddress> Where: subject - The email subject to use. senderAddress - The from address. This address has to be verified in Amazon Pinpoint in the region you're using to send email toAddress - The to address. This address has to be verified in Amazon Pinpoint in the region you're using to send email """ if (args.size != 3) { println(usage) exitProcess(0) } val subject = args[0] val senderAddress = args[1] val toAddress = args[2] sendEmail(subject, senderAddress, toAddress) } suspend fun sendEmail(subjectVal: String?, senderAddress: String, toAddressVal: String) { var content = Content { data = body } val messageBody = Body { text = content } val subContent = Content { data = subjectVal } val message = Message { body = messageBody subject = subContent } val destinationOb = Destination { toAddresses = listOf(toAddressVal) } val emailContent = EmailContent { simple = message } val sendEmailRequest = SendEmailRequest { fromEmailAddress = senderAddress destination = destinationOb this.content = emailContent } PinpointEmailClient { region = "us-east-1" }.use { pinpointemail -> pinpointemail.sendEmail(sendEmailRequest) println("Message Sent") } }
  • Per i dettagli sull'API, SendMessagesconsulta AWSSDK for Kotlin API reference.

Python
SDK per Python (Boto3)
Nota

C'è altro su. GitHub Trova l'esempio completo e scopri di più sulla configurazione e l'esecuzione nel Repository di esempi di codice AWS.

Invia un messaggio e-mail.

import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def send_email_message( pinpoint_client, app_id, sender, to_addresses, char_set, subject, html_message, text_message, ): """ Sends an email message with HTML and plain text versions. :param pinpoint_client: A Boto3 Pinpoint client. :param app_id: The Amazon Pinpoint project ID to use when you send this message. :param sender: The "From" address. This address must be verified in Amazon Pinpoint in the AWS Region you're using to send email. :param to_addresses: The addresses on the "To" line. If your Amazon Pinpoint account is in the sandbox, these addresses must be verified. :param char_set: The character encoding to use for the subject line and message body of the email. :param subject: The subject line of the email. :param html_message: The body of the email for recipients whose email clients can display HTML content. :param text_message: The body of the email for recipients whose email clients don't support HTML content. :return: A dict of to_addresses and their message IDs. """ try: response = pinpoint_client.send_messages( ApplicationId=app_id, MessageRequest={ "Addresses": { to_address: {"ChannelType": "EMAIL"} for to_address in to_addresses }, "MessageConfiguration": { "EmailMessage": { "FromAddress": sender, "SimpleEmail": { "Subject": {"Charset": char_set, "Data": subject}, "HtmlPart": {"Charset": char_set, "Data": html_message}, "TextPart": {"Charset": char_set, "Data": text_message}, }, } }, }, ) except ClientError: logger.exception("Couldn't send email.") raise else: return { to_address: message["MessageId"] for to_address, message in response["MessageResponse"]["Result"].items() } def main(): app_id = "ce796be37f32f178af652b26eexample" sender = "sender@example.com" to_address = "recipient@example.com" char_set = "UTF-8" subject = "Amazon Pinpoint Test (SDK for Python (Boto3))" text_message = """Amazon Pinpoint Test (SDK for Python) ------------------------------------- This email was sent with Amazon Pinpoint using the AWS SDK for Python (Boto3). For more information, see https://aws.amazon.com/sdk-for-python/ """ html_message = """<html> <head></head> <body> <h1>Amazon Pinpoint Test (SDK for Python (Boto3)</h1> <p>This email was sent with <a href='https://aws.amazon.com/pinpoint/'>Amazon Pinpoint</a> using the <a href='https://aws.amazon.com/sdk-for-python/'> AWS SDK for Python (Boto3)</a>.</p> </body> </html> """ print("Sending email.") message_ids = send_email_message( boto3.client("pinpoint"), app_id, sender, [to_address], char_set, subject, html_message, text_message, ) print(f"Message sent! Message IDs: {message_ids}") if __name__ == "__main__": main()

Invia un messaggio SMS.

import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def send_sms_message( pinpoint_client, app_id, origination_number, destination_number, message, message_type, ): """ Sends an SMS message with Amazon Pinpoint. :param pinpoint_client: A Boto3 Pinpoint client. :param app_id: The Amazon Pinpoint project/application ID to use when you send this message. The SMS channel must be enabled for the project or application. :param destination_number: The recipient's phone number in E.164 format. :param origination_number: The phone number to send the message from. This phone number must be associated with your Amazon Pinpoint account and be in E.164 format. :param message: The content of the SMS message. :param message_type: The type of SMS message that you want to send. If you send time-sensitive content, specify TRANSACTIONAL. If you send marketing-related content, specify PROMOTIONAL. :return: The ID of the message. """ try: response = pinpoint_client.send_messages( ApplicationId=app_id, MessageRequest={ "Addresses": {destination_number: {"ChannelType": "SMS"}}, "MessageConfiguration": { "SMSMessage": { "Body": message, "MessageType": message_type, "OriginationNumber": origination_number, } }, }, ) except ClientError: logger.exception("Couldn't send message.") raise else: return response["MessageResponse"]["Result"][destination_number]["MessageId"] def main(): app_id = "ce796be37f32f178af652b26eexample" origination_number = "+12065550199" destination_number = "+14255550142" message = ( "This is a sample message sent from Amazon Pinpoint by using the AWS SDK for " "Python (Boto 3)." ) message_type = "TRANSACTIONAL" print("Sending SMS message.") message_id = send_sms_message( boto3.client("pinpoint"), app_id, origination_number, destination_number, message, message_type, ) print(f"Message sent! Message ID: {message_id}.") if __name__ == "__main__": main()
  • Per i dettagli sull'API, consulta SendMessagesAWSSDK for Python (Boto3) API Reference.