Xbase++ Requires Chilkat v11.0.0+
Xbase++
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 Xbase++ Downloads
LOCAL nSuccess
LOCAL oHttp
LOCAL oResp
LOCAL cBatchToCancel
LOCAL oCancellation
LOCAL oBatch
LOCAL cBatchId
LOCAL oSendTime
LOCAL oMessage
nSuccess := 0
// Assumes Chilkat is unlocked. See Global Unlock Sample.
nSuccess := 0
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
oHttp:AuthToken := "SENDGRID_API_KEY"
oResp := CreateObject("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.
cBatchToCancel := ""
IF (cBatchToCancel != "")
oCancellation := CreateObject("Chilkat.JsonObject")
oCancellation:UpdateString("batch_id", cBatchToCancel)
oCancellation:UpdateString("status", "cancel")
nSuccess := oHttp:HttpJson("POST", "https://api.sendgrid.com/v3/user/scheduled_sends", oCancellation, "application/json", oResp)
IF (nSuccess != 1)
IF (oResp:StatusCode == 0)
? oHttp:LastErrorText
ELSE
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
ENDIF
oHttp:destroy()
oResp:destroy()
oCancellation:destroy()
RETURN
ENDIF
IF (oResp:StatusCode != 201)
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
oHttp:destroy()
oResp:destroy()
oCancellation:destroy()
RETURN
ENDIF
? "Batch cancellation accepted."
oHttp:destroy()
oResp:destroy()
oCancellation:destroy()
RETURN
ENDIF
nSuccess := oHttp:HttpNoBody("POST", "https://api.sendgrid.com/v3/mail/batch", oResp)
IF (nSuccess != 1)
IF (oResp:StatusCode == 0)
? oHttp:LastErrorText
ELSE
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
ENDIF
oHttp:destroy()
oResp:destroy()
oCancellation:destroy()
RETURN
ENDIF
IF (oResp:StatusCode != 201)
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
oHttp:destroy()
oResp:destroy()
oCancellation:destroy()
RETURN
ENDIF
oBatch := CreateObject("Chilkat.JsonObject")
nSuccess := oBatch:Load(oResp:BodyStr)
IF (nSuccess != 1)
? "Unexpected response: expected JSON."
oHttp:destroy()
oResp:destroy()
oCancellation:destroy()
oBatch:destroy()
RETURN
ENDIF
cBatchId := oBatch:StringOf("batch_id")
IF (oBatch:HasMember("batch_id") != 1)
? "Missing batch_id in response."
oHttp:destroy()
oResp:destroy()
oCancellation:destroy()
oBatch:destroy()
RETURN
ENDIF
// Allocate one batch per independently cancelable appointment.
? "Save this batch ID with the appointment: " + cBatchId
oSendTime := CreateObject("Chilkat.CkDateTime")
oSendTime:SetFromCurrentSystemTime()
oSendTime:AddSeconds(3600)
oMessage := CreateObject("Chilkat.JsonObject")
oMessage:UpdateString("batch_id", cBatchId)
oMessage:UpdateInt("send_at", oSendTime:GetAsUnixTime(0))
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.")
nSuccess := oHttp:HttpJson("POST", "https://api.sendgrid.com/v3/mail/send", oMessage, "application/json", oResp)
IF (nSuccess != 1)
IF (oResp:StatusCode == 0)
? oHttp:LastErrorText
ELSE
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
ENDIF
oHttp:destroy()
oResp:destroy()
oCancellation:destroy()
oBatch:destroy()
oSendTime:destroy()
oMessage:destroy()
RETURN
ENDIF
IF (oResp:StatusCode != 202)
? "HTTP " + Str(oResp:StatusCode) + ": " + oResp:BodyStr
oHttp:destroy()
oResp:destroy()
oCancellation:destroy()
oBatch:destroy()
oSendTime:destroy()
oMessage:destroy()
RETURN
ENDIF
? "Reminder scheduled for " + oSendTime:GetAsTimestamp(0)
oHttp:destroy()
oResp:destroy()
oCancellation:destroy()
oBatch:destroy()
oSendTime:destroy()
oMessage:destroy()