Bot Framework: Hello World

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:

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#

BOT-01-01

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

BOT-01-02

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 templateBOT-01-03
  4. If you are familiar with Web API solutions in .NET, the project setup will be quite familiar to you.BOT-01-04
  5. 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
        • Message
        • ContactRelationUpdate
        • ConversationUpdate
        • DeleteUserData
        • EndOfConversation
        • Event
        • Invoke
        • Ping
        • Trigger
        • Typing
      • Timestamp
  1. Press F5 to run the solution. You should see this page.BOT-01-05
  2. Let’s test the Bot.
    • Run the Bot Emulator
    • Enter your endpoint URL (see step 6)
    • Click ConnectBOT-01-06
  3. Type in anything.
    • Your bot should respond with your message and how many characters your message was.BOT-01-07

Dissecting the code

  1. 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.BOT-01-08
  2. 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 RootDialogBOT-01-09
  1. Looking at the RootDialogBOT-01-10
    • 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.

 

References

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s