Skip to main content

Complete On-Premise and Fully Customisable Chat Bot - Part 2 - Agent Building Using Botkit

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.

Building the Agent

Preparing the environment

We will start with setting up the environment for setting up the agent.

Install the Node. 

  1. You can get the installer from here. Install the node after it gets downloaded. 
  2. Once it gets succesfuly installed, open the command prompt and type.

  1. If the output shows some version number means the installation is success.

Installing the botkit using NPM (Node Package Manager)

  1. Choose some directory and make the following directory structure as follows.

This is going to be the initial directory structure.

  1.  Execute the following commands to move into the Botkit folder
    1. cd Botkit
  2. Initialise the npm package.json using 
    1. npm init
  3. You can see the package.json file created after the wizard.
and the package.json will look like above

  1. Add the botkit package using the command (--save is to save the package to the package.json)
    1. npm install botkit --save
  2. Install the Visual Studio code as the editor for the node js. Download Link
  3. Open the Botkit folder in the visual studio code.
  4. 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.
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.

  • 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.


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

Comments

Popular posts from this blog

How to access the each view of item by position in Recycler View in android ?

How to access the each view of item by position in Recycler View in android ? There are some methods to access the view of each item view to change or update the view during the run time. To access the view of the item view , consider the Recycler view as below, RecyclerView mainRecyclerView = (RecyclerView)view.findViewById(R.id.main_recycler_view); RecyclerAdapter adapter = new RecyclerAdapter(mContext); mainRecyclerView.setAdapter(adapter); main.setLayoutManager(new LinearLayoutManager(mContext));  To access the itemView of each position the following functions can be used,  1. View itemView = mainRecyclerView.getChildAt(positionOfItem);  2. View itemView = mainRecyclerView.findViewHolderForAdapterPosition(Position).itemView;  3. View itemView = mainRecyclerView.findViewHolderForPosition(Position).itemView;  4. Long itemId = mainRecyclerView.getAdapter().getItemId(position);       View itemView = mainRecyclerView.findViewHolderForItemId(itemId);  5. View

A.P.I call or async await not working in Array forEach ?

H ello Readers, Welcome back. You would have wondered why does forEach function on the array does not wait for any asynchronous function even if we provide the async await in the node js. If you are the person, wondered about this ever, this post is right for you. Non working example : Lets consider the below snippet, this will not wait for the asynchronous process to wait. I am making a setTimeout to mock the API call async process. This will result the count as, Output : count = 0 OMG !! Why it doesn't work ? Answer probably might be lying inside the Array prototype from JavaScript. Lets take a look inside the "Array.prototype.forEach" function. From the snippet its clear that, the for loop in which they call the callback function does not wait for the asynchronous process to run in the callback. So this forEach is not build for asynchronous process itself. So can't I use forEach any more for running a asynchronous function ? Answer g