In this video, find out how to configure Direct Line. Using Direct Line API, develop a client to send messages to your bot by issuing HTTP requests.
- [Instructor] To see Direct Line in action you can double-click on their Direct Line box solution and this should open in Visual Studio, and look as follows: On the right panel here you'll see the solution explorer with a couple of projects, the DirectLineBot project and the DirectLineSample project. You are not required to use the DirectLineBot, so you can use any bot you like as long as you register the bot with the Microsoft Bot Framework. Once you complete the registration you would update the bot's web.config page, so in this case here you'll see the MicrosoftAppId, the password and your BotId.
These are some mandatory fields that will need to be filled in the web.config. Other than that, you'll also need to go to the dev.botframework.com page and once you have this registered you will need to publish it, but before you publish, let's add the Direct Line channel. In this case I already have it running, but if you do not have it, you can click on Direct Line from the list of channels here, and then once you click on a Configure Direct Line you will see a couple of secret keys you can always regenerate by selecting Regenerate.
And We'll also use version 3.0 as the API version for our Direct Line API. Unlike 1.1, there are a few features with the 3.0 you know here there's more support for rich media, you can send attachments, and so on and so forth, which is not present in 1.1. Once you have the secret keys, we go back to the code and we'll need to pass this secret key as directLineSecret, which goes into line 25, along with the botId and user.
You can also specify this in app.config, actually, I would recommend writing this in app.config, which is a more clean way of doing it. We can specify the DirectLineSecret, your botId, and also the user. So all of this will come from one configuration page. Yeah, and that's it, you're otherwise good to go. Now, looking at the code more specifically, some of the things that I would like to pay attention to is in the startBot conversation method of program.cs class, here we are creating a new DirectLineClient, and starting a conversation here, and user messagces are then sent to the bot using the Direct Line Client method, which is PosActivityAsync, so notice that we have a conversation here, and then we're getting the I'd and the userMessage, and they're passed into this method, PostActivityAsync.
Messages from the bot are continuously pulled from the API in another thread in the ReadBotMessageAsync method. Besides this, now if you look at our DirectLineBot dialogue, here this is a very simple bot, I would take a message, check its text and with a bunch of use-case statements, so if you ever see a show me a hero card, we'll respond with a hero card attachment, similarly with the bot framework message, if you see that message, send me a bot framework message.
Otherwise, by default, this will add like an echo bot with the text, you said, at the start of the message that you sent. Now, let's run this code, so we should see a command where we can pass our message, I'm going to begin with a simple message here, my first message using Direct Line. And then, I should also see a conversation ID, which, I'm going to use that to construct my URL to get a response from the bot.
To get a response from the bot, you can use any REST Client. The one I really like is curl, you know you can run that from another terminal, or if you're looking for a more, like a rich UI based REST client, there are plenty of web based REST clients that you can also download and install. The one that I like is called Advanced REST client, and this you can get from the web site https://advancedrestclient.com. Basically if you just hit this button, get it now, it'll install in your apps, and you can launch the app by clicking this button, and here you're good to go, so the only change I need to make is add the URL and pass a few parameters with the method GET.
So my message to get a response is, https://directline.botframework.com/api/conversations now this is where I would need to pass my conversation ID. So I'm going to delete this, and copy this conversation ID, and I will also need a header, so my header will be Authorization, and use the word Bear at the start, along with the secret key, so, I know that you can get from...
Here and let's paste it there, and now we're good to go. Let's send this message... So, that was success, so you'll see a bunch of getting started messages like, Welcome to the Bot to showcase the DirectLine API, da di da di da, and your first message, which was, My first message using Direct Line. So this went from the client user, and the echoed message came from the bot, where you have, you said, at the beginning of your message. You can also play around with more messages just like this, so you can play with this hero card, or an attachment, but in essence, you're now armed with communicating directly with your bot using Direct Line API.