Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Schedule or cancel an SMS with Twilio

See more Twilio Examples

Live messaging operation. Choose one action per run. Scheduling requires an SMS-capable sender in a Twilio Messaging Service. Supply its MG SID, a recipient in E.164 format, and a send time from 15 minutes to 35 days in the future. The example uses UTC one hour from now. Twilio will send the scheduled message unless it is canceled in time.

Save the returned Message SID. To cancel later, select the cancel action and supply that SID under the same account. Check the returned status. If the message has already left the scheduled state, fetch its current status.

Twilio documentation

Chilkat Rust Downloads

Rust
let mut success = false;

// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Load an API key for the Twilio account from private application configuration.
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
http.set_basic_auth(true);
http.set_login("YOUR_API_KEY_SID");
http.set_password("YOUR_API_KEY_SECRET");

let url = chilkat::StringBuilder::new();
let _ = url.append("https://api.twilio.com/2010-04-01/Accounts/");
let _ = url.append("YOUR_ACCOUNT_SID");
let _ = url.append("/Messages");
// Set this to true on a later run to cancel the saved message.
let cancel_message = false;
let req = chilkat::HttpRequest::new();
let send_time = chilkat::DateTime::new();
req.set_http_verb("POST");
req.set_content_type("application/x-www-form-urlencoded");
let mut expected_http = 201;
let mut expected_status = "scheduled".to_string();
if !cancel_message {
    if send_time.set_from_current_system_time().is_err() {
        println!("{}", send_time.last_error_text());
        return;
    }

    if send_time.add_seconds(3600).is_err() {
        println!("{}", send_time.last_error_text());
        return;
    }

    req.add_param("MessagingServiceSid", "YOUR_MESSAGING_SERVICE_SID");
    req.add_param("To", "RECIPIENT_E164_NUMBER");
    req.add_param("Body", "Scheduled message.");
    req.add_param("ScheduleType", "fixed");
    req.add_param("SendAt", &send_time.get_as_timestamp(false).unwrap_or_default());
} else {
    // Update the individual message. This branch never creates another message.
    let _ = url.append("/");
    let _ = url.append("SAVED_SCHEDULED_MESSAGE_SID");
    req.add_param("Status", "canceled");
    expected_http = 200;
    expected_status = "canceled".to_string();
}

let _ = url.append(".json");
let resp = chilkat::HttpResponse::new();
let reply = chilkat::JsonObject::new();

success = http.http_req(&url.get_as_string().unwrap_or_default(), &req, &resp).is_ok();
if resp.status_code() == 0 {
    println!("No HTTP response from Twilio.");
    return;
}

let parsed = reply.load(&resp.body_str()).is_ok();
if (!success) || (resp.status_code() != expected_http) {
    println!("Twilio returned HTTP {}", resp.status_code());
    if (parsed) && (reply.json_type_of("code") == 2) {
        println!("Twilio error: {}  {}", reply.int_of("code"), reply.string_of("message").unwrap_or_default());
    }

    return;
}

if !parsed {
    println!("Expected a JSON response from Twilio.");
    return;
}

if (reply.json_type_of("sid") != 1) || (reply.json_type_of("status") != 1) {
    println!("The response is missing the message SID or status.");
    return;
}

println!("Message SID: {}", reply.string_of("sid").unwrap_or_default());
println!("Current status: {}", reply.string_of("status").unwrap_or_default());
if !reply.string_of_equals("status", &expected_status, true) {
    println!("The requested state was not confirmed. Check this SID before trying again.");
    return;
}

if !cancel_message {
    println!("Save the Message SID to check or cancel the scheduled message.");
}