Building a Chatbot in Python (using chatterbot) and deploying it on web.

Original article was published by Aman kumar on Artificial Intelligence on Medium

Building a Chatbot in Python (using chatterbot) and deploying it on web.

What is Chatbot?

A chatbot is an artificial intelligence (AI) software that can simulate a conversation (or a chat) with a user in natural language through messaging applications, websites, mobile apps or through the telephone.

Today almost every company has a chatbot deployed to engage with the users. Some of the ways in which companies are using chatbots are:

  • To deliver Crime information
  • to connect customers and their finances
  • As customer support

How Bots Help Us?

  • Analysis data and reports it periodically
  • Performs repetitive tasks such as processing a particular type of transaction
  • Imports/exports data between systems
  • Processes swivel chair data entry
  • Enters data to forms, creates, edits and retrieves data from databases
  • Executes email campaigns, follow up, tracking and archiving

What are Self learning chatbots?

A cognitive bot learns by observing people at work. It is achieved by constantly and repeatedly analysing the processes, corrections and transactions of the employer by the bot. The bot thus gains the knowledge to process the incoming data and think and perform the suitable action, getting smarter and more accurate over time. It automatically extracts the data needed for decision making and continuously learns from employer’s feedback. It uses NLP, ML, knowledge representation, reasoning, massive parallel computation and Rapid Domain Adaptation.

NLP(Natural Language Processing) :

The field of study that focuses on the interactions between human language and computers is called Natural Language Processing, or NLP for short. It sits at the intersection of computer science, artificial intelligence, and computational linguistics .NLP is a way for computers to analyze, understand, and derive meaning from human language in a smart and useful way. By utilizing NLP, developers can organize and structure knowledge to perform tasks such as automatic summarization, translation, named entity recognition, relationship extraction, sentiment analysis, speech recognition, and topic segmentation.

Building chatbot using chatterbot

Chatterbot :

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. For more details about the ideas and concepts behind ChatterBot see the process flow diagram.

Installing Chatbot Required Libraries

All the libraries are installed in Virtualenv .

“Check how to make a virtual environment” and activate it

pip install Flaskpip install chatterbotpip install chatterbot-corpus

Creating chatbot in python :

After the installation of all the libraries, now create a python file and paste the below code :

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

# Creating ChatBot Instance
chatbot = ChatBot(
'import_path': 'chatterbot.logic.BestMatch',
'default_response': 'I am sorry, but I do not understand. I am still learning.',
'maximum_similarity_threshold': 0.90

# Training with Personal Ques & Ans
training_data_simple = open('training_data/normal.txt').read().splitlines()
training_data_personal = open('training_data/all.txt').read().splitlines()

training_data = training_data_simple + training_data_personal

trainer = ListTrainer(chatbot)

# Training with English Corpus Data
trainer_corpus = ChatterBotCorpusTrainer(chatbot)

In the above code, we are creating a chatterbot instance. Then we are training our chatbot with ListTrainer with our data.

We are loading data set from ‘training_data/normal.txt’ and ‘training_data/all.txt’ you can edit these files and can add more as much of your choice

can get simple coversation data from here.

What is chatterbot corpus?

The ChatterBot Corpus is a project containing user-contributed dialog data that can be used to train chat bots to communicate.learn more about chatterbot corpus.

In the above code, we are creating a chatterbot instance. Then we are training our chatbot with ListTrainer with our personal question and answers.

Creating web UI for chatbot using flask :

Above you have installed Flask ,Now create an file and paste below code:

from chatterbot import ChatBot
from chatbot import chatbot
from flask import Flask, render_template, request

app = Flask(__name__)
app.static_folder = 'static'

def home():
return render_template("index.html")

def get_bot_response():
userText = request.args.get('msg')
return str(chatbot.get_response(userText))

if __name__ == "__main__":

In the above code, we are creating routes for our web application. In get_bot_response() we are taking input from html form and after processing chatbot giving response.

HTML template for chatbot :

After creating a folder templates,inside it add the index.html

create a static folder inside it create styles folder and add styles.css inside it.

Run the Chatbot :

After done with the code,Run the flask app “” by running the following statement below in terminal


your chatbot will start running here.

It will look like this:

You can use your own UI and make it more presentable.

If there is some error try to solve it by searching the problems on internet you will be able to find the errors in github and stackoverflow.

Deploying on Herokuapp :

Steps to deploy Your project on herokuapp :

Making a prockle file :

make a procfile and following code in it :

web: gunicorn app:app

Making a requirements.txt file :

make a requirements.txt file and add all the techstack with their version in it

include the following code in it :


And now Deploy it on Herokuapp

your Project will get deployed if it shows some error like this:

Then try to find out your error :

  1. Open command prompt and write heroku login.
  2. login page will open in your browser tap login there
  3. then type “heroku logs — app ‘your project name’ and then press enter”
  4. then your errors will be there on the terminal search it as you can easily find it out on web(github or stack overflow) . mainly the error can be of missing requirements so please check that first and then again tryto deploy

After these steps also you find error just try the procedure given below:


I hope you will find this helpful, after reading you will try to create a chatbot for your own,internet has a lot of resources where you can gain knowledge about chatbots.

You can see my project on Github.

Thank You and Happy Learning 🙂