Files
email_alerts/test_whatsapp.py
T
2025-07-25 11:31:36 +01:00

67 lines
2.4 KiB
Python

#!/usr/bin/env python3
"""
WhatsApp Test Script
"""
import os
from dotenv import load_dotenv
from twilio.rest import Client
from twilio.base.exceptions import TwilioException
load_dotenv()
def test_whatsapp_connection():
"""Test WhatsApp connection and provide opt-in instructions"""
account_sid = os.getenv("TWILIO_ACCOUNT_SID")
auth_token = os.getenv("TWILIO_AUTH_TOKEN")
from_number = os.getenv("TWILIO_WHATSAPP_NUMBER")
to_number = os.getenv("WHATSAPP_TO_NUMBER")
print("🔍 WhatsApp Configuration Check:")
print(f" Account SID: {account_sid[:10]}..." if account_sid else "❌ Not set")
print(f" Auth Token: {'✅ Set' if auth_token else '❌ Not set'}")
print(f" From Number: {from_number}")
print(f" To Number: {to_number}")
print()
if not all([account_sid, auth_token, from_number, to_number]):
print("❌ Missing required environment variables")
return
try:
client = Client(account_sid, auth_token)
# Test message
test_message = "🚀 Email Alerts System Test\n\nThis is a test message to verify WhatsApp connectivity."
print("📱 Sending test WhatsApp message...")
message = client.messages.create(
from_=f"whatsapp:{from_number}",
body=test_message,
to=f"whatsapp:{to_number}"
)
print(f"✅ Test message sent successfully!")
print(f" Message SID: {message.sid}")
print(f" Status: {message.status}")
print()
print("📋 If you don't receive the message, you need to opt-in:")
print(f" 1. Open WhatsApp on your phone")
print(f" 2. Send 'join <your-opt-in-code>' to {from_number}")
print(f" 3. Or send any message to {from_number} to start the conversation")
print()
print("🔗 Twilio WhatsApp Setup Guide:")
print(" https://www.twilio.com/docs/whatsapp/quickstart/python")
except TwilioException as e:
print(f"❌ Twilio Error: {e}")
print()
print("🔧 Troubleshooting:")
print(" 1. Check your Twilio account is active")
print(" 2. Verify WhatsApp Business API is enabled")
print(" 3. Ensure the phone numbers are in correct format (+1234567890)")
print(" 4. Check if you need to opt-in to receive messages")
if __name__ == "__main__":
test_whatsapp_connection()