Building a Chatbot with Rasa and Node.js in python is the most trending topics since the advent of the Artificial Intelligence in day to day like.
In our earlier topic we have provided the 6 Best Chatbot Development Framework in Python where in Rasa was also explained and yes, it’s still the most liked Chatbot Framework amongst the developers. You can also explore to many more Chatbot frameworks available, but when it comes to Rasa it’s one of my favorites.
In this tutorial, we will build a chatbot with Rasa and Node.js. We will show you the complete implementation of the Chatbot with Rasa and Node.js in a step-by-step manner and at the end you will be able to have your first Chatbot with Rasa and Node.js.
Before we start let me provide some brief introduction to actually what is a Chatbot. This will be helpful for the new learners who have just started to implement Chatbot or is trying to implement Chatbot with Rasa and Node.js. If you are an advanced developer, then you can skip this and jump to actual implementation of Chatbot with Rasa and Node.js.
What is a Chatbot?
A chatbot is a computer program designed to simulate human-like conversation with users. It uses artificial intelligence (AI) and natural language processing (NLP) techniques to understand and respond to user inputs in a conversational manner.
For beginners, a chatbot can be seen as a software application that interacts with users through a chat interface, typically via text or voice. It can be deployed on various platforms such as websites, messaging apps, or voice assistants. The goal is to provide automated support, answer questions, offer recommendations, or assist with specific tasks without human intervention.
Chatbot with Rasa and Node.js: Requirements
Before we begin, make sure you have the following installed on your computer:
- Python 3
- Node.js
You will also need to install the following Python packages:
pip install rasa
If you are using a virtual environment, make sure you activate it before installing the packages.
Chatbot with Rasa and Node.js: Creating a New Rasa Project
To create a new Rasa project, run the following command:
rasa init
This will create a new project with the following directory structure:
rasa_project ├── actions ├── data │ ├── nlu.md │ └── stories.md ├── models ├── tests ├── config.yml ├── credentials.yml ├── domain.yml └── endpoints.yml
Chatbot with Rasa and Node.js: Creating a Node.js Server
We will now create a Node.js server to interact with our Rasa chatbot. Create a new directory and navigate to it:
mkdir chatbot cd chatbot
Create a new package.json file:
npm init
Install the following dependencies:
npm install express request
Create a new file called index.js and add the following code:
const express = require('express'); const bodyParser = require('body-parser'); const request = require('request'); const app = express(); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); const PORT = process.env.PORT || 5000; app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); }); app.get('/', (req, res) => { res.send('Hello World!'); });
This will start a new server on port 5000 and display the message “Hello World!” when you visit http://localhost:5000 in your browser.
Chatbot with Rasa and Node.js: Connecting the Server to the Chatbot
Open the credentials.yml file in your Rasa project and add the following:
rest: url: "http://localhost:5000/webhooks/rest/webhook"
This will allow the Rasa chatbot to communicate with the Node.js server.
Create a new file in the actions directory called actions.py and add the following code:
from typing import Dict, Text, Any, List from rasa_sdk import Action, Tracker from rasa_sdk.executor import CollectingDispatcher class ActionHelloWorld(Action): def name(self) -> Text: return "action_hello_world" def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: dispatcher.utter_message(text="Hello World!") return []
This action will respond with “Hello World!” when triggered.
Add the following code to the domain.yml file:
actions: - action_hello_world
This will allow the Rasa chatbot to use the “action_hello_world” action.
Chatbot with Rasa and Node.js: Testing the Chatbot
Start the Node.js server:
node index.js
Start the Rasa server:
rasa run actions --actions actions
Open a new terminal window and navigate to your Rasa project directory. Start the Rasa shell:
rasa shell
Type “Hello” in the chatbot and press enter. The chatbot should respond with “Hello World!”.
In this tutorial, we learned how to create a chatbot using the Rasa framework and Node.js. We also learned how to connect the chatbot to a Node.js server and test it in the Rasa shell. Now that you have the basics down, you can start building your own chatbots with Rasa!
Checkout our earlier blogpost wherein we have talked about implementing Chatbot with in 5 Steps using Microsoft Bot Framework and also Chatbot implementation with Rasa and Angular. This will further help you in enhancing your skills of designing a better Chatbot.
Want to learn more about Python, checkout the Python Official Documentation for detail.