Chilkat Online Tools

PureBasic / Sunshine Conversations API / List Messages

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

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

    CkJsonObject::ckUpdateString(queryParams,"page[after]","5e1606762556d93e9c176f69")
    CkJsonObject::ckUpdateString(queryParams,"page[before]","5e1606762556d93e9c176f69")
    CkJsonObject::ckUpdateInt(queryParams,"page[size]",10)

    resp.i = CkHttp::ckQuickRequestParams(http,"GET","https://domain.com/v2/apps/{{appId}}/conversations/{{conversationId}}/messages",queryParams)
    If CkHttp::ckLastMethodSuccess(http) = 0
        Debug CkHttp::ckLastErrorText(http)
        CkHttp::ckDispose(http)
        CkJsonObject::ckDispose(queryParams)
        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(queryParams)
        CkStringBuilder::ckDispose(sbResponseBody)
        CkJsonObject::ckDispose(jResp)
        ProcedureReturn
    EndIf

    CkHttpResponse::ckDispose(resp)

    ; Sample JSON response:
    ; (Sample code for parsing the JSON response is shown below)

    ; {
    ;   "messages": [
    ;     {
    ;       "id": "5e552ef595e5206375bb835d",
    ;       "received": "2019-03-21T18:48:52.760Z",
    ;       "author": {
    ;         "type": "business",
    ;         "userId": "5963c0d619a30a2e00de36b8",
    ;         "displayName": "Steve",
    ;         "avatarUrl": "https://www.gravatar.com/image.jpg"
    ;       },
    ;       "content": {
    ;         "type": "text",
    ;         "text": "Hello!",
    ;         "actions": [
    ;           {
    ;             "value": "<Error: Too many levels of nesting to fake this schema>"
    ;           },
    ;           {
    ;             "value": "<Error: Too many levels of nesting to fake this schema>"
    ;           }
    ;         ]
    ;       },
    ;       "source": {
    ;         "type": "ios",
    ;         "integrationId": "de13bee15b51033b34162411",
    ;         "originalMessageId": "5f40256af057d0000dda9bd7",
    ;         "originalMessageTimestamp": "2019-03-21T18:45:53.720Z",
    ;         "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://EGnaLcOXhxGTLKJ.jpuft043dnis59F",
    ;           "info": {},
    ;           "raw": {}
    ;         },
    ;         "device": {
    ;           "id": "de13bee15b51033b34162411",
    ;           "type": "web",
    ;           "guid": "in laboris ex",
    ;           "clientId": "amet minim do eu",
    ;           "status": "active",
    ;           "integrationId": "sed enim aliquip commodo",
    ;           "lastSeen": "elit proident",
    ;           "pushNotificationToken": "officia labore",
    ;           "info": {},
    ;           "appVersion": "irure deserunt qui veniam consequat"
    ;         }
    ;       },
    ;       "quotedMessage": {
    ;         "type": "message",
    ;         "message": {
    ;           "value": "<Error: Too many levels of nesting to fake this schema>"
    ;         }
    ;       },
    ;       "metadata": {
    ;         "lang": "en-ca"
    ;       },
    ;       "deleted": true
    ;     },
    ;     {
    ;       "id": "5e552ef595e5206375bb835d",
    ;       "received": "2019-03-21T18:48:52.760Z",
    ;       "author": {
    ;         "type": "business",
    ;         "userId": "5963c0d619a30a2e00de36b8",
    ;         "displayName": "Steve",
    ;         "avatarUrl": "https://www.gravatar.com/image.jpg"
    ;       },
    ;       "content": {
    ;         "type": "text",
    ;         "text": "Hello!",
    ;         "actions": [
    ;           {
    ;             "value": "<Error: Too many levels of nesting to fake this schema>"
    ;           },
    ;           {
    ;             "value": "<Error: Too many levels of nesting to fake this schema>"
    ;           }
    ;         ]
    ;       },
    ;       "source": {
    ;         "type": "ios",
    ;         "integrationId": "de13bee15b51033b34162411",
    ;         "originalMessageId": "5f40256af057d0000dda9bd7",
    ;         "originalMessageTimestamp": "2019-03-21T18:45:53.720Z",
    ;         "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://bnAEzvgnDimaLnqEYLUCQXbU.ywrnS-uvD-0kBryUe",
    ;           "info": {},
    ;           "raw": {}
    ;         },
    ;         "device": {
    ;           "id": "de13bee15b51033b34162411",
    ;           "type": "web",
    ;           "guid": "laborum eiusmod adipisicing esse",
    ;           "clientId": "labore adipisicing eiusmod",
    ;           "status": "inactive",
    ;           "integrationId": "aute",
    ;           "lastSeen": "anim reprehenderit proident pariatur incididunt",
    ;           "pushNotificationToken": "qui dolore Excepteur in",
    ;           "info": {},
    ;           "appVersion": "incididunt labore dolore pariatur"
    ;         }
    ;       },
    ;       "quotedMessage": {
    ;         "type": "message",
    ;         "message": {
    ;           "value": "<Error: Too many levels of nesting to fake this schema>"
    ;         }
    ;       },
    ;       "metadata": {
    ;         "lang": "en-ca"
    ;       },
    ;       "deleted": true
    ;     }
    ;   ],
    ;   "meta": {
    ;     "hasMore": false,
    ;     "afterCursor": "55c8d9758590aa1900b9b9f6",
    ;     "beforeCursor": "55c8d9758590aa1900b9b9f6"
    ;   },
    ;   "links": {
    ;     "prev": "https://api.smooch.io/v2/apps?page[before]=fcafad804c39a39648004af9",
    ;     "next": "https://api.smooch.io/v2/apps?page[after]=5ea868f862cdd24abf010b38"
    ;   }
    ; }

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

    id.s
    received.s
    v_Type.s
    UserId.s
    DisplayName.s
    AvatarUrl.s
    contentType.s
    Text.s
    sourceType.s
    IntegrationId.s
    OriginalMessageId.s
    OriginalMessageTimestamp.s
    Id.s
    ClientType.s
    Status.s
    ClientIntegrationId.s
    ExternalId.s
    LastSeen.s
    LinkedAt.s
    ClientDisplayName.s
    ClientAvatarUrl.s
    DeviceId.s
    DeviceType.s
    Guid.s
    ClientId.s
    DeviceStatus.s
    DeviceIntegrationId.s
    DeviceLastSeen.s
    PushNotificationToken.s
    AppVersion.s
    quotedMessageType.s
    Value.s
    Lang.s
    deleted.i
    j.i
    count_j.i
    value.s

    HasMore.i = CkJsonObject::ckBoolOf(jResp,"meta.hasMore")
    AfterCursor.s = CkJsonObject::ckStringOf(jResp,"meta.afterCursor")
    BeforeCursor.s = CkJsonObject::ckStringOf(jResp,"meta.beforeCursor")
    Prev.s = CkJsonObject::ckStringOf(jResp,"links.prev")
    v_Next.s = CkJsonObject::ckStringOf(jResp,"links.next")
    i.i = 0
    count_i.i = CkJsonObject::ckSizeOfArray(jResp,"messages")
    While i < count_i
        CkJsonObject::setCkI(jResp, i)
        id = CkJsonObject::ckStringOf(jResp,"messages[i].id")
        received = CkJsonObject::ckStringOf(jResp,"messages[i].received")
        v_Type = CkJsonObject::ckStringOf(jResp,"messages[i].author.type")
        UserId = CkJsonObject::ckStringOf(jResp,"messages[i].author.userId")
        DisplayName = CkJsonObject::ckStringOf(jResp,"messages[i].author.displayName")
        AvatarUrl = CkJsonObject::ckStringOf(jResp,"messages[i].author.avatarUrl")
        contentType = CkJsonObject::ckStringOf(jResp,"messages[i].content.type")
        Text = CkJsonObject::ckStringOf(jResp,"messages[i].content.text")
        sourceType = CkJsonObject::ckStringOf(jResp,"messages[i].source.type")
        IntegrationId = CkJsonObject::ckStringOf(jResp,"messages[i].source.integrationId")
        OriginalMessageId = CkJsonObject::ckStringOf(jResp,"messages[i].source.originalMessageId")
        OriginalMessageTimestamp = CkJsonObject::ckStringOf(jResp,"messages[i].source.originalMessageTimestamp")
        Id = CkJsonObject::ckStringOf(jResp,"messages[i].source.client.id")
        ClientType = CkJsonObject::ckStringOf(jResp,"messages[i].source.client.type")
        Status = CkJsonObject::ckStringOf(jResp,"messages[i].source.client.status")
        ClientIntegrationId = CkJsonObject::ckStringOf(jResp,"messages[i].source.client.integrationId")
        ExternalId = CkJsonObject::ckStringOf(jResp,"messages[i].source.client.externalId")
        LastSeen = CkJsonObject::ckStringOf(jResp,"messages[i].source.client.lastSeen")
        LinkedAt = CkJsonObject::ckStringOf(jResp,"messages[i].source.client.linkedAt")
        ClientDisplayName = CkJsonObject::ckStringOf(jResp,"messages[i].source.client.displayName")
        ClientAvatarUrl = CkJsonObject::ckStringOf(jResp,"messages[i].source.client.avatarUrl")
        DeviceId = CkJsonObject::ckStringOf(jResp,"messages[i].source.device.id")
        DeviceType = CkJsonObject::ckStringOf(jResp,"messages[i].source.device.type")
        Guid = CkJsonObject::ckStringOf(jResp,"messages[i].source.device.guid")
        ClientId = CkJsonObject::ckStringOf(jResp,"messages[i].source.device.clientId")
        DeviceStatus = CkJsonObject::ckStringOf(jResp,"messages[i].source.device.status")
        DeviceIntegrationId = CkJsonObject::ckStringOf(jResp,"messages[i].source.device.integrationId")
        DeviceLastSeen = CkJsonObject::ckStringOf(jResp,"messages[i].source.device.lastSeen")
        PushNotificationToken = CkJsonObject::ckStringOf(jResp,"messages[i].source.device.pushNotificationToken")
        AppVersion = CkJsonObject::ckStringOf(jResp,"messages[i].source.device.appVersion")
        quotedMessageType = CkJsonObject::ckStringOf(jResp,"messages[i].quotedMessage.type")
        Value = CkJsonObject::ckStringOf(jResp,"messages[i].quotedMessage.message.value")
        Lang = CkJsonObject::ckStringOf(jResp,"messages[i].metadata.lang")
        deleted = CkJsonObject::ckBoolOf(jResp,"messages[i].deleted")
        j = 0
        count_j = CkJsonObject::ckSizeOfArray(jResp,"messages[i].content.actions")
        While j < count_j
            CkJsonObject::setCkJ(jResp, j)
            value = CkJsonObject::ckStringOf(jResp,"messages[i].content.actions[j].value")
            j = j + 1
        Wend
        i = i + 1
    Wend


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


    ProcedureReturn
EndProcedure

Curl Command

curl -G -d "page[after]=5e1606762556d93e9c176f69"
	-d "page[before]=5e1606762556d93e9c176f69"
	-d "page[size]=10"
	-u 'username:password'
https://domain.com/v2/apps/{{appId}}/conversations/{{conversationId}}/messages

Postman Collection Item JSON

{
  "name": "List Messages",
  "_postman_id": "9f9b265c-7546-4b49-98bf-7e4d85bae5f2",
  "request": {
    "method": "GET",
    "header": [
    ],
    "url": {
      "raw": "{{url}}/v2/apps/{{appId}}/conversations/{{conversationId}}/messages?page[after]=5e1606762556d93e9c176f69&page[before]=5e1606762556d93e9c176f69&page[size]=10",
      "host": [
        "{{url}}"
      ],
      "path": [
        "v2",
        "apps",
        "{{appId}}",
        "conversations",
        "{{conversationId}}",
        "messages"
      ],
      "query": [
        {
          "key": "page[after]",
          "value": "5e1606762556d93e9c176f69",
          "description": "Contains parameters for applying cursor pagination."
        },
        {
          "key": "page[before]",
          "value": "5e1606762556d93e9c176f69",
          "description": "Contains parameters for applying cursor pagination."
        },
        {
          "key": "page[size]",
          "value": "10",
          "description": "Contains parameters for applying cursor pagination."
        }
      ]
    },
    "description": "List all messages for a particular conversation. This API is paginated through\n[cursor pagination](#section/Introduction/API-pagination-and-records-limits), in the _backwards_ direction, with the\nmost recent (i.e. last) page of messages being returned by default. The `hasMore` flag indicates whether more messages\nexist in the direction you are currently paginating through. To page backwards in the history, use the `beforeCursor` or follow the `prev`\nlink. The page size limit is fixed at 100 messages per page.\n\n```shell\n/v2/apps/:appId/conversations/:conversationId/messages?page[before]=5f32b88acf6bf25073b2be56\n```\n"
  },
  "response": [
    {
      "id": "550b7411-8e33-4840-8701-23face25cb9a",
      "name": "Ok",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "description": {
              "content": "Added as a part of security scheme: bearer",
              "type": "text/plain"
            },
            "key": "Authorization",
            "value": "Bearer <token>"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/conversations/:conversationId/messages?page[after]=5e1606762556d93e9c176f69&page[before]=5e1606762556d93e9c176f69&page[size]=10",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "conversations",
            ":conversationId",
            "messages"
          ],
          "query": [
            {
              "key": "page[after]",
              "value": "5e1606762556d93e9c176f69"
            },
            {
              "key": "page[before]",
              "value": "5e1606762556d93e9c176f69"
            },
            {
              "key": "page[size]",
              "value": "10"
            }
          ],
          "variable": [
            {
              "key": "appId"
            },
            {
              "key": "conversationId"
            }
          ]
        }
      },
      "status": "OK",
      "code": 200,
      "_postman_previewlanguage": "json",
      "header": [
        {
          "key": "Content-Type",
          "value": "application/json"
        }
      ],
      "cookie": [
      ],
      "body": "{\n \"messages\": [\n  {\n   \"id\": \"5e552ef595e5206375bb835d\",\n   \"received\": \"2019-03-21T18:48:52.760Z\",\n   \"author\": {\n    \"type\": \"business\",\n    \"userId\": \"5963c0d619a30a2e00de36b8\",\n    \"displayName\": \"Steve\",\n    \"avatarUrl\": \"https://www.gravatar.com/image.jpg\"\n   },\n   \"content\": {\n    \"type\": \"text\",\n    \"text\": \"Hello!\",\n    \"actions\": [\n     {\n      \"value\": \"<Error: Too many levels of nesting to fake this schema>\"\n     },\n     {\n      \"value\": \"<Error: Too many levels of nesting to fake this schema>\"\n     }\n    ]\n   },\n   \"source\": {\n    \"type\": \"ios\",\n    \"integrationId\": \"de13bee15b51033b34162411\",\n    \"originalMessageId\": \"5f40256af057d0000dda9bd7\",\n    \"originalMessageTimestamp\": \"2019-03-21T18:45:53.720Z\",\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://EGnaLcOXhxGTLKJ.jpuft043dnis59F\",\n     \"info\": {},\n     \"raw\": {}\n    },\n    \"device\": {\n     \"id\": \"de13bee15b51033b34162411\",\n     \"type\": \"web\",\n     \"guid\": \"in laboris ex\",\n     \"clientId\": \"amet minim do eu\",\n     \"status\": \"active\",\n     \"integrationId\": \"sed enim aliquip commodo\",\n     \"lastSeen\": \"elit proident\",\n     \"pushNotificationToken\": \"officia labore\",\n     \"info\": {},\n     \"appVersion\": \"irure deserunt qui veniam consequat\"\n    }\n   },\n   \"quotedMessage\": {\n    \"type\": \"message\",\n    \"message\": {\n     \"value\": \"<Error: Too many levels of nesting to fake this schema>\"\n    }\n   },\n   \"metadata\": {\n    \"lang\": \"en-ca\"\n   },\n   \"deleted\": true\n  },\n  {\n   \"id\": \"5e552ef595e5206375bb835d\",\n   \"received\": \"2019-03-21T18:48:52.760Z\",\n   \"author\": {\n    \"type\": \"business\",\n    \"userId\": \"5963c0d619a30a2e00de36b8\",\n    \"displayName\": \"Steve\",\n    \"avatarUrl\": \"https://www.gravatar.com/image.jpg\"\n   },\n   \"content\": {\n    \"type\": \"text\",\n    \"text\": \"Hello!\",\n    \"actions\": [\n     {\n      \"value\": \"<Error: Too many levels of nesting to fake this schema>\"\n     },\n     {\n      \"value\": \"<Error: Too many levels of nesting to fake this schema>\"\n     }\n    ]\n   },\n   \"source\": {\n    \"type\": \"ios\",\n    \"integrationId\": \"de13bee15b51033b34162411\",\n    \"originalMessageId\": \"5f40256af057d0000dda9bd7\",\n    \"originalMessageTimestamp\": \"2019-03-21T18:45:53.720Z\",\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://bnAEzvgnDimaLnqEYLUCQXbU.ywrnS-uvD-0kBryUe\",\n     \"info\": {},\n     \"raw\": {}\n    },\n    \"device\": {\n     \"id\": \"de13bee15b51033b34162411\",\n     \"type\": \"web\",\n     \"guid\": \"laborum eiusmod adipisicing esse\",\n     \"clientId\": \"labore adipisicing eiusmod\",\n     \"status\": \"inactive\",\n     \"integrationId\": \"aute\",\n     \"lastSeen\": \"anim reprehenderit proident pariatur incididunt\",\n     \"pushNotificationToken\": \"qui dolore Excepteur in\",\n     \"info\": {},\n     \"appVersion\": \"incididunt labore dolore pariatur\"\n    }\n   },\n   \"quotedMessage\": {\n    \"type\": \"message\",\n    \"message\": {\n     \"value\": \"<Error: Too many levels of nesting to fake this schema>\"\n    }\n   },\n   \"metadata\": {\n    \"lang\": \"en-ca\"\n   },\n   \"deleted\": true\n  }\n ],\n \"meta\": {\n  \"hasMore\": false,\n  \"afterCursor\": \"55c8d9758590aa1900b9b9f6\",\n  \"beforeCursor\": \"55c8d9758590aa1900b9b9f6\"\n },\n \"links\": {\n  \"prev\": \"https://api.smooch.io/v2/apps?page[before]=fcafad804c39a39648004af9\",\n  \"next\": \"https://api.smooch.io/v2/apps?page[after]=5ea868f862cdd24abf010b38\"\n }\n}"
    },
    {
      "id": "b77a129c-bc78-489f-9630-1dbd1d0aa6bd",
      "name": "Message not found",
      "originalRequest": {
        "method": "GET",
        "header": [
          {
            "description": {
              "content": "Added as a part of security scheme: bearer",
              "type": "text/plain"
            },
            "key": "Authorization",
            "value": "Bearer <token>"
          }
        ],
        "url": {
          "raw": "{{baseUrl}}/v2/apps/:appId/conversations/:conversationId/messages?page[after]=5e1606762556d93e9c176f69&page[before]=5e1606762556d93e9c176f69&page[size]=10",
          "host": [
            "{{baseUrl}}"
          ],
          "path": [
            "v2",
            "apps",
            ":appId",
            "conversations",
            ":conversationId",
            "messages"
          ],
          "query": [
            {
              "key": "page[after]",
              "value": "5e1606762556d93e9c176f69"
            },
            {
              "key": "page[before]",
              "value": "5e1606762556d93e9c176f69"
            },
            {
              "key": "page[size]",
              "value": "10"
            }
          ],
          "variable": [
            {
              "key": "appId"
            },
            {
              "key": "conversationId"
            }
          ]
        }
      },
      "status": "Not Found",
      "code": 404,
      "_postman_previewlanguage": "text",
      "header": [
        {
          "key": "Content-Type",
          "value": "text/plain"
        }
      ],
      "cookie": [
      ],
      "body": ""
    }
  ]
}