본문 바로가기

Bot 기술노트

BotFramework Rest API로 사용하기

@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을 추가해준다.

![](https://user-images.githubusercontent.com/12658717/29804300-b1442d8c-8cbc-11e7-99c1-7800d474c7d4.png)


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

![](https://user-images.githubusercontent.com/12658717/29804346-fa28479a-8cbc-11e7-9293-b166efa90148.png)

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

![](https://user-images.githubusercontent.com/12658717/29805064-2c2a9a22-8cc2-11e7-95b5-515fb9caa048.png)


- 위에서 받아온 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)으로 작성해준다.

![](https://user-images.githubusercontent.com/12658717/29805419-b1bebca2-8cc4-11e7-9e61-c4df90ab116e.png)

<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에 포함된 주고 받은 모든 메시지를 확인 할 수 있다.