Unicode C Requires Chilkat v11.0.0+
Unicode 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 Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkHttpResponseW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkDateTimeW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkHttpResponseW resp;
const wchar_t *batchToCancel;
HCkJsonObjectW cancellation;
HCkJsonObjectW batch;
const wchar_t *batchId;
HCkDateTimeW sendTime;
HCkJsonObjectW message;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
success = FALSE;
http = CkHttpW_Create();
CkHttpW_putRequireSslCertVerify(http,TRUE);
// Supply your API key from protected configuration; AuthToken adds the Bearer prefix.
CkHttpW_putAuthToken(http,L"SENDGRID_API_KEY");
resp = CkHttpResponseW_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 = L"";
if (batchToCancel != L"") {
cancellation = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(cancellation,L"batch_id",batchToCancel);
CkJsonObjectW_UpdateString(cancellation,L"status",L"cancel");
success = CkHttpW_HttpJson(http,L"POST",L"https://api.sendgrid.com/v3/user/scheduled_sends",cancellation,L"application/json",resp);
if (success != TRUE) {
if (CkHttpResponseW_getStatusCode(resp) == 0) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
}
else {
wprintf(L"HTTP %d: %s\n",CkHttpResponseW_getStatusCode(resp),CkHttpResponseW_bodyStr(resp));
}
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(cancellation);
return;
}
if (CkHttpResponseW_getStatusCode(resp) != 201) {
wprintf(L"HTTP %d: %s\n",CkHttpResponseW_getStatusCode(resp),CkHttpResponseW_bodyStr(resp));
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(cancellation);
return;
}
wprintf(L"Batch cancellation accepted.\n");
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(cancellation);
return;
}
success = CkHttpW_HttpNoBody(http,L"POST",L"https://api.sendgrid.com/v3/mail/batch",resp);
if (success != TRUE) {
if (CkHttpResponseW_getStatusCode(resp) == 0) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
}
else {
wprintf(L"HTTP %d: %s\n",CkHttpResponseW_getStatusCode(resp),CkHttpResponseW_bodyStr(resp));
}
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(cancellation);
return;
}
if (CkHttpResponseW_getStatusCode(resp) != 201) {
wprintf(L"HTTP %d: %s\n",CkHttpResponseW_getStatusCode(resp),CkHttpResponseW_bodyStr(resp));
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(cancellation);
return;
}
batch = CkJsonObjectW_Create();
success = CkJsonObjectW_Load(batch,CkHttpResponseW_bodyStr(resp));
if (success != TRUE) {
wprintf(L"Unexpected response: expected JSON.\n");
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(cancellation);
CkJsonObjectW_Dispose(batch);
return;
}
batchId = CkJsonObjectW_stringOf(batch,L"batch_id");
if (CkJsonObjectW_HasMember(batch,L"batch_id") != TRUE) {
wprintf(L"Missing batch_id in response.\n");
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(cancellation);
CkJsonObjectW_Dispose(batch);
return;
}
// Allocate one batch per independently cancelable appointment.
wprintf(L"Save this batch ID with the appointment: %s\n",batchId);
sendTime = CkDateTimeW_Create();
CkDateTimeW_SetFromCurrentSystemTime(sendTime);
CkDateTimeW_AddSeconds(sendTime,3600);
message = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(message,L"batch_id",batchId);
CkJsonObjectW_UpdateInt(message,L"send_at",CkDateTimeW_GetAsUnixTime(sendTime,FALSE));
CkJsonObjectW_UpdateString(message,L"from.email",L"sender@example.com");
CkJsonObjectW_UpdateString(message,L"personalizations[0].to[0].email",L"recipient@example.com");
CkJsonObjectW_UpdateString(message,L"subject",L"Appointment reminder");
CkJsonObjectW_UpdateString(message,L"content[0].type",L"text/plain");
CkJsonObjectW_UpdateString(message,L"content[0].value",L"Your appointment is tomorrow at 9:30 AM Central.");
success = CkHttpW_HttpJson(http,L"POST",L"https://api.sendgrid.com/v3/mail/send",message,L"application/json",resp);
if (success != TRUE) {
if (CkHttpResponseW_getStatusCode(resp) == 0) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
}
else {
wprintf(L"HTTP %d: %s\n",CkHttpResponseW_getStatusCode(resp),CkHttpResponseW_bodyStr(resp));
}
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(cancellation);
CkJsonObjectW_Dispose(batch);
CkDateTimeW_Dispose(sendTime);
CkJsonObjectW_Dispose(message);
return;
}
if (CkHttpResponseW_getStatusCode(resp) != 202) {
wprintf(L"HTTP %d: %s\n",CkHttpResponseW_getStatusCode(resp),CkHttpResponseW_bodyStr(resp));
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(cancellation);
CkJsonObjectW_Dispose(batch);
CkDateTimeW_Dispose(sendTime);
CkJsonObjectW_Dispose(message);
return;
}
wprintf(L"Reminder scheduled for %s\n",CkDateTimeW_getAsTimestamp(sendTime,FALSE));
CkHttpW_Dispose(http);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(cancellation);
CkJsonObjectW_Dispose(batch);
CkDateTimeW_Dispose(sendTime);
CkJsonObjectW_Dispose(message);
}