Chilkat Online Tools

PowerBuilder / Sunshine Conversations API / Create Client

Back to Collection Items

integer li_rc
oleobject loo_Http
integer li_Success
oleobject loo_Json
oleobject loo_Resp
oleobject loo_SbResponseBody
oleobject loo_JResp
integer li_RespStatusCode
string ls_Id
string ls_V_Type
string ls_Status
string ls_IntegrationId
string ls_ExternalId
string ls_LastSeen
string ls_LinkedAt
string ls_DisplayName
string ls_AvatarUrl

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

loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat_9_5_0.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if

loo_Http.BasicAuth = 1
loo_Http.Login = "username"
loo_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"
//   }
// }

loo_Json = create oleobject
li_rc = loo_Json.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

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

loo_Http.SetRequestHeader("Content-Type","application/json")

loo_Resp = loo_Http.PostJson3("https://domain.com/v2/apps/{{appId}}/users/:userIdOrExternalId/clients","application/json",loo_Json)
if loo_Http.LastMethodSuccess = 0 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_Json
    return
end if

loo_SbResponseBody = create oleobject
li_rc = loo_SbResponseBody.ConnectToNewObject("Chilkat_9_5_0.StringBuilder")

loo_Resp.GetBodySb(loo_SbResponseBody)

loo_JResp = create oleobject
li_rc = loo_JResp.ConnectToNewObject("Chilkat_9_5_0.JsonObject")

loo_JResp.LoadSb(loo_SbResponseBody)
loo_JResp.EmitCompact = 0

Write-Debug "Response Body:"
Write-Debug loo_JResp.Emit()

li_RespStatusCode = loo_Resp.StatusCode
Write-Debug "Response Status Code = " + string(li_RespStatusCode)
if li_RespStatusCode >= 400 then
    Write-Debug "Response Header:"
    Write-Debug loo_Resp.Header
    Write-Debug "Failed."
    destroy loo_Resp
    destroy loo_Http
    destroy loo_Json
    destroy loo_SbResponseBody
    destroy loo_JResp
    return
end if

destroy loo_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

ls_Id = loo_JResp.StringOf("client.id")
ls_V_Type = loo_JResp.StringOf("client.type")
ls_Status = loo_JResp.StringOf("client.status")
ls_IntegrationId = loo_JResp.StringOf("client.integrationId")
ls_ExternalId = loo_JResp.StringOf("client.externalId")
ls_LastSeen = loo_JResp.StringOf("client.lastSeen")
ls_LinkedAt = loo_JResp.StringOf("client.linkedAt")
ls_DisplayName = loo_JResp.StringOf("client.displayName")
ls_AvatarUrl = loo_JResp.StringOf("client.avatarUrl")


destroy loo_Http
destroy loo_Json
destroy loo_SbResponseBody
destroy loo_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}"
    }
  ]
}