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 <CkHttpW.h>
#include <CkJsonObjectW.h>
#include <CkHttpResponseW.h>
void ChilkatSample(void)
{
// Assumes Chilkat is unlocked. See Global Unlock Sample.
CkHttpW http;
http.put_RequireSslCertVerify(true);
// Load the key for this One API integration from your application configuration.
http.SetRequestHeader(L"Authorization",L"ONE_API_KEY");
http.SetRequestHeader(L"Accept",L"application/json");
const wchar_t *firstPhone = L"FIRST_PHONE_NUMBER";
const wchar_t *secondPhone = L"SECOND_PHONE_NUMBER";
CkJsonObjectW body;
body.UpdateString(L"messages[0].channel",L"sms");
body.UpdateString(L"messages[0].to",firstPhone);
body.UpdateString(L"messages[0].content",L"Your appointment with Lee is at 10:00.");
body.UpdateString(L"messages[0].clientMessageId",L"appt-1042");
body.UpdateString(L"messages[1].channel",L"sms");
body.UpdateString(L"messages[1].to",secondPhone);
body.UpdateString(L"messages[1].content",L"Your appointment with Sam is at 14:30.");
body.UpdateString(L"messages[1].clientMessageId",L"appt-1043");
CkHttpResponseW resp;
bool sent = http.HttpJson(L"POST",L"https://platform.clickatell.com/v1/message",body,L"application/json",resp);
if (resp.get_StatusCode() == 0) {
wprintf(L"No HTTP response. Check the message report before resending.\n");
return;
}
CkJsonObjectW reply;
bool parsed = reply.Load(resp.bodyStr());
if ((sent != true) || ((resp.get_StatusCode() != 202) && (resp.get_StatusCode() != 207))) {
wprintf(L"Send returned HTTP %d\n",resp.get_StatusCode());
if (reply.HasMember(L"error.description") == true) {
wprintf(L"%s\n",reply.stringOf(L"error.description"));
}
return;
}
if (reply.HasMember(L"error.code") == true) {
if (reply.IsNullOf(L"error.code") != true) {
wprintf(L"Submission error %s %s\n",reply.stringOf(L"error.code"),reply.stringOf(L"error.description"));
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.
CkJsonObjectW results;
int i = 0;
while ((i < 2)) {
body.put_I(i);
const wchar_t *appointment = body.stringOf(L"messages[i].clientMessageId");
const wchar_t *phone = body.stringOf(L"messages[i].to");
CkJsonObjectW result;
bool found = reply.FindRecord2(L"messages",L"to",phone,true,result);
results.put_I(i);
results.UpdateString(L"appointments[i].clientMessageId",appointment);
if ((found != true) || (result.JsonTypeOf(L"accepted") != 5)) {
results.UpdateString(L"appointments[i].outcome",L"unknown");
wprintf(L"%s has no confirmed result.\n",appointment);
}
else {
if (result.BoolOf(L"accepted") == true) {
results.UpdateString(L"appointments[i].outcome",L"accepted");
if (result.JsonTypeOf(L"apiMessageId") == 1) {
results.UpdateString(L"appointments[i].apiMessageId",result.stringOf(L"apiMessageId"));
}
wprintf(L"%s accepted.\n",appointment);
}
else {
results.UpdateString(L"appointments[i].outcome",L"rejected");
results.UpdateString(L"appointments[i].reason",result.stringOf(L"error.description"));
wprintf(L"%s rejected: %s\n",appointment,result.stringOf(L"error.description"));
}
}
i = i + 1;
}
// This demo writes a snapshot. In an application, update the two appointment records.
bool saved = results.WriteFile(L"appointment-send-results.json");
if (saved != true) {
wprintf(L"Could not save the results. Reconcile the submitted messages before retrying.\n");
return;
}
wprintf(L"Saved appointment-send-results.json\n");
}