Rust Requires Chilkat v11.0.0+
Rust
List Twilio message history
See more Twilio Examples
List inbound and outbound messages, newest sent date first. Print the Message SID, direction, and status so you can distinguish incoming messages from outbound submissions and see their latest state.
Each response contains a messages array and a next_page_uri. Follow that URI until it is null. PageSize limits one response. Use credentials for the account whose history you want to read.
Chilkat Rust Downloads
let mut success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
let http = chilkat::Http::new();
http.set_require_ssl_cert_verify(true);
http.set_basic_auth(true);
// Load the owning Account SID and an API key from application configuration.
let account_sid = "YOUR_ACCOUNT_SID".to_string();
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(&account_sid);
let _ = url.append("/Messages.json");
let _ = url.append("?PageSize=50");
let resp = chilkat::HttpResponse::new();
let reply = chilkat::JsonObject::new();
let mut total = 0;
let mut more = true;
while more {
success = http.http_no_body("GET", &url.get_as_string().unwrap_or_default(), &resp).is_ok();
if resp.status_code() == 0 {
println!("{}", http.last_error_text());
return;
}
let parsed = reply.load(&resp.body_str()).is_ok();
if (!success) || (resp.status_code() != 200) {
println!("HTTP {}", resp.status_code());
if (parsed) && (reply.has_member("code")) {
println!("Twilio error {}: {}", reply.int_of("code"), reply.string_of("message").unwrap_or_default());
}
return;
}
if !parsed {
println!("Expected a JSON response.");
return;
}
if reply.json_type_of("messages") != 4 {
println!("Expected a messages array.");
return;
}
let count = reply.size_of_array("messages");
let mut i = 0;
while i < count {
reply.set_i(i);
println!("{} {} {}", reply.string_of("messages[i].sid").unwrap_or_default(), reply.string_of("messages[i].direction").unwrap_or_default(), reply.string_of("messages[i].status").unwrap_or_default());
i = i + 1;
}
total = total + count;
// Twilio supplies a relative URI with the cursor and returns null on the last page.
if reply.is_null_of("next_page_uri") {
more = false;
} else {
if reply.json_type_of("next_page_uri") != 1 {
println!("Expected a next-page URI or null.");
return;
}
let _ = url.set_string("https://api.twilio.com");
let _ = url.append(&reply.string_of("next_page_uri").unwrap_or_default());
}
}
println!("Messages found: {}", total);