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à.
Puoi utilizzare l'API Amazon Pinpoint per inviare messaggi SMS (messaggi di testo) a numeri di telefono o endpoint specifici. IDs Questa sezione contiene esempi di codice completi che puoi utilizzare per inviare messaggi SMS tramite l'API Amazon Pinpoint utilizzando un AWS SDK. Il tuo account deve essere in produzione e disporre di un'identità di origine attiva in grado di inviare messaggi SMS.
Per altri esempi di codice su endpoint, segmenti e canali, consulta Esempi di codice.
Utilizza questo esempio per inviare un messaggio SMS tramite l'AWS SDK per .NET
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 IAM esistente. Per ulteriori informazioni, consulta l'argomento relativo alla configurazione delle credenziali AWS nella Guida per gli sviluppatori di AWS SDK per .NET .
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;
}
}