Spaces:
Sleeping
Sleeping
Commit ·
560940e
1
Parent(s): 255e41f
feat(sprint-16): implement Coverage Intelligence Dashboard and WhatsApp expiry alerts
Browse files- whatsapp/expiry_alerts.py +104 -0
whatsapp/expiry_alerts.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import httpx
|
| 4 |
+
from datetime import date, timedelta
|
| 5 |
+
from typing import List, Dict, Any, cast
|
| 6 |
+
from supabase import create_client, Client
|
| 7 |
+
|
| 8 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 9 |
+
from config import settings
|
| 10 |
+
|
| 11 |
+
SUPABASE_URL = settings.SUPABASE_URL
|
| 12 |
+
SUPABASE_KEY = settings.SUPABASE_KEY
|
| 13 |
+
# META_API_TOKEN = os.environ.get("META_API_TOKEN") # In a real scenario, this is the WhatsApp API token
|
| 14 |
+
|
| 15 |
+
def generate_alert_payload(scheme: Dict[str, Any], phone_number: str) -> Dict[str, Any]:
|
| 16 |
+
"""
|
| 17 |
+
Format the WhatsApp API payload for the expiry alert.
|
| 18 |
+
"""
|
| 19 |
+
title = scheme.get("title", "Government Scheme")
|
| 20 |
+
deadline = scheme.get("deadline_date", "Unknown Date")
|
| 21 |
+
url = scheme.get("source_url", "https://myscheme.gov.in")
|
| 22 |
+
|
| 23 |
+
return {
|
| 24 |
+
"messaging_product": "whatsapp",
|
| 25 |
+
"to": phone_number,
|
| 26 |
+
"type": "template",
|
| 27 |
+
"template": {
|
| 28 |
+
"name": "scheme_expiry_alert",
|
| 29 |
+
"language": {
|
| 30 |
+
"code": "en_US"
|
| 31 |
+
},
|
| 32 |
+
"components": [
|
| 33 |
+
{
|
| 34 |
+
"type": "body",
|
| 35 |
+
"parameters": [
|
| 36 |
+
{
|
| 37 |
+
"type": "text",
|
| 38 |
+
"text": title
|
| 39 |
+
},
|
| 40 |
+
{
|
| 41 |
+
"type": "text",
|
| 42 |
+
"text": deadline
|
| 43 |
+
}
|
| 44 |
+
]
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
"type": "button",
|
| 48 |
+
"sub_type": "url",
|
| 49 |
+
"index": "0",
|
| 50 |
+
"parameters": [
|
| 51 |
+
{
|
| 52 |
+
"type": "text",
|
| 53 |
+
"text": url
|
| 54 |
+
}
|
| 55 |
+
]
|
| 56 |
+
}
|
| 57 |
+
]
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
def main() -> None:
|
| 62 |
+
if not SUPABASE_URL or not SUPABASE_KEY:
|
| 63 |
+
print("❌ Missing SUPABASE_URL or SUPABASE_KEY")
|
| 64 |
+
return
|
| 65 |
+
|
| 66 |
+
supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
|
| 67 |
+
today = date.today()
|
| 68 |
+
alert_date = (today + timedelta(days=7)).isoformat()
|
| 69 |
+
today_str = today.isoformat()
|
| 70 |
+
|
| 71 |
+
print(f"📡 Scanning for schemes expiring between {today_str} and {alert_date}...")
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
response = supabase.table("schemes") \
|
| 75 |
+
.select("id, title, deadline_date, source_url") \
|
| 76 |
+
.lte("deadline_date", alert_date) \
|
| 77 |
+
.gte("deadline_date", today_str) \
|
| 78 |
+
.eq("is_active", True) \
|
| 79 |
+
.execute()
|
| 80 |
+
|
| 81 |
+
schemes = cast(List[Dict[str, Any]], response.data or [])
|
| 82 |
+
|
| 83 |
+
if not schemes:
|
| 84 |
+
print("✅ No schemes expiring within the next 7 days.")
|
| 85 |
+
return
|
| 86 |
+
|
| 87 |
+
print(f"⚠️ Found {len(schemes)} schemes approaching their deadline.")
|
| 88 |
+
|
| 89 |
+
# Simulate fetching subscribed users (in a full setup, this comes from whatsapp_subscriptions table)
|
| 90 |
+
mock_subscribers = ["+919876543210"]
|
| 91 |
+
|
| 92 |
+
for scheme in schemes:
|
| 93 |
+
print(f" Generating alerts for: {scheme.get('title')} (Deadline: {scheme.get('deadline_date')})")
|
| 94 |
+
|
| 95 |
+
for user in mock_subscribers:
|
| 96 |
+
payload = generate_alert_payload(scheme, user)
|
| 97 |
+
# In production, this would be an httpx.post to graph.facebook.com/v17.0/.../messages
|
| 98 |
+
print(f" [DRY RUN] Sent WhatsApp payload to {user} for scheme ID: {scheme.get('id')}")
|
| 99 |
+
|
| 100 |
+
except Exception as e:
|
| 101 |
+
print(f"❌ Alert generation failed: {e}")
|
| 102 |
+
|
| 103 |
+
if __name__ == "__main__":
|
| 104 |
+
main()
|