🎄 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. 🎂
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:
- Use SQL queries to find birthdays matching today’s date.
- Send SMS notifications using Twilio’s Python SDK.
- 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:
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.
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:
Step 5: Test Your Script
Run the script to test everything:
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:
🎁 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! 🚀
Source:
https://www.digitalocean.com/community/tutorials/checking-birthdays-and-sending-sms-notifications