Rust
Rust
Send SMS Text Message to Phone Number
See more Amazon SNS Examples
Uses Amazon SNS to send an SMS text message to a phone number.See SNS Publish for more information.
Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let rest = chilkat::Rest::new();
// Connect to the Amazon AWS REST server.
// such as https://sns.us-west-2.amazonaws.com/
let b_tls = true;
let port = 443;
let b_auto_reconnect = true;
let _ = rest.connect("sns.us-west-2.amazonaws.com", port, b_tls, b_auto_reconnect).is_ok();
// Provide AWS credentials for the REST call.
let auth_aws = chilkat::AuthAws::new();
auth_aws.set_access_key("AWS_ACCESS_KEY");
auth_aws.set_secret_key("AWS_SECRET_KEY");
// the region should match our URL above..
auth_aws.set_region("us-west-2");
auth_aws.set_service_name("sns");
let _ = rest.set_auth_aws(&auth_aws);
let _ = rest.add_query_param("Action", "Publish");
// ----------------------------------------------------
// Please use your own phone number here for testing...
// ----------------------------------------------------
let _ = rest.add_query_param("PhoneNumber", "+15551239999");
let _ = rest.add_query_param("Message", "This is a test message from Amazon SNS");
let Ok(response_xml) = rest.full_request_no_body("GET", "/") else {
println!("{}", rest.last_error_text());
return;
};
// A successful response will have a status code equal to 200.
if rest.response_status_code() != 200 {
println!("response status code = {}", rest.response_status_code());
println!("response status text = {}", rest.response_status_text());
println!("response header: {}", rest.response_header());
println!("response body: {}", response_xml);
return;
}
// Examine the successful XML response.
let xml = chilkat::Xml::new();
let _ = xml.load_xml(&response_xml);
println!("{}", xml.get_xml().unwrap_or_default());
// To get the MessageId
println!("MessageId: {}", xml.chilkat_path("PublishResult|MessageId|*").unwrap_or_default());
// A sample successful response:
// <?xml version="1.0" encoding="utf-8"?>
// <PublishResponse xmlns="http://sns.amazonaws.com/doc/2010-03-31/">
// <PublishResult>
// <MessageId>5ffca506-6ff6-502c-bca3-f098971bcb9c</MessageId>
// </PublishResult>
// <ResponseMetadata>
// <RequestId>6c815022-050e-5930-a7b4-05bcfde35280</RequestId>
// </ResponseMetadata>
// </PublishResponse>