Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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.

Twilio documentation

Chilkat Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oHttp
LOCAL oUrl
LOCAL nCancelMessage
LOCAL oReq
LOCAL oSendTime
LOCAL nExpectedHttp
LOCAL cExpectedStatus
LOCAL nTimeOk
LOCAL oResp
LOCAL oReply
LOCAL nParsed

nSuccess := 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Load an API key for the Twilio account from private application configuration.
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
oHttp:BasicAuth := 1
oHttp:Login := "YOUR_API_KEY_SID"
oHttp:Password := "YOUR_API_KEY_SECRET"

oUrl := CreateObject("Chilkat.StringBuilder")
oUrl:Append("https://api.twilio.com/2010-04-01/Accounts/")
oUrl:Append("YOUR_ACCOUNT_SID")
oUrl:Append("/Messages")
//  Set this to true on a later run to cancel the saved message.
nCancelMessage := 0
oReq := CreateObject("Chilkat.HttpRequest")
oSendTime := CreateObject("Chilkat.CkDateTime")
oReq:HttpVerb := "POST"
oReq:ContentType := "application/x-www-form-urlencoded"
nExpectedHttp := 201
cExpectedStatus := "scheduled"
IF (nCancelMessage != 1)
    nTimeOk := oSendTime:SetFromCurrentSystemTime()
    IF (nTimeOk != 1)
        ? oSendTime:LastErrorText
        oHttp:destroy()
        oUrl:destroy()
        oReq:destroy()
        oSendTime:destroy()
        RETURN
    ENDIF

    nTimeOk := oSendTime:AddSeconds(3600)
    IF (nTimeOk != 1)
        ? oSendTime:LastErrorText
        oHttp:destroy()
        oUrl:destroy()
        oReq:destroy()
        oSendTime:destroy()
        RETURN
    ENDIF

    oReq:AddParam("MessagingServiceSid", "YOUR_MESSAGING_SERVICE_SID")
    oReq:AddParam("To", "RECIPIENT_E164_NUMBER")
    oReq:AddParam("Body", "Scheduled message.")
    oReq:AddParam("ScheduleType", "fixed")
    oReq:AddParam("SendAt", oSendTime:GetAsTimestamp(0))
ELSE
    //  Update the individual message. This branch never creates another message.
    oUrl:Append("/")
    oUrl:Append("SAVED_SCHEDULED_MESSAGE_SID")
    oReq:AddParam("Status", "canceled")
    nExpectedHttp := 200
    cExpectedStatus := "canceled"
ENDIF

oUrl:Append(".json")
oResp := CreateObject("Chilkat.HttpResponse")
oReply := CreateObject("Chilkat.JsonObject")

nSuccess := oHttp:HttpReq(oUrl:GetAsString(), oReq, oResp)
IF (oResp:StatusCode == 0)
    ? "No HTTP response from Twilio."
    oHttp:destroy()
    oUrl:destroy()
    oReq:destroy()
    oSendTime:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

nParsed := oReply:Load(oResp:BodyStr)
IF ((nSuccess != 1) .OR. (oResp:StatusCode != nExpectedHttp))
    ? "Twilio returned HTTP " + Str(oResp:StatusCode)
    IF ((nParsed == 1) .AND. (oReply:JsonTypeOf("code") == 2))
        ? "Twilio error: " + Str(oReply:IntOf("code")) + "  " + oReply:StringOf("message")
    ENDIF

    oHttp:destroy()
    oUrl:destroy()
    oReq:destroy()
    oSendTime:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (nParsed != 1)
    ? "Expected a JSON response from Twilio."
    oHttp:destroy()
    oUrl:destroy()
    oReq:destroy()
    oSendTime:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF ((oReply:JsonTypeOf("sid") != 1) .OR. (oReply:JsonTypeOf("status") != 1))
    ? "The response is missing the message SID or status."
    oHttp:destroy()
    oUrl:destroy()
    oReq:destroy()
    oSendTime:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

? "Message SID: " + oReply:StringOf("sid")
? "Current status: " + oReply:StringOf("status")
IF (oReply:StringOfEquals("status", cExpectedStatus, 1) != 1)
    ? "The requested state was not confirmed. Check this SID before trying again."
    oHttp:destroy()
    oUrl:destroy()
    oReq:destroy()
    oSendTime:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (nCancelMessage != 1)
    ? "Save the Message SID to check or cancel the scheduled message."
ENDIF

oHttp:destroy()
oUrl:destroy()
oReq:destroy()
oSendTime:destroy()
oResp:destroy()
oReply:destroy()