B4X Requires Chilkat v11.0.0+
B4X
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 B4X Downloads
Dim success As Boolean = False
' Assumes Chilkat is unlocked. See Global Unlock Sample.
' Load an API key for the Twilio account from private application configuration.
Dim http As ChilkatHttp
http.Initialize("http")
http.RequireSslCertVerify = True
http.BasicAuth = True
http.Login = "YOUR_API_KEY_SID"
http.Password = "YOUR_API_KEY_SECRET"
Dim url As ChilkatStringBuilder
url.Initialize
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.
Dim cancelMessage As Boolean = False
Dim req As ChilkatHttpRequest
req.Initialize
Dim sendTime As ChilkatDateTime
sendTime.Initialize
req.HttpVerb = "POST"
req.ContentType = "application/x-www-form-urlencoded"
Dim expectedHttp As Int = 201
Dim expectedStatus As String = "scheduled"
If cancelMessage <> True Then
Dim timeOk As Boolean = sendTime.SetFromCurrentSystemTime
If timeOk <> True Then
Log(sendTime.LastErrorText)
Return
End If
timeOk = sendTime.AddSeconds(3600)
If timeOk <> True Then
Log(sendTime.LastErrorText)
Return
End If
req.AddParam("MessagingServiceSid", "YOUR_MESSAGING_SERVICE_SID")
req.AddParam("To", "RECIPIENT_E164_NUMBER")
req.AddParam("Body", "Scheduled message.")
req.AddParam("ScheduleType", "fixed")
req.AddParam("SendAt", sendTime.GetAsTimestamp(False))
Else
' Update the individual message. This branch never creates another message.
url.Append("/")
url.Append("SAVED_SCHEDULED_MESSAGE_SID")
req.AddParam("Status", "canceled")
expectedHttp = 200
expectedStatus = "canceled"
End If
url.Append(".json")
Dim resp As ChilkatHttpResponse
resp.Initialize
Dim reply As ChilkatJsonObject
reply.Initialize
success = http.HttpReq(url.GetAsString, req, resp)
If resp.StatusCode = 0 Then
Log("No HTTP response from Twilio.")
Return
End If
Dim parsed As Boolean = reply.Load(resp.BodyStr)
If (success <> True) Or (resp.StatusCode <> expectedHttp) Then
Log("Twilio returned HTTP " & resp.StatusCode)
If (parsed = True) And (reply.JsonTypeOf("code") = 2) Then
Log("Twilio error: " & reply.IntOf("code") & " " & reply.StringOf("message"))
End If
Return
End If
If parsed <> True Then
Log("Expected a JSON response from Twilio.")
Return
End If
If (reply.JsonTypeOf("sid") <> 1) Or (reply.JsonTypeOf("status") <> 1) Then
Log("The response is missing the message SID or status.")
Return
End If
Log("Message SID: " & reply.StringOf("sid"))
Log("Current status: " & reply.StringOf("status"))
If reply.StringOfEquals("status", expectedStatus, True) <> True Then
Log("The requested state was not confirmed. Check this SID before trying again.")
Return
End If
If cancelMessage <> True Then
Log("Save the Message SID to check or cancel the scheduled message.")
End If