See how the Connector, activities, and messages fit into a bot application as Scott modifies the Visual Studio solution to incorporate all three.
- [Instructor] We're back in Visual Studio 2015 and I've opened up the OfficePointBot solution that we created earlier in this session. Let's take a look at a couple of the out of the box lines of code that relate to the connector activity in messages. So you'll notice since this is web API, we've got this default post method that's been created for us, and it's passing an object of type activity. Remember an activity is the housing for all of the events that go back and forth between users and our bot.
And we're essentially saying if this activity is of typed message then run this code, otherwise, go to this handle system message. The important thing here is you'll notice the first line of code when we're going to interact back to our users through our bot is to instantiate a new object of type connector client. And there's a number of overrides here but, in general, we just pass back the service url of that activity and create a connection through the connector client to that.
And then this code just going to look for the length of the text that comes through in the activity. Now, this text will be in this example what has been received in the bot from a user. So if a user types hello world the text will be hello world. And we're going to check the length and then we're going to create a new activity for the reply, so we're going to take this activity and create a reply, this will maintain the thread and there's a number of overrides for create replies well, but in this example we're just going to pass back the text.
You sent this text, which was of this length in characters. And then the important pieces we're going to then, asynchronously say await connector dot conversations reply to activity async. Which means we're going to just take this activity, create a reply and send the reply back. This way we keep it in the same thread. And then, of course, if an activity comes through that's not a message, it's going to go down to the handle system messages event.
And this template by default just fills this in for us with an IF ELSE statement that basically says if it's anything other then a message, go to this case in the IF ELSE statement. For example, here we have a typing event or a ping event. So let's take a look at what's going to happen, so we'll load this up and again we're going to test this through out this session initially in the bot emulator. So let's... run that, now in order to test this easily, let's go ahead and pull this up in our bot emulator.
There's a couple of configuration pieces you need to add to the emulator in order to work with it appropriately. The first one is the bot url, this is your service url. You can always find this service url by going to your solution and going to properties and going to the web tab and you can see that this is loading on local host port 3980. And then, since this is web API, it's going to be followed by API slash the name of the controller and, of course, we have the messages controller.
And then the Microsoft app ID and app password will come from your web config file. But in development if we just keep these blank in both our web config and the emulator, it's going to run fine. It's going to give us a little warning here that says hey there's no password, so security's disabled and that's just fine for us. So let's go ahead and put a break point here and watch what comes through, we'll put one here and here. And in the bot emulator, we'll just type something in like hello world.
And there it passes it off to our post event, it says that, that type is of message. It's going to grab the length, which is 11 characters, it's going to create a reply that's essentially going to say, you sent hello world, which was 11 characters and we're going to pop that reply into the conversations collection for that connector. This line of code is going to send it back to our bot. So let's take a look at that and there we go, you sent hello world, which was 11 characters.
Now, this is a great opportunity to kind of test out some of these other events just to see how they might be handled. We can put a break point, for example, in typing in ping and instead of having to worry about actually typing or doing a ping or controlling that, we can just use the emulator to emulate it. So if we want to descend a typing in the emulator, we can just say, in this drop down list, typing send a typing activity and it comes through and you'll see that that's of type typing.
Or we could send a ping and there we go, it's now of type ping. So it's pretty easy to work with but that's a little bit boring so why don't we add a couple other pieces to that to see how we could populate that initial message that comes through in our activity. Let's go back up here to the reply and remember we're creating a reply here that's just text. One of the cool things about these activity messages is that we can put rich text or content, we can create a number of types of cards like hero cards, we could do receipts, we can have carousel-type lists, we can do all types of fun stuff that we'll get into later in this session.
But we can also do essentially mark up or what the bot emulator calls mark down content. Using mark down properties, so here we've got this reply and I'm just creating this reply you sent this text which was this length, I can change the text format to mark down and I can pop in some additional text into that message using mark down escape characters.
So, for example, a double asterisks means make this bold. Or if I put it in these brackets, and then follow it by a url in parenthesis, this will be a link to a url. Or I could do these double tildes and that would be a strikethrough. So again we're creating this reply, that reply's going to have this text. I'm going to change the text format of the reply to mark down and I'm going to populate it with this mark down text label.
So let's take a look at how that would appear to our users in a bot. So now if I pull up the emulator, let's say hello world now. And even though I spelled hello world wrong, you'll see that it sent you sent hello wordl, which was 11 characters, how ever you should try this out at Bing and you'll notice this is an actual url.
Clickable url and never use Google that is strikethrough. So we said however you should try this, which was bold, out at Bing, which is a clickable link, and never use Google. So just creating our text replies is pretty powerful. It's almost as if we had rich text. Now, a bunch of control over the coloring and things like that doesn't really happen and most of this is because we need to make sure that we're supporting whatever type of mark down will be available for the clients via the channels.
Okay, cool. So again we create a connector object, we create a reply, and we pass it back to the connector object in the conversations. Pretty simple but again a little bit boring. Let's go back over to our slide deck and see what other kind of cool things we can do instead.
Note: This course was created by Wintellect. We are honored to host this content in our library.
- Installation and setup