AutoIt Requires Chilkat v11.0.0+
AutoIt
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 AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
; Load an API key for the Twilio account from private application configuration.
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
$oHttp.BasicAuth = True
$oHttp.Login = "YOUR_API_KEY_SID"
$oHttp.Password = "YOUR_API_KEY_SECRET"
$oUrl = ObjCreate("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.
Local $bCancelMessage = False
$oReq = ObjCreate("Chilkat.HttpRequest")
$oSendTime = ObjCreate("Chilkat.CkDateTime")
$oReq.HttpVerb = "POST"
$oReq.ContentType = "application/x-www-form-urlencoded"
Local $iExpectedHttp = 201
Local $sExpectedStatus = "scheduled"
If ($bCancelMessage <> True) Then
Local $bTimeOk = $oSendTime.SetFromCurrentSystemTime()
If ($bTimeOk <> True) Then
ConsoleWrite($oSendTime.LastErrorText & @CRLF)
Exit
EndIf
$bTimeOk = $oSendTime.AddSeconds(3600)
If ($bTimeOk <> True) Then
ConsoleWrite($oSendTime.LastErrorText & @CRLF)
Exit
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(False)
Else
; Update the individual message. This branch never creates another message.
$oUrl.Append("/")
$oUrl.Append("SAVED_SCHEDULED_MESSAGE_SID")
$oReq.AddParam "Status","canceled"
$iExpectedHttp = 200
$sExpectedStatus = "canceled"
EndIf
$oUrl.Append(".json")
$oResp = ObjCreate("Chilkat.HttpResponse")
$oReply = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oHttp.HttpReq($oUrl.GetAsString(),$oReq,$oResp)
If ($oResp.StatusCode = 0) Then
ConsoleWrite("No HTTP response from Twilio." & @CRLF)
Exit
EndIf
Local $bParsed = $oReply.Load($oResp.BodyStr)
If (($bSuccess <> True) Or ($oResp.StatusCode <> $iExpectedHttp)) Then
ConsoleWrite("Twilio returned HTTP " & $oResp.StatusCode & @CRLF)
If (($bParsed = True) And ($oReply.JsonTypeOf("code") = 2)) Then
ConsoleWrite("Twilio error: " & $oReply.IntOf("code") & " " & $oReply.StringOf("message") & @CRLF)
EndIf
Exit
EndIf
If ($bParsed <> True) Then
ConsoleWrite("Expected a JSON response from Twilio." & @CRLF)
Exit
EndIf
If (($oReply.JsonTypeOf("sid") <> 1) Or ($oReply.JsonTypeOf("status") <> 1)) Then
ConsoleWrite("The response is missing the message SID or status." & @CRLF)
Exit
EndIf
ConsoleWrite("Message SID: " & $oReply.StringOf("sid") & @CRLF)
ConsoleWrite("Current status: " & $oReply.StringOf("status") & @CRLF)
If ($oReply.StringOfEquals("status",$sExpectedStatus,True) <> True) Then
ConsoleWrite("The requested state was not confirmed. Check this SID before trying again." & @CRLF)
Exit
EndIf
If ($bCancelMessage <> True) Then
ConsoleWrite("Save the Message SID to check or cancel the scheduled message." & @CRLF)
EndIf