Lua Requires Chilkat v11.0.0+
Lua
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 Lua Downloads
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 firstPhone = "FIRST_PHONE_NUMBER"
local secondPhone = "SECOND_PHONE_NUMBER"
local body = chilkat.JsonObject.new()
body:update_string("messages[0].channel", "sms")
body:update_string("messages[0].to", firstPhone)
body:update_string("messages[0].content", "Your appointment with Lee is at 10:00.")
body:update_string("messages[0].clientMessageId", "appt-1042")
body:update_string("messages[1].channel", "sms")
body:update_string("messages[1].to", secondPhone)
body:update_string("messages[1].content", "Your appointment with Sam is at 14:30.")
body:update_string("messages[1].clientMessageId", "appt-1043")
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) and (resp.status_code ~= 207)) 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
-- Look up each submitted appointment in the reply using its unique recipient.
-- This works even if the result order changes or clientMessageId is absent.
local results = chilkat.JsonObject.new()
local i = 0
while i < 2 do
body.i = i
local appointment = body:string_of("messages[i].clientMessageId")
local phone = body:string_of("messages[i].to")
local result = chilkat.JsonObject.new()
local found = reply:find_record2("messages", "to", phone, true, result)
results.i = i
results:update_string("appointments[i].clientMessageId", appointment)
if (not found) or (result:json_type_of("accepted") ~= 5) then
results:update_string("appointments[i].outcome", "unknown")
print(appointment .. " has no confirmed result.")
elseif result:bool_of("accepted") then
results:update_string("appointments[i].outcome", "accepted")
if result:json_type_of("apiMessageId") == 1 then
results:update_string("appointments[i].apiMessageId", result:string_of("apiMessageId"))
end
print(appointment .. " accepted.")
else
results:update_string("appointments[i].outcome", "rejected")
results:update_string("appointments[i].reason", result:string_of("error.description"))
print(appointment .. " rejected: " .. result:string_of("error.description"))
end
i = i + 1
end
-- This demo writes a snapshot. In an application, update the two appointment records.
local saved = results:write_file("appointment-send-results.json")
if not saved then
print("Could not save the results. Reconcile the submitted messages before retrying.")
return
end
print("Saved appointment-send-results.json")