1. Home
  2. Kore.ai Conversational Platform
  3. SDKs & APIs
  4. Web Socket Connect & RTM

Web Socket Connect & RTM

Kore.ai SDK libraries use the web socket channel to communicate with Kore.ai Bots Platform. To initialize a web socket session, the Kore.ai SDKs use the following Bots Platform endpoints. When using the Kore.ai Bots Platform SDKs, only the JWT token must be generated and passed to the client SDK.

Initializing a Web Socket Session

This section describes how the Kore.ai SDKs initialize a web socket session and is for reference only.

Prerequisites:

The client app is registered and Client ID and Secret Key are generated in the Kore.ai Bot Builder tool.

Step1:

The JWT is generated at server side using client app credentials and user information. The JWT is passed to the client app.

Step 2:

The client app exchanges the JWT token for the accessToken on the Bots Platform using the following Bots Platform endpoint. This section describes how to format the request to post a message in a chat with the /api/1.1/oAuth/token/jwtgrant endpoint based on the following JSON syntax.

POST
https://{{APIHost}}/api/1.1/oAuth/token/jwtgrant

{
    "assertion": “{{JWT Token}}”,
    "botInfo": {
        "chatBot": "{{Bot Name}}",
        "taskBotId": "st-f74a3430-3b19-55a3-be41-1ab1a35c4685"
    }
)

Sample cURL

curl 'https://bots.kore.ai/api/1.1/oAuth/token/jwtgrant' -H 'content-type: application/json'  --data-binary '{"assertion":"{JWT Token}}","botInfo":{"chatBot":"{{Bot Name}}","taskBotId":"st-f74a3430-3b19-55a3-be41-1ab1a35c4685"}}

Response

The follow sample JSON response shows the accessToken and user associated with that token.

{
    "authorization": {
        "accessToken": "UTzo00l3F2iLdwTL8NWh5Rb_FshGzHr2-yPMOXtxRlnr0RB7MvMbn_",
        "refreshToken": "fRew0kzAbSXxMTqTYE_-ochgaXPjlInDdTwfmdtUoHm-hZgIKbYBE_lee",
        "token_type": "bearer",
        "expiresDate": "2023-05-03T05:52:15.133Z",
        "refreshExpiresDate": "2023-05-03T05:52:15.133Z",
        "issuedDate": "2016-06-28T05:52:15.133Z"
    },
    "userInfo": {
        "userId": "u-ebe4f052-5b14-5d6c-9d89-fdc7206be9db",
        "accountId": "5742eec14ff88a297bda6ea6",
        "orgId": "o-88aad7f1-0d32-5765-93d7-f40c80402114",
        "domain": "testadmin3.xyz",
        "identity": "sethi@testadmin3.xyz",
        "enrollType": "paid",
        "managedBy": "5742eec14ff88a297bda6ea6",
        "fName": "John",
        "lName": "Smith",
        "profImage": "avatar.png"
    }
}

Step 3:

The client app acquires the web socket URL using the following endpoint. This section describes how to format the request to get the web socket URL with the /api/1.1/rtm/start endpoint based on the following JSON syntax.

Note: This URL is short lived and expires in 30 seconds. You must ensure it is used to connect before expiration.

POST
https://{{APIHost}}/api/1.1/rtm/start

{
    "botInfo": {
        "chatBot": "Twitter",
        "taskBotId": "st-f74a3430-3b19-55a3-be41-1ab1a35c4685"
    }
}

The accessToken is passed in the bearer parameter in the Authorization header.

Sample cURL

curl 'https://bots.kore.ai/api/1.1/rtm/start' -H 'content-type: application/json' -H 'Authorization: bearer {{accessToken}}' --data-binary '{"botInfo":{"chatBot":"{{Bot Name}}","taskBotId":"st-f74a3430-3b19-55a3-be41-1ab1a35c4685"}'

Response

The following sample JSON response shows the web socket URL.

{"url":"wss://bots.kore.com:443/rtm/bot?sid=GhKrtrEC61g7hAnmvKAVnJIHG0DS1Lzv"}

RTM Events Reference

The following real-time events can exchanged between the client app and the Kore.ai Bots Platform

RTM Client Events

This section describes the RTM Client Event JSON responses sent to the Kore.ai Bots Platform from the client app over a web socket.

Event Type: /bot.message

Description: Triggered when a user posts a message. The following payload is used to send a user-typed message to the Bots Platform.

{
    "clientMessageId": 1466692440896,
    "message": {
        "body": "Here is the message.",
        "attachments": [

      ]
    },
    "resourceid": "/bot.message",
    "botInfo": {
        "chatBot": "CNN",
        "taskBotId": "st-8aaf0939-c34a-5976-8e2e-5c91e685b2ce"
    },
    "id": 1466692440896
}

RTM Server Events

This section describes the RTM Server Event JSON responses sent to the client app over a web socket.

Event Type: ack

Description: An acknowledgement sent whenever an event is received from the client app. The following payload is used to acknowledge an event from the client app.

{
    "ok": true,
    "replyto": 1466692440896,
    "message": "delivered",
    "type": "ack"
}

Event Type: bot_response

Description: An acknowledgement sent whenever a message is processed from the client app. The following payload is sent when a message from the client app is processed.

{
    "type": "bot_response",
    "from": "bot",
    "message": [
        {
            "type": "text",
            "cInfo": {
                "body": " Hi.  "
            }
      }
   ],
    "botInfo": {
        "chatBot": "CNN",
        "taskBotId": "st-8aaf0939-c34a-5976-8e2e-5c91e685b2ce"
    },
    "createdOn": "2016-06-23T14:34:00.025Z",
    "icon": "https://devbots.kore.com/api/getMediaStream/market/f-683e82be-fc25-5921-bf41-4104780f71c2.png"
}

Event Type: user_message

Description: Sent to the client app when a user enters and sends a message to the server from another simultaneous session. The following sample payload is sent to the client app.

{
    "botInfo": {
        "chatBot": "CNN",
        "taskBotId": "st-8aaf0939-c34a-5976-8e2e-5c91e685b2ce"
    },
    "from": "self",
    "message": {
        "body": "how are you doing ",
        "attachments": []
    },
    "id": 1466692871803,
    "type": "user_message"
}

Next Steps

The Bots Platform API endpoints and RTM events can be used with your client app or Kore.ai Bot SDKs. For more information, see the Kore.ai Bots SDK.

Was this article helpful to you? Yes 2 No 1