Microsoft has introduced Bot Framework which allows developers to build intelligent conversationalists across a multitude of channels (i.e. Skype, Facebook Messenger, etc.).

It can also 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 in this post, we will create a simple bot.

Getting Started

To get started, you will need the following:

  1. Visual Studio – I am using VS 2015 Enterprise.
  2. 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#

  1. Bot Emulator
  2. Install Builder.Extensions via NuGet.

Create your first bot

  1. Open Visual Studio.
  2. Click File > New > Project.
  3. On the left pane, click Visual C#. You should be able to see the Bot Application template.

  1. If you are familiar with Web API solutions in .NET, the project setup will be quite familiar to you.

  1. If you check the MessagesController, you should see a POST method.
    • 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 of the activity:
      • Text – the content of the message
      • Type – indicates what the activity is
        • Message
        • ContactRelationUpdate
        • ConversationUpdate
        • DeleteUserData
        • EndOfConversation
        • Event
        • Invoke
        • Ping
        • Trigger
        • Typing
      • Timestamp
  1. Press F5 to run the solution. You should see this page:

  1. Let’s test the Bot.
    • Run the Bot Emulator.
    • Enter your endpoint URL (see step 6)
      • We do not need the Microsoft App ID and Microsoft App Password for now.
    • Click Connect.

  1. Type in anything.
    • Your bot should respond with your message and how many characters your message was.

Dissecting the code

  1. Click the message you sent 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.

  1. 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.

  1. 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 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 called with the Dialog Context (context) and the Activity (result) as parameters.

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.