Posted on

Build universal bot using NodeJs

Universal bot development using NodeJs

Build universal bot using NodeJs


Build universal bot using NodeJs

Microsoft has recently released Bot framework: it is a very useful framework to build and connect intelligent bots to interact with your users naturally wherever they are, from Telegram to Skype, Slack, Facebook and other popular services.

This article shows how to build universal bot using NodeJs and Bot framework. The purpose is to build an bot which can recognise and describe an image using  Microsoft Cognitive Services.

I have already wrote about bot framework in the following article:  Developing artificial intelligence using .NET.

The demo code is available on Github.

Setup node project

First of all, create a folder for your bot and initialize the node project using:

npm init

Next, you need to install the project dependencies by running:

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

Restify is a is a node.js module built specifically to enable you to build correct REST web services and botbuilder contains the Bot framework.


Here’s a simple diagram of the node project. It illustrates the architecture or the project:

Universal bot development using NodeJs



The ConfigurationHelper.js contains an object which represents the configurations of the bot:

In particular:

  • CHAT_CONNECTOR contains the ID and Password of your bot, which can be obtained here.
  • COMPUTER_VISION_SERVICE contains the Url and the API Key of the Cognitive Services which can be obtained here.


The BotHelper.js contains some utils methods to extract Urls from incoming messages:


The VisionService.js contains some methods to retrieve information from Microsoft Cognitive services and to extract the response sent by the bot:

app.js (Entry point)

app.js is the main entry point of the node server, it runs all processes used by the bot:

Deploy the project

In order to use the bot on the messaging platforms, is necessary to deploy the node project on an hosting provider. There are a lot of options, for example: AWS, Microsoft Azure or Heroku.

In case of a simple demo, I think Heroku is the best choice: it’s very immediate and simple. You can connect your Heroku app with github repository, or upload the source code on server.

Setup bot using bot framework

Once you have deployed the node app on server, you need to register the bot  at the following page:, in order to distribute the bot on all messaging platform supported.

Final Result

Universal bot development using NodeJs


Final thoughts

Bot framework allows developers to build universal bot using Node.js or .NET framework.

Why chat bots are important to your business?

  • Available anytime: consumers are often annoyed when businesses only seem to keep banker’s hours. Consumers don’t all work banker’s hours and need to be able to contact a company any time of the day or night for assistance;
  • Converting Data to Personalized Advertisements: a bot can send you shoppable looks. Depending on which photos and products you have liked or previously purchased, it can send you product recommendations or deliver coupons for in-store purchases;
  • Natural Language Communication: consumers need to believe they are speaking to a real person. Chatbots are programmed to react specifically to direct responses from consumers, and offer the right products for their needs;

The demo code is available on Github.

Cheers 🙂

Posted on

Querying MongoDB using .NET Core

Querying MongoDB using .NET Core

Querying MongoDB using .NET Core

Querying MongoDB using .NET Core


The following article shows how to querying MongoDB using .NET Core.

MongoDB has recently released  the .NET Core support inside the C# driver (Net Core Support Driver). In fact, 2.3.0 version of C# Drivers has already been released to Nuget.

Querying MongoDB using .NET Core

The demo code is available on GitHub.

Setup the demo database

Firstly, you need to install MongoDB on your OS. Before continue, make sure that MongoDB is running correctly using:


Next, execute the following dump which creates the demo database:

mongo demoDB dump.js

Setup .NET Core project

I have previously discussed about .NET Core in the following articles:

Introducing ASP.NET 5 on Ubuntu

Future of ASP.NET is open source and cross platform

.NET Core and MVC: Customize view paths

There are different ways to use .NET Core on your OS, the following example uses the Yeoman aspnet-generator to scaffold a Web API template. It creates the following folder structure:
Samueles-MacBook-Pro:Blog.DotNetCoreMongoDb samueleresca$ tree
├── Controllers
│   └── ValuesController.cs
├── Dockerfile
├── Models
│   └── PostModel.cs
├── Program.cs
├── Properties
│   └── launchSettings.json
├── Startup.cs
├── appsettings.json
├── project.json
├── project.lock.json
├── web.config
└── wwwroot


project.json specifies the packages used by the project. Add the MongoDb drivers and launch the dotnet restore command inside the project folder to restore packages:

Defining model

You need to define a model which reflects the structure of your MongoDB collection, add the PostModel.cs file inside the Model folder:

Defining repository

PostsRepository is the core part of the project: it uses the MongoDB drivers to implement the CRUD operations on the Posts collection. Add the PostsRepository.cs file inside the Repository folder:

Defining controller

The PostsController handles the incoming HTTP requests and invokes the PostsRepository to return the query result:


You can run the webserver using the dotnet run command, and call Action methods  using a generic Web API client:

GET http://localhost:5000/api/posts
POST http://localhost:5000/api/posts?title="Title1"&content="Content1"&name="Test"

The code of this article is available on GitHub, at the following link.


Cheers 🙂

Posted on

Developing artificial intelligence using .NET

Developing AI using .Net

Developing artificial intelligence using .NET

Developing artificial intelligence using .Net
Microsoft has recently released two important AI services:

  • Microsoft Cognitive Services  are APIs which lets you tap into an ever-growing collection of powerful AI algorithms developed by experts in the fields of computer vision, speech, natural language processing, knowledge extraction and web search;
  • Bot framework is a very useful framework to build and connect intelligent bots to interact with your users naturally wherever they are, from Telegram to Skype, Slack, Facebook and other popular services;

This article discovers this services and shows how developing artificial intelligence using .NET.


The demo shows how to build a Bot that can automatically caption photos sent by users.


  • Visual studio 2015 community edition , download here;
  • Bot conversation emulator, click here to download;
  • Microsoft cognitive services subscription, sign in here;
  • Visual studio Bot Framework .NET template, which can be downloaded here. To install, save the zip file to your Visual Studio 2015 templates directory which is traditionally in "%USERPROFILE%\Documents\Visual Studio 2015\Templates\ProjectTemplates\Visual C#\";

Setup the bot project

Create a new C# project using the new Bot Application template:

Developing artificial intelligence using .NET

Visual studio generates a new controller, by default, called MessageController, which is the main entry point of your bot:

Setup the bot emulator

The emulator gives the possibility to test your bots locally. You can download the emulator here.

Developing AI using .Net

The emulator requires 3 params:

  • The Url for your bot set the localhost:<port> pulled from the last step. You will need to add the path “/api/messages” to your URL when using the Bot Application template;
  • MicrosoftAppId field is NOT required to test bots locally;
  • MicrosoftAppPassword field is NOT required to test bots locally;

Important: you need to run your bot project in Visual Studio, to use the bot emulator.

Setup images recognition (Microsoft cognitive services: Vision)

Microsoft Cognitive Services APIs offer the possibility to implement AI inside our applications. There are different services:  Speech, Language, Knowledge, Vision and Search. This demo uses the Vision service to caption photos sent by users.

Class schema

Developing artificial intelligence using .Net


Firstly, add the IVisionConnector.cs interface and VisionConnector.cs class to your project:

The IVisionConnector interface is referenced by MessagesController and describes main methods which are used by VisionConnector. API Token is required by VisionServiceClient to consume APIs. You can get the APIs token here.

VisionConnector implements methods to communicate with Microsoft Cognitive API.

Next, go to MessagesController.cs class file and replace the following code:

MessageController class watches  all incoming images and returns the detected caption.

All together now!

Finally, run your Visual studio project and set the bot URL on Bot emulator:

Developing artificial intelligence using .NET

Final thoughts

It’s very easy developing artificial intelligence using .NET, Microsoft Cognitive Services  and Bot framework .

They let you build cross-platform apps with powerful algorithms using just a few lines of code.

Bot framework  is compatibile with the most famous chats: Facebook, Telegram, Skype and Whatsapp.

The example is available on GitHub.

Cheers 🙂