Sample code for 30+ languages & platforms
Tcl Requires Chilkat v11.0.0+

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 Tcl Downloads

Tcl

load ./chilkat.dll

set success 0

#  Assumes Chilkat is unlocked. See Global Unlock Sample.
set success 0
set http [new_CkHttp]

CkHttp_put_RequireSslCertVerify $http 1
#  Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
CkHttp_put_AuthToken $http "SENDGRID_API_KEY"
set resp [new_CkHttpResponse]

#  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.
set batchToCancel ""
if {$batchToCancel != ""} then {
    set cancellation [new_CkJsonObject]

    CkJsonObject_UpdateString $cancellation "batch_id" $batchToCancel
    CkJsonObject_UpdateString $cancellation "status" "cancel"
    set success [CkHttp_HttpJson $http "POST" "https://api.sendgrid.com/v3/user/scheduled_sends" $cancellation "application/json" $resp]
    if {$success != 1} then {
        if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
            puts [CkHttp_lastErrorText $http]
        }         else {
            puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
        }

        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $cancellation
        exit
    }

    if {[CkHttpResponse_get_StatusCode $resp] != 201} then {
        puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
        delete_CkHttp $http
        delete_CkHttpResponse $resp
        delete_CkJsonObject $cancellation
        exit
    }

    puts "Batch cancellation accepted."
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $cancellation
    exit
}

set success [CkHttp_HttpNoBody $http "POST" "https://api.sendgrid.com/v3/mail/batch" $resp]
if {$success != 1} then {
    if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
        puts [CkHttp_lastErrorText $http]
    }     else {
        puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
    }

    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $cancellation
    exit
}

if {[CkHttpResponse_get_StatusCode $resp] != 201} then {
    puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $cancellation
    exit
}

set batch [new_CkJsonObject]

set success [CkJsonObject_Load $batch [CkHttpResponse_bodyStr $resp]]
if {$success != 1} then {
    puts "Unexpected response: expected JSON."
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $cancellation
    delete_CkJsonObject $batch
    exit
}

set batchId [CkJsonObject_stringOf $batch "batch_id"]
if {[CkJsonObject_HasMember $batch "batch_id"] != 1} then {
    puts "Missing batch_id in response."
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $cancellation
    delete_CkJsonObject $batch
    exit
}

#  Allocate one batch per independently cancelable appointment.
puts "Save this batch ID with the appointment: $batchId"
set sendTime [new_CkDateTime]

CkDateTime_SetFromCurrentSystemTime $sendTime
CkDateTime_AddSeconds $sendTime 3600
set message [new_CkJsonObject]

CkJsonObject_UpdateString $message "batch_id" $batchId
CkJsonObject_UpdateInt $message "send_at" [CkDateTime_GetAsUnixTime $sendTime 0]
CkJsonObject_UpdateString $message "from.email" "sender@example.com"
CkJsonObject_UpdateString $message "personalizations[0].to[0].email" "recipient@example.com"
CkJsonObject_UpdateString $message "subject" "Appointment reminder"
CkJsonObject_UpdateString $message "content[0].type" "text/plain"
CkJsonObject_UpdateString $message "content[0].value" "Your appointment is tomorrow at 9:30 AM Central."

set success [CkHttp_HttpJson $http "POST" "https://api.sendgrid.com/v3/mail/send" $message "application/json" $resp]
if {$success != 1} then {
    if {[CkHttpResponse_get_StatusCode $resp] == 0} then {
        puts [CkHttp_lastErrorText $http]
    }     else {
        puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
    }

    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $cancellation
    delete_CkJsonObject $batch
    delete_CkDateTime $sendTime
    delete_CkJsonObject $message
    exit
}

if {[CkHttpResponse_get_StatusCode $resp] != 202} then {
    puts "HTTP [CkHttpResponse_get_StatusCode $resp]: [CkHttpResponse_bodyStr $resp]"
    delete_CkHttp $http
    delete_CkHttpResponse $resp
    delete_CkJsonObject $cancellation
    delete_CkJsonObject $batch
    delete_CkDateTime $sendTime
    delete_CkJsonObject $message
    exit
}

puts "Reminder scheduled for [CkDateTime_getAsTimestamp $sendTime 0]"

delete_CkHttp $http
delete_CkHttpResponse $resp
delete_CkJsonObject $cancellation
delete_CkJsonObject $batch
delete_CkDateTime $sendTime
delete_CkJsonObject $message