Chilkat Online Tools

C / Sunshine Conversations API / Create Webhook

Back to Collection Items

#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>

void ChilkatSample(void)
    {
    HCkHttp http;
    BOOL success;
    HCkJsonObject json;
    HCkHttpResponse resp;
    HCkStringBuilder sbResponseBody;
    HCkJsonObject jResp;
    int respStatusCode;
    const char *strVal;
    const char *Target;
    const char *Id;
    const char *Version;
    const char *Secret;
    BOOL IncludeFullUser;
    BOOL IncludeFullSource;
    int i;
    int count_i;

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

    http = CkHttp_Create();

    CkHttp_putBasicAuth(http,TRUE);
    CkHttp_putLogin(http,"username");
    CkHttp_putPassword(http,"password");

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

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

    // {
    //   "target": "http://example.com/callback",
    //   "triggers": [
    //     "conversation:read",
    //     "conversation:message"
    //   ],
    //   "includeFullUser": false,
    //   "includeFullSource": false
    // }

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"target","http://example.com/callback");
    CkJsonObject_UpdateString(json,"triggers[0]","conversation:read");
    CkJsonObject_UpdateString(json,"triggers[1]","conversation:message");
    CkJsonObject_UpdateBool(json,"includeFullUser",FALSE);
    CkJsonObject_UpdateBool(json,"includeFullSource",FALSE);

    CkHttp_SetRequestHeader(http,"Content-Type","application/json");

    resp = CkHttp_PostJson3(http,"https://domain.com/v2/apps/{{appId}}/integrations/:integrationId/webhooks","application/json",json);
    if (CkHttp_getLastMethodSuccess(http) == FALSE) {
        printf("%s\n",CkHttp_lastErrorText(http));
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        return;
    }

    sbResponseBody = CkStringBuilder_Create();
    CkHttpResponse_GetBodySb(resp,sbResponseBody);

    jResp = CkJsonObject_Create();
    CkJsonObject_LoadSb(jResp,sbResponseBody);
    CkJsonObject_putEmitCompact(jResp,FALSE);

    printf("Response Body:\n");
    printf("%s\n",CkJsonObject_emit(jResp));

    respStatusCode = CkHttpResponse_getStatusCode(resp);
    printf("Response Status Code = %d\n",respStatusCode);
    if (respStatusCode >= 400) {
        printf("Response Header:\n");
        printf("%s\n",CkHttpResponse_header(resp));
        printf("Failed.\n");
        CkHttpResponse_Dispose(resp);
        CkHttp_Dispose(http);
        CkJsonObject_Dispose(json);
        CkStringBuilder_Dispose(sbResponseBody);
        CkJsonObject_Dispose(jResp);
        return;
    }

    CkHttpResponse_Dispose(resp);

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

    // {
    //   "webhook": {
    //     "target": "http://example.com/callback",
    //     "triggers": [
    //       "conversation:read",
    //       "conversation:message"
    //     ],
    //     "id": "5e554d2cac66fb73a3c01871",
    //     "version": "v2",
    //     "secret": "8564b3e6a8b20a4bdb68b05ce9bc5936",
    //     "includeFullUser": false,
    //     "includeFullSource": false
    //   }
    // }

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

    // Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.

    Target = CkJsonObject_stringOf(jResp,"webhook.target");
    Id = CkJsonObject_stringOf(jResp,"webhook.id");
    Version = CkJsonObject_stringOf(jResp,"webhook.version");
    Secret = CkJsonObject_stringOf(jResp,"webhook.secret");
    IncludeFullUser = CkJsonObject_BoolOf(jResp,"webhook.includeFullUser");
    IncludeFullSource = CkJsonObject_BoolOf(jResp,"webhook.includeFullSource");
    i = 0;
    count_i = CkJsonObject_SizeOfArray(jResp,"webhook.triggers");
    while (i < count_i) {
        CkJsonObject_putI(jResp,i);
        strVal = CkJsonObject_stringOf(jResp,"webhook.triggers[i]");
        i = i + 1;
    }



    CkHttp_Dispose(http);
    CkJsonObject_Dispose(json);
    CkStringBuilder_Dispose(sbResponseBody);
    CkJsonObject_Dispose(jResp);

    }

Curl Command

curl -X POST
	-u 'username:password'
	-H "Content-Type: application/json"
	-d '{
    "target": "http://example.com/callback",
    "triggers": [
        "conversation:read",
        "conversation:message"
    ],
    "includeFullUser": false,
    "includeFullSource": false
}'
https://domain.com/v2/apps/{{appId}}/integrations/:integrationId/webhooks

Postman Collection Item JSON

{
  "name": "Create Webhook",
  "_postman_id": "47d7af27-93ff-40c9-9e36-a7342a5d4f8a",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"target\": \"http://example.com/callback\",\n    \"triggers\": [\n        \"conversation:read\",\n        \"conversation:message\"\n    ],\n    \"includeFullUser\": false,\n    \"includeFullSource\": false\n}"
    },
    "url": {
      "raw": "{{url}}/v2/apps/{{appId}}/integrations/:integrationId/webhooks",
      "host": [
        "{{url}}"
      ],
      "path": [
        "v2",
        "apps",
        "{{appId}}",
        "integrations",
        ":integrationId",
        "webhooks"
      ],
      "variable": [
        {
          "key": "integrationId"
        }
      ]
    },
    "description": "Creates a new webhook associated with a Sunshine Conversations Connect integration or a custom integration."
  },
  "response": [
    {
      "id": "0c6f800c-d3f7-4e7c-ae06-a6cf1e7985d9",
      "name": "Forbidden",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": {
              "content": "Added as a part of security scheme: bearer",
              "type": "text/plain"
            },
            "key": "Authorization",
            "value": "Bearer <token>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"target\": \"http://example.com/callback\",\n    \"triggers\": [\n        \"conversation:read\",\n        \"conversation:message\"\n    ],\n    \"includeFullUser\": false,\n    \"includeFullSource\": false\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/integrations/:integrationId/webhooks",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "integrations",
            ":integrationId",
            "webhooks"
          ],
          "variable": [
            {
              "key": "appId"
            },
            {
              "key": "integrationId"
            }
          ]
        }
      },
      "status": "Forbidden",
      "code": 403,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/plain"
        }
      ],
      "cookie": [
      ],
      "body": ""
    },
    {
      "id": "1645606e-cd40-40f5-ae34-6fe7c379886d",
      "name": "Created",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": {
              "content": "Added as a part of security scheme: bearer",
              "type": "text/plain"
            },
            "key": "Authorization",
            "value": "Bearer <token>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"target\": \"http://example.com/callback\",\n    \"triggers\": [\n        \"conversation:read\",\n        \"conversation:message\"\n    ],\n    \"includeFullUser\": false,\n    \"includeFullSource\": false\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/integrations/:integrationId/webhooks",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "integrations",
            ":integrationId",
            "webhooks"
          ],
          "variable": [
            {
              "key": "appId"
            },
            {
              "key": "integrationId"
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"webhook\": {\n  \"target\": \"http://example.com/callback\",\n  \"triggers\": [\n   \"conversation:read\",\n   \"conversation:message\"\n  ],\n  \"id\": \"5e554d2cac66fb73a3c01871\",\n  \"version\": \"v2\",\n  \"secret\": \"8564b3e6a8b20a4bdb68b05ce9bc5936\",\n  \"includeFullUser\": false,\n  \"includeFullSource\": false\n }\n}"
    },
    {
      "id": "7cf11ba6-34bb-4e40-9301-a40bbe5499e6",
      "name": "Not Found",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": {
              "content": "Added as a part of security scheme: bearer",
              "type": "text/plain"
            },
            "key": "Authorization",
            "value": "Bearer <token>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"target\": \"http://example.com/callback\",\n    \"triggers\": [\n        \"conversation:read\",\n        \"conversation:message\"\n    ],\n    \"includeFullUser\": false,\n    \"includeFullSource\": false\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/integrations/:integrationId/webhooks",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "integrations",
            ":integrationId",
            "webhooks"
          ],
          "variable": [
            {
              "key": "appId"
            },
            {
              "key": "integrationId"
            }
          ]
        }
      },
      "status": "Not Found",
      "code": 404,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/plain"
        }
      ],
      "cookie": [
      ],
      "body": ""
    },
    {
      "id": "a4f6e8c6-3d78-46b6-af4d-2a4fd70c8295",
      "name": "Bad Request",
      "originalRequest": {
        "method": "POST",
        "header": [
          {
            "description": {
              "content": "Added as a part of security scheme: bearer",
              "type": "text/plain"
            },
            "key": "Authorization",
            "value": "Bearer <token>"
          }
        ],
        "body": {
          "mode": "raw",
          "raw": "{\n    \"target\": \"http://example.com/callback\",\n    \"triggers\": [\n        \"conversation:read\",\n        \"conversation:message\"\n    ],\n    \"includeFullUser\": false,\n    \"includeFullSource\": false\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/integrations/:integrationId/webhooks",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "integrations",
            ":integrationId",
            "webhooks"
          ],
          "variable": [
            {
              "key": "appId"
            },
            {
              "key": "integrationId"
            }
          ]
        }
      },
      "status": "Bad Request",
      "code": 400,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/plain"
        }
      ],
      "cookie": [
      ],
      "body": ""
    }
  ]
}