Intelligent Chatbot using Microsoft BOT framework

Srimathi Jagadeesan
hackgenius
Published in
4 min readDec 24, 2022

--

How I built another chatbot using another popular framework

Hey Everyone! Welcome to another bot tutorial:) Are you excited? Then read my article till the end to understand the bot terminology. Let’s get started!

What is Microsoft bot framework?

A framework for building enterprise-grade conversational AI experiences. The bot framework is a collection of SDKs that are used to develop chatbots. This technology is used to integrate bots with many platforms like Slack, Kik, Line, Skype and many more.

Bot_Framework

How to develop a chatbot using the Microsoft framework?

That’s so simple, first, you should install some packages. Firstly, you’ve to install Node js, if you do not have one then install using this link.

Open your cmd prompt and install npm.

npm install -g windows-build-tools

Leave for a few minutes since it takes some time to install particular packages.

Now you’ve to install a bot builder for creating a chatbot. For that type the below command in the cmd prompt.

npm install -g yo generator-botbuilder
yo botbuilder

After installing, create a name for your bot and give a description finally select the programming language, like below!

packages

If you’re done , then open up your Visual studio code by typing the cmd

code .

Then your Vs code would be,

packages

Open index.js, and put the below code.



const path = require('path');

const dotenv = require('dotenv');
// Import required bot configuration.
const ENV_FILE = path.join(__dirname, '.env');
dotenv.config({ path: ENV_FILE });

const restify = require('restify');

// Import required bot services.
// See https://aka.ms/bot-services to learn more about the different parts of a bot.
const {
CloudAdapter,
ConfigurationServiceClientCredentialFactory,
createBotFrameworkAuthenticationFromConfiguration
} = require('botbuilder');

// This bot's main dialog.
const { EchoBot } = require('./bot');

// Create HTTP server
const server = restify.createServer();
server.use(restify.plugins.bodyParser());

server.listen(process.env.port || process.env.PORT || 3978, () => {
console.log(`\n${ server.name } listening to ${ server.url }`);
console.log('\nGet Bot Framework Emulator: https://aka.ms/botframework-emulator');
console.log('\nTo talk to your bot, open the emulator select "Open Bot"');
});

const credentialsFactory = new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: process.env.MicrosoftAppId,
MicrosoftAppPassword: process.env.MicrosoftAppPassword,
MicrosoftAppType: process.env.MicrosoftAppType,
MicrosoftAppTenantId: process.env.MicrosoftAppTenantId
});

const botFrameworkAuthentication = createBotFrameworkAuthenticationFromConfiguration(null, credentialsFactory);

// Create adapter.
// See https://aka.ms/about-bot-adapter to learn more about adapters.
const adapter = new CloudAdapter(botFrameworkAuthentication);

// Catch-all for errors.
const onTurnErrorHandler = async (context, error) => {
// This check writes out errors to console log .vs. app insights.
// NOTE: In production environment, you should consider logging this to Azure
// application insights.
console.error(`\n [onTurnError] unhandled error: ${ error }`);

// Send a trace activity, which will be displayed in Bot Framework Emulator
await context.sendTraceActivity(
'OnTurnError Trace',
`${ error }`,
'https://www.botframework.com/schemas/error',
'TurnError'
);

// Send a message to the user
await context.sendActivity('The bot encountered an error or bug.');
await context.sendActivity('To continue to run this bot, please fix the bot source code.');
};

// Set the onTurnError for the singleton CloudAdapter.
adapter.onTurnError = onTurnErrorHandler;

// Create the main dialog.
const myBot = new EchoBot();

// Listen for incoming requests.
server.post('/api/messages', async (req, res) => {
// Route received a request to adapter for processing
await adapter.process(req, res, (context) => myBot.run(context));
});

// Listen for Upgrade requests for Streaming.
server.on('upgrade', async (req, socket, head) => {
// Create an adapter scoped to this WebSocket connection to allow storing session data.
const streamingAdapter = new CloudAdapter(botFrameworkAuthentication);

// Set onTurnError for the CloudAdapter created for each connection.
streamingAdapter.onTurnError = onTurnErrorHandler;

await streamingAdapter.process(req, socket, head, (context) => myBot.run(context));
});
Install restify by ,

npm install restify -g

Install dotenv by ,

npm install dotenv --save

Run your code node index.js

After that, you’ll get an HTTP like localhost:3978 and copy, and paste it to the bot URL.

Now it’s time to install the bot emulator.

What is Bot Emulator?

Bot Framework Emulator is a desktop application that allows bot developers to test and debug bots, either locally or remotely.

Install by clicking on this link

bot emulator

Paste the link in the bot URL, and Click connect. Yeah! Your bot is ready, let’s check. I’ve attached the snap of the output.

This is actually an echo bot. I will provide a GitHub link where you can get the echo bot code.

echo bot
bot

I’m providing my GitHub profile here. Thanks for reading my blog, that’s all for now.

Stay tuned for more articles related to AI, ML, Chatbot & RPA:)

--

--

Srimathi Jagadeesan
hackgenius

Conversational AI || Mentored 500+ Students || Bot Builder|| Tech blogger|| Technical Trainer || Self learner.