PowerBuilder Requires Chilkat v11.0.0+
PowerBuilder
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 PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_Url
long li_CancelMessage
oleobject loo_Req
oleobject loo_SendTime
long li_ExpectedHttp
string ls_ExpectedStatus
long li_TimeOk
oleobject loo_Resp
oleobject loo_Reply
long li_Parsed
li_Success = 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Load an API key for the Twilio account from private application configuration.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
destroy loo_Http
MessageBox("Error","Connecting to COM object failed")
return
end if
loo_Http.RequireSslCertVerify = 1
loo_Http.BasicAuth = 1
loo_Http.Login = "YOUR_API_KEY_SID"
loo_Http.Password = "YOUR_API_KEY_SECRET"
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")
loo_Url.Append("https://api.twilio.com/2010-04-01/Accounts/")
loo_Url.Append("YOUR_ACCOUNT_SID")
loo_Url.Append("/Messages")
// Set this to true on a later run to cancel the saved message.
li_CancelMessage = 0
loo_Req = create oleobject
li_rc = loo_Req.ConnectToNewObject("Chilkat.HttpRequest")
loo_SendTime = create oleobject
li_rc = loo_SendTime.ConnectToNewObject("Chilkat.CkDateTime")
loo_Req.HttpVerb = "POST"
loo_Req.ContentType = "application/x-www-form-urlencoded"
li_ExpectedHttp = 201
ls_ExpectedStatus = "scheduled"
if li_CancelMessage <> 1 then
li_TimeOk = loo_SendTime.SetFromCurrentSystemTime()
if li_TimeOk <> 1 then
Write-Debug loo_SendTime.LastErrorText
destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_SendTime
return
end if
li_TimeOk = loo_SendTime.AddSeconds(3600)
if li_TimeOk <> 1 then
Write-Debug loo_SendTime.LastErrorText
destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_SendTime
return
end if
loo_Req.AddParam("MessagingServiceSid","YOUR_MESSAGING_SERVICE_SID")
loo_Req.AddParam("To","RECIPIENT_E164_NUMBER")
loo_Req.AddParam("Body","Scheduled message.")
loo_Req.AddParam("ScheduleType","fixed")
loo_Req.AddParam("SendAt",loo_SendTime.GetAsTimestamp(0))
else
// Update the individual message. This branch never creates another message.
loo_Url.Append("/")
loo_Url.Append("SAVED_SCHEDULED_MESSAGE_SID")
loo_Req.AddParam("Status","canceled")
li_ExpectedHttp = 200
ls_ExpectedStatus = "canceled"
end if
loo_Url.Append(".json")
loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")
loo_Reply = create oleobject
li_rc = loo_Reply.ConnectToNewObject("Chilkat.JsonObject")
li_Success = loo_Http.HttpReq(loo_Url.GetAsString(),loo_Req,loo_Resp)
if loo_Resp.StatusCode = 0 then
Write-Debug "No HTTP response from Twilio."
destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_SendTime
destroy loo_Resp
destroy loo_Reply
return
end if
li_Parsed = loo_Reply.Load(loo_Resp.BodyStr)
if (li_Success <> 1) OR (loo_Resp.StatusCode <> li_ExpectedHttp) then
Write-Debug "Twilio returned HTTP " + string(loo_Resp.StatusCode)
if (li_Parsed = 1) AND (loo_Reply.JsonTypeOf("code") = 2) then
Write-Debug "Twilio error: " + string(loo_Reply.IntOf("code")) + " " + loo_Reply.StringOf("message")
end if
destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_SendTime
destroy loo_Resp
destroy loo_Reply
return
end if
if li_Parsed <> 1 then
Write-Debug "Expected a JSON response from Twilio."
destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_SendTime
destroy loo_Resp
destroy loo_Reply
return
end if
if (loo_Reply.JsonTypeOf("sid") <> 1) OR (loo_Reply.JsonTypeOf("status") <> 1) then
Write-Debug "The response is missing the message SID or status."
destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_SendTime
destroy loo_Resp
destroy loo_Reply
return
end if
Write-Debug "Message SID: " + loo_Reply.StringOf("sid")
Write-Debug "Current status: " + loo_Reply.StringOf("status")
if loo_Reply.StringOfEquals("status",ls_ExpectedStatus,1) <> 1 then
Write-Debug "The requested state was not confirmed. Check this SID before trying again."
destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_SendTime
destroy loo_Resp
destroy loo_Reply
return
end if
if li_CancelMessage <> 1 then
Write-Debug "Save the Message SID to check or cancel the scheduled message."
end if
destroy loo_Http
destroy loo_Url
destroy loo_Req
destroy loo_SendTime
destroy loo_Resp
destroy loo_Reply