Sample code for 30+ languages & platforms
Xbase++ 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 Xbase++ Downloads

Xbase++
LOCAL oHttp
LOCAL oBody
LOCAL oResp
LOCAL nSent
LOCAL oReply
LOCAL nParsed

//  Assumes Chilkat is unlocked. See Global Unlock Sample.
oHttp := CreateObject("Chilkat.Http")
oHttp:RequireSslCertVerify := 1
//  Load the key for this One API integration from your application configuration.
oHttp:SetRequestHeader("Authorization", "ONE_API_KEY")
oHttp:SetRequestHeader("Accept", "application/json")

oBody := CreateObject("Chilkat.JsonObject")
oBody:UpdateString("messages[0].channel", "whatsapp")
oBody:UpdateString("messages[0].to", "PHONE_NUMBER")
oBody: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.
oBody:UpdateString("messages[0].button.items[0].type", "reply")
oBody:UpdateString("messages[0].button.items[0].title", "Confirm")
oBody:UpdateString("messages[0].button.items[0].postbackData", "appt-1042-confirm")
oBody:UpdateString("messages[0].button.items[1].type", "reply")
oBody:UpdateString("messages[0].button.items[1].title", "Reschedule")
oBody:UpdateString("messages[0].button.items[1].postbackData", "appt-1042-reschedule")

oResp := CreateObject("Chilkat.HttpResponse")
nSent := oHttp:HttpJson("POST", "https://platform.clickatell.com/v1/message", oBody, "application/json", oResp)
IF (oResp:StatusCode == 0)
    ? "No HTTP response. Check the message report before resending."
    oHttp:destroy()
    oBody:destroy()
    oResp:destroy()
    RETURN
ENDIF

oReply := CreateObject("Chilkat.JsonObject")
nParsed := oReply:Load(oResp:BodyStr)
IF ((nSent != 1) .OR. (oResp:StatusCode != 202))
    ? "Send returned HTTP " + Str(oResp:StatusCode)
    IF (oReply:HasMember("error.description") == 1)
        ? oReply:StringOf("error.description")
    ENDIF

    oHttp:destroy()
    oBody:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (oReply:HasMember("error.code") == 1)
    IF (oReply:IsNullOf("error.code") != 1)
        ? "Submission error " + oReply:StringOf("error.code") + "  " + oReply:StringOf("error.description")
        oHttp:destroy()
        oBody:destroy()
        oResp:destroy()
        oReply:destroy()
        RETURN
    ENDIF

ENDIF

//  HTTP acceptance and the recipient result are separate checks.
IF ((nParsed != 1) .OR. (oReply:JsonTypeOf("messages[0].accepted") != 5))
    ? "Missing recipient result. Check the message report."
    oHttp:destroy()
    oBody:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (oReply:BoolOf("messages[0].accepted") != 1)
    ? "Message rejected: " + oReply:StringOf("messages[0].error.code") + "  " + oReply:StringOf("messages[0].error.description")
    oHttp:destroy()
    oBody:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

IF (oReply:JsonTypeOf("messages[0].apiMessageId") != 1)
    ? "Accepted without a message ID. Check the message report."
    oHttp:destroy()
    oBody:destroy()
    oResp:destroy()
    oReply:destroy()
    RETURN
ENDIF

? "Accepted. Message ID: " + oReply:StringOf("messages[0].apiMessageId")

oHttp:destroy()
oBody:destroy()
oResp:destroy()
oReply:destroy()