Microsoft Bot Framework Part 1: Getting Started

One of the many big announcements made at Microsoft’s //BUILD conference in March was Microsoft’s brand new “Bot Framework.” They did this cool demonstration of how to build an automated bot that could order pizza for you just by typing messages to it in plain English!

I have been working on building Bots using the Microsoft Bot Framework. Today I’m going to share my experience surrounding the bots I’ve been putting together, and the basics of what you need to know about building them.

What I used

Microsoft Bot Framework is a powerful tool that makes it easy to create a single back end and then publish to a bunch of different platforms called Channels. You can use any of the following to get started:

I used Node.js, for the ease of using lightweight text editors and command line interfaces with speedy download times.
If you're more of a Visual Studio 2015, C# or .NET fan, Kat Harris has a great set of posts about how to use the Bot Connector Template for Visual Studio.

Install the Tools

For the sake of brevity, I’m going to assume you already have a few things:

Developer accounts on one or more communication services (such as Slack) where your Bot will communicate.

Set up a GitHub repository

First, go to GitHub.com, log in, and click the '+' in the top right to create a new repository. Give it a name and a short description. Make it public. Initialize with a README. Set .gitignore to add "Node," and set license to 'ISC' if you want.
Click Create repository. Copy to clipboard the URL from GitHub. (You may need to click “Clone or download” to see URL.)

Obtain a GitHub repo from a URL

Open up Node.js command prompt, navigate to the folder directory you wish to clone into. (“dir” shows all folders.) The following instructions show you how to CLONE from a repository with files already in it; you do NOT need to CREATE a new folder for your bot:

Watch this screenshot gif to see how.

Install Dependencies

Get the BotBuilder and Restify modules using npm. Do the same for any other modules you want, such as a RESTful API.

>npm install --save botbuilder
>npm install --save restify

I plan on uploading this project onto Microsoft Azure. Making a file called server.js or app.js (or index.js) will automatically cause Azure to recognize it as a Node.js app. Azure will immediately look for a package.json file and install all dependencies listed therein. Make sure package.json is up-to-date with your dependencies before you publish to Azure.
The entry point for the app should be at the main folder level.

>explorer .

[Opens Windows Explorer in the current folder]

>code .

[Opens VS Code in the current file. Check to see dependencies are accurately opening.]

Write the app.js File

Once you have those configured, we can create an app.js file that will setup our application.

In the Node.js cmd prompt, type:

>cls>app.js

[Creates a file called “app.js” in your file explorer. Edit this through VS Code.]

If you want to save your changes so far and push the new files to your repo, you can either use Visual Studio Code to commit and push to GitHub (see screenshot gif), or type these steps into Node.js Command Prompt:

Set up a Microsoft Azure App Service

You’ve probably noticed that you need to do something about that appId: 'YourAppId', appPassword: 'YourAppPassword' code. The AppId is chosen and the AppPassword is generated when your Bot is registered with the Microsoft Bot Framework Connector.

When you fill out the registration form, you’ll need to have deployed a web app to Azure to get your Endpoint URL.

How to create a Microsoft Account and Azure Subscription:

By default, the bot should be published as a Microsoft Azure App Service.

Click on “App Services” in the left blue bar, then click on “Add +”. Fill out the form to create a Web App service. Give it a unique name (preferably what you want YourAppId to be), set the default resource group, app service plan, and server location, using your Azure subscription. (Don’t have one? You can get a free trial from here: https://azure.microsoft.com/en-us/)

Click “Create” when you’re done. The URL assigned to this new web app will be YourAppId.azurewebsites.net. Save this URL for later.

How to create an Azure-accessible REST endpoint exposing a callback for the Connector service:

Register a Bot

Click on “Register a Bot” and fill out the registration form following the site’s instructions.

Copy and paste the URL you saved from your Azure portal into the “Endpoint” field. Prefix the URL with “https” instead of “http”. Add “/API/Messages” to the end of the pasted URL. Azure will take care of providing HTTPS support on your bot.

(CAUTION: this endpoint URL is CaSe SeNsItIvE! Writing "API/Messages" in one place and "api/messages" in another will break your project!)

You can use http://aka.ms/privacy if you need a quick privacy URL. You can use your GitHub repository ReadMe URL as your bot’s website.

When you get to the bottom of the form, under Config Info, type in a new AppID of your own choosing.

Click on “Register” and hit “OK!”

Take note of your newly generated AppPassword and YourAppId.

Watch this screenshot gif to see how.

Keep your App ID and your AppPassword handy while you click on Microsoft Bot Framework's Documentation. In the table of contents, you can search for "Bot Builder for Node.js" > "Getting Started" to view Microsoft's documentation on making conversational bots using Node.js. This is where I found the above "Hello World" code.

In your app.js code, swap out 'YourAppId' and 'YourAppPassword' for the values that you just generated by registering your bot.

Test your bot (Windows Only)

Install the emulator from here and then start your bot in the Node.js command prompt window.

>node app.js

Start the emulator, enter your AppId, AppPassword, and the localhost REST endpoint, and say “hello” to your bot. (Note that the first request after your Bot starts up can take 20-30s as Azure starts up the web service for the first time. Subsequent requests will be quick.) This simple viewer will let you see the JSON object returned by your Bot.

Check out Microsoft Bot Framework Part 2 for more info on how to protect your code secrets by using Environment Variables and complete a Hello World bot!

Hi,
i am able to post my sample project on azure and if test connection in bot registration page i am getting ‘ok’ response.
actually my problem is i am not getting reply from BOT if i type ‘hi’ it should give ‘Hello world’ but it’s not working.

When trying to test my connection to the bot…I have appended /api/messages correctly as far as i can i don’t see the error BUT i keep getting “NotFound The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.” error. ;-(