Chilkat Online Tools

SQL Server / Sunshine Conversations API / Create Conversation

Back to Collection Items

-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @iTmp0 int
    -- Important: Do not use nvarchar(max).  See the warning about using nvarchar(max).
    DECLARE @sTmp0 nvarchar(4000)
    -- This example assumes the Chilkat API to have been previously unlocked.
    -- See Global Unlock Sample for sample code.

    DECLARE @http int
    -- Use "Chilkat_9_5_0.Http" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    DECLARE @success int

    EXEC sp_OASetProperty @http, 'BasicAuth', 1
    EXEC sp_OASetProperty @http, 'Login', 'username'
    EXEC sp_OASetProperty @http, 'Password', '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"
    --   }
    -- }

    DECLARE @json int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @json OUT

    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'type', 'personal'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'participants[0].userId', '42589ad070d43be9b00ff7e5'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'participants[0].subscribeSDKClient', 0
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'participants[1].userId', '42589ad070d43be9b00ff7e5'
    EXEC sp_OAMethod @json, 'UpdateBool', @success OUT, 'participants[1].subscribeSDKClient', 0
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'displayName', 'nost'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'description', 'Conversation between Rogers and Carl.'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'iconUrl', 'https://www.gravatar.com/image.jpg'
    EXEC sp_OAMethod @json, 'UpdateString', @success OUT, 'metadata.lang', 'en-ca'

    EXEC sp_OAMethod @http, 'SetRequestHeader', NULL, 'Content-Type', 'application/json'

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/', 'application/json', @json
    EXEC sp_OAGetProperty @http, 'LastMethodSuccess', @iTmp0 OUT
    IF @iTmp0 = 0
      BEGIN
        EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        RETURN
      END

    DECLARE @sbResponseBody int
    -- Use "Chilkat_9_5_0.StringBuilder" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbResponseBody OUT

    EXEC sp_OAMethod @resp, 'GetBodySb', @success OUT, @sbResponseBody

    DECLARE @jResp int
    -- Use "Chilkat_9_5_0.JsonObject" for versions of Chilkat < 10.0.0
    EXEC @hr = sp_OACreate 'Chilkat.JsonObject', @jResp OUT

    EXEC sp_OAMethod @jResp, 'LoadSb', @success OUT, @sbResponseBody
    EXEC sp_OASetProperty @jResp, 'EmitCompact', 0


    PRINT 'Response Body:'
    EXEC sp_OAMethod @jResp, 'Emit', @sTmp0 OUT
    PRINT @sTmp0

    DECLARE @respStatusCode int
    EXEC sp_OAGetProperty @resp, 'StatusCode', @respStatusCode OUT

    PRINT 'Response Status Code = ' + @respStatusCode
    IF @respStatusCode >= 400
      BEGIN

        PRINT 'Response Header:'
        EXEC sp_OAGetProperty @resp, 'Header', @sTmp0 OUT
        PRINT @sTmp0

        PRINT 'Failed.'
        EXEC @hr = sp_OADestroy @resp

        EXEC @hr = sp_OADestroy @http
        EXEC @hr = sp_OADestroy @json
        EXEC @hr = sp_OADestroy @sbResponseBody
        EXEC @hr = sp_OADestroy @jResp
        RETURN
      END
    EXEC @hr = sp_OADestroy @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

    DECLARE @Id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Id OUT, 'conversation.id'
    DECLARE @v_Type nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_Type OUT, 'conversation.type'
    DECLARE @ActiveSwitchboardIntegrationId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @ActiveSwitchboardIntegrationId OUT, 'conversation.activeSwitchboardIntegration.id'
    DECLARE @Name nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Name OUT, 'conversation.activeSwitchboardIntegration.name'
    DECLARE @IntegrationId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @IntegrationId OUT, 'conversation.activeSwitchboardIntegration.integrationId'
    DECLARE @IntegrationType nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @IntegrationType OUT, 'conversation.activeSwitchboardIntegration.integrationType'
    DECLARE @PendingSwitchboardIntegrationId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @PendingSwitchboardIntegrationId OUT, 'conversation.pendingSwitchboardIntegration.id'
    DECLARE @PendingSwitchboardIntegrationName nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @PendingSwitchboardIntegrationName OUT, 'conversation.pendingSwitchboardIntegration.name'
    DECLARE @PendingSwitchboardIntegrationIntegrationId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @PendingSwitchboardIntegrationIntegrationId OUT, 'conversation.pendingSwitchboardIntegration.integrationId'
    DECLARE @PendingSwitchboardIntegrationIntegrationType nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @PendingSwitchboardIntegrationIntegrationType OUT, 'conversation.pendingSwitchboardIntegration.integrationType'
    DECLARE @IsDefault int
    EXEC sp_OAMethod @jResp, 'BoolOf', @IsDefault OUT, 'conversation.isDefault'
    DECLARE @DisplayName nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @DisplayName OUT, 'conversation.displayName'
    DECLARE @Description nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Description OUT, 'conversation.description'
    DECLARE @IconUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @IconUrl OUT, 'conversation.iconUrl'
    DECLARE @Lang nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Lang OUT, 'conversation.metadata.lang'
    DECLARE @BusinessLastRead nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @BusinessLastRead OUT, 'conversation.businessLastRead'
    DECLARE @LastUpdatedAt nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @LastUpdatedAt OUT, 'conversation.lastUpdatedAt'

    EXEC @hr = sp_OADestroy @http
    EXEC @hr = sp_OADestroy @json
    EXEC @hr = sp_OADestroy @sbResponseBody
    EXEC @hr = sp_OADestroy @jResp


END
GO

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}"
    }
  ]
}