Sample code for 30+ languages & platforms
PowerBuilder Requires Chilkat v11.6.0+

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.

Sinch Engage API reference

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Config
string ls_ApiHost
oleobject loo_Http
oleobject loo_Url
oleobject loo_Resp
oleobject loo_Result
long li_Count
oleobject loo_Confirmation
long i
oleobject loo_Fac
oleobject loo_Hash
string ls_Digest
oleobject loo_Archive

li_Success = 0

//  Assumes Chilkat is unlocked. See Global Unlock Sample.

//  Store real credentials in private configuration, never in distributed client code.
loo_Config = create oleobject
li_rc = loo_Config.ConnectToNewObject("Chilkat.JsonObject")
if li_rc < 0 then
    destroy loo_Config
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Config.Strict = 1
li_Success = loo_Config.LoadFile("sinch-engage.json")
if li_Success <> 1 then
    Write-Debug "Could not load private sinch-engage.json."
    destroy loo_Config
    return
end if

if (loo_Config.JsonTypeOf("api_key") <> 1) OR (loo_Config.StringOfEquals("api_key","",1) = 1) OR (loo_Config.JsonTypeOf("api_secret") <> 1) OR (loo_Config.StringOfEquals("api_secret","",1) = 1) then
    Write-Debug "The private configuration must contain api_key and api_secret."
    destroy loo_Config
    return
end if

//  Choose the host for your Engage account: au, eu, or us. Do not guess from the recipient.
ls_ApiHost = "https://au.app.api.sinch.com"
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")

loo_Http.RequireSslCertVerify = 1
loo_Http.FollowRedirects = 0
loo_Http.ConnectTimeout = 20
loo_Http.ReadTimeout = 30
loo_Http.Login = loo_Config.StringOf("api_key")
loo_Http.Password = loo_Config.StringOf("api_secret")
loo_Http.BasicAuth = 1
loo_Http.SetRequestHeader("Accept","application/json")
loo_Url = create oleobject
li_rc = loo_Url.ConnectToNewObject("Chilkat.StringBuilder")

loo_Resp = create oleobject
li_rc = loo_Resp.ConnectToNewObject("Chilkat.HttpResponse")

loo_Result = create oleobject
li_rc = loo_Result.ConnectToNewObject("Chilkat.JsonObject")

loo_Result.Strict = 1
loo_Url.Clear()
loo_Url.Append(ls_ApiHost)
loo_Url.Append("/v1/replies")
li_Success = loo_Http.HttpNoBody("GET",loo_Url.GetAsString(),loo_Resp)
//  HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
if (li_Success <> 1) AND (loo_Resp.StatusCode = 0) then
    Write-Debug "HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write."
    destroy loo_Config
    destroy loo_Http
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Result
    return
end if

if (loo_Resp.StatusCode <> 200) then
    Write-Debug "Unexpected HTTP status: " + string(loo_Resp.StatusCode)
    if loo_Resp.StatusCode = 429 then
        Write-Debug "Retry-After: " + loo_Resp.GetHeaderField("Retry-After")
    end if

    destroy loo_Config
    destroy loo_Http
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Result
    return
end if

li_Success = loo_Result.Load(loo_Resp.BodyStr)
if li_Success <> 1 then
    Write-Debug "Expected a JSON response."
    destroy loo_Config
    destroy loo_Http
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Result
    return
end if

if loo_Result.SizeOfArray("replies") < 0 then
    Write-Debug "Expected an array in the response."
    destroy loo_Config
    destroy loo_Http
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Result
    return
end if

li_Count = loo_Result.SizeOfArray("replies")
if li_Count = 0 then
    Write-Debug "No pending records."
    destroy loo_Config
    destroy loo_Http
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Result
    return
end if

if li_Count > 100 then
    Write-Debug "Unexpected batch size; nothing was acknowledged."
    destroy loo_Config
    destroy loo_Http
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Result
    return
end if

loo_Confirmation = create oleobject
li_rc = loo_Confirmation.ConnectToNewObject("Chilkat.JsonObject")

i = 0
do while (i < li_Count)
    loo_Result.I = i
    loo_Confirmation.I = i
    if (loo_Result.JsonTypeOf("replies[i].reply_id") <> 1) OR (loo_Result.StringOfEquals("replies[i].reply_id","",1) = 1) then
        Write-Debug "The response did not contain the expected ID."
        destroy loo_Config
        destroy loo_Http
        destroy loo_Url
        destroy loo_Resp
        destroy loo_Result
        destroy loo_Confirmation
        return
    end if

    loo_Confirmation.UpdateString("reply_ids[i]",loo_Result.StringOf("replies[i].reply_id"))
    i = i + 1
loop
//  Preserve the complete payload, including channel-specific and future fields.
loo_Fac = create oleobject
li_rc = loo_Fac.ConnectToNewObject("Chilkat.FileAccess")

li_Success = loo_Fac.DirEnsureExists("engage-inbox/replies")
if li_Success <> 1 then
    Write-Debug "Could not create the private inbox directory; no acknowledgement sent."
    destroy loo_Config
    destroy loo_Http
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Result
    destroy loo_Confirmation
    destroy loo_Fac
    return
end if

loo_Hash = create oleobject
li_rc = loo_Hash.ConnectToNewObject("Chilkat.Crypt2")

loo_Hash.HashAlgorithm = "sha256"
loo_Hash.Charset = "utf-8"
loo_Hash.EncodingMode = "hex"
ls_Digest = loo_Hash.HashStringENC(loo_Resp.BodyStr)
if loo_Hash.LastMethodSuccess <> 1 then
    Write-Debug "Could not name the archive; no acknowledgement sent."
    destroy loo_Config
    destroy loo_Http
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Result
    destroy loo_Confirmation
    destroy loo_Fac
    destroy loo_Hash
    return
end if

loo_Archive = create oleobject
li_rc = loo_Archive.ConnectToNewObject("Chilkat.StringBuilder")

loo_Archive.Append("engage-inbox/replies/")
loo_Archive.Append(ls_Digest)
loo_Archive.Append(".json")
li_Success = loo_Result.WriteFile(loo_Archive.GetAsString())
if li_Success <> 1 then
    Write-Debug "Could not save the records; no acknowledgement sent."
    destroy loo_Config
    destroy loo_Http
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Result
    destroy loo_Confirmation
    destroy loo_Fac
    destroy loo_Hash
    destroy loo_Archive
    return
end if

//  Acknowledge only after the checked archive write succeeds.
loo_Url.Clear()
loo_Url.Append(ls_ApiHost)
loo_Url.Append("/v1/replies/confirmed")
li_Success = loo_Http.HttpJson("POST",loo_Url.GetAsString(),loo_Confirmation,"application/json",loo_Resp)
//  HttpJson/HttpNoBody also return false for HTTP 4xx/5xx responses.
if (li_Success <> 1) AND (loo_Resp.StatusCode = 0) then
    Write-Debug "HTTP transport failed. Inspect diagnostics privately; do not blindly retry a write."
    destroy loo_Config
    destroy loo_Http
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Result
    destroy loo_Confirmation
    destroy loo_Fac
    destroy loo_Hash
    destroy loo_Archive
    return
end if

if (loo_Resp.StatusCode <> 202) then
    Write-Debug "Unexpected HTTP status: " + string(loo_Resp.StatusCode)
    if loo_Resp.StatusCode = 429 then
        Write-Debug "Retry-After: " + loo_Resp.GetHeaderField("Retry-After")
    end if

    destroy loo_Config
    destroy loo_Http
    destroy loo_Url
    destroy loo_Resp
    destroy loo_Result
    destroy loo_Confirmation
    destroy loo_Fac
    destroy loo_Hash
    destroy loo_Archive
    return
end if

Write-Debug "Archived and acknowledged records: " + string(li_Count)


destroy loo_Config
destroy loo_Http
destroy loo_Url
destroy loo_Resp
destroy loo_Result
destroy loo_Confirmation
destroy loo_Fac
destroy loo_Hash
destroy loo_Archive