Files
email_alerts_v2/test_twilio.py
T

115 lines
3.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
"""
Test Twilio credentials and WhatsApp configuration
"""
import os
from dotenv import load_dotenv
from twilio.rest import Client
# Load environment variables
load_dotenv()
def test_credentials():
print("=" * 60)
print("TWILIO CREDENTIALS TEST")
print("=" * 60)
# Get credentials
account_sid = (os.getenv("TWILIO_ACCOUNT_SID") or "").strip()
auth_token = (os.getenv("TWILIO_AUTH_TOKEN") or "").strip()
from_number = (os.getenv("TWILIO_WHATSAPP_NUMBER") or "").strip()
to_number = (os.getenv("WHATSAPP_TO_NUMBER") or "").strip()
# Check if credentials exist
print("\n1. Checking environment variables:")
print(f" TWILIO_ACCOUNT_SID: {'✓ Found' if account_sid else '✗ Missing'}")
if account_sid:
print(f" Value: {account_sid[:8]}...{account_sid[-4:]}")
print(f" TWILIO_AUTH_TOKEN: {'✓ Found' if auth_token else '✗ Missing'}")
if auth_token:
print(f" Value: {'*' * len(auth_token[:4])}...{auth_token[-4:]}")
print(f" TWILIO_WHATSAPP_NUMBER: {'✓ Found' if from_number else '✗ Missing'}")
if from_number:
print(f" Value: {from_number}")
print(f" WHATSAPP_TO_NUMBER: {'✓ Found' if to_number else '✗ Missing'}")
if to_number:
print(f" Value: {to_number}")
# Test authentication
if not (account_sid and auth_token):
print("\n❌ Missing required credentials!")
print("\nPlease check your .env file and ensure:")
print(" - TWILIO_ACCOUNT_SID is set")
print(" - TWILIO_AUTH_TOKEN is set")
print(" - No extra spaces or quotes around values")
return False
print("\n2. Testing Twilio authentication...")
try:
client = Client(account_sid, auth_token)
# Try to fetch account info to verify auth
account = client.api.accounts(account_sid).fetch()
print(" ✓ Authentication successful!")
print(f" Account: {account.friendly_name}")
print(f" Status: {account.status}")
except Exception as e:
print(" ✗ Authentication failed!")
print(f" Error: {e}")
print("\n Troubleshooting:")
print(" 1. Go to https://console.twilio.com")
print(" 2. Navigate to Account > API keys & tokens")
print(" 3. Copy the Account SID and Auth Token")
print(" 4. Update your .env file with correct values")
print(" 5. Make sure there are NO quotes or spaces")
return False
# Test WhatsApp sender configuration
print("\n3. Testing WhatsApp sender configuration...")
if not from_number:
print(" ✗ TWILIO_WHATSAPP_NUMBER not set")
print(" Please set up WhatsApp sender at:")
print(" https://console.twilio.com/us1/develop/sms/senders/whatsapp-senders")
return False
if not from_number.startswith("+"):
print(f" ⚠ Warning: Phone number should start with '+': {from_number}")
print(f" ✓ WhatsApp sender configured: {from_number}")
# Test recipient
print("\n4. Testing recipient configuration...")
if not to_number:
print(" ✗ WHATSAPP_TO_NUMBER not set")
return False
if not to_number.startswith("+"):
print(f" ⚠ Warning: Phone number should start with '+': {to_number}")
print(f" ✓ Recipient configured: {to_number}")
print("\n" + "=" * 60)
print("✓ ALL CHECKS PASSED - Ready to send WhatsApp messages!")
print("=" * 60)
return True
if __name__ == "__main__":
success = test_credentials()
if success:
print("\nYou can now send test messages with:")
print(
" python -c 'from src.whatsapp_sender import WhatsAppSender; "
'w = WhatsAppSender(); print(w.send_alert("Test message"))\''
)
else:
print("\n❌ Please fix the issues above before sending WhatsApp messages")