✆+91-9916812177 | contact@beingdatum.com

Build a Chatbot using Python/Flask

Build a Chatbot using Python/Flask


Chatbot eases the pain that the industries are facing today. The purpose of chatbot is to support and scale business teams in their relations with customers. It could live in any major chat applications like Facebook Messenger, Slack, Telegram, Text Messages, etc.

This article shows how to create a simple chatbot in Python & Flask using the ChatterBot library. Our bot will be used for small talk, as well as to answer some math questions. Here, we’ll scratch the surface of what’s possible in building custom chatbots and NLP in general.

Let’s talk about Chatterbot, with the help of which we are planning to build our chatbot using Python/Flask.

ChatterBot is a Python library that makes it easy to generate automated responses to a user’s input. ChatterBot uses a selection of machine learning algorithms to produce different types of responses. This makes it easy for developers to create chat bots and automate conversations with users. 

An example of typical input would be something like this:

user: Good morning! How are you doing?
bot:  I am doing very well, thank you for asking.
user: You're welcome.
bot:  Do you like hats?

How ChatterBot Works

ChatterBot is a Python library designed to make it easy to create software that can engage in conversation.

An untrained instance of ChatterBot starts off with no knowledge of how to communicate. Each time a user enters a statement, the library saves the text that they entered and the text that the statement was in response to. As ChatterBot receives more input the number of responses that it can reply and the accuracy of each response in relation to the input statement increase.

The program selects the closest matching response by searching for the closest matching known statement that matches the input, it then chooses a response from the selection of known responses to that statement.

Process flow diagram

ChatterBot process flow diagram

Let’s build our chatbot 🙂

Installing dependencies

pip install chatterbot
pip install chatterbot_corpus

Importing Classes – Getting started!!

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
from chatterbot.trainers import ListTrainer

Creating the bot

We are creating a Flask app, to get started with Flask, you can visit here

app = Flask(__name__)
#bot = ChatBot("Pikachu")

We can create and train the bot by creating an instance of ListTrainer and supplying it with the lists of strings:

trainer = ListTrainer(bot)

Getting started with the training part, there are different ways how we can train the bot, by this,

trainer.train(['What is your name?', 'My name is Pikachu'])
trainer.train(['How are you?', 'I am good' ])
trainer.train(['Bye?', 'Bye, see you later' ])

or, we can also train by this,

conversation = [
    "How are you doing?",
    "I'm doing great.",
    "That is good to hear",
    "Thank you.",
    "You're welcome."


Training the Bot with corpus of data

You can use your own or an existing corpus of data to train a bot. For example, you can use some corpus provided by chatterbot (inbuilt features):

corpus_trainer = ChatterBotCorpusTrainer(bot)

The run() method of Flask class runs the application on the local development server.

def home():    
    return render_template("home.html") 
def get_bot_response():    
    userText = request.args.get('msg')    
    return str(bot.get_response(userText)) 
if __name__ == "__main__":    

Yay, our first model is ready, let’s test our bot.
The above given Python script is executed from Python shell.

Go to Anaconda Prompt, and run the below query.

python app.py

Below message in Python shell is seen, which indicates that our App is now hosted at or localhost:5000

* Running on (Press CTRL+C to quit)


  • Chatterbot
  • easetemplate.com for using the free templates as shown above

To download the entire code, please visit here

Need a guide to become a Data Scientist? Check this blog.

Special thanks to Nitish Keshari for designing the page.

Keep following our posts at beingdatum.com, cheers!!

2 responses on "Build a Chatbot using Python/Flask"

  1. Awesome compilation 👌

Leave a Message

Your email address will not be published. Required fields are marked *

© BeingDatum. All rights reserved.