Unicode C Requires Chilkat v11.0.0+
Unicode C
Clickatell One API: Send personalized SMS reminders in a batch
See more Clickatell Examples
Send two appointments in one request with a different text and application ID for each. Use two distinct recipient numbers in international digits. For a US two-way integration, add its provisioned from number to each message.
Read each recipient result even when HTTP 207 reports a mixed outcome. Save the accepted provider ID against the appointment. A missing result calls for checking the message report before retrying. A client message ID helps correlate records and does not guarantee deduplication.
Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkHttpResponseW.h>
void ChilkatSample(void)
{
HCkHttpW http;
const wchar_t *firstPhone;
const wchar_t *secondPhone;
HCkJsonObjectW body;
HCkHttpResponseW resp;
BOOL sent;
HCkJsonObjectW reply;
BOOL parsed;
HCkJsonObjectW results;
int i;
const wchar_t *appointment;
const wchar_t *phone;
HCkJsonObjectW result;
BOOL found;
BOOL saved;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
http = CkHttpW_Create();
CkHttpW_putRequireSslCertVerify(http,TRUE);
// Load the key for this One API integration from your application configuration.
CkHttpW_SetRequestHeader(http,L"Authorization",L"ONE_API_KEY");
CkHttpW_SetRequestHeader(http,L"Accept",L"application/json");
firstPhone = L"FIRST_PHONE_NUMBER";
secondPhone = L"SECOND_PHONE_NUMBER";
body = CkJsonObjectW_Create();
CkJsonObjectW_UpdateString(body,L"messages[0].channel",L"sms");
CkJsonObjectW_UpdateString(body,L"messages[0].to",firstPhone);
CkJsonObjectW_UpdateString(body,L"messages[0].content",L"Your appointment with Lee is at 10:00.");
CkJsonObjectW_UpdateString(body,L"messages[0].clientMessageId",L"appt-1042");
CkJsonObjectW_UpdateString(body,L"messages[1].channel",L"sms");
CkJsonObjectW_UpdateString(body,L"messages[1].to",secondPhone);
CkJsonObjectW_UpdateString(body,L"messages[1].content",L"Your appointment with Sam is at 14:30.");
CkJsonObjectW_UpdateString(body,L"messages[1].clientMessageId",L"appt-1043");
resp = CkHttpResponseW_Create();
sent = CkHttpW_HttpJson(http,L"POST",L"https://platform.clickatell.com/v1/message",body,L"application/json",resp);
if (CkHttpResponseW_getStatusCode(resp) == 0) {
wprintf(L"No HTTP response. Check the message report before resending.\n");
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(body);
CkHttpResponseW_Dispose(resp);
return;
}
reply = CkJsonObjectW_Create();
parsed = CkJsonObjectW_Load(reply,CkHttpResponseW_bodyStr(resp));
if ((sent != TRUE) || ((CkHttpResponseW_getStatusCode(resp) != 202) && (CkHttpResponseW_getStatusCode(resp) != 207))) {
wprintf(L"Send returned HTTP %d\n",CkHttpResponseW_getStatusCode(resp));
if (CkJsonObjectW_HasMember(reply,L"error.description") == TRUE) {
wprintf(L"%s\n",CkJsonObjectW_stringOf(reply,L"error.description"));
}
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(body);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
return;
}
if (CkJsonObjectW_HasMember(reply,L"error.code") == TRUE) {
if (CkJsonObjectW_IsNullOf(reply,L"error.code") != TRUE) {
wprintf(L"Submission error %s %s\n",CkJsonObjectW_stringOf(reply,L"error.code"),CkJsonObjectW_stringOf(reply,L"error.description"));
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(body);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
return;
}
}
// Look up each submitted appointment in the reply using its unique recipient.
// This works even if the result order changes or clientMessageId is absent.
results = CkJsonObjectW_Create();
i = 0;
while ((i < 2)) {
CkJsonObjectW_putI(body,i);
appointment = CkJsonObjectW_stringOf(body,L"messages[i].clientMessageId");
phone = CkJsonObjectW_stringOf(body,L"messages[i].to");
result = CkJsonObjectW_Create();
found = CkJsonObjectW_FindRecord2(reply,L"messages",L"to",phone,TRUE,result);
CkJsonObjectW_putI(results,i);
CkJsonObjectW_UpdateString(results,L"appointments[i].clientMessageId",appointment);
if ((found != TRUE) || (CkJsonObjectW_JsonTypeOf(result,L"accepted") != 5)) {
CkJsonObjectW_UpdateString(results,L"appointments[i].outcome",L"unknown");
wprintf(L"%s has no confirmed result.\n",appointment);
}
else {
if (CkJsonObjectW_BoolOf(result,L"accepted") == TRUE) {
CkJsonObjectW_UpdateString(results,L"appointments[i].outcome",L"accepted");
if (CkJsonObjectW_JsonTypeOf(result,L"apiMessageId") == 1) {
CkJsonObjectW_UpdateString(results,L"appointments[i].apiMessageId",CkJsonObjectW_stringOf(result,L"apiMessageId"));
}
wprintf(L"%s accepted.\n",appointment);
}
else {
CkJsonObjectW_UpdateString(results,L"appointments[i].outcome",L"rejected");
CkJsonObjectW_UpdateString(results,L"appointments[i].reason",CkJsonObjectW_stringOf(result,L"error.description"));
wprintf(L"%s rejected: %s\n",appointment,CkJsonObjectW_stringOf(result,L"error.description"));
}
}
i = i + 1;
}
// This demo writes a snapshot. In an application, update the two appointment records.
saved = CkJsonObjectW_WriteFile(results,L"appointment-send-results.json");
if (saved != TRUE) {
wprintf(L"Could not save the results. Reconcile the submitted messages before retrying.\n");
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(body);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkJsonObjectW_Dispose(results);
CkJsonObjectW_Dispose(result);
return;
}
wprintf(L"Saved appointment-send-results.json\n");
CkHttpW_Dispose(http);
CkJsonObjectW_Dispose(body);
CkHttpResponseW_Dispose(resp);
CkJsonObjectW_Dispose(reply);
CkJsonObjectW_Dispose(results);
CkJsonObjectW_Dispose(result);
}