On-the-Dot Bot

numb3r on Flickr

Never be late again with the help of this neat coding project!

Sometimes you need someone, or something, to remind you of a task or an event, like doing the laundry or going to a meeting. The ScheduleBot was created for this very purpose by saying the reminder to you at the time you want. After answering some questions to the bot about what your event is, when it takes place, and when you should be reminded, the bot will save your request and remind you.

ScheduleBot can run on any computer. However, I used a Raspberry Pi. That way, I can find a room that I spend most of my time in and insert the Raspberry Pi there. If you were to run the code on a laptop, you would have to power it off eventually, but the ScheduleBot program can (and must) run continuously. Running the code on a PC would mean that you would have to stay in the room with the PC to hear your reminders, but that might not be the room where you would like to hear your reminders. However, you can pick the location for the Raspberry Pi that would be most convenient for you.

Materials

  • Raspberry Pi
  • 2 DC Adapters
  • Keyboard
  • Mouse
  • Speakers
  • Screen
  • Python programming language

Setup

Set up the Raspberry Pi. The Raspberry Pi [3] is a small computer that is powered by one single circuit board. The Raspberry Pi may be much smaller than the average computer, but it can still do what a regular computer can. However, the Raspberry Pi may take longer to do what a computer can do.

    1. Figures 1-4 explain which plugs are which, why they are needed for the Raspberry Pi, and how to insert them. Follow the pictures and plug them accordingly.
    2. Turn on the Raspberry Pi. It will turn on automatically when the two power plugs are in, or hit the power button on the Raspberry Pi.
    3. Connect to the internet for initial software installation.
Figure 1: The black DC adapter to the screen. The white DC adapter connects to the Pi, circled in Figure 3.
Figure 2: Photo showing connections for the speakers, mouse, and screen to the Raspberry Pi. The blue circle indicates the power for the Raspberry Pi.
Figure 3:
The black plug on the left is the DC adapter used to power the screen. The white HDMI plug is used to connect the Raspberry Pi to the screen.
Figure 4:
Here is a close-up of what the Raspberry Pi’s plugs should look like. The pink/red is to connect the mouse, the black is for the speakers, the left white is the cable for power, and the right white cable connects to the screen.

Set up the Python packages. Open a terminal window and install the following Python libraries (Python 2 and 3 will already be installed on your Raspberry Pi but you need to use Python 3):

  • keyboard [1]: enables you to access keypresses from the keyboard while code is executing.
  • pyttsx3 (Python Text-to-Speech X-Platform): a library that converts text given to it into speech in order for you to program your ScheduleBot to speak your event reminders.

Write the code. Open a text editor on Raspberry Pi to begin writing code. Name code in a way you can remember (ex: scheduleBot.py) but make sure it ends in .py!

Figure 5

1. Import the datetime, keyboard, pyttsx3, and time libraries. In addition to the previously installed libraries, datetime (which is already installed into Python by default) is used to access the current date and time during execution. We have three times of interest associated with our program flow. Current time describes the precise current time, the event time is when the event takes place, and the reminder time is when to execute the reminder. When you see your reminder, it will show the event time in military time. In the program, for comparisons, the times will be compared by the event hour (before the colon) and the event minute (after the colon). (See Figure 5)

Figure 6

2. Write a function called convert_date_string_format with the parameter date_with_dashes. This function will take the string date_with_dashes and will change it from YYYY-MM-DD to MM/DD/YYYY. The function should return the converted date. (See Figure 6)

Figure 7

3. Write the function time1_less_than_eq_time2 with the purpose of checking if one time is before another so the program can compare times. For example, if one time is before or after another, the computer can know if an event is in the past or if the reminder time is after the event time. (See Figure 7)

Figure 8

4. Write a function sort_events to sort the events in chronological order from the event that will occur soonest to the event that will occur latest. The first two events in the list are checked to see if they are in the correct chronological order. If they aren’t, the code swaps them and checks the next two elements. If they are, the code doesn’t switch them and keeps checking. We repeat this procedure n times, where n is the length of the list, to ensure that all elements are sorted to their true position. This is called BubbleSort. (See Figure 8)

Figure 9

5. Make a Class [4] called Event. A Class is a data type that describes the data it has and what a programmer can do with it. (See Figure 9). Make sure this Class has:

    1. event_name (the name of the event)
    2. event_time (the time of the event)
    3. event_date (the date of the event)
    4. event_reminder_date (the date of the reminder for the event)
    5. event_reminder_time (the time of the reminder for the event)
Figure 10
Figure 11: The program may not execute its time check at the precise reminder time, so the reminder should be executed when the current time is in the yellow region and not after the event time.

6. Write the function checkForReminders. This function will check for reminders to make sure if there are any reminders needed and when to execute them. You should use the function time1_less_eq_time2 because it will let you compare the current time and the reminder time so the computer can know whether or not to give out a reminder. (See Figures 10 and 11)

Figure 12

7. In the main function (if __name__ == ‘__main__’), there’s a few things you can do. First, you can make an “if” statement that will make a new event when you press the letter “n” on your keyboard. Second, you can make an “if” statement in your code that will print upcoming events in the ScheduleBot (in chronological order) when you press the letter “p”. Use the function sort_events to put the events in chronological order, and make a for loop to print each event in the list_of_events, one event at a time. (See Figure 12)

Add functions to validate the data. Some functions are to make sure that the information is given to it in the way expected and meets all requirements. These functions are important because they help make the program run correctly, give you accurate reminders, and store your event data correctly.

Figure 13

1. Write a function called eventIsInThePast. The function should return True if the event is in the past, and False if not. The event given by the user must be in the future, so if the user gives the program an event that is in the past, the program needs to know so it can let the user know what they did wrong. If it didn’t, there would be no way for the program to know, and it would create an invalid event. (See Figure 13)

Figure 14

2. Write a function called validMilitaryTime. This function is going to make sure that your event’s time is valid. Make sure the function returns True if the military time is valid, and False if not. (See Figure 14)

Figure 15

3. The next function you need to code is to make sure the date is valid. Name the function validDate. The function should return True if the date is valid, and False otherwise. (See Figure 15)

Figure 16

4. This function is going to check if the event is valid. Name the function checkValidEvent. The criteria I use for the events are:

  1. Name is less than or equal to 300 characters
  2. Is in the future
  3. Event is less than two years into the future
  4. The event’s military time is valid
  5. The reminder’s date is no more than a month before the event
  6. The reminder’s time is valid military time

If all criteria apply to the event, the function should return True, and if the event does not follow the criteria, return False and have your function print what is wrong with the event. (See Figure 16)

Run the code. Open the terminal on the Raspberry Pi and run your program using the command “sudo python scheduleBot.py”. Sudo [2] is used to let someone use code as the super user. This is the super user because this user can perform actions that other users can’t. This will let you access scheduleBot.py (because you will be accessing the code as another user). Sudo also allows you to use files and make actions that wouldn’t be allowed if as another user. This is important so that the program can use the computer speakers to tell us reminders, and the keyboard to type in events and use the n key to make a new event. If you wish to test the code, we have written tests which you can check out on GitHub, linked at the bottom of the article.

Customize your ScheduleBot code. Some parts of the code here are just customizations for this version of the ScheduleBot. For example, I wanted to have a limit for the reminder time for it to be no more than a month before an event, so I added that customization. Customizations are not required. You can make your own customizations if you wish.

Figure 17

1. Write a function called reminder_date_within_a_month_of_event. This will make sure the reminder date is within a month before the event it’s reminding you of. This is just one of the custom restrictions I put in my code. It is completely optional, and you can skip this step if you would like. (See Figure 17)

Figure 18

2. Write a function titled two_years_future. This function is going to check to see if the event is farther than two years into the future, for the events can’t go further than that time. The function should return “True” if the function’s event is within two years of the future, and “False” otherwise. This is another customization in my code and is not required. You can skip this step if you want to. (See Figure 18)

The ScheduleBot will help you remember what you need to do. There were definitely challenges to making the bot, from converting it across the computers to simple debugging errors. During the making of this robot, the popular Python code has done many spectacular things that you may not expect, such as being able to say words and being able to put your own input in, without writing code. Unlike other calendars, the ScheduleBot actually will say the reminder you requested for. The bot shows the amazing things that Python can do, and that you can make so many amazing things with this coding language. Code for ScheduleBot

Learnmore

Hook and simulate keyboard events on Windows and Linux

https://github.com/boppreh/keyboard

Linux 101: Introduction to sudo

https://www.linux.com/training-tutorials/linux-101-introduction-sudo/

Teach, Learn, and Make with Raspberry Pi

https://www.raspberrypi.org/

Codecademy Introduction to Classes

https://www.codecademy.com/courses/learn-python-3/lessons/data-types/exercises/class

Text-to-speech x-platform

https://pypi.org/project/pyttsx3/

What Happened to Schedule Bot?

https://www.youtube.com/watch?v=3gzMCoku6ek

Also In The August 2022 Issue

Looking for free computing resources for your classroom? Check out Computing at School!

From bulletproof vests to stronger tires, Kevlar is a really versatile material!

Time to learn how humans have kept track of time over the centuries

Check out how programmers are competing to make an even simpler Wordle!

Your dreams of having a robotic friend may be closer than you think!

Leave your mark on the world wide web with your own website!

Discover the true origins of the great poop emoji!

Collection of the Learnmore links from all of the August 2022 stories, in one place.

Interesting stories about computer science, software programming, and technology from August 2022.