How to Create a Simple Python Chatbot on the Raspberry Pi

I have recently been working on a new project that required scraping data from other websites and have been slowly learning python. I usually work with PHP but I knew python will work faster and better. I also have always wanted to create a simple chatbot that I can input text into and it would reply with something meaningful. Creating a chatbot with a huge database of words and actually understanding what is being said is not an easy task and is something that is way over my head. I was browsing Reddit and found out that you can get results in JSON format is you append ‘.json‘ to the end of the URL. This got me thinking, what if I search Reddit with a question or comment and pick a random search result and reply with one of the top comments. This would fill my urge in creating a chatbot and it would also be a good starting point if I ever wanted to go deeper into creating a database and store answers for the chatbot to get a personality of its own.

I figured the Raspberry Pi would be an easy starting point since I have a bunch of them laying around and it is already setup with a Python environment. You can easily replicate this in any Python environment but I tested out my chatbot on the Raspberry Pi.

Objective

To create a chatbot in Python on the Raspberry Pi that searches Reddit and replies using a Reddit comment

Material

You will need the following:

Instructions

Create a new file on the Raspberry Pi and call it main.py

Copy the code into main.py and save the file. To run the file open a terminal window on the Raspberry Pi and navigate to the folder the file is located in. Run the following command:

You should get a prompt asking you to “Ask me anything: “. Ask the chatbot anything and see what reply you get.

Python Source Code

 

Input / Outputs

Below are some sample Input / Outputs that I got from my chatbot.

Hopefully this Reddit Chatbot inspires you to work on something and even expand on it. You can store answers in a database for future use or you can analyze them and train the chatbot. If you use my code for anything please let me know, I would love to hear on how you expanded it.

2 Comments

  • Frank

    October 1, 2018

    With some questions it give the automoderators serious tag notice as a reply. please fix

    Reply
    • dayz

      October 3, 2018

      You can add a filter in the code to ignore those tags

      Reply

Leave a Reply