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

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.

Clickatell API reference

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local http = chilkat.Http.new()
http.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")

local recipient = "PHONE_NUMBER"
local pdf = chilkat.BinData.new()
local loaded = pdf:load_file("invoice.pdf")
if not loaded then
    print("Could not read invoice.pdf.")
    return
end

-- Restrict this customer's invoice to that customer's number.
http:set_url_var("recipient", recipient)
local upload = chilkat.HttpResponse.new()
local uploaded = http:http_bd("POST", "https://platform.clickatell.com/v1/media?fileName=invoice.pdf&to={$recipient}", pdf, "application/pdf", upload)
if (not uploaded) or (upload.status_code ~= 201) then
    print("PDF upload failed. HTTP status " .. upload.status_code)
    return
end

local media = chilkat.JsonObject.new()
local decoded = media:load(upload.body_str)
if (not decoded) or (not media:bool_of("accepted")) then
    print("Upload was not accepted: " .. media:string_of("error.description"))
    return
end

local fileId = media:string_of("fileId")
if (media:json_type_of("fileId") ~= 1) or (fileId == "") then
    print("Upload returned no file ID.")
    return
end

local body = chilkat.JsonObject.new()
body:update_string("messages[0].channel", "whatsapp")
body:update_string("messages[0].to", recipient)
body:update_string("messages[0].media.fileId", fileId)
body:update_string("messages[0].media.contentType", "application/pdf")
body:update_string("messages[0].media.fileName", "invoice.pdf")

local resp = chilkat.HttpResponse.new()
local sent = http:http_json("POST", "https://platform.clickatell.com/v1/message", body, "application/json", resp)
if resp.status_code == 0 then
    print("No HTTP response. Check the message report before resending.")
    return
end

local reply = chilkat.JsonObject.new()
local parsed = reply:load(resp.body_str)
if (not sent) or (resp.status_code ~= 202) then
    print("Send returned HTTP " .. resp.status_code)
    if reply:has_member("error.description") then
        print(reply:string_of("error.description"))
    end

    return
end

if reply:has_member("error.code") then
    if not reply:is_null_of("error.code") then
        print("Submission error " .. reply:string_of("error.code") .. "  " .. reply:string_of("error.description"))
        return
    end
end

-- HTTP acceptance and the recipient result are separate checks.
if (not parsed) or (reply:json_type_of("messages[0].accepted") ~= 5) then
    print("Missing recipient result. Check the message report.")
    return
end

if not reply:bool_of("messages[0].accepted") then
    print("Message rejected: " .. reply:string_of("messages[0].error.code") .. "  " .. reply:string_of("messages[0].error.description"))
    return
end

if reply:json_type_of("messages[0].apiMessageId") ~= 1 then
    print("Accepted without a message ID. Check the message report.")
    return
end

print("Accepted. Message ID: " .. reply:string_of("messages[0].apiMessageId"))