Welcome back Folks !!
In our part 1 we are reading about the overview of building the chat bot using botkit and the RASA NLU that runs in the premise without having dependency on the cloud services.
Here we will be looking at developing our Chat Bot Agent server using the botkit framework.
In our part 1 we are reading about the overview of building the chat bot using botkit and the RASA NLU that runs in the premise without having dependency on the cloud services.
Here we will be looking at developing our Chat Bot Agent server using the botkit framework.
Building the Agent
Preparing the environment
We will start with setting up the environment for setting up the agent.
Install the Node.
- You can get the installer from here. Install the node after it gets downloaded.
- Once it gets succesfuly installed, open the command prompt and type.
- If the output shows some version number means the installation is success.
Installing the botkit using NPM (Node Package Manager)
This is going to be the initial directory structure.
and the package.json will look like above
Once the Visual studio code opens, open the index.js from the file explorer on the left.
1. First lets include the header that is needed to include the needed library.
The above code have the comments that are self explanatory.
2. Create the express server instance and the HTTP server instance. Express server will act as a web server which uses the http server to run on a particular port. Both can be created as follows.
3. Now lets create the Bot kit controller instance by using the Bot kit package that we included as follows. The Bot Kit Controller handles all the incoming and the outgoing connections and also the interfaces to handle the message.
Also after creating the controller, let us tell the controller to use the express and the http server as the web server that it needs to use for the communication.
4. Lets now set the bot into action.
5. Next lets make the Bot to hear to the message the user types in. For that we need to use the controller.hear() method to do it. Hear method takes two parameters, one is the 'expression' of the message that needs to be listened up, other is the function(Bot,message) that handles the incoming message.
6. You can provide the multiple hear methods with different expression to handle the message. But if no hear matches the message then to have a fall back mechanism we need to add the following code.
All things are done. Combining everything following will be the look of the "index.js"
Next our job is to run the bot and talk with the bot. Since we have not created the front end. We will use any web socket client to communicate with our bot.
Start our bot using the following command in the current bot folder.
Command : node index.js
This will start the server in the port 3000.
Lets see how to communicate with the bot in the next post ..Follow the link to communicate with our bot. :-)
Next >> http://creospiders.blogspot.com/2018/04/CompleteOn-PremiseandFullyCustomisableChatBotpart3.html
- Execute the following commands to move into the Botkit folder
- cd Botkit
- Initialise the npm package.json using
- npm init
- You can see the package.json file created after the wizard.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "botkit", | |
"version": "1.0.0", | |
"description": "TechSol Chat bot", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Pranav Kumar H M", | |
"license": "MIT" | |
} |
- Add the botkit package using the command (--save is to save the package to the package.json)
- npm install botkit --save
- Install the Visual Studio code as the editor for the node js. Download Link
- Open the Botkit folder in the visual studio code.
- Add a new file named index.js
Now all set for the Agent server. Lets start coding the server.
Once the Visual studio code opens, open the index.js from the file explorer on the left.
1. First lets include the header that is needed to include the needed library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Botkit = require("botkit"); // Botkit agent | |
var debug = require('debug')('botkit:main'); //debug package from Botkit to display the logs | |
var express = require('express'); // Express package to start the http server | |
var bodyParser = require('body-parser'); // body-parser to parse the request body | |
var querystring = require('querystring'); // querystring package to parse the values from the request query string | |
var debug = require('debug')('botkit:webserver'); // Another debug instance for the BOTKit Webserver | |
var http = require('http'); // Http module to initialise the http server along with express. |
2. Create the express server instance and the HTTP server instance. Express server will act as a web server which uses the http server to run on a particular port. Both can be created as follows.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Creating the webserver | |
var webserver = express(); | |
webserver.use(bodyParser.json()); // Including the body parse to the webserver to parse the body | |
webserver.use(bodyParser.urlencoded({ extended: true })); // making the webserver to have the url encoded requests. | |
var server = http.createServer(webserver); // creating the actual http server for out BotKit server | |
server.listen(3000, null, function() { // Listening on port number 3000. Feel free to choose any free port for your bot | |
debug('Express webserver configured and listening at http://localhost:' + 3000); | |
}); |
3. Now lets create the Bot kit controller instance by using the Bot kit package that we included as follows. The Bot Kit Controller handles all the incoming and the outgoing connections and also the interfaces to handle the message.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var controller = Botkit.socketbot({ // Creating the type of Bot.Here we use the socketBot which works on the Websocket connection | |
debug: true, | |
log: true, | |
hostname: 'localhost'}); | |
controller.webserver = webserver; // Provide the running server to the Botkit Controller instance | |
controller.httpserver = server; // Provide the running http server to the Botkit Controller instance |
Also after creating the controller, let us tell the controller to use the express and the http server as the web server that it needs to use for the communication.
4. Lets now set the bot into action.
- controller.openSocketServer(controller.httpserver);
- This will open the websocket server in the http server port that has been specified.
- controller.startTicking();
- This will start the initialize the bot and will set the environment ready to work.
5. Next lets make the Bot to hear to the message the user types in. For that we need to use the controller.hear() method to do it. Hear method takes two parameters, one is the 'expression' of the message that needs to be listened up, other is the function(Bot,message) that handles the incoming message.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Open the web socket server | |
controller.openSocketServer(controller.httpserver); | |
// Start the bot brain in motion!! | |
controller.startTicking(); | |
//Make the bot hear the user message | |
controller.hears('hello','message_received',function(bot, message) { | |
// do something! | |
bot.reply(message, 'Hello human'); | |
}); |
6. You can provide the multiple hear methods with different expression to handle the message. But if no hear matches the message then to have a fall back mechanism we need to add the following code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// default message if there was no response | |
controller.on('message_received', function(bot, message) { | |
bot.reply(message, 'Sorry, I`m not prepared to respond to that message.'); | |
return false; | |
}); |
All things are done. Combining everything following will be the look of the "index.js"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Botkit = require("botkit"); // Botkit agent | |
var debug = require('debug')('botkit:main'); //debug package from Botkit to display the logs | |
var express = require('express'); // Express package to start the http server | |
var bodyParser = require('body-parser'); // body-parser to parse the request body | |
var querystring = require('querystring'); // querystring package to parse the values from the request query string | |
var debug = require('debug')('botkit:webserver'); // Another debug instance for the BOTKit Webserver | |
var http = require('http'); // Http module to initialise the http server along with express. | |
var controller = Botkit.socketbot({ // Creating the type of Bot.Here we use the socketBot which works on the Websocket connection | |
debug: true, | |
log: true, | |
hostname: 'localhost'}); | |
//Creating the webserver | |
var webserver = express(); | |
webserver.use(bodyParser.json()); // Including the body parse to the webserver to parse the body | |
webserver.use(bodyParser.urlencoded({ extended: true })); // making the webserver to have the url encoded requests. | |
var server = http.createServer(webserver); // creating the actual http server for out BotKit server | |
server.listen(3000, null, function() { // Listening on port number 3000. Feel free to choose any free port for your bot | |
debug('Express webserver configured and listening at http://localhost:' + 3000); | |
}); | |
controller.webserver = webserver; // Provide the running server to the Botkit Controller instance | |
controller.httpserver = server; // Provide the running http server to the Botkit Controller instance | |
// Open the web socket server | |
controller.openSocketServer(controller.httpserver); | |
// Start the bot brain in motion!! | |
controller.startTicking(); | |
//Make the bot hear the user message | |
controller.hears('hello','message_received',function(bot, message) { | |
// do something! | |
bot.reply(message, 'Hello human'); | |
}); | |
console.log('I AM ONLINE! COME TALK TO ME: http://localhost:' + 3000) | |
// default message if there was no response | |
controller.on('message_received', function(bot, message) { | |
bot.reply(message, 'Sorry, I`m not prepared to respond to that message.'); | |
return false; | |
}); |
Next our job is to run the bot and talk with the bot. Since we have not created the front end. We will use any web socket client to communicate with our bot.
Start our bot using the following command in the current bot folder.
Command : node index.js
This will start the server in the port 3000.
Lets see how to communicate with the bot in the next post ..Follow the link to communicate with our bot. :-)
Next >> http://creospiders.blogspot.com/2018/04/CompleteOn-PremiseandFullyCustomisableChatBotpart3.html
Comments
Post a Comment