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

Clickatell One API: Send an approved WhatsApp shipment template

See more Clickatell Examples

Send a business-initiated shipment update, including outside an open customer service window. First approve a template on the WhatsApp integration with two positional body placeholders for the order number and delivery date. Supply its exact name and approved language. The keys 1 and 2 refer to those placeholders in order.

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"

    //  Example approved wording: Order {{1}} is due for delivery on {{2}}.
    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].template.templateName" "shipment_update" To iSuccess
    Get ComUpdateString Of hoBody "messages[0].template.language" "en_US" To iSuccess
    Get ComUpdateString Of hoBody "messages[0].template.body.parameters.1" "ORD-1042" To iSuccess
    Get ComUpdateString Of hoBody "messages[0].template.body.parameters.2" "March 19" 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