DataFlex Requires Chilkat v11.0.0+
DataFlex
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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoHttp
Handle hoUrl
Boolean iCancelMessage
Variant vReq
Handle hoReq
Handle hoSendTime
Integer iExpectedHttp
String sExpectedStatus
Boolean iTimeOk
Variant vResp
Handle hoResp
Handle hoReply
Boolean iParsed
String sTemp1
Integer iTemp1
Integer iTemp2
Boolean bTemp1
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Load an API key for the Twilio account from private application configuration.
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Set ComBasicAuth Of hoHttp To True
Set ComLogin Of hoHttp To "YOUR_API_KEY_SID"
Set ComPassword Of hoHttp To "YOUR_API_KEY_SECRET"
Get Create (RefClass(cComChilkatStringBuilder)) To hoUrl
If (Not(IsComObjectCreated(hoUrl))) Begin
Send CreateComObject of hoUrl
End
Get ComAppend Of hoUrl "https://api.twilio.com/2010-04-01/Accounts/" To iSuccess
Get ComAppend Of hoUrl "YOUR_ACCOUNT_SID" To iSuccess
Get ComAppend Of hoUrl "/Messages" To iSuccess
// Set this to true on a later run to cancel the saved message.
Move False To iCancelMessage
Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
If (Not(IsComObjectCreated(hoReq))) Begin
Send CreateComObject of hoReq
End
Get Create (RefClass(cComCkDateTime)) To hoSendTime
If (Not(IsComObjectCreated(hoSendTime))) Begin
Send CreateComObject of hoSendTime
End
Set ComHttpVerb Of hoReq To "POST"
Set ComContentType Of hoReq To "application/x-www-form-urlencoded"
Move 201 To iExpectedHttp
Move "scheduled" To sExpectedStatus
If (iCancelMessage <> True) Begin
Get ComSetFromCurrentSystemTime Of hoSendTime To iTimeOk
If (iTimeOk <> True) Begin
Get ComLastErrorText Of hoSendTime To sTemp1
Showln sTemp1
Procedure_Return
End
Get ComAddSeconds Of hoSendTime 3600 To iTimeOk
If (iTimeOk <> True) Begin
Get ComLastErrorText Of hoSendTime To sTemp1
Showln sTemp1
Procedure_Return
End
Send ComAddParam To hoReq "MessagingServiceSid" "YOUR_MESSAGING_SERVICE_SID"
Send ComAddParam To hoReq "To" "RECIPIENT_E164_NUMBER"
Send ComAddParam To hoReq "Body" "Scheduled message."
Send ComAddParam To hoReq "ScheduleType" "fixed"
Get ComGetAsTimestamp Of hoSendTime False To sTemp1
Send ComAddParam To hoReq "SendAt" sTemp1
End
Else Begin
// Update the individual message. This branch never creates another message.
Get ComAppend Of hoUrl "/" To iSuccess
Get ComAppend Of hoUrl "SAVED_SCHEDULED_MESSAGE_SID" To iSuccess
Send ComAddParam To hoReq "Status" "canceled"
Move 200 To iExpectedHttp
Move "canceled" To sExpectedStatus
End
Get ComAppend Of hoUrl ".json" To iSuccess
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get Create (RefClass(cComChilkatJsonObject)) To hoReply
If (Not(IsComObjectCreated(hoReply))) Begin
Send CreateComObject of hoReply
End
Get ComGetAsString Of hoUrl To sTemp1
Get pvComObject of hoReq to vReq
Get pvComObject of hoResp to vResp
Get ComHttpReq Of hoHttp sTemp1 vReq vResp To iSuccess
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 0) Begin
Showln "No HTTP response from Twilio."
Procedure_Return
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoReply sTemp1 To iParsed
Get ComStatusCode Of hoResp To iTemp1
If ((iSuccess <> True) Or (iTemp1 <> iExpectedHttp)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Twilio returned HTTP " iTemp1
Get ComJsonTypeOf Of hoReply "code" To iTemp1
If ((iParsed = True) And (iTemp1 = 2)) Begin
Get ComIntOf Of hoReply "code" To iTemp1
Get ComStringOf Of hoReply "message" To sTemp1
Showln "Twilio error: " iTemp1 " " sTemp1
End
Procedure_Return
End
If (iParsed <> True) Begin
Showln "Expected a JSON response from Twilio."
Procedure_Return
End
Get ComJsonTypeOf Of hoReply "sid" To iTemp1
Get ComJsonTypeOf Of hoReply "status" To iTemp2
If ((iTemp1 <> 1) Or (iTemp2 <> 1)) Begin
Showln "The response is missing the message SID or status."
Procedure_Return
End
Get ComStringOf Of hoReply "sid" To sTemp1
Showln "Message SID: " sTemp1
Get ComStringOf Of hoReply "status" To sTemp1
Showln "Current status: " sTemp1
Get ComStringOfEquals Of hoReply "status" sExpectedStatus True To bTemp1
If (bTemp1 <> True) Begin
Showln "The requested state was not confirmed. Check this SID before trying again."
Procedure_Return
End
If (iCancelMessage <> True) Begin
Showln "Save the Message SID to check or cancel the scheduled message."
End
End_Procedure