Chilkat Online Tools

SQL Server / Sunshine Conversations API / Create Client

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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.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.

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

    DECLARE @json int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.JsonObject', @json OUT

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

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

    DECLARE @resp int
    EXEC sp_OAMethod @http, 'PostJson3', @resp OUT, 'https://domain.com/v2/apps/{{appId}}/users/:userIdOrExternalId/clients', '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
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.StringBuilder', @sbResponseBody OUT

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

    DECLARE @jResp int
    EXEC @hr = sp_OACreate 'Chilkat_9_5_0.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)

    -- {
    --   "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

    DECLARE @Id nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Id OUT, 'client.id'
    DECLARE @v_Type nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @v_Type OUT, 'client.type'
    DECLARE @Status nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @Status OUT, 'client.status'
    DECLARE @IntegrationId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @IntegrationId OUT, 'client.integrationId'
    DECLARE @ExternalId nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @ExternalId OUT, 'client.externalId'
    DECLARE @LastSeen nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @LastSeen OUT, 'client.lastSeen'
    DECLARE @LinkedAt nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @LinkedAt OUT, 'client.linkedAt'
    DECLARE @DisplayName nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @DisplayName OUT, 'client.displayName'
    DECLARE @AvatarUrl nvarchar(4000)
    EXEC sp_OAMethod @jResp, 'StringOf', @AvatarUrl OUT, 'client.avatarUrl'

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