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

Clickatell One API: Send WhatsApp reply buttons

See more Clickatell Examples

Send an appointment question with Confirm and Reschedule buttons within 24 hours of the customer’s last WhatsApp message. This standalone example ends when Clickatell returns the send result. The response gives the outbound message ID. A customer’s eventual selection is a separate inbound event.

Clickatell API reference

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
oleobject loo_Http
oleobject loo_Body
oleobject loo_Resp
long li_Sent
oleobject loo_Reply
long li_Parsed

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_Http.RequireSslCertVerify = 1
//  Load the key for this One API integration from your application configuration.
loo_Http.SetRequestHeader("Authorization","ONE_API_KEY")
loo_Http.SetRequestHeader("Accept","application/json")

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

loo_Body.UpdateString("messages[0].channel","whatsapp")
loo_Body.UpdateString("messages[0].to","PHONE_NUMBER")
loo_Body.UpdateString("messages[0].button.body","Can you attend your appointment tomorrow at 10:00?")
//  The visible titles can be translated. Each button has a distinct application ID.
loo_Body.UpdateString("messages[0].button.items[0].type","reply")
loo_Body.UpdateString("messages[0].button.items[0].title","Confirm")
loo_Body.UpdateString("messages[0].button.items[0].postbackData","appt-1042-confirm")
loo_Body.UpdateString("messages[0].button.items[1].type","reply")
loo_Body.UpdateString("messages[0].button.items[1].title","Reschedule")
loo_Body.UpdateString("messages[0].button.items[1].postbackData","appt-1042-reschedule")

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

li_Sent = loo_Http.HttpJson("POST","https://platform.clickatell.com/v1/message",loo_Body,"application/json",loo_Resp)
if loo_Resp.StatusCode = 0 then
    Write-Debug "No HTTP response. Check the message report before resending."
    destroy loo_Http
    destroy loo_Body
    destroy loo_Resp
    return
end if

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

li_Parsed = loo_Reply.Load(loo_Resp.BodyStr)
if (li_Sent <> 1) OR (loo_Resp.StatusCode <> 202) then
    Write-Debug "Send returned HTTP " + string(loo_Resp.StatusCode)
    if loo_Reply.HasMember("error.description") = 1 then
        Write-Debug loo_Reply.StringOf("error.description")
    end if

    destroy loo_Http
    destroy loo_Body
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if loo_Reply.HasMember("error.code") = 1 then
    if loo_Reply.IsNullOf("error.code") <> 1 then
        Write-Debug "Submission error " + loo_Reply.StringOf("error.code") + "  " + loo_Reply.StringOf("error.description")
        destroy loo_Http
        destroy loo_Body
        destroy loo_Resp
        destroy loo_Reply
        return
    end if

end if

//  HTTP acceptance and the recipient result are separate checks.
if (li_Parsed <> 1) OR (loo_Reply.JsonTypeOf("messages[0].accepted") <> 5) then
    Write-Debug "Missing recipient result. Check the message report."
    destroy loo_Http
    destroy loo_Body
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if loo_Reply.BoolOf("messages[0].accepted") <> 1 then
    Write-Debug "Message rejected: " + loo_Reply.StringOf("messages[0].error.code") + "  " + loo_Reply.StringOf("messages[0].error.description")
    destroy loo_Http
    destroy loo_Body
    destroy loo_Resp
    destroy loo_Reply
    return
end if

if loo_Reply.JsonTypeOf("messages[0].apiMessageId") <> 1 then
    Write-Debug "Accepted without a message ID. Check the message report."
    destroy loo_Http
    destroy loo_Body
    destroy loo_Resp
    destroy loo_Reply
    return
end if

Write-Debug "Accepted. Message ID: " + loo_Reply.StringOf("messages[0].apiMessageId")


destroy loo_Http
destroy loo_Body
destroy loo_Resp
destroy loo_Reply