Lua Requires Chilkat v11.0.0+
Lua
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 Lua Downloads
local chilkat = require("chilkat")
local success = false
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.basic_auth = true
-- Load the owning Account SID and an API key from application configuration.
local accountSid = "YOUR_ACCOUNT_SID"
http.login = "YOUR_API_KEY_SID"
http.password = "YOUR_API_KEY_SECRET"
local url = chilkat.StringBuilder.new()
url:append("https://api.twilio.com/2010-04-01/Accounts/")
url:append(accountSid)
url:append("/Messages.json")
url:append("?PageSize=50")
local resp = chilkat.HttpResponse.new()
local reply = chilkat.JsonObject.new()
local total = 0
local more = true
while more do
success = http:http_no_body("GET", url:get_as_string(), resp)
if resp.status_code == 0 then
print(http.last_error_text)
return
end
local parsed = reply:load(resp.body_str)
if (not success) or (resp.status_code ~= 200) then
print("HTTP " .. resp.status_code)
if (parsed) and (reply:has_member("code")) then
print("Twilio error " .. reply:int_of("code") .. ": " .. reply:string_of("message"))
end
return
end
if not parsed then
print("Expected a JSON response.")
return
end
if reply:json_type_of("messages") ~= 4 then
print("Expected a messages array.")
return
end
local count = reply:size_of_array("messages")
local i = 0
while i < count do
reply.i = i
print(reply:string_of("messages[i].sid") .. " " .. reply:string_of("messages[i].direction") .. " " .. reply:string_of("messages[i].status"))
i = i + 1
end
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") then
more = false
else
if reply:json_type_of("next_page_uri") ~= 1 then
print("Expected a next-page URI or null.")
return
end
url:set_string("https://api.twilio.com")
url:append(reply:string_of("next_page_uri"))
end
end
print("Messages found: " .. total)