How To Code Your Own Chatbot In Python

You don’t even need to know how to code!

Alex Agboola
5 min readFeb 17, 2024

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 nanohub.org and create your account.

Your screen should look like this

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

2) Start a new session

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

Then click on the subsection “All Tools”.

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

Jupyter Notebook (202105)

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

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:

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.

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

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:

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.

Example

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),
],

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

Example

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 ... 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])
else:
bot_message = random.choice(responses["default"]) return bot_message

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.

Example

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

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

Conclusion

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!

Originally published at https://dev.to on February 17, 2024.

--

--

Alex Agboola

I am a young full-stack developer with over four years of experience. I have a great interest in AI. I also like to write about what I think is right.