发送 SMS 消息 - Amazon Pinpoint

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

发送 SMS 消息

您可以使用 Amazon Pinpoint API 将短信(文本消息)发送到特定电话号码或端点 ID。本节包含完整的代码示例,您可以使用这些示例通过软件开发工具包通过 Amazon Pinpoint API 发送短信。 AWS 您的帐户必须处于生产状态,并且您拥有可以发送 SMS 消息的有效发起身份。

有关端点、区段和渠道的更多代码示例,请参阅代码示例

C#

参照此示例,使用 AWS SDK for .NET 发送 SMS 消息。此示例假定您已安装和配置 AWS SDK for .NET。有关更多信息,请参阅《AWS SDK for .NET 开发人员指南》中的入门

此示例假定您正在使用共享凭证文件来指定现有 IAM 用户的访问密钥和私密访问密钥。有关更多信息,请参阅《AWS SDK for .NET 开发人员指南》中的配置 AWS 凭证

using Amazon; using Amazon.Pinpoint; using Amazon.Pinpoint.Model; using Microsoft.Extensions.Configuration; namespace SendSmsMessage; public class SendSmsMessageMainClass { public static async Task Main(string[] args) { var configuration = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("settings.json") // Load test settings from .json file. .AddJsonFile("settings.local.json", true) // Optionally load local settings. .Build(); // 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/ string 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. string originationNumber = configuration["OriginationNumber"]!; // The recipient's phone number. For best results, you should specify the // phone number in E.164 format. string destinationNumber = configuration["DestinationNumber"]!; // The 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. string appId = configuration["AppId"]!; // 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. MessageType messageType = MessageType.TRANSACTIONAL; // The registered keyword associated with the originating short code. string? registeredKeyword = configuration["RegisteredKeyword"]; // 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 string? senderId = configuration["SenderId"]; try { var response = await SendSmsMessage(region, appId, destinationNumber, originationNumber, registeredKeyword, senderId, messageType); Console.WriteLine($"Message sent to {response.MessageResponse.Result.Count} recipient(s)."); foreach (var messageResultValue in response.MessageResponse.Result.Select(r => r.Value)) { Console.WriteLine($"{messageResultValue.MessageId} Status: {messageResultValue.DeliveryStatus}"); } } catch (Exception ex) { Console.WriteLine("The message wasn't sent. Error message: " + ex.Message); } } public static async Task<SendMessagesResponse> SendSmsMessage( string region, string appId, string destinationNumber, string originationNumber, string? keyword, string? senderId, MessageType messageType) { // The content of the SMS message. string message = "This message was sent through Amazon Pinpoint using" + " the AWS SDK for .NET. Reply STOP to opt out."; var client = new AmazonPinpointClient(RegionEndpoint.GetBySystemName(region)); SendMessagesRequest sendRequest = new SendMessagesRequest { ApplicationId = appId, MessageRequest = new MessageRequest { Addresses = new Dictionary<string, AddressConfiguration> { { destinationNumber, new AddressConfiguration { ChannelType = ChannelType.SMS } } }, MessageConfiguration = new DirectMessageConfiguration { SMSMessage = new SMSMessage { Body = message, MessageType = MessageType.TRANSACTIONAL, OriginationNumber = originationNumber, SenderId = senderId, Keyword = keyword } } } }; SendMessagesResponse response = await client.SendMessagesAsync(sendRequest); return response; } }
Java

参照此示例,使用 AWS SDK for Java 发送 SMS 消息。此示例假定您已安装和配置该 SDK for Java。有关更多信息,请参阅《AWS SDK for Java 开发人员指南》中的入门

此示例假定您正在使用共享凭证文件来指定现有 IAM 用户的访问密钥和私密访问密钥。有关更多信息,请参阅《AWS SDK for Java 开发人员指南》中的设置默认凭证和区域

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

有关完整的 SDK 示例,请参阅上GitHubSendMessage.java。

JavaScript (Node.js)

使用此示例通过使用 Node.js JavaScript 中的AWS 软件开发工具包发送短信。此示例假设您已经在 Node.js JavaScript 中安装并配置了 SDK。有关更多信息,请参阅 Node.js 开发人员指南 JavaScript 中的 S AWS DK 入门

此示例假定您正在使用共享凭证文件来指定现有 IAM 用户的访问密钥和私密访问密钥。有关更多信息,请参阅 Node.js 开发人员指南中的在开发AWS 工具包 JavaScript 中设置凭证

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

参照此示例,使用 AWS SDK for Python (Boto3) 发送 SMS 消息。此示例假定您已安装和配置该 SDK for Python。有关更多信息,请参阅适用于 Python 的 AWS SDK (Boto3) 快速入门

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

您也可以使用消息模板发送短信,如以下示例所示:

import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) def send_templated_sms_message( pinpoint_client, project_id, destination_number, message_type, origination_number, template_name, template_version, ): """ Sends an SMS message to a specific phone number using a pre-defined template. :param pinpoint_client: A Boto3 Pinpoint client. :param project_id: An Amazon Pinpoint project (application) ID. :param destination_number: The phone number to send the message to. :param message_type: The type of SMS message (promotional or transactional). :param origination_number: The phone number that the message is sent from. :param template_name: The name of the SMS template to use when sending the message. :param template_version: The version number of the message template. :return The ID of the message. """ try: response = pinpoint_client.send_messages( ApplicationId=project_id, MessageRequest={ "Addresses": {destination_number: {"ChannelType": "SMS"}}, "MessageConfiguration": { "SMSMessage": { "MessageType": message_type, "OriginationNumber": origination_number, } }, "TemplateConfiguration": { "SMSTemplate": {"Name": template_name, "Version": template_version} }, }, ) except ClientError: logger.exception("Couldn't send message.") raise else: return response["MessageResponse"]["Result"][destination_number]["MessageId"] def main(): region = "us-east-1" origination_number = "+18555550001" destination_number = "+14255550142" project_id = "7353f53e6885409fa32d07cedexample" message_type = "TRANSACTIONAL" template_name = "My_SMS_Template" template_version = "1" message_id = send_templated_sms_message( boto3.client("pinpoint", region_name=region), project_id, destination_number, message_type, origination_number, template_name, template_version, ) print(f"Message sent! Message ID: {message_id}.") if __name__ == "__main__": main()

此示例假定您正在使用共享凭证文件来指定现有 IAM 用户的访问密钥和秘密访问密钥。有关更多信息,请参阅《AWS SDK for Python (Boto3) API 参考》中的凭证