@markdown
# Bot Framework Rest API로 사용하기
___
- REST API 서비스를 가능하게 해주는 Direct Line API 사용한다.
- Direct Line API는 기본적으로 제공해주는 Skype, Slack 채널들 이외에 모바일 앱, 웹 등으로 Bot과 연결시켜주는 API이다.
- 기본적으로 REST 방식의 API로 Microsoft Bot Framework와 연동할 수 있게 공개되어 있다.
## Direct Line API Secret Keys 받아오기
- 먼저 위에서 만든 Bot의 추가 채널로 Direct Line을 추가해준다.

- 추가가 완료되면 Secret Keys를 얻을 수 있다.

<br/>
## 대화 시작을 위한 Token 받아오기
- Post 전송 테스트를 위해 `Postman` 프로그램 사용
<pre><code class="json" style="font-size:15px">Method : POST
URL : https://directline.botframework.com/v3/directline/conversations
Authorization : Bearer YOUR_SECRET_KEY
</code></pre>

- 위에서 받아온 Secret Keys를 Post Header에 포함시켜 Request하면, 응답으로 대화 ID, 토큰 값을 Json 형태로 Response 받을 수 있다.
<pre><code class="json" style="font-size:15px">{
"conversationId": "6**************623DnB7I12",
"token": "********8eAc.dAA.NgB3****************4g0wE.l8MLMDxtlAE.JweT*************",
"expires_in": 1800,
"streamUrl": "wss://directline.botframework.com/v3/directline/conversations/6**************623DnB7I12/stream?watermark=-&t=mojUuQmsHog0wE.VdNfjZ7RrxNB5WKIyuo0",
"referenceGrammarId": "49adeb4a-a45b-5d5e-86f2-0d14263aacc0"
}
</code></pre>
<br/>
## Bot에 메시지 보내기
- 메시지 받을 Bot을 실행시켜놓고 응답 받을 준비를 한다.
- 메시지는 json 형태로 보낼 것이기에 Postman 프로그램에서 Body를 JSON(application/json)으로 작성해준다.

<pre><code class="json" style="font-size:15px">Method : POST
URL : https://directline.botframework.com/v3/directline/conversations/{conversationId}/activities
Authorization : Bearer TOKEN
Content-Type : application/json
body
{
"type": "message",
"from": {
"id": "6**************623DnB7I12"
},
"text": "안녕?"
}
</code></pre>
<br/>
## Bot 응답 메시지 받기
- Post 메시지 send 한 후 같은 주소로 GET 요청하게 되면 응답 메시지가 출력된다.
<pre><code class="json" style="font-size:15px">{
"activities": [
{
"type": "message",
"id": "6**************623DnB7I12|0000001",
"timestamp": "2017-08-29T04:06:27.7225767Z",
"channelId": "directline",
"from": {
"id": "6**************623DnB7I12"
},
"conversation": {
"id": "6**************623DnB7I12"
},
"text": "안녕?"
},
{
"type": "message",
"id": "6**************623DnB7I12|0000002",
"timestamp": "2017-08-29T04:06:30.311396Z",
"channelId": "directline",
"from": {
"id": "card_bot_test",
"name": "card_bot_test"
},
"conversation": {
"id": "6**************623DnB7I12"
},
"text": "안녕하세요.",
"replyToId": "6**************623DnB7I12|0000001"
}
],
"watermark": "2"
}
</code></pre>
- 위의 응답처럼 대화ID에 포함된 주고 받은 모든 메시지를 확인 할 수 있다.
'Bot 기술노트' 카테고리의 다른 글
웹페이지 게시물 업데이트 알람 봇 만들기(2) (0) | 2019.07.14 |
---|---|
웹페이지 게시물 업데이트 알람 봇 만들기(1) (12) | 2019.07.11 |
BotFramework으로 간단한 챗봇 만들기 (0) | 2017.08.29 |
python - tweepy 사용한 트위터 크롤링(crawling) (10) | 2017.08.05 |