Sample code for 30+ languages & platforms
DataFlex

WaTrend Send WhatsApp Text

See more WaTrend Examples

Send a WhatsApp text.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoHttp
    Variant vReq
    Handle hoReq
    Handle hoSbUrl
    String sResponseBodyStr
    Variant vResp
    Handle hoResp
    Variant vSbResponseBody
    Handle hoSbResponseBody
    Integer iRespStatusCode
    Handle hoJResp
    String sStatus
    String sTemp1

    Move False To iSuccess

    // This example assumes the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End

    // Use your actual access token instead of 555555555555555555555555555555
    Get Create (RefClass(cComChilkatHttpRequest)) To hoReq
    If (Not(IsComObjectCreated(hoReq))) Begin
        Send CreateComObject of hoReq
    End
    Send ComAddParam To hoReq "number" "84933313xxx"
    Send ComAddParam To hoReq "type" "text"
    Send ComAddParam To hoReq "message" "This is a test message"
    Send ComAddParam To hoReq "instance_id" "609ACF283XXXX"
    Send ComAddParam To hoReq "access_token" "555555555555555555555555555555"

    // Note: The WaTrend online documentation indicate a POST should be used.
    // However, it seems you might actually need to send a GET request.
    // It is unclear.  
    // If a GET is neeed, you would just send to the URL w/ query params like this:
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbUrl
    If (Not(IsComObjectCreated(hoSbUrl))) Begin
        Send CreateComObject of hoSbUrl
    End
    Get ComAppend Of hoSbUrl "https://app.watrend.com/api/send.php?" To iSuccess
    Get ComGetUrlEncodedParams Of hoReq To sTemp1
    Get ComAppend Of hoSbUrl sTemp1 To iSuccess
    Get ComGetAsString Of hoSbUrl To sTemp1
    Get ComQuickGetStr Of hoHttp sTemp1 To sResponseBodyStr
    // The responseBodyStr contains the JSON response from the server..

    Set ComHttpVerb Of hoReq To "POST"
    Set ComContentType Of hoReq To "application/x-www-form-urlencoded"

    Get Create (RefClass(cComChilkatHttpResponse)) To hoResp
    If (Not(IsComObjectCreated(hoResp))) Begin
        Send CreateComObject of hoResp
    End
    Get pvComObject of hoReq to vReq
    Get pvComObject of hoResp to vResp
    Get ComHttpReq Of hoHttp "https://app.watrend.com/api/send.php" vReq vResp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbResponseBody
    If (Not(IsComObjectCreated(hoSbResponseBody))) Begin
        Send CreateComObject of hoSbResponseBody
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComGetBodySb Of hoResp vSbResponseBody To iSuccess

    Get ComStatusCode Of hoResp To iRespStatusCode
    Showln "Response Status Code = " iRespStatusCode
    If (iRespStatusCode >= 400) Begin
        Showln "Response Header:"
        Get ComHeader Of hoResp To sTemp1
        Showln sTemp1
        Showln "Failed."
        Procedure_Return
    End

    Get ComBodyStr Of hoResp To sTemp1
    Showln sTemp1

    // Both success and failed responses use 200 status code.

    // A success response contains this JSON in the response body:
    // {"status":"success", ... }

    // A failed response will contain something like this:
    // {"status":"error","message":"License Invalidated"}

    Get Create (RefClass(cComChilkatJsonObject)) To hoJResp
    If (Not(IsComObjectCreated(hoJResp))) Begin
        Send CreateComObject of hoJResp
    End
    Get pvComObject of hoSbResponseBody to vSbResponseBody
    Get ComLoadSb Of hoJResp vSbResponseBody To iSuccess

    Get ComStringOf Of hoJResp "status" To sStatus
    Showln "status: " sStatus


End_Procedure