DEV Community

Cover image for How to code your own chatbot in Python
Alex Agboola
Alex Agboola

Posted on • Updated on

How to code your own chatbot in Python

AI or Artificial Intelligence can be so advanced that it can be in self-driving vehicles capable of navigating through busy streets, personal virtual assistants who answer our questions and have conversations with us, and ways to systems capable of recognizing diseases in medical images. AI can be really complicated, even in the most simplest things like chatbots.

But there is a way to create your own chatbot without all of the fancy APIs or know a lot about AI. All you need is an email, and some basic knowledge in Python.

How To Create Your Own Chatbot

Creating your own chatbot is a lot simpler than you might imagine. It doesn't take thousands of lines to code; possibly less than 200 lines.

Nanohub makes it easy to create your own chatbot. A student created a template for you and all you have to do it tweak it slightly to personalizes your chatbot.

Don't worry if you don't know a lot of Python or even at all, the student made it easy to know what you are doing, and I will even help you too.

The Setup

The setup is most likely the hardest part of the whole process, so make sure to follow me step by step.

1) Make an account

Go to and create your account.

The login/signup screen

Your screen should look like this

Once you quickly make you account, you should be on the dashboard:

Nanohub's Dashboard

Your dashboard should look similar to this

2) Start a new session

Go to "My Tools" section on the most right-hand side of your dashboard.

An arrow pointing to the "My Tools" section

Then click on the subsection "All Tools".

Now search and favorite (by clicking on the heart) this tool:

Jupyter Notebook (202105)

My cursor is about to favorite the tool Jupyter Notebook (202105)

Click on Jupyter Notebook (202105) and it should take you to this screen:

A GIF of me clicking on Jupyter Notebook (202105)

When you click on the tool, it should take you to that screen

Now, click on Launch Tool (It should take a while)

3) Upload and start your tool

After awhile, you should be on this page:

Jupyter Notebook (202105) Page

You won't see any files if this is your first time on Nanohub. I have used it before, that's why you can see these files.

Now, click this link to download your chatbot template: Download File.

Once your file has finished downloading, click on the "Upload" button.

An arrow pointing to the "Upload" button

Once you have uploaded, you should see a new file on your screen:

A red-outlined box of the newest file I have just uploaded

Click on the file you have just uploaded and then you are officially done with the setup.

NOTE: Make sure to delete the error in the code:

 a GIF of me deleting the error inside the code

The Personalization Part

The best part! You could spend hours or even days on this part, so below, I will give you some suggestions and how to code them.

1) Complete the TODOs

If you just want to do some simple tweaks, there are a bunch of "TODO" comments for you to complete. You don't have to do it all, but they help you know what to do and personalize.


A GIF of me changing h=the name of the chatbot


name = "The Best Bot"
Enter fullscreen mode Exit fullscreen mode

2) Customize the responses

If you look deeper into the code, you will see some responses.

For example, this is one response to when you say "how are you?":

"how are you?": [ 
"I am feeling {0}".format(mood),
"I don’t ever wanna feel like I did that day, take me to the place I love, take me all the way",
"Eh, with the birds I'll share this lonley view, scar tissue that I wish you saw."
"{0}! How about you?".format(mood), ],
Enter fullscreen mode Exit fullscreen mode

You can edit responses or even add some more responses that matches your chatbot.


A quick GIF on how to add a response


"what is 2 + 2?": [
    "2 + 2 = 4",
    "I don't know, {0}!".format(user_name),
    "Easy, 2 + 2 = 5!"
Enter fullscreen mode Exit fullscreen mode


def user_intent(user_input_text):
    #if user_input_text is empty, no need to check for user intent.
    if len(user_input_text) == 0:
        return ""

    #this next command changes creates a list of lowercase words from input
    x_text = user_input_text.lower().split()
    #y_text is the intent keyword written exactly as in responses.
    y_text = ""

    #TODO: use this construct if you have many keywords for one intent.
    #the values in square brackets are your possible keywords
    if any(True for x in x_text if x in ["hello","hi", "hey", "greetings"]):
        y_text = "Hello"

    elif any(True for x in x_text if x in ["bye","goodbye", "exit", "done"]): 
        y_text = "Bye"

    #TODO: use this construct if you have only one keyword for one intent.
    elif "from" in x_text: 
        y_text = "Where are you from?"

    elif "born" in x_text: 
        y_text = "Where were you born?"

    elif "name" in x_text: 
        y_text = "what's your name?"

    elif "can you do" in x_text: 
        y_text = "What can you do?"

    elif "weather" in x_text: 
        y_text = "what's today's weather?"

    elif "robot" in x_text: 
        y_text = "Are you a robot?"

    elif "how are" in x_text: 
        y_text = "how are you?"

    elif "feeling" in x_text: 
        y_text = "how are you?"

    elif "why" in x_text: 
        y_text = "Why?"

    elif "good" in x_text: 
        y_text = "Good"

    elif "okay" in x_text: 
        y_text = "Okay"
     #The following code tells the chatbot "if the user says anything about math, do this
    elif "math" in x_text: 
        y_text = "what is 2 + 2?"
    #TODO: Add or remove as many elif as you need, leave this else intact.
        y_text = ""

    return y_text

Enter fullscreen mode Exit fullscreen mode

3) CHALLENGE: Add an API or a library in your code for a smarter chatbot

This is a little harder than the other two suggestions because I want to challenge my more intermediate audience.

The code already has a library imported:

#calling import random will allow Python to use commands in the random library
import random

Enter fullscreen mode Exit fullscreen mode


The code in this cell will get a random message given the intent_keyword
#to send to the user associated with the keywords in the response
#if it cannot find the intent, it will return the default message

def respond(intent_keyword):

    if intent_keyword in responses: 
        bot_message = random.choice(responses[intent_keyword])

        bot_message = random.choice(responses["default"])

    return bot_message
Enter fullscreen mode Exit fullscreen mode
This code randomizes the responses. That's why you get a different response even if you ask the same question.

You can add another library or API into the code.


Maybe try a weather library that gets the actual weather in your city.

Tell me in the comments what API/library you tried.


To sum things up, it is pretty easy to create your own chatbot using Nanohub, and you don't even need to know a lot of Python!

Top comments (2)

aayyusshh_69 profile image
Aayush Pokharel

I suggest you add code snippets

alex_the_dev profile image
Alex Agboola

Ok, thanks for the suggestion :)