C Requires Chilkat v11.0.0+
C
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 C Downloads
#include <C_CkHttp.h>
#include <C_CkHttpResponse.h>
#include <C_CkJsonObject.h>
#include <C_CkDateTime.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttp http;
HCkHttpResponse resp;
const char *batchToCancel;
HCkJsonObject cancellation;
HCkJsonObject batch;
const char *batchId;
HCkDateTime sendTime;
HCkJsonObject message;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = FALSE;
http = CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,TRUE);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
CkHttp_putAuthToken(http,"SENDGRID_API_KEY");
resp = CkHttpResponse_Create();
// 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 != "") {
cancellation = CkJsonObject_Create();
CkJsonObject_UpdateString(cancellation,"batch_id",batchToCancel);
CkJsonObject_UpdateString(cancellation,"status","cancel");
success = CkHttp_HttpJson(http,"POST","https://api.sendgrid.com/v3/user/scheduled_sends",cancellation,"application/json",resp);
if (success != TRUE) {
if (CkHttpResponse_getStatusCode(resp) == 0) {
printf("%s\n",CkHttp_lastErrorText(http));
}
else {
printf("HTTP %d: %s\n",CkHttpResponse_getStatusCode(resp),CkHttpResponse_bodyStr(resp));
}
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(cancellation);
return;
}
if (CkHttpResponse_getStatusCode(resp) != 201) {
printf("HTTP %d: %s\n",CkHttpResponse_getStatusCode(resp),CkHttpResponse_bodyStr(resp));
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(cancellation);
return;
}
printf("Batch cancellation accepted.\n");
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(cancellation);
return;
}
success = CkHttp_HttpNoBody(http,"POST","https://api.sendgrid.com/v3/mail/batch",resp);
if (success != TRUE) {
if (CkHttpResponse_getStatusCode(resp) == 0) {
printf("%s\n",CkHttp_lastErrorText(http));
}
else {
printf("HTTP %d: %s\n",CkHttpResponse_getStatusCode(resp),CkHttpResponse_bodyStr(resp));
}
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(cancellation);
return;
}
if (CkHttpResponse_getStatusCode(resp) != 201) {
printf("HTTP %d: %s\n",CkHttpResponse_getStatusCode(resp),CkHttpResponse_bodyStr(resp));
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(cancellation);
return;
}
batch = CkJsonObject_Create();
success = CkJsonObject_Load(batch,CkHttpResponse_bodyStr(resp));
if (success != TRUE) {
printf("Unexpected response: expected JSON.\n");
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(cancellation);
CkJsonObject_Dispose(batch);
return;
}
batchId = CkJsonObject_stringOf(batch,"batch_id");
if (CkJsonObject_HasMember(batch,"batch_id") != TRUE) {
printf("Missing batch_id in response.\n");
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(cancellation);
CkJsonObject_Dispose(batch);
return;
}
// Allocate one batch per independently cancelable appointment.
printf("Save this batch ID with the appointment: %s\n",batchId);
sendTime = CkDateTime_Create();
CkDateTime_SetFromCurrentSystemTime(sendTime);
CkDateTime_AddSeconds(sendTime,3600);
message = CkJsonObject_Create();
CkJsonObject_UpdateString(message,"batch_id",batchId);
CkJsonObject_UpdateInt(message,"send_at",CkDateTime_GetAsUnixTime(sendTime,FALSE));
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.");
success = CkHttp_HttpJson(http,"POST","https://api.sendgrid.com/v3/mail/send",message,"application/json",resp);
if (success != TRUE) {
if (CkHttpResponse_getStatusCode(resp) == 0) {
printf("%s\n",CkHttp_lastErrorText(http));
}
else {
printf("HTTP %d: %s\n",CkHttpResponse_getStatusCode(resp),CkHttpResponse_bodyStr(resp));
}
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(cancellation);
CkJsonObject_Dispose(batch);
CkDateTime_Dispose(sendTime);
CkJsonObject_Dispose(message);
return;
}
if (CkHttpResponse_getStatusCode(resp) != 202) {
printf("HTTP %d: %s\n",CkHttpResponse_getStatusCode(resp),CkHttpResponse_bodyStr(resp));
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(cancellation);
CkJsonObject_Dispose(batch);
CkDateTime_Dispose(sendTime);
CkJsonObject_Dispose(message);
return;
}
printf("Reminder scheduled for %s\n",CkDateTime_getAsTimestamp(sendTime,FALSE));
CkHttp_Dispose(http);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(cancellation);
CkJsonObject_Dispose(batch);
CkDateTime_Dispose(sendTime);
CkJsonObject_Dispose(message);
}