Tutorial: Building a Birthday Reminder Script in Python

Tutorial: Building a Birthday Reminder Script in Python

Have you ever forgotten a friend's birthday and then had to go through an awkward conversation later on? Well fear not, I got you! With Python, you can create a simple yet effective script to ensure you are the first person to wish the next year or at any rate get a reminder to wish them before the day ends. In this tutorial, we'll walk through creating a birthday reminder script from scratch which will notify you whenever a birthday is due.


Prerequisites

  • A windows 10/11 system (Sorry Linux folks)

  • Python installed in your system

  • Basic knowledge of Python programming.

  • winotify library installed (for Windows notifications). Install it by running:


Step 1: Plan of action

Let’s get brainstorming! Start by mapping out a flow chart to outline what the program will do, gather the necessary tools, and dive into coding.

For example, the app will:

  1. Store birthday data: Use a dictionary to mimic a birthday database. Simple!

  2. Check today’s date: Use Python’s datetime module and conditional statements to find matches.

  3. Send a notification: Use the winotify module to notify you on Windows when there’s a birthday match.

That’s it! Now, let’s code! 🎉


Step 2: Define the Birthday Data

We'll use a Python dictionary to store birthdays. Each key will be a date in MM-DD format, and the value will be the person's name. Here's an example:

birthdays = {
    '02-07': 'Alex Johnson',
    '02-18': 'Taylor Smith',
    '03-01': 'Jordan Brown',
    '04-01': 'Morgan Williams',
    '04-17': 'Casey Davis',
}

Step 3: Write the Function to Check Today's Birthday

We'll use Python's datetime module to get today's date and match it against the dictionary keys. Here's the function:

def checkBirthday():
    import datetime

    today = datetime.date.today()
    today_str = today.strftime('%m-%d')  # Extract month and day

    if today_str in birthdays.keys():
        getName = birthdays[today_str]
        sendNotificationInWindows(getName)
    else:
        print("No birthday today!") # Added for clarification. Can be removed

Curious about sendNotificationInWindows(getName)? Hold tight, we’ll get there soon!


Step 4: Create a Function to Send Notifications

Our final component! We'll use the winotify library to send Windows notifications. Create a function named sendNotificationInWindows that takes a name and displays a notification:

def sendNotificationInWindows(name):
    from winotify import Notification, audio

    notification = Notification(app_id="Birthday Reminder",
                                title=f"Today is {name}'s Birthday!",
                                msg="Wish him/her a very happy birthday!",
                                duration="long",
                                icon=r"C:\PycharmProjects\happy-birthday.png")  # Update the path to your icon file

    notification.set_audio(audio.LoopingCall2, loop=True)
    notification.show()

Breakdown of the code:

This function uses winotify to create and display the notification with a custom title, message, and icon. The icon image can be any image file but you must include the path in icon .

  1. Notification(app_id, title, msg, duration, icon):

    • app_id: A unique identifier for the notification (e.g., "Birthday Reminder").

    • title: The title of the notification, dynamically including the person's name.

    • msg: The message body, which can include wishes or any other text.

    • duration: Defines how long the notification stays on the screen (e.g., "long" or "short").

    • icon: Path to the icon file displayed with the notification. You can remove this if you prefer.

  2. set_audio(audio.LoopingCall2, loop=True):

  3. show():

    • Displays the notification.

Step 5: Run the Script

Finally, set up the script to execute the checkBirthday function when the script runs:

if __name__ == "__main__":
    checkBirthday()

Run the script from your code editor or from the command prompt using: python birthday_reminder.py. Replace birthday_reminder.py with the file name you have given to your script.

If today's date matches a birthday, a notification will pop up. Otherwise, you'll see "No birthday today!" in the console.


What if two people share the same birthday?

Ask yourself: Will this script work if two or more people have the same birthday? Currently, it assumes only one person can have a birthday on a given day. But let’s be honest, who wants to keep it simple when you can have a birthday bash with a whole group? 🎉

To handle multiple birthdays on the same date, we’ll need to spice up both the dictionary and the checkBirthday function.

Step 7: Update the Dictionary to Handle Multiple Birthdays

If multiple people have the same birth date, put their names in a list.

birthdays = {
    '02-07': 'Alex Johnson',
    '02-18': 'Taylor Smith',
    '03-01': 'Jordan Brown',
    '04-01': ['Morgan Williams', 'Casey Davis'],
    '04-17': 'Bailey Miller',
}

Step 8: Update the checkBirthday function

We need to check if the getName variable is a list or string.

def checkBirthday():
    import datetime

    today = datetime.date.today()
    today_str = today.strftime('%m-%d')  # Extract month and day

    if today_str in birthdays.keys():
        getName = birthdays[today_str]
        # check if multiple people has birthdays today
        if isinstance(getName, list):
            for i in range(1, len(getName)):
                getName[0] += f"'s and {getName[i]}"
            sendNotificationInWindows(getName[0])
        else:
            sendNotificationInWindows(getName)
    else:
        print("No birthday today!") # Added for clarification. Can be removed

The for loop concatenates the names into a single string and then the sendNotificationInWindows is called.

Test the functionality of this modification by running the script again. I have attached one screenshot of the notification popping up.

Notification Pop Up


Final Notes

  • You can further enhance this program to allow you to add, edit or remove birthdays by taking input.

  • You can also perhaps automatically send birthday messages by integrating email or SMS programs.

  • Schedule this script in your Windows device to run automatically using the task schedular. (I’ll post a tutorial for this soon!)

Feel free to share your improvements and feedback!