Sinch Engage: Archive and Acknowledge Replies
See more Sinch Engage Examples
Fetch one batch of up to 100 pending replies, save the entire batch in a private local inbox, then acknowledge only those record IDs. Acknowledged records stop appearing in subsequent polls. Run again to drain further batches; this is a queue, not page-number pagination.
This example treats a checked local archive write as acceptance for later processing. A production inbox should commit durable, atomic storage and deduplicate by account plus reply_id before applying business effects. A failed acknowledgement can cause redelivery. Coordinate consumers: polling covers the account and acknowledgements affect other pollers. Protect the archive because it contains personal data.
Do not assume every reply has content: RCS free-text replies can use a body array. Saving the whole JSON preserves both forms. Suggestion taps require webhooks and do not appear here. Replies may be unsolicited and need not match a message_id.
Sinch Engage is the renamed MessageMedia platform. This uses the Engage API, not Sinch Conversation API credentials or endpoints. Create an Engage API key/secret and place {"api_key":"YOUR_KEY","api_secret":"YOUR_SECRET"} in private sinch-engage.json. Select the account's regional HTTPS host: https://au.app.api.sinch.com, https://eu.app.api.sinch.com, or https://us.app.api.sinch.com. Basic authentication here means the API key/secret over verified TLS. Do not ship account credentials in a public mobile, browser, or desktop application.
Transport success and API success are checked separately. On HTTP 429, honor Retry-After before a bounded retry. Check account permissions and regional credentials for 401/403. A timeout or 5xx response after submitting a write can leave its outcome uncertain: reconcile before retrying, especially sends and resource creation. Metadata is correlation data, not an idempotency key. Inspect error bodies privately because they can contain phone numbers and message content.
Chilkat C Downloads
#include <C_CkJsonObject.h>
#include <C_CkHttp.h>
#include <C_CkStringBuilder.h>
#include <C_CkHttpResponse.h>
#include <C_CkFileAccess.h>
#include <C_CkCrypt2.h>
void ChilkatSample(void)
{
BOOL success;
HCkJsonObject config;
const char *apiHost;
HCkHttp http;
HCkStringBuilder url;
HCkHttpResponse resp;
HCkJsonObject result;
int count;
HCkJsonObject confirmation;
int i;
HCkFileAccess fac;
HCkCrypt2 hash;
const char *digest;
HCkStringBuilder archive;
success = FALSE;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Store real credentials in private configuration, never in distributed client code.
config = CkJsonObject_Create();
CkJsonObject_putStrict(config,TRUE);
success = CkJsonObject_LoadFile(config,"sinch-engage.json");
if (success != TRUE) {
printf("Could not load private sinch-engage.json.\n");
CkJsonObject_Dispose(config);
return;
}
if ((CkJsonObject_JsonTypeOf(config,"api_key") != 1) || (CkJsonObject_StringOfEquals(config,"api_key","",TRUE) == TRUE) || (CkJsonObject_JsonTypeOf(config,"api_secret") != 1) || (CkJsonObject_StringOfEquals(config,"api_secret","",TRUE) == TRUE)) {
printf("The private configuration must contain api_key and api_secret.\n");
CkJsonObject_Dispose(config);
return;
}
// Choose the host for your Engage account: au, eu, or us. Do not guess from the recipient.
apiHost = "https://au.app.api.sinch.com";
http = CkHttp_Create();
CkHttp_putRequireSslCertVerify(http,TRUE);
CkHttp_putFollowRedirects(http,FALSE);
CkHttp_putConnectTimeout(http,20);
CkHttp_putReadTimeout(http,30);
CkHttp_putLogin(http,CkJsonObject_stringOf(config,"api_key"));
CkHttp_putPassword(http,CkJsonObject_stringOf(config,"api_secret"));
CkHttp_putBasicAuth(http,TRUE);
CkHttp_SetRequestHeader(http,"Accept","application/json");
url = CkStringBuilder_Create();
resp = CkHttpResponse_Create();
result = CkJsonObject_Create();
CkJsonObject_putStrict(result,TRUE);
CkStringBuilder_Clear(url);
CkStringBuilder_Append(url,apiHost);
CkStringBuilder_Append(url,"/v1/replies");
success = CkHttp_HttpNoBody(http,"GET",CkStringBuilder_getAsString(url),resp);
// HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
if ((success != TRUE) && (CkHttpResponse_getStatusCode(resp) == 0)) {
printf("HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write.\n");
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
return;
}
if ((CkHttpResponse_getStatusCode(resp) != 200)) {
printf("Unexpected HTTP status: %d\n",CkHttpResponse_getStatusCode(resp));
if (CkHttpResponse_getStatusCode(resp) == 429) {
printf("Retry-After: %s\n",CkHttpResponse_getHeaderField(resp,"Retry-After"));
}
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
return;
}
success = CkJsonObject_Load(result,CkHttpResponse_bodyStr(resp));
if (success != TRUE) {
printf("Expected a JSON response.\n");
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
return;
}
if (CkJsonObject_SizeOfArray(result,"replies") < 0) {
printf("Expected an array in the response.\n");
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
return;
}
count = CkJsonObject_SizeOfArray(result,"replies");
if (count == 0) {
printf("No pending records.\n");
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
return;
}
if (count > 100) {
printf("Unexpected batch size; nothing was acknowledged.\n");
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
return;
}
confirmation = CkJsonObject_Create();
i = 0;
while ((i < count)) {
CkJsonObject_putI(result,i);
CkJsonObject_putI(confirmation,i);
if ((CkJsonObject_JsonTypeOf(result,"replies[i].reply_id") != 1) || (CkJsonObject_StringOfEquals(result,"replies[i].reply_id","",TRUE) == TRUE)) {
printf("The response did not contain the expected ID.\n");
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(confirmation);
return;
}
CkJsonObject_UpdateString(confirmation,"reply_ids[i]",CkJsonObject_stringOf(result,"replies[i].reply_id"));
i = i + 1;
}
// Preserve the complete payload, including channel-specific and future fields.
fac = CkFileAccess_Create();
success = CkFileAccess_DirEnsureExists(fac,"engage-inbox/replies");
if (success != TRUE) {
printf("Could not create the private inbox directory; no acknowledgement sent.\n");
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(confirmation);
CkFileAccess_Dispose(fac);
return;
}
hash = CkCrypt2_Create();
CkCrypt2_putHashAlgorithm(hash,"sha256");
CkCrypt2_putCharset(hash,"utf-8");
CkCrypt2_putEncodingMode(hash,"hex");
digest = CkCrypt2_hashStringENC(hash,CkHttpResponse_bodyStr(resp));
if (CkCrypt2_getLastMethodSuccess(hash) != TRUE) {
printf("Could not name the archive; no acknowledgement sent.\n");
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(confirmation);
CkFileAccess_Dispose(fac);
CkCrypt2_Dispose(hash);
return;
}
archive = CkStringBuilder_Create();
CkStringBuilder_Append(archive,"engage-inbox/replies/");
CkStringBuilder_Append(archive,digest);
CkStringBuilder_Append(archive,".json");
success = CkJsonObject_WriteFile(result,CkStringBuilder_getAsString(archive));
if (success != TRUE) {
printf("Could not save the records; no acknowledgement sent.\n");
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(confirmation);
CkFileAccess_Dispose(fac);
CkCrypt2_Dispose(hash);
CkStringBuilder_Dispose(archive);
return;
}
// Acknowledge only after the checked archive write succeeds.
CkStringBuilder_Clear(url);
CkStringBuilder_Append(url,apiHost);
CkStringBuilder_Append(url,"/v1/replies/confirmed");
success = CkHttp_HttpJson(http,"POST",CkStringBuilder_getAsString(url),confirmation,"application/json",resp);
// HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
if ((success != TRUE) && (CkHttpResponse_getStatusCode(resp) == 0)) {
printf("HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write.\n");
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(confirmation);
CkFileAccess_Dispose(fac);
CkCrypt2_Dispose(hash);
CkStringBuilder_Dispose(archive);
return;
}
if ((CkHttpResponse_getStatusCode(resp) != 202)) {
printf("Unexpected HTTP status: %d\n",CkHttpResponse_getStatusCode(resp));
if (CkHttpResponse_getStatusCode(resp) == 429) {
printf("Retry-After: %s\n",CkHttpResponse_getHeaderField(resp,"Retry-After"));
}
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(confirmation);
CkFileAccess_Dispose(fac);
CkCrypt2_Dispose(hash);
CkStringBuilder_Dispose(archive);
return;
}
printf("Archived and acknowledged records: %d\n",count);
CkJsonObject_Dispose(config);
CkHttp_Dispose(http);
CkStringBuilder_Dispose(url);
CkHttpResponse_Dispose(resp);
CkJsonObject_Dispose(result);
CkJsonObject_Dispose(confirmation);
CkFileAccess_Dispose(fac);
CkCrypt2_Dispose(hash);
CkStringBuilder_Dispose(archive);
}