AutoIt Requires Chilkat v11.0.0+
AutoIt
SendGrid: Schedule a reminder and cancel it by batch ID
See more SendGrid Examples
Schedule a reminder one hour from now. Save its batch ID when it is allocated. If the appointment is canceled, use that saved ID in the cancellation branch. Mail Send accepts a Unix timestamp up to 72 hours ahead; cancel at least 10 minutes before the send time. Cancellation applies to the entire batch.
Create a batch ID ยท Cancel a batch
Chilkat AutoIt Downloads
Local $bSuccess = False
; Assumes Chilkat is unlocked. See Global Unlock Sample.
$bSuccess = False
$oHttp = ObjCreate("Chilkat.Http")
$oHttp.RequireSslCertVerify = True
; Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
$oHttp.AuthToken = "SENDGRID_API_KEY"
$oResp = ObjCreate("Chilkat.HttpResponse")
; On cancellation, set this to the batch ID saved with the original appointment.
; This branch does not create a new batch or send another reminder.
Local $sBatchToCancel = ""
If ($sBatchToCancel <> "") Then
$oCancellation = ObjCreate("Chilkat.JsonObject")
$oCancellation.UpdateString("batch_id",$sBatchToCancel)
$oCancellation.UpdateString("status","cancel")
$bSuccess = $oHttp.HttpJson("POST","https://api.sendgrid.com/v3/user/scheduled_sends",$oCancellation,"application/json",$oResp)
If ($bSuccess <> True) Then
If ($oResp.StatusCode = 0) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Else
ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
EndIf
Exit
EndIf
If ($oResp.StatusCode <> 201) Then
ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
Exit
EndIf
ConsoleWrite("Batch cancellation accepted." & @CRLF)
Exit
EndIf
$bSuccess = $oHttp.HttpNoBody("POST","https://api.sendgrid.com/v3/mail/batch",$oResp)
If ($bSuccess <> True) Then
If ($oResp.StatusCode = 0) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Else
ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
EndIf
Exit
EndIf
If ($oResp.StatusCode <> 201) Then
ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
Exit
EndIf
$oBatch = ObjCreate("Chilkat.JsonObject")
$bSuccess = $oBatch.Load($oResp.BodyStr)
If ($bSuccess <> True) Then
ConsoleWrite("Unexpected response: expected JSON." & @CRLF)
Exit
EndIf
Local $sBatchId = $oBatch.StringOf("batch_id")
If ($oBatch.HasMember("batch_id") <> True) Then
ConsoleWrite("Missing batch_id in response." & @CRLF)
Exit
EndIf
; Allocate one batch per independently cancelable appointment.
ConsoleWrite("Save this batch ID with the appointment: " & $sBatchId & @CRLF)
$oSendTime = ObjCreate("Chilkat.CkDateTime")
$oSendTime.SetFromCurrentSystemTime()
$oSendTime.AddSeconds(3600)
$oMessage = ObjCreate("Chilkat.JsonObject")
$oMessage.UpdateString("batch_id",$sBatchId)
$oMessage.UpdateInt("send_at",$oSendTime.GetAsUnixTime(False))
$oMessage.UpdateString("from.email","sender@example.com")
$oMessage.UpdateString("personalizations[0].to[0].email","recipient@example.com")
$oMessage.UpdateString("subject","Appointment reminder")
$oMessage.UpdateString("content[0].type","text/plain")
$oMessage.UpdateString("content[0].value","Your appointment is tomorrow at 9:30 AM Central.")
$bSuccess = $oHttp.HttpJson("POST","https://api.sendgrid.com/v3/mail/send",$oMessage,"application/json",$oResp)
If ($bSuccess <> True) Then
If ($oResp.StatusCode = 0) Then
ConsoleWrite($oHttp.LastErrorText & @CRLF)
Else
ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
EndIf
Exit
EndIf
If ($oResp.StatusCode <> 202) Then
ConsoleWrite("HTTP " & $oResp.StatusCode & ": " & $oResp.BodyStr & @CRLF)
Exit
EndIf
ConsoleWrite("Reminder scheduled for " & $oSendTime.GetAsTimestamp(False) & @CRLF)