Delphi ActiveX Requires Chilkat v11.0.0+
Delphi ActiveX
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 Delphi ActiveX Downloads
var
success: Integer;
http: TChilkatHttp;
resp: TChilkatHttpResponse;
batchToCancel: WideString;
cancellation: TChilkatJsonObject;
batch: TChilkatJsonObject;
batchId: WideString;
sendTime: TCkDateTime;
message: TChilkatJsonObject;
begin
success := 0;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success := 0;
http := TChilkatHttp.Create(Self);
http.RequireSslCertVerify := 1;
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
http.AuthToken := 'SENDGRID_API_KEY';
resp := TChilkatHttpResponse.Create(Self);
// 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.
batchToCancel := '';
if (batchToCancel <> '') then
begin
cancellation := TChilkatJsonObject.Create(Self);
cancellation.UpdateString('batch_id',batchToCancel);
cancellation.UpdateString('status','cancel');
success := http.HttpJson('POST','https://api.sendgrid.com/v3/user/scheduled_sends',cancellation.ControlInterface,'application/json',resp.ControlInterface);
if (success <> 1) then
begin
if (resp.StatusCode = 0) then
begin
Memo1.Lines.Add(http.LastErrorText);
end
else
begin
Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
end;
Exit;
end;
if (resp.StatusCode <> 201) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
Exit;
end;
Memo1.Lines.Add('Batch cancellation accepted.');
Exit;
end;
success := http.HttpNoBody('POST','https://api.sendgrid.com/v3/mail/batch',resp.ControlInterface);
if (success <> 1) then
begin
if (resp.StatusCode = 0) then
begin
Memo1.Lines.Add(http.LastErrorText);
end
else
begin
Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
end;
Exit;
end;
if (resp.StatusCode <> 201) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
Exit;
end;
batch := TChilkatJsonObject.Create(Self);
success := batch.Load(resp.BodyStr);
if (success <> 1) then
begin
Memo1.Lines.Add('Unexpected response: expected JSON.');
Exit;
end;
batchId := batch.StringOf('batch_id');
if (batch.HasMember('batch_id') <> 1) then
begin
Memo1.Lines.Add('Missing batch_id in response.');
Exit;
end;
// Allocate one batch per independently cancelable appointment.
Memo1.Lines.Add('Save this batch ID with the appointment: ' + batchId);
sendTime := TCkDateTime.Create(Self);
sendTime.SetFromCurrentSystemTime();
sendTime.AddSeconds(3600);
message := TChilkatJsonObject.Create(Self);
message.UpdateString('batch_id',batchId);
message.UpdateInt('send_at',sendTime.GetAsUnixTime(0));
message.UpdateString('from.email','sender@example.com');
message.UpdateString('personalizations[0].to[0].email','recipient@example.com');
message.UpdateString('subject','Appointment reminder');
message.UpdateString('content[0].type','text/plain');
message.UpdateString('content[0].value','Your appointment is tomorrow at 9:30 AM Central.');
success := http.HttpJson('POST','https://api.sendgrid.com/v3/mail/send',message.ControlInterface,'application/json',resp.ControlInterface);
if (success <> 1) then
begin
if (resp.StatusCode = 0) then
begin
Memo1.Lines.Add(http.LastErrorText);
end
else
begin
Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
end;
Exit;
end;
if (resp.StatusCode <> 202) then
begin
Memo1.Lines.Add('HTTP ' + IntToStr(resp.StatusCode) + ': ' + resp.BodyStr);
Exit;
end;
Memo1.Lines.Add('Reminder scheduled for ' + sendTime.GetAsTimestamp(0));