Node.js: Developing a chat bot on Microsoft Azure Portal

To develop  chat bot on Microsoft Azure Portal you should have Microsoft Outlook ID and you need to login to the Azure Account and activate the free trial  for next 30 days.

After you have setup the Azure account he we need to go to:

New > AI+Cognitive Services > Web App Bot

As shown in the pic below:

1a
Azure Portal

Click on the Web App BOT and fill the fields as shown below,

then, click on Create.

Your BOTS Dash board will be ready,

1d

Now Go to Build under BOT Management and Click on Open Online Code Editor

1e

After this your development console will be ready,

1f

How can we develop this BOT using Node.js?

Let’s see,

The initial configuration code is already provided,


var restify = require('restify');
var builder = require('botbuilder');
var botbuilder_azure = require("botbuilder-azure");

// Setup Restify Server
var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
});

// Create chat connector for communicating with the Bot Framework Service
var connector = new builder.ChatConnector({
appId: process.env.MicrosoftAppId,
appPassword: process.env.MicrosoftAppPassword,
openIdMetadata: process.env.BotOpenIdMetadata
});

// Listen for messages from users
server.post('/api/messages', connector.listen());

var tableName = 'botdata';
var azureTableClient = new botbuilder_azure.AzureTableClient(tableName, process.env['AzureWebJobsStorage']);
var tableStorage = new botbuilder_azure.AzureBotStorage({ gzipData: false }, azureTableClient);

// Create your bot with a function to receive messages from the user
var bot = new builder.UniversalBot(connector);
bot.set('storage', tableStorage);

Now we have to define the route for our incoming messages,

In the below code, the bot.dialog function will capture all incoming messages, and the session parameter will contain all data relevant to the current conversation.

Moving forward to add the dialogue flow.

Your bot.dialog function would look like this:


bot.dialog('/', function (session, args) {
if (!session.userData.greetings) {
session.send("Hello. What is your name?");
session.userData.greetings = true;
} else if (!session.userData.name) {
getName(session);
}
else if (!session.userData.purpose) {
getPurpose(session);
}
else if (!session.userData.AnswerforPurpose) {
getAnswerforPurpose(session);
}
else if (!session.userData.email) {
getEmail(session);

} else {
session.userData = null;
}
session.endDialog();
});

function getName(session) {
name = session.message.text;
session.userData.name = name;
session.send("Hello, " + name + ". How may i help you? you can ask me like 'Book me a flight to New York or Show me available flight for New York'");
}

function getPurpose(session) {
purpose = session.message.text;
session.userData.purpose = purpose;
session.send('Here are available flights to New York: 1) US Airlines - $1000 2) Emirates - $1100 --> please select any one');
}

function getAnswerforPurpose(session) {
AnswerforPurpose = session.message.text;
session.userData.AnswerforPurpose = AnswerforPurpose;
session.send('ok, we will book for ' + AnswerforPurpose + '... please provide us your email id');
}

//function for getting user email
function getEmail(session) {
var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
email = session.message.text;
if (re.test(email)) {
session.userData.email = email;
session.send("Thank you, " + session.userData.name + ". We had sent you an email with details of your flight.");
} else {
session.send("Please type a valid email address. For example: test@hotmail.com");
}
}

Your BOT will be ready when you will test it in the Web App Chat Box.

The conversation for this bot would look like below,

1g
1
1h
2

To learn more about the BOT, read the official docs.

Advertisement

2 thoughts on “Node.js: Developing a chat bot on Microsoft Azure Portal

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 )

Facebook photo

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

Connecting to %s