Kore.ai SDK 라이브러리는 웹 소켓 채널을 사용하여 Kore.ai 봇 플랫폼과 통신합니다. 웹 소켓 세션을 초기화하려면, Kore.ai SDK는 다음 봇 플랫폼 엔드포인트를 사용합니다. Kore.ai 봇 플랫폼 SDK를 사용하는 경우, JWT 토큰만 생성하고 클라이언트 SDK로 전달해야 합니다.
초기화
이 섹션에서는 Kore.ai SDK가 웹 소켓을 초기화하는 방법을 설명하며 참조 전용입니다.
필수 구성 요소
클라이언트 앱이 등록되어 있고, 클라이언트 ID 및 시크릿 키가 Kore.ai 봇 빌더 도구에서 생성되어 있습니다.
1단계
JWT가 클라이언트 앱 자격 증명 및 사용자 정보를 사용하여 서버 측에서 생성됩니다. JWT가 클라이언트 앱으로 전달됩니다.
2단계
클라이언트 앱은 다음 봇 플랫폼 엔드포인트를 사용하여 봇 플랫폼에서 JWT 토큰을 accessToken으로 교환합니다. 이 섹션에서는 다음 JSON 구문을 기반으로 /api/1.1/oAuth/token/jwtgrant 엔드포인트를 사용하여 대화에서 메시지 게시 요청 형식을 지정하는 방법을 설명합니다. 
POST
https://{{APIHost}}/api/1.1/oAuth/token/jwtgrant
{
    "assertion": “{{JWT Token}}”,
    "botInfo": {
        "chatBot": "{{Bot Name}}",
        "taskBotId": "st-f74a3430-3b19-55a3-be41-1ab1a35c4685"
    }
)
샘플 cURL
curl 'https://{{APIHost}}/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"}}
응답
다음 샘플 JSON 응답은 accessToken 및 해당 토큰과 관련된 사용자를 보여줍니다.
{
   "authorization": {
	"accessToken": "ZdV2OL_UZ_MvHog-rs8k9KJFNWBICvquSc3jpeaRDE_-",
                 // access token to user at /api/rtm
	"token_type": "bearer",
	"expiresDate": "2019-06-28T06:52:23.160Z",
                //expiry date for access token
	"issuedDate": "2019-02-28T06:52:23.160Z" 
               //access token issuedDate
	},
   "userInfo": {
	"userId": "u-4f6c68e0-551a-5dd9-a33a-1af3dc9cadcc", 
                 // user id which is unique for the user and can be used at bot messages api
	"accountId": "5c66514d09ab3565deb2e30a", 
                 // account id in which the user is present
	"orgId": "o-88aad7f1-0d32-5765-93d7-f40c80402114",
                // organization id of the user
	"identity": "cs-5b08ed1e-5fa7-5aaa-9c21-28bf8c90b739/admin1212@qakore.xyz", 
                //identity of user from the channel perspective
	"enrollType": "free", 
                //Enrollment type (Free/ Paid etc)
	"managedBy": "5c66514d09ab3565deb2e30a", 
                //(The account id)
	"fName": "", 
                //first name of the user
                // (fetched if the user is registered on the platform)
	"lName": "" 
                //last name of the user
                // (fetched if the user is registered on the platform)
	}
}
		3단계
클라이언트 앱은 다음 엔드포인트를 사용하여 웹 소켓 URL을 획득합니다. 이 섹션에서는 다음 JSON 구문을 기반으로 /api/1.1/rtm/start 엔드포인트를 사용하여 웹 소켓 URL을 가져오는 요청 형식을 지정하는 방법을 설명합니다.
참고: 이 URL은 오래가지 않고 30초 후에 만료됩니다. 만료되기 전에 연결에 사용되는지 확인해야 합니다.
POST
https://{{APIHost}}/api/1.1/rtm/start
{
    "botInfo": {
        "chatBot": "Twitter",
        "taskBotId": "st-f74a3430-3b19-55a3-be41-1ab1a35c4685"
    }
}
accessToken은 권한 부여 헤더의 bearer 매개변수에서 전달됩니다.
샘플 cURL
curl 'https://{{APIHost}}/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"}'
응답
다음 샘플 JSON 응답은 웹 소켓 URL을 보여줍니다.
{"url":"wss://xxx.yyy.com:443/rtm/bot?sid=GhKrtrEC61g7hAnmvKAVnJIHG0DS1Lzv"}
		RTM 이벤트 참조
다음 실시간 이벤트는 클라이언트 앱 및 Kore.ai 봇 플랫폼 간에 교환할 수 있습니다.
RTM 클라이언트 이벤트
이 섹션에서는 웹 소켓을 통해 클라이언트 앱에서 Kore.ai 봇 플랫폼으로 전송된 RTM 클라이언트 이벤트 JSON 응답을 설명합니다. 이벤트 유형: /bot.message 설명: 사용자가 메시지를 게시할 때 실행됩니다. 다음 페이로드는 사용자가 입력한 메시지를 봇 플랫폼으로 보내는 데 사용됩니다.
{
    "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 서버 이벤트
이 섹션에서는 웹 소켓을 통해 클라이언트 앱으로 전송된 RTM 서버 이벤트 JSON 응답을 설명합니다. 이벤트 유형: ack 설명: 클라이언트 앱에서 이벤트가 수신될 때마다 전송되는 확인 응답입니다. 다음 페이로드는 클라이언트 앱으로부터 이벤트를 확인 응답하는 데 사용됩니다.
{
    "ok": true,
    "replyto": 1466692440896,
    "message": "delivered",
    "type": "ack"
}
이벤트 유형: bot_response 설명: 클라이언트 앱에서 메시지가 처리될 때마다 전송되는 확인 응답입니다. 클라이언트 앱에서 메시지가 처리될 때마다 다음 페이로드가 전송됩니다.
{
    "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"
}
이벤트 유형: user_message 
설명: 사용자가 다른 세션에서 동시에 메시지를 입력하고 서버에 메시지를 보낼 때 클라이언트 앱으로 전송됩니다. 다음 샘플 페이로드는 클라이언트 앱으로 전송됩니다.
{
    "botInfo": {
        "chatBot": "CNN",
        "taskBotId": "st-8aaf0939-c34a-5976-8e2e-5c91e685b2ce"
    },
    "from": "self",
    "message": {
        "body": "how are you doing ",
        "attachments": []
    },
    "id": 1466692871803,
    "type": "user_message"
}
		대화 기록 가져오기
클라이언트 앱은 위의 2단계에서 생성된 액세스 토큰을 사용하여 봇 플랫폼 엔드포인트 /api/botmessages/rtm?botId={{botId}}를 요청하여 사용자 및 봇 간에 교환된 이전 메시지를 가져옵니다. 언급된 엔드포인트의 응답을 대화창에 대화 기록을 표시하는 데 사용할 수 있습니다. 이 섹션에서는 다음 JSON 구문을 기반으로 /api/botmessages/rtm 엔드포인트를 사용하여 웹 소켓 URL을 가져오는 요청 형식을 지정하는 방법을 설명합니다.
GET https://{{APIHost}}/api/botmessages/rtm?botId={{botId}}
질의 매개변수:
- botId(필수): 봇 ID이며, 봇의 일반 설정 페이지에서 액세스할 수 있습니다.
- skip/offset(옵션): 건너뛸 메시지 수입니다.
- limit(옵션): 가져올 메시지 수입니다.
- accessToken: 헤더에 권한 부여 매개변수로 bearer와 함께 전달됩니다
샘플 cURL
curl 'https://{{APIHost}}/api/botmessages/rtm?botId={{botId}}&limit=10' \
  -H 'accept: application/json' \
  -H 'Authorization: bearer -{{accessToken}}' \
  -H 'content-type: application/json'
샘플 응답
{
  "total": 2,
  "moreAvailable": false,
  "messages": [
    {
      "_id": "ms-8a83d099-dd93-5b6b-836b-ac77bdfe9d4a",
      "channels": [
        {
          "type": "rtm"
        }
      ],
      "type": "incoming",
      "status": "sent to cs",
      "createdBy": "u-211f287f-17c8-51ac-b768-15c68e19afb2",
      "lmodifiedBy": "u-211f287f-17c8-51ac-b768-15c68e19afb2",
      "lmodifiedOn": "2019-12-03T05:07:44.008Z",
      "botId": "st-2f579dbf-9dc1-548c-972f-f19d33160a07",
      "orgId": "o-44741779-1971-5ab9-a0f7-aaf4e21dbe4e",
      "accountId": "5a675150ff600b3d7665d2a6",
      "isBB": 0,
      "ms": 1,
      "chnl": "rtm",
      "components": [
        {
          "_id": "cp-1edc5033-a9cb-5011-8b2b-5986b7fa45cc",
          "cT": "text",
          "data": {
            "text": "Hi"
          },
          "thumbnails": [
           
          ]
        }
      ],
      "createdOn": "2019-12-03T05:07:44.097Z",
      "timestampValue": 1575349664097,
      "__v": 0,
      "lang": "en",
      "sessionId": "5de5eda04cdcff145752dbf6",
      "nodeType": 0,
      "tr0_I": "dg-61e5609f-76e7-5c42-b66f-7bb8b3af2433:intent0",
      "tr0_O": "dg-61e5609f-76e7-5c42-b66f-7bb8b3af2433:entity1:4a04988ee640428d1847fc433081be0b",
      "tr0_T": "0",
      "tr_isSS": 1,
      "resourceid": "messagestore",
      "tags": {
        "messageTags": [
         
        ],
        "userTags": [
         
        ],
        "sessionTags": [
         
        ]
      }
    },
    {
      "_id": "ms-80e8dbf3-ca69-5ee2-a2c1-1c6a70100c08",
      "channels": [
        {
          "type": "rtm"
        }
      ],
      "type": "outgoing",
      "status": "pending",
      "lmodifiedOn": "2019-12-03T05:07:45.165Z",
      "createdBy": "u-211f287f-17c8-51ac-b768-15c68e19afb2",
      "components": [
        {
          "_id": "cp-209a0641-130b-57ce-b0d3-d651691084bb",
          "cT": "text",
          "data": {
            "text": "Please enter the text to comment on the issue"
          },
          "thumbnails": [
           
          ]
        }
      ],
      "botId": "st-2f579dbf-9dc1-548c-972f-f19d33160a07",
      "orgId": "o-44741779-1971-5ab9-a0f7-aaf4e21dbe4e",
      "accountId": "5a675150ff600b3d7665d2a6",
      "tN": "Add Comment to Issue",
      "isBB": 0,
      "ms": 1,
      "chnl": "rtm",
      "lang": "en",
      "createdOn": "2019-12-03T05:07:45.171Z",
      "timestampValue": 1575349665171,
      "__v": 0,
      "sessionId": "5de5eda04cdcff145752dbf6",
      "resourceid": "messagestore",
      "tags": {
        "messageTags": [
         
        ],
        "userTags": [
         
        ],
        "sessionTags": [
         
        ]
      }
    }
  ]
}
		다음 단계
봇 플랫폼 API 엔드포인트 및 RTM 이벤트는 클라이언트 앱 또는 Kore.ai 봇 SDK와 함께 사용할 수 있습니다. 자세한 내용은, Kore.ai 봇 SDK를 참조하세요.