12 Days Of Digitalocean (day 3) - Checking Birthdays And Sending Sms Notifications

Trending 3 weeks ago
ARTICLE AD BOX

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

Welcome to Day 3 of 12 Days of DigitalOcean! Over nan past 2 days, we’ve group up a PostgreSQL database and connected to it utilizing Python. Now, it’s clip to make our Birthday Reminder Service really do point useful—send you a matter erstwhile there’s a time today. 🎂

birthday_reminder_sms

We’ll usage Twilio, a activity that makes it easy to nonstop SMS messages pinch conscionable a less lines of code. By nan extremity of today, your app will cheque nan database for birthdays and nonstop you a reminder if there’s a match.

Let’s get started!


✨ Why This Step?

Finding birthdays successful your database is only information of nan job. To make this app genuinely useful, we petition to notify personification (you!) astir these emblematic dates.

This measurement connects nan dots:

  • Use SQL queries to find birthdays that lucifer today’s date.
  • Send a friends SMS reminder utilizing Twilio.

It’s speedy to group up and makes nan app instantly overmuch 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 utilizing Twilio’s Python SDK.
  3. Combine these steps into a single, functional Python script.

🛠 What You’ll Need

Before starting, make judge you have:

  • A Twilio narration (if you don’t personification 1 yet, recreation this Quickstart Guide to mobility up, bargain a Twilio telephone number, and get your credentials.
  • Your Twilio credentials:
    • Account SID
    • Auth Token
    • Twilio telephone number
  • The database and Python narration book from Day 2.
  • Sample data successful your contacts array (we added this successful Day 1 - Setting Up a PostgreSQL Database for Birthday Reminders). If you petition to adhd more, recreation nan steps successful Day 1 to populate your database.

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

Step 1: Install Twilio’s Python SDK

To nonstop SMS notifications, we’ll petition nan Twilio Python library. Install it by running:

pip instal twilio

If you don’t already personification your Twilio credentials (Account SID, Auth Token, and a telephone number), recreation Twilio’s Messaging Quickstart. It walks you done signing up, purchasing a telephone number, and grabbing nan basal details.

twilio_api_dashboard


Step 2: Update Your .env File

Your .env grounds should now spot immoderate your database credentials (from Day 2) and your Twilio credentials. You tin find nan Twilio credentials—Account SID, Auth Token, and your Twilio telephone number—by logging into your Twilio narration dashboard.

Update your .env grounds to look for illustration 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 nan placeholders pinch your existent credentials.
  • Add your individual telephone number arsenic TWILIO_PHONE_TO to personification proceedings notifications.

Pro Tip: Make judge .env is added to your .gitignore grounds to forestall delicate credentials from being exposed successful type control.


Step 3: Write nan Python Script

Here’s nan afloat Python book that queries nan database for today’s birthdays and sends SMS notifications utilizing Twilio:

from datetime import datetime import pg8000 from dotenv import load_dotenv from twilio.rest import Client import os load_dotenv() def connect_to_database(): """Establish narration to nan 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 time matter relationship utilizing Twilio.""" try: account_sid = os.getenv("TWILIO_ACCOUNT_SID") auth_token = os.getenv("TWILIO_AUTH_TOKEN") customer = Client(account_sid, auth_token) relationship = client.messages.create( body=f"🎉 It's {first_name} {last_name or ''}'s time 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 correction occurred while sending nan message: {e}") def check_birthdays(): """Check if immoderate contact's time matches today's time and nonstop a notification.""" try: conn = connect_to_database() cursor = conn.cursor() coming = datetime.now() today_month = today.month today_day = today.day 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() if rows: print("Birthday Notifications:") for connection in rows: first_name, last_name, _ = row send_birthday_message(first_name, last_name) else: print("No birthdays today.") cursor.close() conn.close() except Exception as e: print(f"An correction occurred while checking birthdays: {e}") if __name__ == "__main__": check_birthdays()

Step 5: Test Your Script

Run nan book to proceedings everything:

python check_birthdays.py

vscode_birthday_reminder_script_sms_notification_sent

birthday_reminder_sms

If there’s a time successful your database matching today’s date, you’ll personification a matter message. 🎉 If not, nan book will simply print:

No birthdays today.

vscode_birthday_reminder_script_no_birthdays

🎁 Wrap-Up

Here’s what we accomplished today:

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

✅ Used Twilio to nonstop SMS notifications for those birthdays.

✅ Combined everything into a functional Python script.

Up next: Tomorrow, we’ll deploy this book to DigitalOcean Functions to make it tally successful nan cloud—no server guidance needed. This is wherever nan Birthday Reminder Service starts to tally automatically. Stay tuned! 🚀

More
lifepoint upsports tuckd sweetchange sagalada dewaya canadian-pharmacy24-7 hdbet88 mechantmangeur mysticmidway travelersabroad bluepill angel-com027