Sample code for 30+ languages & platforms
PureBasic

WhatsApp Cloud API Send Message

See more WhatsApp Examples

Demonstrates how to send a message using the WhatsApp Cloud API (Business App)

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkHttpResponse.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    success.i = 0

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

    http.i = CkHttp::ckCreate()
    If http.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ; The following JSON is sent in the request body.

    ; {
    ;   "messaging_product": "whatsapp",
    ;   "to": "16302581871",
    ;   "type": "template",
    ;   "template": {
    ;     "name": "hello_world",
    ;     "language": {
    ;       "code": "en_US"
    ;     }
    ;   }
    ; }

    json.i = CkJsonObject::ckCreate()
    If json.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(json,"messaging_product","whatsapp")
    CkJsonObject::ckUpdateString(json,"to","15555555555")
    CkJsonObject::ckUpdateString(json,"type","template")
    CkJsonObject::ckUpdateString(json,"template.name","hello_world")
    CkJsonObject::ckUpdateString(json,"template.language.code","en_US")

    CkHttp::setCkAuthToken(http, "EAANrS5....yFB9Ma")
    CkHttp::ckSetRequestHeader(http,"Content-Type","application/json")

    resp.i = CkHttpResponse::ckCreate()
    If resp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    success = CkHttp::ckHttpJson(http,"POST","https://graph.facebook.com/v18.0/111111111111111/messages",json,"application/json",resp)
    If success = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkHttpResponse::ckDispose(resp)
        ProcedureReturn
    EndIf

    Debug Str(CkHttpResponse::ckStatusCode(resp))
    Debug CkHttpResponse::ckBodyStr(resp)

    ; Here's a screenshot of our WhatsApp test app.

    ; image


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)
    CkHttpResponse::ckDispose(resp)


    ProcedureReturn
EndProcedure