In this article, we will build a simple chatbot in Python programming language. The pre-requisites of this article is familiarity with the Flask microframework. Before getting into the development part, let’s see some basics first.

What is a chatbot?

A chatbot is a computer program which conducts the conversation between the user and a computer by using textual or auditory means. It works as a real-world conversational partner.

You have seen different chatbots in your life Siri, Cortana, Alexa and so forth. As per a review, the chatbot is required to finish around 80% of all work in the coming decades. Presently, chatbots are practically finishing 30% of the tasks. With the expanding boom, it has turned out to be imperative to learn Machine Learning and Artificial Intelligence.

chatbot tasks

The tasks being done by chatbot are like:

  • Present some information
  • Make bookings
  • Make calls etc.

Types of CHatbots

There are two main types of chatbots:

  1. Self Learning Chatbots
  2. Rule-based Chatbots

Self Learning Chatbots

Self learning chatbots use machine learning and artificial intelligence techniques. Such chatbots save the input from the users and use them later.

Rule-based Chatbots

Rule-based chatbots used some predefined set of rules. The responses of the chatbot are based on these rules. It means the solutions such chatbots provide are based on the rules defined.

Chatterbot in python

ChatterBot is a Python’s library for chatbots. Consider the following things when you want to make a chatbot:

  • Who is the target audience?
  • What is the natural language of communication?
  • Provide the responses for the target audience.

Chatterbot is a library in Python which generates responses for the users. It uses a number of machine learning algorithms to produce a variety of responses. It is easy to make chatbots using the Chatterbot library in Python. The chatbot should be designed to be language-independent. It should be trained in multiple languages. The chatbot is trained by the data provided by the user.

WOrking of chatterbot

The chatterbot works in the following manner:

  1. Get the input from the user.
  2. Process the input.
  3. Returns the value that is generated with the highest confidence value.
  4. Return the response to the user.

Training the chatbot

You have made a chatbot. Now, how it would produce the responses that you want. For this, there is a library called chatterbot-corpus. It basically has all the responses. We use it to train the bot. Then, it generates the relevant responses that we want.

Developing a chatbot using flask

We will make a Flask chatbot. Flask is a microframework used for web development. We will follow the process given below:

  1. Make a web app using the flask.
  2. Make a directory for the templates.
  3. Train the bot.
  4. Make conversation with the bot.

Project and Libraries setup

I will be using PyCharm to develop this simple chatbot. Create a Flask project using PyCharm. Following libraries are required:

  • chatterbot
  • chatterbot-corpus
  • pytz
  • sqlite3

You can easily install these libraries by going into File>Settings>Project Interpreter.

I have already written the HTML template and CSS. You can just copy and paste it.


<!DOCTYPE html>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="">
    <link rel="stylesheet" href="/static/style.css">
    <script src=""></script>
    <h1 class="jumbotron text-center">Chatterbot in Python using Flask Framework</h1>
<div class="container">
    <div class="row">
        <div class="col-sm-6 offset-sm-3">
            <div id="chatbox" class="border border-success">
                <p class="botText"><span>Hi! I'm Chatterbot</span></p>
            <div id="userInput">
                <input id="textInput" class="form-control" type="text" name="msg" placeholder="Type Your Message Here">
                <input id="buttonInput" class="btn btn-success form-control" type="submit" value="Send">

    function getResponse() {
        let userText = $("#textInput").val();
        let userHtml = '<p class="userText"><span>' + userText + '</span></p>';
        document.getElementById('userInput').scrollIntoView({block: 'start', behavior: 'smooth'});
        $.get("/get", { msg: userText }).done(function(data) {
        var botHtml = '<p class="botText"><span>' + data + '</span></p>';
        document.getElementById('userInput').scrollIntoView({block: 'start', behavior: 'smooth'});
    $("#textInput").keypress(function(e) {
    //if enter key is pressed
        if(e.which == 13) {
    $("#buttonInput").click(function() {
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>


#textInput {
    border: none;
    border-bottom: 3px solid aqua;

.userText {
    color: white;
    font-family: monospace;
    font-size: 17px;
    text-align: right;
    line-height: 30px;
.userText span {
    background-color: #009688;
    padding: 10px;
    border-radius: 2px;
.botText {
    color: white;
    font-family: monospace;
    font-size: 17px;
    text-align: left;
    line-height: 30px;
.botText span {
    background-color: #EF5350;
    padding: 10px;
    border-radius: 2px;

And here is the core of the app.

from flask import Flask, render_template, request
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

app = Flask(__name__)
#create chatbot
englishBot = ChatBot("Chatterbot", storage_adapter="")
trainer = ChatterBotCorpusTrainer(englishBot)
trainer.train("chatterbot.corpus.english") #train the chatter bot for english

#define app routes
def index():
    return render_template("index.html")

#function for the bot response
def get_bot_response():
    userText = request.args.get('msg')
    return str(englishBot.get_response(userText))

if __name__ == "__main__":

The above piece of code creates an English language chatbot. It is trained for the language English. The chatterbot has knowledge about literature, money, politics, science, etc. Let’s see a demo.

Chatterbot Demo

Thank you for reading this article, comment below if you find any difficulty.

Here’re some more related Articles:

— Sending Sms Using Python And Twilio API

— Make A Command-line Based Image Re-sizer In Python 3 Using Pillow

— Developing Chat Application in Python with Source Code


Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, we will receive an affiliate commission. Regardless, we only recommend products or services we use personally and believe will add value to our readers.


  1. Anonymous

    I’ve tried this but it shows an internal server error. How should i resolve that?

    • Abhishek Gaur

      Did you got any answer how to resolve internal server error

  2. Nice article but can we write such bot for Laravel?