Basics of Bot Development

What is a Bot?
To begin with , If i think i can give better conventional explanation than Microsoft about "What is a BOT?", I will be an idiot.  Check here for that conventional stuff.
But, i have funny way to begin this series.

Got it ?
Jokes aside, The way we have a automated robot to do regular checks and clean the dust, the same way an app which performs some moderate to intelligent tasks without human intervention is a BOT. This can free man hours for better job, "Imagining and Thinking".

You should check Microsoft Bot Framework page for great information.  Then whats the point of this post? you may ask.

What we will be doing different and detailed, in this and following posts?
My objective is to give better understanding on below aspects:
           1. Build a basic bot and show how to test it.
           2. Build a bot with enhanced features (in memory storage and better ways of communication)
           3. Integration with a web service to perform certain tasks
           4. Creating a full blown functional bot.
           5. Hosting the Bot in Azure and consuming on O365 Products.

Lets start with how to build a basic bot.

Install Node ,  Bot Builder SDK and Restify.

Node : Microsoft BOTs can be built using wither C# or Node Js. We choose Node for its simplicity and ease of Development.
Bot Builder SDK : Microsoft's framework for BOT development.
Restify : Basically you may need azure subscription and create an App service and generate App Identity and App secret. Then use those in the Bot Configuration , then deploy the BOT and host it on Azure. Then these bots can be consumed by one of the Office applications like either Skype, MS Teams , Sharepoint Online sites and many others.  But with Restify, you can instantly host the bot on a API endpoint, ready for testing.

Download Node and install.
Create a folder where you want to create your first bot. Open Powershell or command window navigate to that path.

Now run "npm init" . This will create a package file with all the information collected from this command.

Now run "npm install --save botbuilder".  This will create a modules folder in which it installs all the files required for Bot SDK.

Now run "npm install --save restify". With this it will install files required to host the bot on a API end.

Go to the path where you choose to create bot, now create a new file name app.js.
Add below code to it.

var restify = require('restify');
var builder = require('botbuilder');

// 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

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

// Receive messages from the user and respond by echoing each message back (prefixed with 'You said:')
var bot = new builder.UniversalBot(connector, function (session) {
    session.send("You said: %s", session.message.text);

save and close the file. I put comments in green so code is self explanatory.
The only response of our basic bot is "You Said : blah blah blah . . ."

Now open powershell or command window and run "Node app.js". This command will make the bot host on localhost as an API end point.

Now restify hosted our bot on http://localhost:3978 and we can interact the bot on http://localhost:3978/api/messages

Its time to put our bot to test. Download Bot Framework Emulator.

While running out bot on restify, open the emulator and click on "New Bot" and add details as shown below.

Once save and connect you are good to go to test.
You can even see the service calls and parameters and responses in the log.

There, here is out first basic bot.
In the end you might be having same question as i have after the first demo.

How many lines of code do i need to write for a proper response to many questions or queries from users?
Please note that we are working on a functional bot, not a QnA Bot.

QnA Bot queries a source (either FAQ page or a PDF or any other posts) and bring the answers from that source by matching the words from the user query or question.

Function Bot is designed for a specific purpose . like managing your calendar or raising incidents or querying user base for specific skill and so on.

Next post, we will see various new controls and better ways of communications in bot.

No comments:

Post a Comment