Lua Requires Chilkat v11.0.0+
Lua
Schedule or cancel an SMS with Twilio
See more Twilio Examples
Live messaging operation. Choose one action per run. Scheduling requires an SMS-capable sender in a Twilio Messaging Service. Supply its MG SID, a recipient in E.164 format, and a send time from 15 minutes to 35 days in the future. The example uses UTC one hour from now. Twilio will send the scheduled message unless it is canceled in time.
Save the returned Message SID. To cancel later, select the cancel action and supply that SID under the same account. Check the returned status. If the message has already left the scheduled state, fetch its current status.
Chilkat Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Load an API key for the Twilio account from private application configuration.
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.basic_auth = true
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("YOUR_ACCOUNT_SID")
url:append("/Messages")
-- Set this to true on a later run to cancel the saved message.
local cancelMessage = false
local req = chilkat.HttpRequest.new()
local sendTime = chilkat.DateTime.new()
req.http_verb = "POST"
req.content_type = "application/x-www-form-urlencoded"
local expectedHttp = 201
local expectedStatus = "scheduled"
if not cancelMessage then
local timeOk = sendTime:set_from_current_system_time()
if not timeOk then
print(sendTime.last_error_text)
return
end
timeOk = sendTime:add_seconds(3600)
if not timeOk then
print(sendTime.last_error_text)
return
end
req:add_param("MessagingServiceSid", "YOUR_MESSAGING_SERVICE_SID")
req:add_param("To", "RECIPIENT_E164_NUMBER")
req:add_param("Body", "Scheduled message.")
req:add_param("ScheduleType", "fixed")
req:add_param("SendAt", sendTime:get_as_timestamp(false))
else
-- Update the individual message. This branch never creates another message.
url:append("/")
url:append("SAVED_SCHEDULED_MESSAGE_SID")
req:add_param("Status", "canceled")
expectedHttp = 200
expectedStatus = "canceled"
end
url:append(".json")
local resp = chilkat.HttpResponse.new()
local reply = chilkat.JsonObject.new()
local success = http:http_req(url:get_as_string(), req, resp)
if resp.status_code == 0 then
print("No HTTP response from Twilio.")
return
end
local parsed = reply:load(resp.body_str)
if (not success) or (resp.status_code ~= expectedHttp) then
print("Twilio returned HTTP " .. resp.status_code)
if (parsed) and (reply:json_type_of("code") == 2) then
print("Twilio error: " .. reply:int_of("code") .. " " .. reply:string_of("message"))
end
return
end
if not parsed then
print("Expected a JSON response from Twilio.")
return
end
if (reply:json_type_of("sid") ~= 1) or (reply:json_type_of("status") ~= 1) then
print("The response is missing the message SID or status.")
return
end
print("Message SID: " .. reply:string_of("sid"))
print("Current status: " .. reply:string_of("status"))
if not reply:string_of_equals("status", expectedStatus, true) then
print("The requested state was not confirmed. Check this SID before trying again.")
return
end
if not cancelMessage then
print("Save the Message SID to check or cancel the scheduled message.")
end