Tutorial

12 Days of DigitalOcean (Day 3) - Checking Birthdays and Sending SMS Notifications

Published on December 19, 2024
12 Days of DigitalOcean (Day 3) - Checking Birthdays and Sending SMS Notifications

🎄 12 Days of DigitalOcean: Checking Birthdays and Sending SMS Notifications 🎁

Welcome to Day 3 of 12 Days of DigitalOcean! Over the past two days, we’ve set up a PostgreSQL database and connected to it using Python. Now, it’s time to make our Birthday Reminder Service actually do something useful—send you a text when there’s a birthday today. 🎂

birthday_reminder_sms

We’ll use Twilio, a service that makes it easy to send SMS messages with just a few lines of code. By the end of today, your app will check the database for birthdays and send you a reminder if there’s a match.

Let’s get started!


✨ Why This Step?

Finding birthdays in your database is only part of the job. To make this app truly useful, we need to notify someone (you!) about these special dates.

This step connects the dots:

  • Use SQL queries to find birthdays that match today’s date.
  • Send a friendly SMS reminder using Twilio.

It’s quick to set up and makes the app instantly more practical.


🚀 What You’ll Learn

Here’s what we’ll tackle today:

  1. Use SQL queries to find birthdays matching today’s date.
  2. Send SMS notifications using Twilio’s Python SDK.
  3. Combine these steps into a single, functional Python script.

🛠 What You’ll Need

Before starting, make sure you have:

  • A Twilio account (if you don’t have one yet, follow this Quickstart Guide to sign up, buy a Twilio phone number, and get your credentials.
  • Your Twilio credentials:
    • Account SID
    • Auth Token
    • Twilio phone number
  • The database and Python connection script from Day 2.
  • Sample data in your contacts table (we added this in Day 1 - Setting Up a PostgreSQL Database for Birthday Reminders). If you need to add more, follow the steps in Day 1 to populate your database.

🧑‍🍳 Recipe for Day 3: Checking Birthdays and Sending Notifications

Step 1: Install Twilio’s Python SDK

To send SMS notifications, we’ll need the Twilio Python library. Install it by running:

pip install twilio

If you don’t already have your Twilio credentials (Account SID, Auth Token, and a phone number), follow Twilio’s Messaging Quickstart. It walks you through signing up, purchasing a phone number, and grabbing the necessary details.

twilio_api_dashboard


Step 2: Update Your .env File

Your .env file should now include both your database credentials (from Day 2) and your Twilio credentials. You can find the Twilio credentials—Account SID, Auth Token, and your Twilio phone number—by logging into your Twilio account dashboard.

Update your .env file to look like this:

# Database credentials
DB_HOST=<your-database-hostname>
DB_NAME=<your-database-name>
DB_USER=<your-database-username>
DB_PASSWORD=<your-database-password>
DB_PORT=5432  # Default PostgreSQL port

# Twilio credentials
TWILIO_ACCOUNT_SID=<your-twilio-account-sid>
TWILIO_AUTH_TOKEN=<your-twilio-auth-token>
TWILIO_PHONE_FROM=<your-twilio-phone-number>
TWILIO_PHONE_TO=<your-personal-phone-number>
  • Replace the placeholders with your actual credentials.
  • Add your personal phone number as TWILIO_PHONE_TO to receive test notifications.

Pro Tip: Make sure .env is added to your .gitignore file to prevent sensitive credentials from being exposed in version control.


Step 3: Write the Python Script

Here’s the full Python script that queries the database for today’s birthdays and sends SMS notifications using Twilio:

# check_birthdays.py

from datetime import datetime
import pg8000
from dotenv import load_dotenv
from twilio.rest import Client
import os

# Load environment variables
load_dotenv()

def connect_to_database():
    """Establish connection to the database."""
    return pg8000.connect(
        host=os.getenv("DB_HOST"),
        database=os.getenv("DB_NAME"),
        user=os.getenv("DB_USER"),
        password=os.getenv("DB_PASSWORD"),
        port=int(os.getenv("DB_PORT"))
    )

def send_birthday_message(first_name, last_name):
    """Send a birthday text message using Twilio."""
    try:
        # Twilio setup
        account_sid = os.getenv("TWILIO_ACCOUNT_SID")
        auth_token = os.getenv("TWILIO_AUTH_TOKEN")
        client = Client(account_sid, auth_token)

        # Compose the message
        message = client.messages.create(
            body=f"🎉 It's {first_name} {last_name or ''}'s birthday today! 🎂",
            from_=os.getenv("TWILIO_PHONE_FROM"),
            to=os.getenv("TWILIO_PHONE_TO")
        )

        print(
            f"Message sent to {os.getenv('TWILIO_PHONE_TO')} for {first_name} {last_name or ''}. Message SID: {message.sid}"
        )
    except Exception as e:
        print(f"An error occurred while sending the message: {e}")

def check_birthdays():
    """Check if any contact's birthday matches today's date and send a notification."""
    try:
        conn = connect_to_database()
        cursor = conn.cursor()

        # Get today's month and day
        today = datetime.now()
        today_month = today.month
        today_day = today.day

        # Query to fetch contacts whose birthday matches today's date
        cursor.execute(
            """
            SELECT first_name, last_name, birthday
            FROM contacts
            WHERE EXTRACT(MONTH FROM birthday) = %s
              AND EXTRACT(DAY FROM birthday) = %s;
            """,
            (today_month, today_day)
        )

        rows = cursor.fetchall()

        # Notify for each matching contact
        if rows:
            print("Birthday Notifications:")
            for row in rows:
                first_name, last_name, _ = row
                send_birthday_message(first_name, last_name)
        else:
            print("No birthdays today.")

        # Close the cursor and connection
        cursor.close()
        conn.close()

    except Exception as e:
        print(f"An error occurred while checking birthdays: {e}")

if __name__ == "__main__":
    check_birthdays()

Step 5: Test Your Script

Run the script to test everything:

python check_birthdays.py

vscode_birthday_reminder_script_sms_notification_sent

birthday_reminder_sms

If there’s a birthday in your database matching today’s date, you’ll receive a text message. 🎉 If not, the script will simply print:

No birthdays today.

vscode_birthday_reminder_script_no_birthdays

🎁 Wrap-Up

Here’s what we accomplished today:

✅ Queried the database for birthdays matching today’s date.

✅ Used Twilio to send SMS notifications for those birthdays.

✅ Combined everything into a functional Python script.

Up next: Tomorrow, we’ll deploy this script to DigitalOcean Functions to make it run in the cloud—no server management needed. This is where the Birthday Reminder Service starts to run automatically. Stay tuned! 🚀

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.