Chilkat Online Tools

PowerBuilder / Infobip WhatsApp / Create WhatsApp Template

Back to Collection Items

integer li_rc
oleobject loo_Http
integer li_Success
oleobject loo_Json
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
string ls_Id
integer li_BusinessAccountId
string ls_Name
string ls_Language
string ls_Status
string ls_Category
string ls_Body
string ls_V_Type

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

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat_9_5_0.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

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

// The following JSON is sent in the request body.

// {
//   "name": "example_template_name_{{$randomInt}}_{{$randomInt}}",
//   "language": "en",
//   "category": "ACCOUNT_UPDATE",
//   "structure": {
//     "body": "body {{1}} content",
//     "type": "TEXT"
//   }
// }

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_Json.UpdateString("name","example_template_name_{{$randomInt}}_{{$randomInt}}")
loo_Json.UpdateString("language","en")
loo_Json.UpdateString("category","ACCOUNT_UPDATE")
loo_Json.UpdateString("structure.body","body {{1}} content")
loo_Json.UpdateString("structure.type","TEXT")

loo_Http.SetRequestHeader("Authorization","App {{INFOBIP_API_KEY}}")
loo_Http.SetRequestHeader("Content-Type","application/json")

loo_Resp = loo_Http.PostJson3("https://domain.com/whatsapp/1/senders/:sender/templates","application/json",loo_Json)
if loo_Http.LastMethodSuccess = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Json
    return
end if

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

loo_Resp.GetBodySb(loo_SbResponseBody)

loo_JResp = create oleobject
li_rc = loo_JResp.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0

Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()

li_RespStatusCode = loo_Resp.StatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
    Write-Debug "Response Header:"
    Write-Debug loo_Resp.Header
    Write-Debug "Failed."
    destroy loo_Resp
    destroy loo_Http
    destroy loo_Json
    destroy loo_SbResponseBody
    destroy loo_JResp
    return
end if

destroy loo_Resp

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

// {
//   "id": "1072288653542716",
//   "businessAccountId": 2125987241021795,
//   "name": "example_template_name_767_413",
//   "language": "en",
//   "status": "PENDING",
//   "category": "ACCOUNT_UPDATE",
//   "structure": {
//     "body": "body {{1}} content",
//     "type": "TEXT"
//   }
// }

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

ls_Id = loo_JResp.StringOf("id")
li_BusinessAccountId = loo_JResp.IntOf("businessAccountId")
ls_Name = loo_JResp.StringOf("name")
ls_Language = loo_JResp.StringOf("language")
ls_Status = loo_JResp.StringOf("status")
ls_Category = loo_JResp.StringOf("category")
ls_Body = loo_JResp.StringOf("structure.body")
ls_V_Type = loo_JResp.StringOf("structure.type")


destroy loo_Http
destroy loo_Json
destroy loo_SbResponseBody
destroy loo_JResp

Curl Command

curl -X POST
	-H "Authorization: App {{INFOBIP_API_KEY}}"
	-H "Content-Type: application/json"
	-d '{
    "name": "example_template_name_{{$randomInt}}_{{$randomInt}}",
    "language": "en",
    "category": "ACCOUNT_UPDATE",
    "structure": {
        "body": "body {{1}} content",
        "type": "TEXT"
    }
}'
https://domain.com/whatsapp/1/senders/:sender/templates

Postman Collection Item JSON

{
  "name": "Create WhatsApp Template",
  "event": [
    {
      "listen": "prerequest",
      "script": {
        "exec": [
          ""
        ],
        "type": "text/javascript"
      }
    },
    {
      "listen": "test",
      "script": {
        "exec": [
          "pm.test(\"Status code is 201, template created\", function () {",
          "    pm.response.to.have.status(201);",
          "});"
        ],
        "type": "text/javascript"
      }
    }
  ],
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"name\": \"example_template_name_{{$randomInt}}_{{$randomInt}}\",\n    \"language\": \"en\",\n    \"category\": \"ACCOUNT_UPDATE\",\n    \"structure\": {\n        \"body\": \"body {{1}} content\",\n        \"type\": \"TEXT\"\n    }\n}",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "url": {
      "raw": "{{baseUrl}}/whatsapp/1/senders/:sender/templates",
      "host": [
        "{{baseUrl}}"
      ],
      "path": [
        "whatsapp",
        "1",
        "senders",
        ":sender",
        "templates"
      ],
      "variable": [
        {
          "key": "sender",
          "value": "{{senderNumber}}",
          "description": "(Required) Registered WhatsApp sender number. Must be in international format."
        }
      ]
    },
    "description": "Create WhatsApp template. Created template will be submitted for WhatsApp's review and approval. Once approved, template can be sent to end-users. Refer to [template guidelines](https://www.infobip.com/docs/whatsapp/message-types#guidelines-amp-best-practices) for additional info."
  },
  "response": [
    {
      "name": "Text template simple",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"name\": \"example_template_name_{{$randomInt}}_{{$randomInt}}\",\n    \"language\": \"en\",\n    \"category\": \"ACCOUNT_UPDATE\",\n    \"structure\": {\n        \"body\": \"body {{1}} content\",\n        \"type\": \"TEXT\"\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/whatsapp/1/senders/:sender/templates",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "whatsapp",
            "1",
            "senders",
            ":sender",
            "templates"
          ],
          "variable": [
            {
              "key": "sender",
              "value": "{{senderNumber}}",
              "description": "(Required) Registered WhatsApp sender number. Must be in international format."
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_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 10:36:58 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Server",
          "value": "SMS API"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "key": "X-Request-Id",
          "value": "1636367818279895837"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"id\": \"1072288653542716\",\n    \"businessAccountId\": 2125987241021795,\n    \"name\": \"example_template_name_767_413\",\n    \"language\": \"en\",\n    \"status\": \"PENDING\",\n    \"category\": \"ACCOUNT_UPDATE\",\n    \"structure\": {\n        \"body\": \"body {{1}} content\",\n        \"type\": \"TEXT\"\n    }\n}"
    },
    {
      "name": "Complex template with buttons and image",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "key": "Content-Type",
            "value": "application/json"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"name\": \"example_template_name_{{$randomInt}}_{{$randomInt}}\",\n    \"language\": \"en\",\n    \"category\": \"ACCOUNT_UPDATE\",\n    \"structure\": {\n        \"header\": {\n            \"format\": \"IMAGE\"\n        },\n        \"body\": \"body {{1}} content\",\n        \"footer\": \"this is footer\",\n        \"buttons\": [\n            {\n                \"text\": \"Yes\",\n                \"type\": \"QUICK_REPLY\"\n            },\n            {\n                \"text\": \"No\",\n                \"type\": \"QUICK_REPLY\"\n            }\n        ],\n        \"type\": \"MEDIA\"\n    }\n}",
          "options": {
            "raw": {
              "language": "json"
            }
          }
        },
        "url": {
          "raw": "{{baseUrl}}/whatsapp/1/senders/:sender/templates",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "whatsapp",
            "1",
            "senders",
            ":sender",
            "templates"
          ],
          "variable": [
            {
              "key": "sender",
              "value": "{{senderNumber}}",
              "description": "(Required) Registered WhatsApp sender number. Must be in international format."
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_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 10:50:20 GMT"
        },
        {
          "key": "Content-Type",
          "value": "application/json"
        },
        {
          "key": "Server",
          "value": "SMS API"
        },
        {
          "key": "Transfer-Encoding",
          "value": "chunked"
        },
        {
          "key": "X-Request-Id",
          "value": "1636368620308994838"
        }
      ],
      "cookie": [
      ],
      "body": "{\n    \"id\": \"452107629693501\",\n    \"businessAccountId\": 2125987241021795,\n    \"name\": \"example_template_name_225_786\",\n    \"language\": \"en\",\n    \"status\": \"PENDING\",\n    \"category\": \"ACCOUNT_UPDATE\",\n    \"structure\": {\n        \"header\": {\n            \"format\": \"IMAGE\"\n        },\n        \"body\": \"body {{1}} content\",\n        \"footer\": \"this is footer\",\n        \"buttons\": [\n            {\n                \"text\": \"Yes\",\n                \"type\": \"QUICK_REPLY\"\n            },\n            {\n                \"text\": \"No\",\n                \"type\": \"QUICK_REPLY\"\n            }\n        ],\n        \"type\": \"MEDIA\"\n    }\n}"
    }
  ]
}