Amazon Pinpoint contoh menggunakan SDK for JavaScript (v3) - AWS SDKContoh Kode

Ada lebih banyak AWS SDK contoh yang tersedia di GitHub repo SDKContoh AWS Dokumen.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Amazon Pinpoint contoh menggunakan SDK for JavaScript (v3)

Contoh kode berikut menunjukkan cara melakukan tindakan dan menerapkan skenario umum dengan menggunakan AWS SDK for JavaScript (v3) dengan Amazon Pinpoint.

Tindakan adalah kutipan kode dari program yang lebih besar dan harus dijalankan dalam konteks. Sementara tindakan menunjukkan cara memanggil fungsi layanan individual, Anda dapat melihat tindakan dalam konteks dalam skenario terkait.

Setiap contoh menyertakan tautan ke kode sumber lengkap, di mana Anda dapat menemukan instruksi tentang cara mengatur dan menjalankan kode dalam konteks.

Tindakan

Contoh kode berikut menunjukkan cara menggunakanSendMessages.

SDKuntuk JavaScript (v3)
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

Buat klien dalam modul terpisah dan ekspor klien tersebut.

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

Kirim pesan email.

// 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. const subject = "Amazon Pinpoint Test (AWS SDK for JavaScript in Node.js)"; // The email body for recipients with non-HTML email clients. const 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. const 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. const 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 { MessageResponse } = await pinClient.send( new SendMessagesCommand(params), ); if (!MessageResponse) { throw new Error("No message response."); } if (!MessageResponse.Result) { throw new Error("No message result."); } const recipientResult = MessageResponse.Result[toAddress]; if (recipientResult.StatusCode !== 200) { throw new Error(recipientResult.StatusMessage); } console.log(recipientResult.MessageId); } catch (err) { console.log(err.message); } }; run();

Kirim SMS pesan.

// Import required AWS SDK clients and commands for Node.js import { SendMessagesCommand } from "@aws-sdk/client-pinpoint"; import { pinClient } from "./libs/pinClient.js"; /* 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.*/ const messageType = "TRANSACTIONAL"; // The registered keyword associated with the originating short code. const 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.*/ const senderId = "MySenderID"; // Specify the parameters to pass to the API. const 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)); console.log( `Message sent! ${data.MessageResponse.Result[destinationNumber].StatusMessage}`, ); } catch (err) { console.log(err); } }; run();
  • Untuk API detailnya, lihat SendMessagesdi AWS SDK for JavaScript APIReferensi.