|
| 1 | +# Connect directly to a Bot - Direct Line |
| 2 | + |
| 3 | +## Objectives |
| 4 | + |
| 5 | +In certain scenarios such as testing, communication directly to your bot may be required. Communication between your bot and your own client application can be performed using Direct Line API. This hands-on lab introduces key concepts related to Direct Line API. |
| 6 | + |
| 7 | +## Setup |
| 8 | + |
| 9 | +For this lab, you can use a published Bot to talk to via Direct Line API. |
| 10 | + |
| 11 | +Download the sample from: |
| 12 | + |
| 13 | +https://github.com/Microsoft/BotBuilder-Samples/tree/master/CSharp/core-DirectLine and import the solution in Visual Studio. |
| 14 | + |
| 15 | +In the DirectLineBot solution, you will find two projects – DirectLineBot and DirectLineSampleClient. If you do not have a published app, you can publish DirectLineBot. The previous labs should have prepared you with the steps related to publishing bots. |
| 16 | + |
| 17 | +DirectLineSampleClient is the client that will send messages to the bot. |
| 18 | + |
| 19 | +## Authentication |
| 20 | + |
| 21 | +Direct Line API requests can be authenticated either by using a secret that you obtain from the Direct Line channel configuration page in the Bot Framework Portal. Go to the Bot Framework Portal and find your bot. Add Direct Line via *Connect to channels* for your bot. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +You can obtain a Secret Key from the Direct Line channel after adding as shown below: |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +**Security Scope** |
| 30 | + |
| 31 | +Secret Key: Secret key is application wide and is embedded in the client application. Every conversation that is initiated by the application would use the same secret. This makes it very convenient. |
| 32 | + |
| 33 | +Tokens: A token is conversation specific. You request a token using that secret and you can initiate a conversation with that token. Its valid for 30 minutes from when it’s issued but it can be refreshed. |
| 34 | + |
| 35 | +## App Config |
| 36 | + |
| 37 | +The Secret key obtained from *Configure Direct Line* in the Bot Framework Portal is then added to the Configuration settings in App.config file as shown below. In addition, for the published bot, capture the bot id and enter in the appSettings part of App.config from DirectLineSampleClient project. The relevant lines of App.config to enter in the App.config are listed as follows: |
| 38 | + |
| 39 | +``` |
| 40 | +<add key="DirectLineSecret" value="YourBotDirectLineSecret" /> |
| 41 | +<add key="BotId" value="YourBotId" /> |
| 42 | +``` |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +## Sending and Receiving Messages |
| 47 | + |
| 48 | +Using Direct Line API, a client can send messages to your bot by issuing HTTP Post requests. A client can receive messages from your bot either via WebSocket stream or by issuing HTTP GET requests. In this lab, we will explore HTTP Get option to receive messages. |
| 49 | + |
| 50 | +1. Run project DirectLineSampleClient after making the Config changes. |
| 51 | + |
| 52 | +2. Submit a message via console and obtain the conversation id using the below line: |
| 53 | + |
| 54 | +````Console.WriteLine("Conversation ID:" + conversation.ConversationId);```` |
| 55 | + |
| 56 | + |
| 57 | + |
| 58 | +3. Once you have the conversation id, you can retrieve user and bot messages using HTTP Get. To retrieve messages for a specific conversation, you can issue a GET request to https://directline.botframework.com/api/conversations/{conversationId}/messages endpoint. You will also need to pass the Secret Key as part of raw header (i.e. Authorization: Bearer {secretKey}). |
| 59 | + |
| 60 | +4. Use any Rest Client to receive messages via HTTP Get. |
| 61 | + |
| 62 | + * Web based Rest Clients: |
| 63 | + |
| 64 | + You can use https://advancedrestclient.com/ with chrome for receiving messages from the bot. The below images indicate the conversations obtained from *Advanced Rest client*. Note the conversation "Hi there" and the corresponding bot response that is echoed back. |
| 65 | + |
| 66 | +  |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +  |
| 72 | + |
| 73 | + * Curl: |
| 74 | + |
| 75 | + Alternatively, you can also use curl for communicating with the bot. You can download curl from |
| 76 | + https://curl.haxx.se/download.html |
| 77 | + |
| 78 | + Open terminal and go to the location where curl is installed and run the below command for a specific conversation: |
| 79 | + |
| 80 | +``` |
| 81 | +curl -H "Authorization:Bearer {SecretKey}" https://directline.botframework.com/api/conversations/{conversationId}/messages -XGET |
| 82 | +``` |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +5. Direct Line API 3.0 |
| 87 | + |
| 88 | + With 3.0, you can also send rich media such as images or hero cards unlike the earlier versions. In DirectLineBotDialog.cs, one of the case statements looks for the text "send me a botframework image" to send image |
| 89 | + |
| 90 | +```c# |
| 91 | +case "send me a botframework image": |
| 92 | + |
| 93 | + reply.Text = $"Sample message with an Image attachment"; |
| 94 | + |
| 95 | + var imageAttachment = new Attachment() |
| 96 | + { |
| 97 | + ContentType = "image/png", |
| 98 | + ContentUrl = "https://docs.microsoft.com/en-us/bot-framework/media/how-it-works/architecture-resize.png", |
| 99 | + }; |
| 100 | + |
| 101 | + reply.Attachments.Add(imageAttachment); |
| 102 | +``` |
| 103 | + |
| 104 | +Enter this text using the client and view the results via curl as shown below. You will find the image url displayed in the images array. |
| 105 | + |
| 106 | + |
0 commit comments