Rust Requires Chilkat v11.0.0+
Rust
Clickatell One API: Upload and send a WhatsApp PDF
See more Clickatell Examples
Send a local invoice.pdf during the customer’s open WhatsApp service window. Upload the binary PDF for one recipient, then reference the returned fileId in a separate message to the same recipient. The upload and message submission have separate acceptance results.
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 recipient = "PHONE_NUMBER".to_string();
let pdf = chilkat::BinData::new();
if pdf.load_file("invoice.pdf").is_err() {
println!("Could not read invoice.pdf.");
return;
}
// Restrict this customer's invoice to that customer's number.
let _ = http.set_url_var("recipient", &recipient);
let upload = chilkat::HttpResponse::new();
let uploaded = http.http_bd("POST", "https://platform.clickatell.com/v1/media?fileName=invoice.pdf&to={$recipient}", &pdf, "application/pdf", &upload).is_ok();
if (!uploaded) || (upload.status_code() != 201) {
println!("PDF upload failed. HTTP status {}", upload.status_code());
return;
}
let media = chilkat::JsonObject::new();
let decoded = media.load(&upload.body_str()).is_ok();
if (!decoded) || (!media.bool_of("accepted")) {
println!("Upload was not accepted: {}", media.string_of("error.description").unwrap_or_default());
return;
}
let file_id = media.string_of("fileId").unwrap_or_default();
if (media.json_type_of("fileId") != 1) || (file_id == "") {
println!("Upload returned no file ID.");
return;
}
let body = chilkat::JsonObject::new();
let _ = body.update_string("messages[0].channel", "whatsapp");
let _ = body.update_string("messages[0].to", &recipient);
let _ = body.update_string("messages[0].media.fileId", &file_id);
let _ = body.update_string("messages[0].media.contentType", "application/pdf");
let _ = body.update_string("messages[0].media.fileName", "invoice.pdf");
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) {
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;
}
}
// HTTP acceptance and the recipient result are separate checks.
if (!parsed) || (reply.json_type_of("messages[0].accepted") != 5) {
println!("Missing recipient result. Check the message report.");
return;
}
if !reply.bool_of("messages[0].accepted") {
println!("Message rejected: {} {}", reply.string_of("messages[0].error.code").unwrap_or_default(), reply.string_of("messages[0].error.description").unwrap_or_default());
return;
}
if reply.json_type_of("messages[0].apiMessageId") != 1 {
println!("Accepted without a message ID. Check the message report.");
return;
}
println!("Accepted. Message ID: {}", reply.string_of("messages[0].apiMessageId").unwrap_or_default());