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. 🎂
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!
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:
It’s quick to set up and makes the app instantly more practical.
Here’s what we’ll tackle today:
Before starting, make sure you have:
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.
.env
FileYour .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>
Pro Tip: Make sure .env
is added to your .gitignore
file to prevent sensitive credentials from being exposed in version control.
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()
Run the script to test everything:
python check_birthdays.py
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.
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.
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!