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 Lua Downloads
local chilkat = require("chilkat")
-- Assumes Chilkat is unlocked. See Global Unlock Sample.
-- Store real credentials in private configuration, never in distributed client code.
local config = chilkat.JsonObject.new()
config.strict = true
local success = config:load_file("sinch-engage.json")
if not success then
print("Could not load private sinch-engage.json.")
return
end
if (config:json_type_of("api_key") ~= 1) or ((config:string_of_equals("api_key", "", true)) or ((config:json_type_of("api_secret") ~= 1) or (config:string_of_equals("api_secret", "", true)))) then
print("The private configuration must contain api_key and api_secret.")
return
end
-- Choose the host for your Engage account: au, eu, or us. Do not guess from the recipient.
local apiHost = "https://au.app.api.sinch.com"
local http = chilkat.Http.new()
http.require_ssl_cert_verify = true
http.follow_redirects = false
http.connect_timeout = 20
http.read_timeout = 30
http.login = config:string_of("api_key")
http.password = config:string_of("api_secret")
http.basic_auth = true
http:set_request_header("Accept", "application/json")
local url = chilkat.StringBuilder.new()
local resp = chilkat.HttpResponse.new()
local result = chilkat.JsonObject.new()
result.strict = true
url:clear()
url:append(apiHost)
url:append("/v1/replies")
success = http:http_no_body("GET", url:get_as_string(), resp)
-- HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
if (not success) and (resp.status_code == 0) then
print("HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write.")
return
end
if resp.status_code ~= 200 then
print("Unexpected HTTP status: " .. resp.status_code)
if resp.status_code == 429 then
print("Retry-After: " .. resp:get_header_field("Retry-After"))
end
return
end
success = result:load(resp.body_str)
if not success then
print("Expected a JSON response.")
return
end
if result:size_of_array("replies") < 0 then
print("Expected an array in the response.")
return
end
local count = result:size_of_array("replies")
if count == 0 then
print("No pending records.")
return
end
if count > 100 then
print("Unexpected batch size; nothing was acknowledged.")
return
end
local confirmation = chilkat.JsonObject.new()
local i = 0
while i < count do
result.i = i
confirmation.i = i
if (result:json_type_of("replies[i].reply_id") ~= 1) or (result:string_of_equals("replies[i].reply_id", "", true)) then
print("The response did not contain the expected ID.")
return
end
confirmation:update_string("reply_ids[i]", result:string_of("replies[i].reply_id"))
i = i + 1
end
-- Preserve the complete payload, including channel-specific and future fields.
local fac = chilkat.FileAccess.new()
success = fac:dir_ensure_exists("engage-inbox/replies")
if not success then
print("Could not create the private inbox directory; no acknowledgement sent.")
return
end
local hash = chilkat.Crypt2.new()
hash.hash_algorithm = "sha256"
hash.charset = "utf-8"
hash.encoding_mode = "hex"
local digest = hash:hash_string_enc(resp.body_str)
if not hash.last_method_success then
print("Could not name the archive; no acknowledgement sent.")
return
end
local archive = chilkat.StringBuilder.new()
archive:append("engage-inbox/replies/")
archive:append(digest)
archive:append(".json")
success = result:write_file(archive:get_as_string())
if not success then
print("Could not save the records; no acknowledgement sent.")
return
end
-- Acknowledge only after the checked archive write succeeds.
url:clear()
url:append(apiHost)
url:append("/v1/replies/confirmed")
success = http:http_json("POST", url:get_as_string(), confirmation, "application/json", resp)
-- HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
if (not success) and (resp.status_code == 0) then
print("HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write.")
return
end
if resp.status_code ~= 202 then
print("Unexpected HTTP status: " .. resp.status_code)
if resp.status_code == 429 then
print("Retry-After: " .. resp:get_header_field("Retry-After"))
end
return
end
print("Archived and acknowledged records: " .. count)