Sample code for 30+ languages & platforms
Visual FoxPro 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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loHttp
LOCAL loUrl
LOCAL lnCancelMessage
LOCAL loReq
LOCAL loSendTime
LOCAL lnExpectedHttp
LOCAL lcExpectedStatus
LOCAL lnTimeOk
LOCAL loResp
LOCAL loReply
LOCAL lnParsed

lnSuccess = 0

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

loUrl = CreateObject('Chilkat.StringBuilder')
loUrl.Append("https://api.twilio.com/2010-04-01/Accounts/")
loUrl.Append("YOUR_ACCOUNT_SID")
loUrl.Append("/Messages")
*  Set this to true on a later run to cancel the saved message.
lnCancelMessage = 0
loReq = CreateObject('Chilkat.HttpRequest')
loSendTime = CreateObject('Chilkat.CkDateTime')
loReq.HttpVerb = "POST"
loReq.ContentType = "application/x-www-form-urlencoded"
lnExpectedHttp = 201
lcExpectedStatus = "scheduled"
IF (lnCancelMessage <> 1) THEN
    lnTimeOk = loSendTime.SetFromCurrentSystemTime()
    IF (lnTimeOk <> 1) THEN
        ? loSendTime.LastErrorText
        RELEASE loHttp
        RELEASE loUrl
        RELEASE loReq
        RELEASE loSendTime
        CANCEL
    ENDIF

    lnTimeOk = loSendTime.AddSeconds(3600)
    IF (lnTimeOk <> 1) THEN
        ? loSendTime.LastErrorText
        RELEASE loHttp
        RELEASE loUrl
        RELEASE loReq
        RELEASE loSendTime
        CANCEL
    ENDIF

    loReq.AddParam("MessagingServiceSid","YOUR_MESSAGING_SERVICE_SID")
    loReq.AddParam("To","RECIPIENT_E164_NUMBER")
    loReq.AddParam("Body","Scheduled message.")
    loReq.AddParam("ScheduleType","fixed")
    loReq.AddParam("SendAt",loSendTime.GetAsTimestamp(0))
ELSE
    *  Update the individual message. This branch never creates another message.
    loUrl.Append("/")
    loUrl.Append("SAVED_SCHEDULED_MESSAGE_SID")
    loReq.AddParam("Status","canceled")
    lnExpectedHttp = 200
    lcExpectedStatus = "canceled"
ENDIF

loUrl.Append(".json")
loResp = CreateObject('Chilkat.HttpResponse')
loReply = CreateObject('Chilkat.JsonObject')

lnSuccess = loHttp.HttpReq(loUrl.GetAsString(),loReq,loResp)
IF (loResp.StatusCode = 0) THEN
    ? "No HTTP response from Twilio."
    RELEASE loHttp
    RELEASE loUrl
    RELEASE loReq
    RELEASE loSendTime
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

lnParsed = loReply.Load(loResp.BodyStr)
IF ((lnSuccess <> 1) OR (loResp.StatusCode <> lnExpectedHttp)) THEN
    ? "Twilio returned HTTP " + STR(loResp.StatusCode)
    IF ((lnParsed = 1) AND (loReply.JsonTypeOf("code") = 2)) THEN
        ? "Twilio error: " + STR(loReply.IntOf("code")) + "  " + loReply.StringOf("message")
    ENDIF

    RELEASE loHttp
    RELEASE loUrl
    RELEASE loReq
    RELEASE loSendTime
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF (lnParsed <> 1) THEN
    ? "Expected a JSON response from Twilio."
    RELEASE loHttp
    RELEASE loUrl
    RELEASE loReq
    RELEASE loSendTime
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF ((loReply.JsonTypeOf("sid") <> 1) OR (loReply.JsonTypeOf("status") <> 1)) THEN
    ? "The response is missing the message SID or status."
    RELEASE loHttp
    RELEASE loUrl
    RELEASE loReq
    RELEASE loSendTime
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

? "Message SID: " + loReply.StringOf("sid")
? "Current status: " + loReply.StringOf("status")
IF (loReply.StringOfEquals("status",lcExpectedStatus,1) <> 1) THEN
    ? "The requested state was not confirmed. Check this SID before trying again."
    RELEASE loHttp
    RELEASE loUrl
    RELEASE loReq
    RELEASE loSendTime
    RELEASE loResp
    RELEASE loReply
    CANCEL
ENDIF

IF (lnCancelMessage <> 1) THEN
    ? "Save the Message SID to check or cancel the scheduled message."
ENDIF

RELEASE loHttp
RELEASE loUrl
RELEASE loReq
RELEASE loSendTime
RELEASE loResp
RELEASE loReply