Let’s Make a Chatbot — Microsoft Bot Framework + Nest.js

Mathieu PICCIOLLI
5 min readMar 4, 2018

In this article we are going to build a Chatbot using Microsoft Bot Framework and Nest.js.

Why this article ? I love Node.js, but Node.js is very permissive and impose nothing. These projects can be a big mess and can quickly become anarchy. For example, when I developed chatbots, I used the Microsoft Bot Builder SDK for Node.js and I wasn’t satisfied with my project structure and I had a lot of trouble to understand the chatbot logic as the project grew up.

Recently, I discovered Nest.js framework which effectively address one of the pain points of Nodejs projects: architecture. Nest aims to provide an application out of the box which allows for effortless creation of highly testable, scalable, loosely coupled and easily maintainable applications. Nest.js Brings TypeScript to Node.js and Express. So, I wanted to try Nest.js for a Chatbot use case. I didn’t find any quickstarter about this topic, so I’ll give you mine. The goal is to provide a clean startup baseline for a bot project that you’ll easily be able to extend for biggest projects

The purpose of this article is not to introduce Nest.js nor Microsoft Bot Framework. For those not familiar with Nest.js: it is a framework for building Node.js web applications. Although Node.js already contains a lot of libraries to develop web applications, none of them effectively address one of the most important subjects: the architecture.

Microsoft Bot framework provides everything developers need to develop and connect smart bots that can interact in natural language with human users, whether using text, Facebook Messenger, SMS, Skype, Slack or other popular services.

Now, let’s get started.

Initialize the project

First, you need to install the Nest.js starter project with Git:

git clone https://github.com/nestjs/typescript-starter.git nestDemo-Chatbot
cd nestDemo-Chatbot

Then install the Bot SDK. Run:

npm install botbuilder --save

Now you have to install all the node dependencies:

npm install

You can delete src/app.controller.ts file, we don’t need it. To check if everything is ok, you can run the project :

npm run start

Now we are ready to get to the heart of the matter.

Start your bot

Modules on Nest.js are the most basic building blocks. Through modules we encapsulate related code that composes our application, like components and controllers. In most cases, you’ll have several modules, each with a closely related set of capabilities. De facto, we will use modules to organize the application structure like that :

First, create a new folder in src/ named bot. This folder will contain all the logic of your Chatbot.

The bot module

You have to create a bot module (bot/bot.module.ts) and declare the list of imported modules which are required bythis module.

Don’t forget to import the bot module in the application module.

The common module

Create a new folder /bot/common and create create common.module.ts file. Common module is a global module registered in the global scope to be used by other modules. In fact, other modules can import and use its exported components and services.

You have to define a chat connector for communicating with the Bot Framework Service (You do not need a Microsoft App ID or Microsoft App Password to run your bot in the Bot Framework Emulator). Use the following code in your bot/common/common.bot.connector.ts file.

Create the file bot/common/common.bot.builder.ts.

The messages module

You have to create an endpoint so the chatbot can listen to user messages. So, create a new folder in bot named messages and a Nest.js controller in src/bot/messages/messages.controller.ts :

To allow the chat connector to capture all messages, create a Nest.js middleware bot/messages/messages.middleware :

Then create the messages module (bot/messages/messages.module.ts) and declare the messages controller . To apply the messages middleware you have to set it up using the configure function.

Next, test your chatbot by using the Bot Framework Emulator. The emulator is a desktop application that lets you test and debug your bot on localhost or running remotely through a tunnel.

Your bot receive messages

First, you’ll need to download and install the emulator. After the download completes, launch the executable and complete the installation process.

Second, run the server:

npm run start

By default, your Nest.js application runs on the port 3000. So, connect bot framework emulator to http://localhost:3000/api/messages and and start chatting with your bot.

For now, your bot should respond with “You said: ” to whatever the user messages the bot.

Edit your Chatbot

Microsoft Bot Framework allows you to manage conversation flow
in order to communicate with a user. Conversations are organized into dialogs. Create a new Nest.js module dialogs.module.ts into /bot/dialogs.

This module imports two dialog modules. For each conversation flow I decide to create a module to separate the different conversation concepts. Create two new folders dialogs/greetings and dialogs/help. In greetings folder create greetings.module.ts and greetings.dialog.ts :

The first dialog flow shows how to create a waterfall dialog.

In help folder create help.module.ts and help.dialog.ts:

The following dialog sends a basic default message.

Don’t forget to import DialogsModule in bot module.

imports: [BotCommonModule, MessagesModule, DialogsModule]

Now you can start two conversations :

Thanks bro for your help.

You are now able to quickly create a chatbot using Nest.js. Thanks to Nest.js modules we have a proper architecture.

Working application can be found on:

--

--