Sample code for 30+ languages & platforms
Lua 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 Lua Downloads

Lua
local chilkat = require("chilkat")

-- Assumes Chilkat is unlocked. See Global Unlock Sample.
local success = false
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
-- Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.auth_token = "SENDGRID_API_KEY"
local resp = chilkat.HttpResponse.new()

-- 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 batchToCancel = ""
if batchToCancel ~= "" then
    local cancellation = chilkat.JsonObject.new()
    cancellation:update_string("batch_id", batchToCancel)
    cancellation:update_string("status", "cancel")
    success = http:http_json("POST", "https://api.sendgrid.com/v3/user/scheduled_sends", cancellation, "application/json", resp)
    if not success then
        if resp.status_code == 0 then
            print(http.last_error_text)
        else
            print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
        end

        return
    end

    if resp.status_code ~= 201 then
        print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
        return
    end

    print("Batch cancellation accepted.")
    return
end

success = http:http_no_body("POST", "https://api.sendgrid.com/v3/mail/batch", resp)
if not success then
    if resp.status_code == 0 then
        print(http.last_error_text)
    else
        print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
    end

    return
end

if resp.status_code ~= 201 then
    print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
    return
end

local batch = chilkat.JsonObject.new()
success = batch:load(resp.body_str)
if not success then
    print("Unexpected response: expected JSON.")
    return
end

local batchId = batch:string_of("batch_id")
if not batch:has_member("batch_id") then
    print("Missing batch_id in response.")
    return
end

-- Allocate one batch per independently cancelable appointment.
print("Save this batch ID with the appointment: " .. batchId)
local sendTime = chilkat.DateTime.new()
sendTime:set_from_current_system_time()
sendTime:add_seconds(3600)
local message = chilkat.JsonObject.new()
message:update_string("batch_id", batchId)
message:update_int("send_at", sendTime:get_as_unix_time(false))
message:update_string("from.email", "sender@example.com")
message:update_string("personalizations[0].to[0].email", "recipient@example.com")
message:update_string("subject", "Appointment reminder")
message:update_string("content[0].type", "text/plain")
message:update_string("content[0].value", "Your appointment is tomorrow at 9:30 AM Central.")

success = http:http_json("POST", "https://api.sendgrid.com/v3/mail/send", message, "application/json", resp)
if not success then
    if resp.status_code == 0 then
        print(http.last_error_text)
    else
        print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
    end

    return
end

if resp.status_code ~= 202 then
    print("HTTP " .. resp.status_code .. ": " .. resp.body_str)
    return
end

print("Reminder scheduled for " .. sendTime:get_as_timestamp(false))