Rust Requires Chilkat v11.0.0+
Rust
Clickatell One API: Send personalized SMS reminders in a batch
See more Clickatell Examples
Send two appointments in one request with a different text and application ID for each. Use two distinct recipient numbers in international digits. For a US two-way integration, add its provisioned from number to each message.
Read each recipient result even when HTTP 207 reports a mixed outcome. Save the accepted provider ID against the appointment. A missing result calls for checking the message report before retrying. A client message ID helps correlate records and does not guarantee deduplication.
Chilkat Rust Downloads
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
// Load the key for this One API integration from your application configuration.
http.set_request_header("Authorization", "ONE_API_KEY");
http.set_request_header("Accept", "application/json");
let first_phone = "FIRST_PHONE_NUMBER".to_string();
let second_phone = "SECOND_PHONE_NUMBER".to_string();
let body = chilkat::JsonObject::new();
let _ = body.update_string("messages[0].channel", "sms");
let _ = body.update_string("messages[0].to", &first_phone);
let _ = body.update_string("messages[0].content", "Your appointment with Lee is at 10:00.");
let _ = body.update_string("messages[0].clientMessageId", "appt-1042");
let _ = body.update_string("messages[1].channel", "sms");
let _ = body.update_string("messages[1].to", &second_phone);
let _ = body.update_string("messages[1].content", "Your appointment with Sam is at 14:30.");
let _ = body.update_string("messages[1].clientMessageId", "appt-1043");
let resp = chilkat::HttpResponse::new();
let sent = http.http_json("POST", "https://platform.clickatell.com/v1/message", &body, "application/json", &resp).is_ok();
if resp.status_code() == 0 {
println!("No HTTP response. Check the message report before resending.");
return;
}
let reply = chilkat::JsonObject::new();
let parsed = reply.load(&resp.body_str()).is_ok();
if (!sent) || ((resp.status_code() != 202) && (resp.status_code() != 207)) {
println!("Send returned HTTP {}", resp.status_code());
if reply.has_member("error.description") {
println!("{}", reply.string_of("error.description").unwrap_or_default());
}
return;
}
if reply.has_member("error.code") {
if !reply.is_null_of("error.code") {
println!("Submission error {} {}", reply.string_of("error.code").unwrap_or_default(), reply.string_of("error.description").unwrap_or_default());
return;
}
}
// Look up each submitted appointment in the reply using its unique recipient.
// This works even if the result order changes or clientMessageId is absent.
let results = chilkat::JsonObject::new();
let mut i = 0;
while i < 2 {
body.set_i(i);
let appointment = body.string_of("messages[i].clientMessageId").unwrap_or_default();
let phone = body.string_of("messages[i].to").unwrap_or_default();
let result = chilkat::JsonObject::new();
let found = reply.find_record2("messages", "to", &phone, true, &result).is_ok();
results.set_i(i);
let _ = results.update_string("appointments[i].clientMessageId", &appointment);
if (!found) || (result.json_type_of("accepted") != 5) {
let _ = results.update_string("appointments[i].outcome", "unknown");
println!("{} has no confirmed result.", appointment);
} else {
if result.bool_of("accepted") {
let _ = results.update_string("appointments[i].outcome", "accepted");
if result.json_type_of("apiMessageId") == 1 {
let _ = results.update_string("appointments[i].apiMessageId", &result.string_of("apiMessageId").unwrap_or_default());
}
println!("{} accepted.", appointment);
} else {
let _ = results.update_string("appointments[i].outcome", "rejected");
let _ = results.update_string("appointments[i].reason", &result.string_of("error.description").unwrap_or_default());
println!("{} rejected: {}", appointment, result.string_of("error.description").unwrap_or_default());
}
}
i = i + 1;
}
// This demo writes a snapshot. In an application, update the two appointment records.
if results.write_file("appointment-send-results.json").is_err() {
println!("Could not save the results. Reconcile the submitted messages before retrying.");
return;
}
println!("Saved appointment-send-results.json");