Lua
Lua
Clickatell Send SMS Text Message
See more Clickatell Examples
Demonstrate how to send a Clickatell SMS text message.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
local http = chilkat.Http.new()
-- Implements the following CURL command:
-- curl -i \
-- -X POST \
-- -H "Content-Type: application/json" \
-- -H "Accept: application/json" \
-- -H "Authorization: API_KEY" \
-- -d '{"content": "Test Message Text", "to": ["PHONE_NUMBER"], "from": "FROM_PHONE_NUMBER" }' \
-- -s https://platform.clickatell.com/messages
-- Use this online tool to generate code from sample JSON:
-- Generate Code to Create JSON
-- The following JSON is sent in the request body.
-- {
-- "content": "Test Message Text",
-- "to": [
-- "PHONE_NUMBER"
-- ]
-- }
local json = chilkat.JsonObject.new()
json:update_string("content", "Test Message Text")
json:update_string("to[0]", "PHONE_NUMBER")
json:update_string("from", "FROM_PHONE_NUMBER")
http:set_request_header("Authorization", "API_KEY")
http:set_request_header("Accept", "application/json")
http:set_request_header("Content-Type", "application/json")
-- If the following URL does not work, then try "https://platform.clickatell.com/v1/message"
local resp = chilkat.HttpResponse.new()
local success = http:http_json("POST", "https://platform.clickatell.com/messages", json, "application/json", resp)
if not success then
print(http.last_error_text)
return
end
local sbResponseBody = chilkat.StringBuilder.new()
resp:get_body_sb(sbResponseBody)
local jResp = chilkat.JsonObject.new()
jResp:load_sb(sbResponseBody)
jResp.emit_compact = false
print("Response Body:")
print(jResp:emit())
local respStatusCode = resp.status_code
print("Response Status Code = " .. respStatusCode)
if respStatusCode >= 400 then
print("Response Header:")
print(resp.header)
print("Failed.")
return
end
-- Sample JSON response:
-- (Sample code for parsing the JSON response is shown below)
-- {
-- "messages": [
-- {
-- "apiMessageId": "66c06ac68e5c4afb953ea9328babfde6",
-- "accepted": true,
-- "to": "16302581871",
-- "errorCode": null,
-- "error": null,
-- "errorDescription": null
-- }
-- ],
-- "errorCode": null,
-- "error": null,
-- "errorDescription": null
-- }
-- Sample code for parsing the JSON response...
-- Use the following online tool to generate parsing code from sample JSON:
-- Generate Parsing Code from JSON
local apiMessageId = ""
local accepted = false
local v_to = ""
local errorCode = jResp:string_of("errorCode")
local error_ = jResp:string_of("error")
local errorDescription = jResp:string_of("errorDescription")
local i = 0
local count_i = jResp:size_of_array("messages")
while i < count_i do
jResp.i = i
apiMessageId = jResp:string_of("messages[i].apiMessageId")
accepted = jResp:bool_of("messages[i].accepted")
v_to = jResp:string_of("messages[i].to")
errorCode = jResp:string_of("messages[i].errorCode")
error_ = jResp:string_of("messages[i].error")
errorDescription = jResp:string_of("messages[i].errorDescription")
i = i + 1
end