Chilkat Online Tools

C / Sunshine Conversations API / Create Client

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 *Id;
    const char *v_Type;
    const char *Status;
    const char *IntegrationId;
    const char *ExternalId;
    const char *LastSeen;
    const char *LinkedAt;
    const char *DisplayName;
    const char *AvatarUrl;

    // 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.

    // {
    //   "matchCriteria": {
    //     "type": "mailgun",
    //     "integrationId": "582dedf230e788746891281a",
    //     "primary": true,
    //     "address": "steveb@channel5.com",
    //     "subject": "New message from {appName}"
    //   },
    //   "confirmation": {
    //     "type": "immediate",
    //     "message": {
    //       "author": {
    //         "type": "business",
    //         "displayName": "Steve",
    //         "avatarUrl": "https://www.gravatar.com/image.jpg"
    //       },
    //       "content": {
    //         "type": "text",
    //         "text": "Hello!"
    //       },
    //       "metadata": {
    //         "lang": "en-ca"
    //       }
    //     }
    //   },
    //   "target": {
    //     "conversationId": "029c31f25a21b47effd7be90"
    //   }
    // }

    json = CkJsonObject_Create();
    CkJsonObject_UpdateString(json,"matchCriteria.type","mailgun");
    CkJsonObject_UpdateString(json,"matchCriteria.integrationId","582dedf230e788746891281a");
    CkJsonObject_UpdateBool(json,"matchCriteria.primary",TRUE);
    CkJsonObject_UpdateString(json,"matchCriteria.address","steveb@channel5.com");
    CkJsonObject_UpdateString(json,"matchCriteria.subject","New message from {appName}");
    CkJsonObject_UpdateString(json,"confirmation.type","immediate");
    CkJsonObject_UpdateString(json,"confirmation.message.author.type","business");
    CkJsonObject_UpdateString(json,"confirmation.message.author.displayName","Steve");
    CkJsonObject_UpdateString(json,"confirmation.message.author.avatarUrl","https://www.gravatar.com/image.jpg");
    CkJsonObject_UpdateString(json,"confirmation.message.content.type","text");
    CkJsonObject_UpdateString(json,"confirmation.message.content.text","Hello!");
    CkJsonObject_UpdateString(json,"confirmation.message.metadata.lang","en-ca");
    CkJsonObject_UpdateString(json,"target.conversationId","029c31f25a21b47effd7be90");

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

    resp = CkHttp_PostJson3(http,"https://domain.com/v2/apps/{{appId}}/users/:userIdOrExternalId/clients","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)

    // {
    //   "client": {
    //     "id": "5c9a34160c89726709136733",
    //     "type": "telegram",
    //     "status": "active",
    //     "integrationId": "582dedf230e788746891281a",
    //     "externalId": "your-own-id",
    //     "lastSeen": "2020-08-20T16:13:07.462Z",
    //     "linkedAt": "2020-06-23T14:33:47.492Z",
    //     "displayName": "Steve",
    //     "avatarUrl": "http://AKMiIDqXoOMCR.yuTZ,HeliL7IBkYuXRbpDPiRbgkrM.MhjHjbX2QqdToaIYNnnWYb8",
    //     "info": {},
    //     "raw": {}
    //   }
    // }

    // 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.

    Id = CkJsonObject_stringOf(jResp,"client.id");
    v_Type = CkJsonObject_stringOf(jResp,"client.type");
    Status = CkJsonObject_stringOf(jResp,"client.status");
    IntegrationId = CkJsonObject_stringOf(jResp,"client.integrationId");
    ExternalId = CkJsonObject_stringOf(jResp,"client.externalId");
    LastSeen = CkJsonObject_stringOf(jResp,"client.lastSeen");
    LinkedAt = CkJsonObject_stringOf(jResp,"client.linkedAt");
    DisplayName = CkJsonObject_stringOf(jResp,"client.displayName");
    AvatarUrl = CkJsonObject_stringOf(jResp,"client.avatarUrl");


    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 '{
    "matchCriteria": {
        "type": "mailgun",
        "integrationId": "582dedf230e788746891281a",
        "primary": true,
        "address": "steveb@channel5.com",
        "subject": "New message from {appName}"
    },
    "confirmation": {
        "type": "immediate",
        "message": {
            "author": {
                "type": "business",
                "displayName": "Steve",
                "avatarUrl": "https://www.gravatar.com/image.jpg"
            },
            "content": {
                "type": "text",
                "text": "Hello!"
            },
            "metadata": {
                "lang": "en-ca"
            }
        }
    },
    "target": {
        "conversationId": "029c31f25a21b47effd7be90"
    }
}'
https://domain.com/v2/apps/{{appId}}/users/:userIdOrExternalId/clients

Postman Collection Item JSON

{
  "name": "Create Client",
  "_postman_id": "e51d89dc-e2b1-4839-9eed-67dc8387cdaf",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"matchCriteria\": {\n        \"type\": \"mailgun\",\n        \"integrationId\": \"582dedf230e788746891281a\",\n        \"primary\": true,\n        \"address\": \"steveb@channel5.com\",\n        \"subject\": \"New message from {appName}\"\n    },\n    \"confirmation\": {\n        \"type\": \"immediate\",\n        \"message\": {\n            \"author\": {\n                \"type\": \"business\",\n                \"displayName\": \"Steve\",\n                \"avatarUrl\": \"https://www.gravatar.com/image.jpg\"\n            },\n            \"content\": {\n                \"type\": \"text\",\n                \"text\": \"Hello!\"\n            },\n            \"metadata\": {\n                \"lang\": \"en-ca\"\n            }\n        }\n    },\n    \"target\": {\n        \"conversationId\": \"029c31f25a21b47effd7be90\"\n    }\n}"
    },
    "url": {
      "raw": "{{url}}/v2/apps/{{appId}}/users/:userIdOrExternalId/clients",
      "host": [
        "{{url}}"
      ],
      "path": [
        "v2",
        "apps",
        "{{appId}}",
        "users",
        ":userIdOrExternalId",
        "clients"
      ],
      "variable": [
        {
          "key": "userIdOrExternalId"
        }
      ]
    },
    "description": "Create a client and link it to a channel specified by the matchCriteria.type."
  },
  "response": [
    {
      "id": "74e580bd-67c5-486e-a95e-0e37794255b3",
      "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    \"matchCriteria\": {\n        \"type\": \"mailgun\",\n        \"integrationId\": \"582dedf230e788746891281a\",\n        \"primary\": true,\n        \"address\": \"steveb@channel5.com\",\n        \"subject\": \"New message from {appName}\"\n    },\n    \"confirmation\": {\n        \"type\": \"immediate\",\n        \"message\": {\n            \"author\": {\n                \"type\": \"business\",\n                \"displayName\": \"Steve\",\n                \"avatarUrl\": \"https://www.gravatar.com/image.jpg\"\n            },\n            \"content\": {\n                \"type\": \"text\",\n                \"text\": \"Hello!\"\n            },\n            \"metadata\": {\n                \"lang\": \"en-ca\"\n            }\n        }\n    },\n    \"target\": {\n        \"conversationId\": \"029c31f25a21b47effd7be90\"\n    }\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/users/:userIdOrExternalId/clients",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "users",
            ":userIdOrExternalId",
            "clients"
          ],
          "variable": [
            {
              "key": "appId"
            },
            {
              "key": "userIdOrExternalId"
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"client\": {\n  \"id\": \"5c9a34160c89726709136733\",\n  \"type\": \"telegram\",\n  \"status\": \"active\",\n  \"integrationId\": \"582dedf230e788746891281a\",\n  \"externalId\": \"your-own-id\",\n  \"lastSeen\": \"2020-08-20T16:13:07.462Z\",\n  \"linkedAt\": \"2020-06-23T14:33:47.492Z\",\n  \"displayName\": \"Steve\",\n  \"avatarUrl\": \"http://AKMiIDqXoOMCR.yuTZ,HeliL7IBkYuXRbpDPiRbgkrM.MhjHjbX2QqdToaIYNnnWYb8\",\n  \"info\": {},\n  \"raw\": {}\n }\n}"
    }
  ]
}