Bots are banging: Getting Started with the Microsoft Bot Framework & Node.js

Mar 2, 2017 · 10 min read

This post comes from Gabrielle (Gabby) Crevecoeur who originally posted this on her blog No Way… She Codes?!? — read more posts from Gabby here. Now, onto the post!

Image from Pixabay.

So I made a new bot but this one is a bit different than my typical NodeBots. In fact, there wasnt even any hardware involved! Instead I used the Microsoft Bot Framework, which is used to build interactive bots that are made to interact with users using natural language. Bots made with this framework can be used on Skype, Slack and even to be implemented in your text messages.

My coworker, Kevin and I teamed up and created a bot that would be handy for many college students! We built a chat bot that will allow you to study your Quizlet flash cards and heres how:

(Keep in mind you can follow this tutorial to get any bot up and running; If you dont want a flash card bot, just ignore the Quizlet parts).

The Quizlet Chat Bot

The Chat Bot created by Kevin and Gabby is actually a flashcards bot! If you are on the go but would like to get your study on, use this bot to study your Quizlet flashcards anytime. So lets get started.


  1. You are going to need an Azure account. Sign up at here for your free trial
  2. Node.js
  3. Quizlet Developer Account

Step 1: Setup on Azure

After you have created your Azure account, it is time to create a web application for the ChatBot to run on. This is where the endpoints for communication with your bot are created.

  1. Go to
  2. Select ‘New’
  3. Then ‘Web + mobile’
  4. And finally ‘Web App’
  5. Create a name for your App
  6. Choose your subscription
  7. Resource Group: choose Default
  8. App Service Plan: choose Default
  9. Click Create.

Once your Web App is created and available in the ‘All Resources’ menu. Go to your Web Apps Overview section and find the url! Save that url somewhere because it will come in handy later.

Step 2: Register Your Bot

After your web app has been created, you will need to register your bot on the bot framework site.

  1. Go to
  2. Give your bot a name, a bot handle (which will be used in the web link of the bot) and the description of your bot
  3. Next, you need to configure your Message Endpoint. This is the url you got from your Azure Web App. You need to be sure you use https at the beginning of the link and add /api/messages to the end of the link. i.e.
  4. Then Generate your Microsoft App Id and Password by pressing the ‘Create Microsoft App ID and password.’
  5. Your App ID will automatically populate and you need to save your App password somewhere separately, because it will be hidden, until you regenerate a new one.
  6. Lastly, you will need to add your APP ID and APP PASSWORD to your Azure settings. Go back to your web app overview, and in the task pnnel, go down to Application Settings.
  7. Scroll down to the App settings section and fill in your APP ID ad APP PASSWORD. The Key column should state MICROSOFT_APP_ID and the value is the App ID you got from Bot registration. Same goes for the App password, except the Key is MICROSOFT_APP_PASSWORD and the value is the App Password you got from Bot registration.

Step 3: Get coding

First create a directory! In the working directory, you will need to set up the projec as a node project and then download the proper node modules.

  1. Initialize the node project npm init
  2. Install proper node modules npm install --save botbuildernpm install --save restify
  3. Create an app.js file in your directory
  4. Create an another js file that will communicate with the quizlet API (in this repository, the file is called api.js)

In your app.js file you will need the following required code just to properly set up your bot:

This is just the bare bones of the bot. Before we add any dialogs, lets be sure your api file is set up correctly.

Quizlet API Code

In order for this bot to work, we are going to have to makw two API calls. One to retrieve the different sets a user has and other to retrieve the cards in a set. Before you can make any of the calls be sure to get your Quizlet Developer Client ID from the Quizlet API 2.0 Developer Dashboard. You can also retrieve the api code from the api.js file in this repository.

Lets take a look at the GetSets function:

Because the api.js file will be used a node module in other files, all variables and functions needed outside of this file will be exported . The GetSets function does a Get call to a Quizlet uri specific to retrieving a users study sets, with username passed into it and the client ID you received from Quizlet.( The username is passed in from outside files calling the function, so it is not a variable you need to instantiate in this file.) The body received back from the API will contain the different study sets and other information associated with them. The rest of this function will manipulate the the body so that we can store the names of the sets (in the set array) and the set IDs (which will be needed in the next function) using a hash table. Lets take a look at the GetTerms function.

The GetTerms function does a Get call to the Quizlet uri specific to retrieving the flashcards of a specified set, using the hash table of set IDs and the client ID. The key parameter will be what set name the user chooses in the bot dialog (in which we will discuss later).The rest of this function will manipulate the the body so that we can store the terms of the flashcards (in the terms array) and the definitions in another array.

Now that we have gone over the api file we can go back to creating your bot. In your app.js file, after the bot setup section, we will add bot dialogs. Before we do that, be sure to add the following right before Bot Setup:

The code we added instantiated a username variable, that will be used to determing the username of the user, the API module that will be referred to as quiz and an index value that will be used later. Under the new variables, you will see a self invoking function (in which you never have to explicitly call it, it will run automatically). The self invoking function is in case you would like to hardcode the username and skip some steps in the bot dialogs we are going to discuss next; if the username is hardcoded it will call the GetSets function in the API, so it is ready for the user.

Dialogs are used to manage the bots conversation with a user. They are called upon the same way you would call a webpage on a website, routing. ie. ‘/’ is the root dialog — which is the first thing the bot will say when the user calls upon it. ‘/test’ is a dialog named test

The Dialog section to app.js is below:

Looking at this code, you see that the dialog starts with the root function. Simply asking the user if they would actually run to the program. The next dialog, ‘/user’, checks to see if the user would like to study; if they choose yes, it then checks to see if there is a hardcoded username or if we need to ask the user for one. If the username was hardcoded it will just jump to to the ‘/subject’ dialog because the users study sets were found already in the self invoking function discussed earlier, if not it will prompt for the username, call the GetSets function with the new username and then call the ‘/subject’ dialog. In the ‘/subject’ dialog the user is prompted as to what study set they would like to study. Once they choose, GetTerms is called based on their decision and then the bot will go to the ‘/study’ dialog. In the ‘/study’ dialog, the act of looking at terms, “flipping” the card for the definition, moving to the next card and possibly exiting early is possible. We use the index variable to keep track of what card we are in for both the term and def arrays.

Lets break down some components of this dialog.


In every dialog, you see a parameter named session. The session object is passed to your dialog handlers anytime your bot receives a message from the user. The session object is the primary mechanism you’ll use to manage messages received from and sent to the user



Waterfalls are seen in several of the dialogs seen above.Waterfalls are used to let you collect input from the user using a sequence of steps. Many dialogs will have several functions inside of them in which one function will be called after the user. Most waterfalls work in a way such that you prompt a user for information in one function, then the answer is passed to the next function, in which you will manipulate the answer received.



As you noticed in the ‘/subject’ example and other functions as well, many times users are asked for an answer, in which we need the data, there is a line that states builder.Prompts.text(). The bot framework has built in prompts available that can be used to collect input from a user.

Different return types of prompts available: builder.Prompts.text(session, "What's your name?");builder.Prompts.number(session, "How many do you want?"); builder.Prompts.time(session, "When is your appointment?"); builder.Prompts.choice(session, "Which color?", "red|green|blue");


So what if you want to know how the user responds to a question, but you dont need access to the answer data? There are Intents! The IntentDialog class lets you listen for the user to say a specific keyword or phrase. Once a user sends a response, you can see if their response “matches” certain words/phrases:


Step 4: Continuous Integration

If you noticed, your web app has no code to know what exactly to run. First in you code directory, create an index.html file, where you simply print “Hello World!”

After push your whole directory to Github! And then you will need to set up continious integration via Github in your Azure Web App. Here is a step by step on how to do so:

Step 5: Testing Your Bot

If you have a Windows Machine! You can test your bot on the Bot Emulator. You can install it here ! You will need your APP ID and APP Password to enter it into the emulator and get to testing.

Node.js Collection

Community-curated content for the millions of Node.js users.


Written by


Node.js is a collaborative open source project dedicated to building and supporting the Node.js platform.

Node.js Collection

Community-curated content for the millions of Node.js users.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade