使用 SDK 的 Amazon Pinpoint 示例 JavaScript V3 - Amazon SDK for JavaScript
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

帮助我们改进Amazon SDK for JavaScript版本 3 (V3) 文档,方法是使用反馈链接,或者在上创建议题或拉取请求GitHub.

这些区域有:Amazon SDK for JavaScriptV3 API 参考指南详细描述了Amazon SDK for JavaScript版本 3 (V3)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

使用 SDK 的 Amazon Pinpoint 示例 JavaScript V3

以下代码示例向您展示如何通过使用Amazon SDK for JavaScript使用 Amazon Pinpoint 的 V3。

操作展示如何调用具体 Amazon Pinpoint 函数的具体代码节选。

方案展示如何通过调用多个 Amazon Pinpoint 函数来完成特定任务的代码示例。

每个示例都包含一个指向 GitHub 的链接,其中包含了有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例显示如何通过 Amazon Pinpoint 发送电子邮件和短信。

适用于 JavaScript V3

在单独的模块中创建客户端并将其导出。

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

发送电子邮件。

// 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 address on the "To" line. If your Amazon Pinpoint account is in the sandbox, this address also has to be verified. Note: All recipient addresses in this example are in arrays, which makes it easier to specify multiple recipients. Alternatively, you can make these variables strings, and then modify the To/Cc/BccAddresses attributes in the params variable so that it passes an array for each recipient type.*/ const senderAddress = "SENDER_ADDRESS"; const toAddress = "RECIPIENT_ADDRESS"; const projectId = "PINPOINT_PROJECT_ID"; //e.g., XXXXXXXX66e4e9986478cXXXXXXXXX // CC and BCC addresses. If your account is in the sandbox, these addresses have to be verified. var ccAddresses = ["cc_recipient1@example.com", "cc_recipient2@example.com"]; var bccAddresses = ["bcc_recipient@example.com"]; // The configuration set that you want to use to send the email. var configuration_set = "ConfigSet"; // 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 message tags that you want to apply to the email. var tag0 = { Name: "key0", Value: "value0" }; var tag1 = { Name: "key1", Value: "value1" }; // The character encoding for the subject line and message body of the email. var charset = "UTF-8"; const params = { ApplicationId: projectId, MessageRequest: { Addresses: { Destination: { ToAddresses: toAddress, // CcAddresses: CC_ADDRESSES, // BccAddresses: BCC_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, }, }, }, }, }, }; const run = async () => { try { const data = await pinClient.send(new SendMessagesCommand(params)); return data; // For unit tests. console.log( "Email sent! Message ID: ", data["MessageResponse"]["Result"][toAddress]["MessageId"] ); } catch (err) { console.log("Error", err); } }; run();

发送 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();
  • GitHub 中查找说明和更多代码。

  • 有关详细信息,请参阅。SendMessagesAmazon SDK for JavaScriptAPI 参考.

适用于 JavaScript V2

发送电子邮件。

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

发送 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']); } });
  • GitHub 中查找说明和更多代码。

  • 有关详细信息,请参阅。SendMessagesAmazon SDK for JavaScriptAPI 参考.