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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoConfig
String sApiHost
Handle hoHttp
Handle hoUrl
Variant vResp
Handle hoResp
Handle hoResult
Integer iCount
Variant vConfirmation
Handle hoConfirmation
Integer i
Handle hoFac
Handle hoHash
String sDigest
Handle hoArchive
String sTemp1
Integer iTemp1
Integer iTemp2
Boolean bTemp1
Boolean bTemp2
Move False To iSuccess
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Store real credentials in private configuration, never in distributed client code.
Get Create (RefClass(cComChilkatJsonObject)) To hoConfig
If (Not(IsComObjectCreated(hoConfig))) Begin
Send CreateComObject of hoConfig
End
Set ComStrict Of hoConfig To True
Get ComLoadFile Of hoConfig "sinch-engage.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load private sinch-engage.json."
Procedure_Return
End
Get ComJsonTypeOf Of hoConfig "api_key" To iTemp1
Get ComStringOfEquals Of hoConfig "api_key" "" True To bTemp1
Get ComJsonTypeOf Of hoConfig "api_secret" To iTemp2
Get ComStringOfEquals Of hoConfig "api_secret" "" True To bTemp2
If ((iTemp1 <> 1) Or (bTemp1 = True) Or (iTemp2 <> 1) Or (bTemp2 = True)) Begin
Showln "The private configuration must contain api_key and api_secret."
Procedure_Return
End
// Choose the host for your Engage account: au, eu, or us. Do not guess from the recipient.
Move "https://au.app.api.sinch.com" To sApiHost
Get Create (RefClass(cComChilkatHttp)) To hoHttp
If (Not(IsComObjectCreated(hoHttp))) Begin
Send CreateComObject of hoHttp
End
Set ComRequireSslCertVerify Of hoHttp To True
Set ComFollowRedirects Of hoHttp To False
Set ComConnectTimeout Of hoHttp To 20
Set ComReadTimeout Of hoHttp To 30
Get ComStringOf Of hoConfig "api_key" To sTemp1
Set ComLogin Of hoHttp To sTemp1
Get ComStringOf Of hoConfig "api_secret" To sTemp1
Set ComPassword Of hoHttp To sTemp1
Set ComBasicAuth Of hoHttp To True
Send ComSetRequestHeader To hoHttp "Accept" "application/json"
Get Create (RefClass(cComChilkatStringBuilder)) To hoUrl
If (Not(IsComObjectCreated(hoUrl))) Begin
Send CreateComObject of hoUrl
End
Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
If (Not(IsComObjectCreated(hoResp))) Begin
Send CreateComObject of hoResp
End
Get Create (RefClass(cComChilkatJsonObject)) To hoResult
If (Not(IsComObjectCreated(hoResult))) Begin
Send CreateComObject of hoResult
End
Set ComStrict Of hoResult To True
Send ComClear To hoUrl
Get ComAppend Of hoUrl sApiHost To iSuccess
Get ComAppend Of hoUrl "/v1/replies" To iSuccess
Get ComGetAsString Of hoUrl To sTemp1
Get pvComObject of hoResp to vResp
Get ComHttpNoBody Of hoHttp "GET" sTemp1 vResp To iSuccess
// HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
Get ComStatusCode Of hoResp To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Showln "HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write."
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
If ((iTemp1 <> 200)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Unexpected HTTP status: " iTemp1
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 429) Begin
Get ComGetHeaderField Of hoResp "Retry-After" To sTemp1
Showln "Retry-After: " sTemp1
End
Procedure_Return
End
Get ComBodyStr Of hoResp To sTemp1
Get ComLoad Of hoResult sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Expected a JSON response."
Procedure_Return
End
Get ComSizeOfArray Of hoResult "replies" To iTemp1
If (iTemp1 < 0) Begin
Showln "Expected an array in the response."
Procedure_Return
End
Get ComSizeOfArray Of hoResult "replies" To iCount
If (iCount = 0) Begin
Showln "No pending records."
Procedure_Return
End
If (iCount > 100) Begin
Showln "Unexpected batch size; nothing was acknowledged."
Procedure_Return
End
Get Create (RefClass(cComChilkatJsonObject)) To hoConfirmation
If (Not(IsComObjectCreated(hoConfirmation))) Begin
Send CreateComObject of hoConfirmation
End
Move 0 To i
While (i < iCount)
Set ComI Of hoResult To i
Set ComI Of hoConfirmation To i
Get ComJsonTypeOf Of hoResult "replies[i].reply_id" To iTemp1
Get ComStringOfEquals Of hoResult "replies[i].reply_id" "" True To bTemp1
If ((iTemp1 <> 1) Or (bTemp1 = True)) Begin
Showln "The response did not contain the expected ID."
Procedure_Return
End
Get ComStringOf Of hoResult "replies[i].reply_id" To sTemp1
Get ComUpdateString Of hoConfirmation "reply_ids[i]" sTemp1 To iSuccess
Move (i + 1) To i
Loop
// Preserve the complete payload, including channel-specific and future fields.
Get Create (RefClass(cComCkFileAccess)) To hoFac
If (Not(IsComObjectCreated(hoFac))) Begin
Send CreateComObject of hoFac
End
Get ComDirEnsureExists Of hoFac "engage-inbox/replies" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not create the private inbox directory; no acknowledgement sent."
Procedure_Return
End
Get Create (RefClass(cComChilkatCrypt2)) To hoHash
If (Not(IsComObjectCreated(hoHash))) Begin
Send CreateComObject of hoHash
End
Set ComHashAlgorithm Of hoHash To "sha256"
Set ComCharset Of hoHash To "utf-8"
Set ComEncodingMode Of hoHash To "hex"
Get ComBodyStr Of hoResp To sTemp1
Get ComHashStringENC Of hoHash sTemp1 To sDigest
Get ComLastMethodSuccess Of hoHash To bTemp1
If (bTemp1 <> True) Begin
Showln "Could not name the archive; no acknowledgement sent."
Procedure_Return
End
Get Create (RefClass(cComChilkatStringBuilder)) To hoArchive
If (Not(IsComObjectCreated(hoArchive))) Begin
Send CreateComObject of hoArchive
End
Get ComAppend Of hoArchive "engage-inbox/replies/" To iSuccess
Get ComAppend Of hoArchive sDigest To iSuccess
Get ComAppend Of hoArchive ".json" To iSuccess
Get ComGetAsString Of hoArchive To sTemp1
Get ComWriteFile Of hoResult sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not save the records; no acknowledgement sent."
Procedure_Return
End
// Acknowledge only after the checked archive write succeeds.
Send ComClear To hoUrl
Get ComAppend Of hoUrl sApiHost To iSuccess
Get ComAppend Of hoUrl "/v1/replies/confirmed" To iSuccess
Get ComGetAsString Of hoUrl To sTemp1
Get pvComObject of hoConfirmation to vConfirmation
Get pvComObject of hoResp to vResp
Get ComHttpJson Of hoHttp "POST" sTemp1 vConfirmation "application/json" vResp To iSuccess
// HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
Get ComStatusCode Of hoResp To iTemp1
If ((iSuccess <> True) And (iTemp1 = 0)) Begin
Showln "HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write."
Procedure_Return
End
Get ComStatusCode Of hoResp To iTemp1
If ((iTemp1 <> 202)) Begin
Get ComStatusCode Of hoResp To iTemp1
Showln "Unexpected HTTP status: " iTemp1
Get ComStatusCode Of hoResp To iTemp1
If (iTemp1 = 429) Begin
Get ComGetHeaderField Of hoResp "Retry-After" To sTemp1
Showln "Retry-After: " sTemp1
End
Procedure_Return
End
Showln "Archived and acknowledged records: " iCount
End_Procedure