How to Create a Chatbot with ChatBot Open Source and Deploy It on the Web

Chatbots have become very popular due to progress in AI, ML and NLP. They are now used on many websites. With increased popularity of chatbots there are many different frameworks to create chatbot. We will explore one of such framework in this post. We will review how to create a chatbot and deploy online based on open source for ChatterBot. Our platform will be Django on pythonanywhere.

This chatbot tutorial introduces the basic concepts and terms needed to understand and deploy chatbot ChatterBot and provides a simple usage example.

What is ChatterBot?

ChatterBot is a Python library that makes it easy to generate automated responses to a user’s input. To produce different types of responses ChatterBot uses a selection of machine learning algorithms such as search and /or classification algorithms. This makes it easy for developers to create chat bots and automate conversations with users. [1]

To install this chatbot open source you need just run:
pip install chatterbot

ChatterBot Features

The functionality of chatterBot is provided through adapters – a pluggable class that allows a ChatBot instance to execute some kind of functionality.

ChatBot has the following groups of adapters
input adapters
output adapters
storage adapters
logic adapters

Inside of each group there several adapters that support different functionalities. For example within logic adapters we can use Best Match Adapter, Time Logic Adapter and few other.

Here is the example how do you run chatbot. Below is the output of running the code:

Result of program run:
USER: How are you doing?
BOT:I am fine
USER: Hello
BOT:Hi there!
USER: Good morning!
BOT:How are today!
USER: Do you like machine learning?
BOT:Yes, I like machine learning
USER: How do I make a neural network?
BOTI am sorry, but I do not understand.
USER: Let us talk about current activities
BOT:What are working now?
USER: I am just browsing Internet for news
BOT:What a waste of time! Dont you have any other things to do?
USER: I am working on python script to make new chatbot
BOT:This is great. Keep working on this

During the testing I confirmed that it correctly responds to new (but similar) inputs even if we did not train exactly.

Once you trained the bot, the result of training is staying there even after turning off/on PC. So if you run program multiple times, you need run training just first time. You still can run training later, for example if you want retrain or update python chatbot code.


This section describes how to deploy ChatterBot on pythonanywhere web hosting site with Django programming web framework.

PythonAnywhere is an online integrated development environment (IDE) and web hosting service based on the Python programming language.[3] It has free account that allows to deploy our Chatbot. Django is needed to connect web front interface with our python code for ChatterBot in the backend. Other web frameworks like Flask can be used on pythonanywhere (instead of Django).

Below is the diagram that is showing setup that will be described below.

Chatbot online diagram
Chatbot online diagram

Here is how ChatterBot can be deployed on pythonanywhere with Django:

  • Go to pythonanywhere and select plan signup
  • Select Create new web framework
  • Select Django
  • Select python version (3.6 was used in this example)
  • Select project name directory
  • It will create the following:

  • create folder cbot under user folder
  • /home/user/cbot

  • inside of this folder create
  • (this is just empty file)

    Inside chatbotpy wrap everything into function runbot like below. In this function we are initiating chatbot object, taking user input, if needed we train chatbot and then asking for chatbot response. The response provided by chatbot is the output of this function. The input of this function is the user input that we are getting through web.

    def runbot(inp, train_bot=False):
     from chatterbot import ChatBot
     chatbot = ChatBot("mychatbot",
                "import_path": "chatterbot.logic.BestMatch",
                "statement_comparison_function": "chatterbot.comparisons.levenshtein_distance",
                "response_selection_method": "chatterbot.response_selection.get_first_response"
                'import_path': 'chatterbot.logic.LowConfidenceAdapter',
                'threshold': 0.65,
                'default_response': 'I am sorry, but I do not understand.'
     if (train_bot == True):
        print "Training"
      Insert here training code from the python code for ChatterBot example 
     response = chatbot.get_response(inp)
     return (response)

    Now update like below code box. Here we are taking user input from web, feeding this input to runbot function and sending output of runbot function (which is chatbot reponse) to web template.

    from django.shortcuts import render
    from cbot.chatbotpy import runbot
    def press_my_buttons(request):
        if request.POST:
            conv=request.POST.get('conv', '')
            user_input=request.POST.get('user_input', '')
            conv=conv + "" + str(user_input) + "\n" + "BOT:"+ str(resp) + "\n"
            conv =  "BOT:"+ str(resp) + "\n";
        return render(request, 'my_template.html', {'conv': conv})

    Now update my_template.html like below. Here we just show new response together with previous conversation information.

    <form method="post">
        {% csrf_token %}
        <textarea rows=20 cols=60>{{conv}}</textarea>
        <input type="textbox" name="user_input" value=""/>
        <button type="submit">Submit</button>
        <input type="hidden" name =conv  value="{{conv}}" />

    Now update some configuration.
    Update or confirm to include the line with settings.

    if __name__ == '__main__':
        os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'cbotdjango.settings')

    Update or confirm to have path like below

    import sys
    path = "/home/user/cbotdjango"
    if path not in sys.path:

    Now you are ready do testing chatbot online and should see the screen similar in the setup diagram on right side.


    We saw how to train ChatterBot and some functionality of it. We investigated how to install ChatterBot on pythonanywhere with Django web framework. Hope this will make easy to deploy chatbot in case you are going this route. If you have any tips or anything else to add, please leave a comment below.

    1. ChatterBot
    2. Python chatbot code example
    3. Python Anywhere

    1 thought on “How to Create a Chatbot with ChatBot Open Source and Deploy It on the Web”

    Leave a Comment