Microsoft has introduced Bot Framework which allows developers to build intelligent conversationalists across a multitude of channels (i.e. Skype, Facebook Messenger, etc.)
It also can be hooked up to Microsoft Cognitive Services which makes it smarter and more sophisticated.
I have been trying out this technology for a while and here are my learnings:
To get started, you will need the following:
- Visual Studio – I am using VS 2015 Enterprise
- Bot Framework Template
- Download here.
- Once downloaded, extract the template.
- Place it on the directory below
- C:\Users\<user>\Documents\Visual Studio 2015\Templates\ProjectTemplates\Visual C#
- Bot Emulator
- Download here.
- Install Builder.Extensions via NuGet.
Create your first bot
- Open Visual Studio
- Click File -> New -> Project
- On the left pane, click Visual C#. You should be able to see the Bot Application template
- If you are familiar with Web API solutions in .NET, the project setup will be quite familiar to you.
- If you check the MessagesController, you should see a POST
- This method will handle incoming messages from the user
- The activity parameter will contain the details of the conversation. For the meantime, we will be focusing on the following properties:
- Text – The content of the message
- Type – indicates what the activity is
- Press F5 to run the solution. You should see this page.
- Let’s test the Bot.
- Run the Bot Emulator
- Enter your endpoint URL (see step 6)
- Click Connect
- Type in anything.
- Your bot should respond with your message and how many characters your message was.
Dissecting the code
- Click your message in the Bot Emulator.
- You should see a JSON payload on the right side of the screen.
- This is the activity object being passed to the POST method of the Messages endpoint.
- On the MessagesController’s POST method, we can verify what happened when we sent the message.
- First, it checks the Activity Type
- If it is a message, it creates a Dialog
- A Dialog is a component of the Bot Framework which contains conversational logic.
- In this example, the Dialog used is RootDialog
- Looking at the RootDialog
- The relevant classes in implementing Dialogs is on Bot.Builder.Dialogs namespace.
- Dialog classes need to be Serializable (do this by adding the [Serializable] attribute).
- The Dialog class needs to implement IDialog and the required method StartAsync
- When a Dialog is constructed, the StartAsync method is called immediately.
- In the StartAsync method, context.Wait(<delegate>) defines the method in the dialog to be called when the next message arrives, in this case we are using MessageReceivedAsync.
- Once a message is received, the delegate specified in the Wait method is passed along with the Dialog Context (context) and the Activity (result)
I hope this post has been helpful in starting your Bot Framework journey. Next time, let’s take a deeper look into dialogs and create our own custom Dialog.