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

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoHttp
    Variant vBody
    Handle hoBody
    Boolean iSuccess
    Variant vResp
    Handle hoResp
    Boolean iSent
    Handle hoReply
    Boolean iParsed
    String sTemp1
    String sTemp2
    Integer iTemp1
    Boolean bTemp1

    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Set ComRequireSslCertVerify Of hoHttp To True
    //  Load the key for this One API integration from your application configuration.
    Send ComSetRequestHeader To hoHttp "Authorization" "ONE_API_KEY"
    Send ComSetRequestHeader To hoHttp "Accept" "application/json"

    Get Create (RefClass(cComChilkatJsonObject)) To hoBody
    If (Not(IsComObjectCreated(hoBody))) Begin
        Send CreateComObject of hoBody
    End
    Get ComUpdateString Of hoBody "messages[0].channel" "whatsapp" To iSuccess
    Get ComUpdateString Of hoBody "messages[0].to" "PHONE_NUMBER" To iSuccess
    Get ComUpdateString Of hoBody "messages[0].button.body" "Can you attend your appointment tomorrow at 10:00?" To iSuccess
    //  The visible titles can be translated. Each button has a distinct application ID.
    Get ComUpdateString Of hoBody "messages[0].button.items[0].type" "reply" To iSuccess
    Get ComUpdateString Of hoBody "messages[0].button.items[0].title" "Confirm" To iSuccess
    Get ComUpdateString Of hoBody "messages[0].button.items[0].postbackData" "appt-1042-confirm" To iSuccess
    Get ComUpdateString Of hoBody "messages[0].button.items[1].type" "reply" To iSuccess
    Get ComUpdateString Of hoBody "messages[0].button.items[1].title" "Reschedule" To iSuccess
    Get ComUpdateString Of hoBody "messages[0].button.items[1].postbackData" "appt-1042-reschedule" To iSuccess

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoBody to vBody
    Get pvComObject of hoResp to vResp
    Get ComHttpJson Of hoHttp "POST" "https://platform.clickatell.com/v1/message" vBody "application/json" vResp To iSent
    Get ComStatusCode Of hoResp To iTemp1
    If (iTemp1 = 0) Begin
        Showln "No HTTP response. Check the message report before resending."
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatJsonObject)) To hoReply
    If (Not(IsComObjectCreated(hoReply))) Begin
        Send CreateComObject of hoReply
    End
    Get ComBodyStr Of hoResp To sTemp1
    Get ComLoad Of hoReply sTemp1 To iParsed
    Get ComStatusCode Of hoResp To iTemp1
    If ((iSent <> True) Or (iTemp1 <> 202)) Begin
        Get ComStatusCode Of hoResp To iTemp1
        Showln "Send returned HTTP " iTemp1
        Get ComHasMember Of hoReply "error.description" To bTemp1
        If (bTemp1 = True) Begin
            Get ComStringOf Of hoReply "error.description" To sTemp1
            Showln sTemp1
        End

        Procedure_Return
    End

    Get ComHasMember Of hoReply "error.code" To bTemp1
    If (bTemp1 = True) Begin
        Get ComIsNullOf Of hoReply "error.code" To bTemp1
        If (bTemp1 <> True) Begin
            Get ComStringOf Of hoReply "error.code" To sTemp1
            Get ComStringOf Of hoReply "error.description" To sTemp2
            Showln "Submission error " sTemp1 "  " sTemp2
            Procedure_Return
        End

    End

    //  HTTP acceptance and the recipient result are separate checks.
    Get ComJsonTypeOf Of hoReply "messages[0].accepted" To iTemp1
    If ((iParsed <> True) Or (iTemp1 <> 5)) Begin
        Showln "Missing recipient result. Check the message report."
        Procedure_Return
    End

    Get ComBoolOf Of hoReply "messages[0].accepted" To bTemp1
    If (bTemp1 <> True) Begin
        Get ComStringOf Of hoReply "messages[0].error.code" To sTemp1
        Get ComStringOf Of hoReply "messages[0].error.description" To sTemp2
        Showln "Message rejected: " sTemp1 "  " sTemp2
        Procedure_Return
    End

    Get ComJsonTypeOf Of hoReply "messages[0].apiMessageId" To iTemp1
    If (iTemp1 <> 1) Begin
        Showln "Accepted without a message ID. Check the message report."
        Procedure_Return
    End

    Get ComStringOf Of hoReply "messages[0].apiMessageId" To sTemp1
    Showln "Accepted. Message ID: " sTemp1


End_Procedure