Chilkat Online Tools

PureBasic / Infobip WhatsApp / Send WhatsApp interactive buttons message

Back to Collection Items

IncludeFile "CkJsonObject.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkHttpResponse.pb"

Procedure ChilkatExample()

    ; This example assumes 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

    success.i

    ; Use this online tool to generate code from sample JSON: Generate Code to Create JSON

    ; The following JSON is sent in the request body.

    ; {
    ;   "from": "{{senderNumber}}",
    ;   "to": "{{receiverNumber}}",
    ;   "messageId": "test-message-{{$randomInt}}",
    ;   "content": {
    ;     "body": {
    ;       "text": "Some text"
    ;     },
    ;     "action": {
    ;       "buttons": [
    ;         {
    ;           "type": "REPLY",
    ;           "id": "1",
    ;           "title": "Yes"
    ;         },
    ;         {
    ;           "type": "REPLY",
    ;           "id": "2",
    ;           "title": "No"
    ;         }
    ;       ]
    ;     }
    ;   },
    ;   "callbackData": "Callback data"
    ; }

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

    CkJsonObject::ckUpdateString(json,"from","{{senderNumber}}")
    CkJsonObject::ckUpdateString(json,"to","{{receiverNumber}}")
    CkJsonObject::ckUpdateString(json,"messageId","test-message-{{$randomInt}}")
    CkJsonObject::ckUpdateString(json,"content.body.text","Some text")
    CkJsonObject::ckUpdateString(json,"content.action.buttons[0].type","REPLY")
    CkJsonObject::ckUpdateString(json,"content.action.buttons[0].id","1")
    CkJsonObject::ckUpdateString(json,"content.action.buttons[0].title","Yes")
    CkJsonObject::ckUpdateString(json,"content.action.buttons[1].type","REPLY")
    CkJsonObject::ckUpdateString(json,"content.action.buttons[1].id","2")
    CkJsonObject::ckUpdateString(json,"content.action.buttons[1].title","No")
    CkJsonObject::ckUpdateString(json,"callbackData","Callback data")

    CkHttp::ckSetRequestHeader(http,"Authorization","App {{INFOBIP_API_KEY}}")
    CkHttp::ckSetRequestHeader(http,"Content-Type","application/json")

    resp.i = CkHttp::ckPostJson3(http,"https://domain.com/whatsapp/1/message/interactive/buttons","application/json",json)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

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

    CkHttpResponse::ckGetBodySb(resp,sbResponseBody)

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

    CkJsonObject::ckLoadSb(jResp,sbResponseBody)
    CkJsonObject::setCkEmitCompact(jResp, 0)

    Debug "Response Body:"
    Debug CkJsonObject::ckEmit(jResp)

    respStatusCode.i = CkHttpResponse::ckStatusCode(resp)
    Debug "Response Status Code = " + Str(respStatusCode)
    If respStatusCode >= 400
        Debug "Response Header:"
        Debug CkHttpResponse::ckHeader(resp)
        Debug "Failed."
        CkHttpResponse::ckDispose(resp)

        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(json)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    CkHttpResponse::ckDispose(resp)

    ; Sample JSON response:
    ; (Sample code for parsing the JSON response is shown below)

    ; {
    ;   "to": "441134960001",
    ;   "messageCount": 1,
    ;   "messageId": "test-message-290",
    ;   "status": {
    ;     "groupId": 1,
    ;     "groupName": "PENDING",
    ;     "id": 7,
    ;     "name": "PENDING_ENROUTE",
    ;     "description": "Message sent to next instance",
    ;     "action": null
    ;   }
    ; }

    ; Sample code for parsing the JSON response...
    ; Use this online tool to generate parsing code from sample JSON: Generate JSON Parsing Code

    v_to.s = CkJsonObject::ckStringOf(jResp,"to")
    messageCount.i = CkJsonObject::ckIntOf(jResp,"messageCount")
    messageId.s = CkJsonObject::ckStringOf(jResp,"messageId")
    GroupId.i = CkJsonObject::ckIntOf(jResp,"status.groupId")
    GroupName.s = CkJsonObject::ckStringOf(jResp,"status.groupName")
    Id.i = CkJsonObject::ckIntOf(jResp,"status.id")
    Name.s = CkJsonObject::ckStringOf(jResp,"status.name")
    Description.s = CkJsonObject::ckStringOf(jResp,"status.description")
    Action.s = CkJsonObject::ckStringOf(jResp,"status.action")


    CkHttp::ckDispose(http)
    CkJsonObject::ckDispose(json)
    CkStringBuilder::ckDispose(sbResponseBody)
    CkJsonObject::ckDispose(jResp)


    ProcedureReturn
EndProcedure

Curl Command

curl -X POST
	-H "Authorization: App {{INFOBIP_API_KEY}}"
	-H "Content-Type: application/json"
	-d '{
    "from": "{{senderNumber}}",
    "to": "{{receiverNumber}}",
    "messageId": "test-message-{{$randomInt}}",
    "content": {
        "body": {
            "text": "Some text"
        },
        "action": {
            "buttons": [
                {
                    "type": "REPLY",
                    "id": "1",
                    "title": "Yes"
                },
                {
                    "type": "REPLY",
                    "id": "2",
                    "title": "No"
                }
            ]
        }
    },
    "callbackData": "Callback data"
}'
https://domain.com/whatsapp/1/message/interactive/buttons

Postman Collection Item JSON

{
  "name": "Send WhatsApp interactive buttons message",
  "event": [
    {
      "listen": "test",
      "script": {
        "exec": [
          "pm.test(\"Status code is 200\", function () {",
          "    pm.response.to.have.status(200);",
          "});",
          "",
          "pm.test(\"Message sent successfully\", function () {",
          "    var jsonData = pm.response.json();",
          "    pm.expect(jsonData.status.description).to.eql(\"Message sent to next instance\");",
          "});"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"from\": \"{{senderNumber}}\",\n    \"to\": \"{{receiverNumber}}\",\n    \"messageId\": \"test-message-{{$randomInt}}\",\n    \"content\": {\n        \"body\": {\n            \"text\": \"Some text\"\n        },\n        \"action\": {\n            \"buttons\": [\n                {\n                    \"type\": \"REPLY\",\n                    \"id\": \"1\",\n                    \"title\": \"Yes\"\n                },\n                {\n                    \"type\": \"REPLY\",\n                    \"id\": \"2\",\n                    \"title\": \"No\"\n                }\n            ]\n        }\n    },\n    \"callbackData\": \"Callback data\"\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/whatsapp/1/message/interactive/buttons",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "whatsapp",
        "1",
        "message",
        "interactive",
        "buttons"
      ]
    },
    "description": "Send an interactive buttons message to a single recipient. Interactive buttons messages can only be successfully delivered, if the recipient has contacted the business within the last 24 hours, otherwise template message should be used."
  },
  "response": [
    {
      "name": "Interactive buttons message simple",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"from\": \"{{senderNumber}}\",\n    \"to\": \"{{receiverNumber}}\",\n    \"messageId\": \"test-message-{{$randomInt}}\",\n    \"content\": {\n        \"body\": {\n            \"text\": \"Some text\"\n        },\n        \"action\": {\n            \"buttons\": [\n                {\n                    \"type\": \"REPLY\",\n                    \"id\": \"1\",\n                    \"title\": \"Yes\"\n                },\n                {\n                    \"type\": \"REPLY\",\n                    \"id\": \"2\",\n                    \"title\": \"No\"\n                }\n            ]\n        }\n    },\n    \"callbackData\": \"Callback data\"\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/whatsapp/1/message/interactive/buttons",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "whatsapp",
            "1",
            "message",
            "interactive",
            "buttons"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "Vary",
          "value": "Access-Control-Request-Method"
        },
        {
          "key": "Vary",
          "value": "Access-Control-Request-Headers"
        },
        {
          "key": "Date",
          "value": "Mon, 08 Nov 2021 09:10:32 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Server",
          "value": "SMS API"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "key": "X-Request-Id",
          "value": "1636362632822738225"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"to\": \"441134960001\",\n    \"messageCount\": 1,\n    \"messageId\": \"test-message-290\",\n    \"status\": {\n        \"groupId\": 1,\n        \"groupName\": \"PENDING\",\n        \"id\": 7,\n        \"name\": \"PENDING_ENROUTE\",\n        \"description\": \"Message sent to next instance\",\n        \"action\": null\n    }\n}"
    },
    {
      "name": "Interactive buttons message with document header",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"from\": \"{{senderNumber}}\",\n    \"to\": \"{{receiverNumber}}\",\n    \"messageId\": \"test-message-{{$randomInt}}\",\n    \"content\": {\n        \"body\": {\n            \"text\": \"Some text\"\n        },\n        \"action\": {\n            \"buttons\": [\n                {\n                    \"type\": \"REPLY\",\n                    \"id\": \"1\",\n                    \"title\": \"Yes\"\n                },\n                {\n                    \"type\": \"REPLY\",\n                    \"id\": \"2\",\n                    \"title\": \"No\"\n                }\n            ]\n        },\n        \"header\": {\n            \"type\": \"DOCUMENT\",\n            \"mediaUrl\": \"{{exampleDocumentPath}}\",\n            \"filename\": \"document.pdf\"\n        },\n        \"footer\": {\n            \"text\": \"Footer\"\n        }\n    },\n    \"callbackData\": \"Callback data\"\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/whatsapp/1/message/interactive/buttons",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "whatsapp",
            "1",
            "message",
            "interactive",
            "buttons"
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Vary",
          "value": "Origin"
        },
        {
          "key": "Vary",
          "value": "Access-Control-Request-Method"
        },
        {
          "key": "Vary",
          "value": "Access-Control-Request-Headers"
        },
        {
          "key": "Date",
          "value": "Mon, 08 Nov 2021 09:46:19 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Server",
          "value": "SMS API"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "key": "X-Request-Id",
          "value": "1636364779407146838"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"to\": \"441134960001\",\n    \"messageCount\": 1,\n    \"messageId\": \"test-message-448\",\n    \"status\": {\n        \"groupId\": 1,\n        \"groupName\": \"PENDING\",\n        \"id\": 7,\n        \"name\": \"PENDING_ENROUTE\",\n        \"description\": \"Message sent to next instance\",\n        \"action\": null\n    }\n}"
    }
  ]
}