Chilkat Online Tools

PureBasic / Sunshine Conversations API / Create Conversation

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

    CkHttp::setCkBasicAuth(http, 1)
    CkHttp::setCkLogin(http, "username")
    CkHttp::setCkPassword(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.

    ; {
    ;   "type": "personal",
    ;   "participants": [
    ;     {
    ;       "userId": "42589ad070d43be9b00ff7e5",
    ;       "subscribeSDKClient": false
    ;     },
    ;     {
    ;       "userId": "42589ad070d43be9b00ff7e5",
    ;       "subscribeSDKClient": false
    ;     }
    ;   ],
    ;   "displayName": "nost",
    ;   "description": "Conversation between Rogers and Carl.",
    ;   "iconUrl": "https://www.gravatar.com/image.jpg",
    ;   "metadata": {
    ;     "lang": "en-ca"
    ;   }
    ; }

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

    CkJsonObject::ckUpdateString(json,"type","personal")
    CkJsonObject::ckUpdateString(json,"participants[0].userId","42589ad070d43be9b00ff7e5")
    CkJsonObject::ckUpdateBool(json,"participants[0].subscribeSDKClient",0)
    CkJsonObject::ckUpdateString(json,"participants[1].userId","42589ad070d43be9b00ff7e5")
    CkJsonObject::ckUpdateBool(json,"participants[1].subscribeSDKClient",0)
    CkJsonObject::ckUpdateString(json,"displayName","nost")
    CkJsonObject::ckUpdateString(json,"description","Conversation between Rogers and Carl.")
    CkJsonObject::ckUpdateString(json,"iconUrl","https://www.gravatar.com/image.jpg")
    CkJsonObject::ckUpdateString(json,"metadata.lang","en-ca")

    CkHttp::ckSetRequestHeader(http,"Content-Type","application/json")

    resp.i = CkHttp::ckPostJson3(http,"https://domain.com/","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)

    ; {
    ;   "conversation": {
    ;     "id": "c93bb9c14dde8ffb94564eae",
    ;     "type": "personal",
    ;     "activeSwitchboardIntegration": {
    ;       "id": "5ef21b86e933b7355c11c604",
    ;       "name": "bot",
    ;       "integrationId": "5ef21b86e933b7355c11c605",
    ;       "integrationType": "zd:agentWorkspace"
    ;     },
    ;     "pendingSwitchboardIntegration": {
    ;       "id": "5ef21b86e933b7355c11c604",
    ;       "name": "bot",
    ;       "integrationId": "5ef21b86e933b7355c11c605",
    ;       "integrationType": "zd:agentWorkspace"
    ;     },
    ;     "isDefault": false,
    ;     "displayName": "pari",
    ;     "description": "Conversation between Rogers and Carl.",
    ;     "iconUrl": "https://www.gravatar.com/image.jpg",
    ;     "metadata": {
    ;       "lang": "en-ca"
    ;     },
    ;     "businessLastRead": "2020-06-23T14:33:47.492Z",
    ;     "lastUpdatedAt": "2020-06-26T14:33:47.120Z"
    ;   }
    ; }

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

    Id.s = CkJsonObject::ckStringOf(jResp,"conversation.id")
    v_Type.s = CkJsonObject::ckStringOf(jResp,"conversation.type")
    ActiveSwitchboardIntegrationId.s = CkJsonObject::ckStringOf(jResp,"conversation.activeSwitchboardIntegration.id")
    Name.s = CkJsonObject::ckStringOf(jResp,"conversation.activeSwitchboardIntegration.name")
    IntegrationId.s = CkJsonObject::ckStringOf(jResp,"conversation.activeSwitchboardIntegration.integrationId")
    IntegrationType.s = CkJsonObject::ckStringOf(jResp,"conversation.activeSwitchboardIntegration.integrationType")
    PendingSwitchboardIntegrationId.s = CkJsonObject::ckStringOf(jResp,"conversation.pendingSwitchboardIntegration.id")
    PendingSwitchboardIntegrationName.s = CkJsonObject::ckStringOf(jResp,"conversation.pendingSwitchboardIntegration.name")
    PendingSwitchboardIntegrationIntegrationId.s = CkJsonObject::ckStringOf(jResp,"conversation.pendingSwitchboardIntegration.integrationId")
    PendingSwitchboardIntegrationIntegrationType.s = CkJsonObject::ckStringOf(jResp,"conversation.pendingSwitchboardIntegration.integrationType")
    IsDefault.i = CkJsonObject::ckBoolOf(jResp,"conversation.isDefault")
    DisplayName.s = CkJsonObject::ckStringOf(jResp,"conversation.displayName")
    Description.s = CkJsonObject::ckStringOf(jResp,"conversation.description")
    IconUrl.s = CkJsonObject::ckStringOf(jResp,"conversation.iconUrl")
    Lang.s = CkJsonObject::ckStringOf(jResp,"conversation.metadata.lang")
    BusinessLastRead.s = CkJsonObject::ckStringOf(jResp,"conversation.businessLastRead")
    LastUpdatedAt.s = CkJsonObject::ckStringOf(jResp,"conversation.lastUpdatedAt")


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


    ProcedureReturn
EndProcedure

Curl Command

curl -X POST
	-u 'username:password'
	-H "Content-Type: application/json"
	-d '{
    "type": "personal",
    "participants": [
        {
            "userId": "42589ad070d43be9b00ff7e5",
            "subscribeSDKClient": false
        },
        {
            "userId": "42589ad070d43be9b00ff7e5",
            "subscribeSDKClient": false
        }
    ],
    "displayName": "nost",
    "description": "Conversation between Rogers and Carl.",
    "iconUrl": "https://www.gravatar.com/image.jpg",
    "metadata": {
        "lang": "en-ca"
    }
}'
https://domain.com/

Postman Collection Item JSON

{
  "name": "Create Conversation",
  "_postman_id": "6688103e-1c82-402c-9e0f-4b96cca896b4",
  "request": {
    "method": "POST",
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      }
    ],
    "body": {
      "mode": "raw",
      "raw": "{\n    \"type\": \"personal\",\n    \"participants\": [\n        {\n            \"userId\": \"42589ad070d43be9b00ff7e5\",\n            \"subscribeSDKClient\": false\n        },\n        {\n            \"userId\": \"42589ad070d43be9b00ff7e5\",\n            \"subscribeSDKClient\": false\n        }\n    ],\n    \"displayName\": \"nost\",\n    \"description\": \"Conversation between Rogers and Carl.\",\n    \"iconUrl\": \"https://www.gravatar.com/image.jpg\",\n    \"metadata\": {\n        \"lang\": \"en-ca\"\n    }\n}"
    },
    "url": "{{url}}/v2/apps/{{appId}}/conversations",
    "description": "Create a conversation for the specified user(s)."
  },
  "response": [
    {
      "id": "04d65a14-1557-46bb-a278-f68f4139173c",
      "name": "App 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    \"type\": \"personal\",\n    \"participants\": [\n        {\n            \"userId\": \"42589ad070d43be9b00ff7e5\",\n            \"subscribeSDKClient\": false\n        },\n        {\n            \"userId\": \"42589ad070d43be9b00ff7e5\",\n            \"subscribeSDKClient\": false\n        }\n    ],\n    \"displayName\": \"nost\",\n    \"description\": \"Conversation between Rogers and Carl.\",\n    \"iconUrl\": \"https://www.gravatar.com/image.jpg\",\n    \"metadata\": {\n        \"lang\": \"en-ca\"\n    }\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/conversations",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "conversations"
          ],
          "variable": [
            {
              "key": "appId"
            }
          ]
        }
      },
      "status": "Not Found",
      "code": 404,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/plain"
        }
      ],
      "cookie": [
      ],
      "body": ""
    },
    {
      "id": "cb8fc1a2-02f1-4c6b-8a0c-311004d1d2ba",
      "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    \"type\": \"personal\",\n    \"participants\": [\n        {\n            \"userId\": \"42589ad070d43be9b00ff7e5\",\n            \"subscribeSDKClient\": false\n        },\n        {\n            \"userId\": \"42589ad070d43be9b00ff7e5\",\n            \"subscribeSDKClient\": false\n        }\n    ],\n    \"displayName\": \"nost\",\n    \"description\": \"Conversation between Rogers and Carl.\",\n    \"iconUrl\": \"https://www.gravatar.com/image.jpg\",\n    \"metadata\": {\n        \"lang\": \"en-ca\"\n    }\n}"
        },
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/conversations",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "conversations"
          ],
          "variable": [
            {
              "key": "appId"
            }
          ]
        }
      },
      "status": "Created",
      "code": 201,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"conversation\": {\n  \"id\": \"c93bb9c14dde8ffb94564eae\",\n  \"type\": \"personal\",\n  \"activeSwitchboardIntegration\": {\n   \"id\": \"5ef21b86e933b7355c11c604\",\n   \"name\": \"bot\",\n   \"integrationId\": \"5ef21b86e933b7355c11c605\",\n   \"integrationType\": \"zd:agentWorkspace\"\n  },\n  \"pendingSwitchboardIntegration\": {\n   \"id\": \"5ef21b86e933b7355c11c604\",\n   \"name\": \"bot\",\n   \"integrationId\": \"5ef21b86e933b7355c11c605\",\n   \"integrationType\": \"zd:agentWorkspace\"\n  },\n  \"isDefault\": false,\n  \"displayName\": \"pari\",\n  \"description\": \"Conversation between Rogers and Carl.\",\n  \"iconUrl\": \"https://www.gravatar.com/image.jpg\",\n  \"metadata\": {\n   \"lang\": \"en-ca\"\n  },\n  \"businessLastRead\": \"2020-06-23T14:33:47.492Z\",\n  \"lastUpdatedAt\": \"2020-06-26T14:33:47.120Z\"\n }\n}"
    }
  ]
}