How to Make a Chatbot Using JS

Published: May 2, 2022

15 min read

According to this research, businesses can save up to 30% on serving customer requests with a chatbot. That's because you no longer have to hire humans to do repetitive tasks like answering basic questions or handling monotonous requests. A chatbot will do it way quicker and cheaper.

But for your chatbot to be truly responsive and human-like, it needs a couple of things prehistoric people (<2010s) didn't have — AI and Machine Learning (ML). They allow the chatbot to perform operations that cannot be described as a simple algorithm or a sequence of actions, therefore providing a greater degree of personalization.

So, in case you're willing to integrate a chatbot into your business processes — you're welcome!

Feel free to jump to sections 4 and 5 if you're interested in the technical side of the issue to see how we implement a chatbot using Node.js.

🤖 Rule-based Chatbot VS AI Chatbot Development

When talking about chatbots, it's important to understand the difference between a rule-based and AI-powered chatbot. A rule-based chatbot has pre-written options for a user to choose from, meaning that you can't type anything that's not on the list.

You can think of it as a bunch of buttons on a player, where you can’t actually press anything but the buttons themselves.

But in case you really like some features of both an AI and a rule-based chatbot, you can get the best of both worlds by building a hybrid chatbot. It will generally use rule-based patterns but also rely on Machine Learning for complex tasks such as sentiment analysis or handling textual requests.

Read our guide on ChatGPT integration.

❓ Why Companies Build a Chatbot: Use Cases

Companies use chatbots for a wide range of purposes like automating appointments booking or making personalized offers. In this article, we'll talk about the most common use cases of AI chatbots.

Making Appointments & Booking

Let's say you have a fitness or healthcare business where your users have to make some kinds of appointments. In that case, AI chatbots may come in handy as they would be much cheaper than a real person answering the calls, but also way more flexible compared to rule-based chatbots, being able to handle new situations, like answering a question it has never seen before.

If you're particularly interested in Booking Apps, we have a special article dedicated to developing an online booking system for applications or websites:

Making Personalized Recommendations

In case you offer users a variety of products, you could integrate a Recommendation System into the AI chatbot. Such a system can provide users with a personalized approach and give recommendations based on their previous searching & buying history.

Receiving Voice Inputs & Giving Voice Outputs

If you'd like to improve the accessibility of your chatbot, adding voice Inputs & Outputs might turn out to be a great idea. For example, this could be useful for interacting with an IoT device, which may not actually have a keyboard or a display. The most popular example can be Alexa from Amazon.

Managing a Few Apps & Services

Let's imagine that your business uses a whole suite of different apps and/or services to deal with different tasks. So that your users don't have to constantly switch between them, you can make one point of access to all of them in your chatbot. For example, if your company has different apps for task management, attendance tracking, and planning, you can set up one chatbot and connect to all those services through their APIs.

⚙️ How to Create a Chatbot: Tech Insights

To better understand the logic behind chatbots development, let's take a look at how they function.

(Pre-) Processing stage

The first step in the workflow of any text-based ML application is preprocessing. We need it because computers aren't able to understand the logic behind sequences of characters (such as this article) or sounds (such as a recorded message) — they only perceive numbers. For that reason, we need to somehow turn texts or voice requests into a bunch of numbers.

In case we have voice input, we first have to convert it into text. For this task we can use open-source solutions such as Kaldi or PocketSphinx for mobile devices. Alternatively, if we don't want to run the speech recognition process on our own server, we can use third-party APIs such as Google's Speech-to-Text or Amazon Transcribe.

The first step in preprocessing is tokenization, which is defining the boundaries between tokens (i.e. words in ML lingo). Then, we have to extract entities — it's usually called Named Entity Recognition (NER). This will allow the chatbot to understand that words like “New York” or “the day after tomorrow” are entities, thus, they function as single units in a sentence and should not be separated.

The last important step in preprocessing is Word Embedding. This will actually allow us to substitute separate words with huge numeric vectors. The way word embeddings (vectors that correspond to words) are calculated isn't that important to understand this issue, but the most popular algorithm is Word2Vec. All these steps can be done in a few lines of code using such libraries as Python's SpaCy or Node's Natural/compromise.

By this point, we should already have all the text converted into a matrix of size (embedding dimension x number of words), which means we are ready to crunch these numbers to understand what the user wants from the chatbot.

Understanding the Intent of a User

An intent in chatbot architecture describes what the user wants the chatbot to do with their message. For example, when you ask your friend “How've you been?”, you expect them to share their recent news. In a similar way, when you ask a chatbot “Can I have a meeting with a doctor tomorrow?”, you expect it to either book an appointment for you, or reject the query in case there's no time available and offer another option.

This idea of having discrete intents combines well with the ML task of classification. The point of it is to classify the input data into two or more discrete categories, e.g. tell whether an email is spam or not. In our case with chatbots, we wish to classify a user message into its intent. This is nicely represented by the following graph:

Creating Fully Connected Neural Networks like the one above is pretty easy, and almost any ML library should provide ways to build them. Some of the most popular ML frameworks for this and other tasks are PyTorch, Tensorflow for JS / Python, and mlpack for C++.

Generating a Response

There are a few approaches to generate a response for a given user message.

The first one is to use a generative language model, such as GPT-3 or a simpler Recurrent Neural Network (RNN). But the problem with this approach when integrating chatbots is that it is either unnecessarily complicated (say hello to GPT-3 with 175 billion parameters) or not good enough (RNN's quality of generated text leaves much to be desired). This approach won't use the intents, and will just write the answer based on the message itself. Generating a message from scratch is a very complex task which requires models trained on loads of textual data, which most people don't have.

Another approach is to have generic pre-written messages that correspond to every intent. This kind of model will be much more lightweight and fit well into chatbots' tasks. But it also enables a higher level of customization. For example, the pre-written messages may have placeholders for the user's name, named entities, or any variables at all. Thus, you achieve relatively customizable answers while keeping the model rather simple.

After getting the response in a textual form, you might optionally want to convert it into speech. For this task, there's a number of speech recognition APIs, such as Google's Text-to-Speech or Amazon Polly.

✅ AI Chatbot Implementation

After we talked about how chatbots function on the inside, let's take a look at the process of chatbot implementation.

The easy way: No-Code Platforms

If you don't want to get your hands dirty with writing a chatbot from scratch, you can choose one out of many available chatbot platforms. The most popular ones are Dialogflow (offers a $600 trial) and Wit.ai (free). They both provide a clear and intuitive UI for creating intents, adding named entities, setting up logical flow between different intents, etc. Such chatbots use ML.

Contact Us!

Read also

How can we help you?

Our clients say

Stormotion client David Lesser, CEO from [object Object]

They were a delight to work with. And they delivered the product we wanted. Stormotion fostered an enjoyable work atmosphere and focused on delivering a bug-free solution.

David Lesser, CEO

Numina