C Requires Chilkat v11.0.0+
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 C Downloads
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
void ChilkatSample(void)
{
HCkHttp http;
const char *firstPhone;
const char *secondPhone;
HCkJsonObject body;
HCkHttpResponse resp;
BOOL sent;
HCkJsonObject reply;
BOOL parsed;
HCkJsonObject results;
int i;
const char *appointment;
const char *phone;
HCkJsonObject result;
BOOL found;
BOOL saved;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
http = CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,TRUE);
// Load the key for this One API integration from your application configuration.
CkHttp_SetRequestHeader(http,"Authorization","ONE_API_KEY");
CkHttp_SetRequestHeader(http,"Accept","application/json");
firstPhone = "FIRST_PHONE_NUMBER";
secondPhone = "SECOND_PHONE_NUMBER";
body = CkJsonObject_Create();
CkJsonObject_UpdateString(body,"messages[0].channel","sms");
CkJsonObject_UpdateString(body,"messages[0].to",firstPhone);
CkJsonObject_UpdateString(body,"messages[0].content","Your appointment with Lee is at 10:00.");
CkJsonObject_UpdateString(body,"messages[0].clientMessageId","appt-1042");
CkJsonObject_UpdateString(body,"messages[1].channel","sms");
CkJsonObject_UpdateString(body,"messages[1].to",secondPhone);
CkJsonObject_UpdateString(body,"messages[1].content","Your appointment with Sam is at 14:30.");
CkJsonObject_UpdateString(body,"messages[1].clientMessageId","appt-1043");
resp = CkHttpResponse_Create();
sent = CkHttp_HttpJson(http,"POST","https://platform.clickatell.com/v1/message",body,"application/json",resp);
if (CkHttpResponse_getStatusCode(resp) == 0) {
printf("No HTTP response. Check the message report before resending.\n");
CkHttp_Dispose(http);
CkJsonObject_Dispose(body);
CkHttpResponse_Dispose(resp);
return;
}
reply = CkJsonObject_Create();
parsed = CkJsonObject_Load(reply,CkHttpResponse_bodyStr(resp));
if ((sent != TRUE) || ((CkHttpResponse_getStatusCode(resp) != 202) && (CkHttpResponse_getStatusCode(resp) != 207))) {
printf("Send returned HTTP %d\n",CkHttpResponse_getStatusCode(resp));
if (CkJsonObject_HasMember(reply,"error.description") == TRUE) {
printf("%s\n",CkJsonObject_stringOf(reply,"error.description"));
}
CkHttp_Dispose(http);
CkJsonObject_Dispose(body);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
return;
}
if (CkJsonObject_HasMember(reply,"error.code") == TRUE) {
if (CkJsonObject_IsNullOf(reply,"error.code") != TRUE) {
printf("Submission error %s %s\n",CkJsonObject_stringOf(reply,"error.code"),CkJsonObject_stringOf(reply,"error.description"));
CkHttp_Dispose(http);
CkJsonObject_Dispose(body);
CkHttpResponse_Dispose(resp);
CkJsonObject_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 = CkJsonObject_Create();
i = 0;
while ((i < 2)) {
CkJsonObject_putI(body,i);
appointment = CkJsonObject_stringOf(body,"messages[i].clientMessageId");
phone = CkJsonObject_stringOf(body,"messages[i].to");
result = CkJsonObject_Create();
found = CkJsonObject_FindRecord2(reply,"messages","to",phone,TRUE,result);
CkJsonObject_putI(results,i);
CkJsonObject_UpdateString(results,"appointments[i].clientMessageId",appointment);
if ((found != TRUE) || (CkJsonObject_JsonTypeOf(result,"accepted") != 5)) {
CkJsonObject_UpdateString(results,"appointments[i].outcome","unknown");
printf("%s has no confirmed result.\n",appointment);
}
else {
if (CkJsonObject_BoolOf(result,"accepted") == TRUE) {
CkJsonObject_UpdateString(results,"appointments[i].outcome","accepted");
if (CkJsonObject_JsonTypeOf(result,"apiMessageId") == 1) {
CkJsonObject_UpdateString(results,"appointments[i].apiMessageId",CkJsonObject_stringOf(result,"apiMessageId"));
}
printf("%s accepted.\n",appointment);
}
else {
CkJsonObject_UpdateString(results,"appointments[i].outcome","rejected");
CkJsonObject_UpdateString(results,"appointments[i].reason",CkJsonObject_stringOf(result,"error.description"));
printf("%s rejected: %s\n",appointment,CkJsonObject_stringOf(result,"error.description"));
}
}
i = i + 1;
}
// This demo writes a snapshot. In an application, update the two appointment records.
saved = CkJsonObject_WriteFile(results,"appointment-send-results.json");
if (saved != TRUE) {
printf("Could not save the results. Reconcile the submitted messages before retrying.\n");
CkHttp_Dispose(http);
CkJsonObject_Dispose(body);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
CkJsonObject_Dispose(results);
CkJsonObject_Dispose(result);
return;
}
printf("Saved appointment-send-results.json\n");
CkHttp_Dispose(http);
CkJsonObject_Dispose(body);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(reply);
CkJsonObject_Dispose(results);
CkJsonObject_Dispose(result);
}