In the recent major update to the Microsoft Bot Framework (v3), a new type of Activity was introduced, a Typing activity. This Activity type sites along side others, such as Message activity which is the most commonly used activity to represent most communications between a bot and a user.
So, what is the Typing activity for and how do you use it?
We are all very used to chatting with people in online scenarios, from Facebook to Skype, it is something that you no doubt do every day. One of the features that is now pretty much standard across chat platforms is the ability to see when the other person is typing. The knowledge that they are typing gives us reassurance that we are not just staring at a screen waiting for something to happen and that they are responding to our message.
However, what happens when you are talking with a bot? No, a bot doesn’t actually type anything, but there will be times that bots that you build need to call off to external services or perform some form of calculation that prevents an immediate response. Previously, in v1 of the Bot Framework, people chatting with our bot were left wondering if their message had been received at all. This is why the new Typing Activity type has been introduced.
Using the Typing Activity you can do two things;
Firstly, we can give the users of our bots the confidence that our bot is doing something and has received their message by sending a Typing activity as a reply to a users message before we continue to process that message. You can achieve this by making a small addition to the Post method within the MessagesController class. In the code below you can see how I create a new Activity object (called isTypingReply) and then set its type so that it is a Typing Activity. Finally we use our connector client to send this new activity as a reply to the conversation.
Secondly, you can detect when a user is typing to your bot and respond in some way or trigger a background task. For example, when a user starts typing to your bot, you might want to check a cache to ensure it hasn’t expired and if it has start to preempt your users request by re-hydrating it. Below shows the default method that is contained in the MessagesController class created using the Bot Application template, and shows you where you can put your code to respond to the user typing.
So there you have it, a very straightforward way to make the conversational experience through your bots even better and more natural and a very welcome addition to the Bot Framework.