Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -696,46 +696,92 @@ class DatabaseService:
|
|
696 |
else:
|
697 |
return "Miscellaneous"
|
698 |
|
699 |
-
#
|
700 |
class TwilioWhatsAppService:
|
701 |
def __init__(self):
|
702 |
-
|
703 |
-
self.
|
|
|
|
|
|
|
704 |
self.whatsapp_number = 'whatsapp:+14155238886'
|
705 |
|
706 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
707 |
try:
|
708 |
self.client = Client(self.account_sid, self.auth_token)
|
709 |
-
|
|
|
|
|
|
|
|
|
|
|
710 |
self.enabled = True
|
|
|
711 |
except Exception as e:
|
712 |
print(f"β Failed to initialize Twilio: {e}")
|
|
|
713 |
self.client = None
|
714 |
self.enabled = False
|
715 |
else:
|
716 |
-
print("β Twilio credentials not
|
|
|
|
|
717 |
self.client = None
|
718 |
self.enabled = False
|
719 |
|
720 |
def send_whatsapp(self, phone, message):
|
|
|
721 |
if not self.enabled or not self.client:
|
722 |
print(f"π± [DEMO MODE] WhatsApp to {phone}: {message}")
|
723 |
return False
|
724 |
|
725 |
try:
|
|
|
|
|
|
|
|
|
726 |
to_whatsapp = f"whatsapp:{phone}"
|
|
|
|
|
|
|
|
|
|
|
|
|
727 |
twilio_message = self.client.messages.create(
|
728 |
body=message,
|
729 |
from_=self.whatsapp_number,
|
730 |
to=to_whatsapp
|
731 |
)
|
732 |
|
733 |
-
print(f"β
WhatsApp sent
|
|
|
|
|
734 |
return True
|
735 |
|
736 |
except Exception as e:
|
737 |
-
print(f"β Failed to send WhatsApp to {phone}
|
738 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
739 |
return False
|
740 |
|
741 |
# Initialize services
|
@@ -745,7 +791,7 @@ ocr_service = OCRService()
|
|
745 |
|
746 |
# Helper functions (unchanged)
|
747 |
def validate_phone_number(phone):
|
748 |
-
pattern = r'^\+\d{1,3}\d{6,14}
|
749 |
return re.match(pattern, phone) is not None
|
750 |
|
751 |
def validate_password(password):
|
@@ -1119,9 +1165,12 @@ def register_user(name, phone, password, confirm_password):
|
|
1119 |
return "β οΈ This number is already registered"
|
1120 |
|
1121 |
msg = f"π¦ Welcome to FinGenius Pro, {name}! Your account has been created successfully. You can now track expenses, manage budgets, and receive instant financial alerts. Start by adding your first balance! π°"
|
1122 |
-
twilio.send_whatsapp(phone, msg)
|
1123 |
|
1124 |
-
|
|
|
|
|
|
|
1125 |
|
1126 |
def add_balance(phone, amount_val, description=""):
|
1127 |
if not phone:
|
@@ -2169,6 +2218,24 @@ with gr.Blocks(title="FinGenius Pro", theme=gr.themes.Soft(), css=custom_css) as
|
|
2169 |
)
|
2170 |
|
2171 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2172 |
demo.launch(
|
2173 |
server_name="0.0.0.0",
|
2174 |
server_port=7860,
|
|
|
696 |
else:
|
697 |
return "Miscellaneous"
|
698 |
|
699 |
+
# Fixed Twilio WhatsApp Service
|
700 |
class TwilioWhatsAppService:
|
701 |
def __init__(self):
|
702 |
+
# Set your Twilio credentials here
|
703 |
+
self.account_sid = os.getenv('TWILIO_ACCOUNT_SID', 'your_account_sid_here')
|
704 |
+
self.auth_token = os.getenv('TWILIO_AUTH_TOKEN', 'your_auth_token_here')
|
705 |
+
|
706 |
+
# For Twilio Sandbox - use this number
|
707 |
self.whatsapp_number = 'whatsapp:+14155238886'
|
708 |
|
709 |
+
# For production Twilio (after approval) - use your approved number
|
710 |
+
# self.whatsapp_number = 'whatsapp:+1234567890' # Your approved WhatsApp Business number
|
711 |
+
|
712 |
+
print(f"π§ Twilio Configuration:")
|
713 |
+
print(f" Account SID: {self.account_sid[:10]}... (masked)")
|
714 |
+
print(f" Auth Token: {'*' * len(self.auth_token) if self.auth_token != 'your_auth_token_here' else 'Not set'}")
|
715 |
+
print(f" WhatsApp Number: {self.whatsapp_number}")
|
716 |
+
|
717 |
+
if self.account_sid != 'your_account_sid_here' and self.auth_token != 'your_auth_token_here' and TWILIO_AVAILABLE:
|
718 |
try:
|
719 |
self.client = Client(self.account_sid, self.auth_token)
|
720 |
+
|
721 |
+
# Test the connection by getting account info
|
722 |
+
account = self.client.api.accounts(self.account_sid).fetch()
|
723 |
+
print(f"β
Twilio WhatsApp Service initialized successfully")
|
724 |
+
print(f" Account Status: {account.status}")
|
725 |
+
print(f" Account Name: {account.friendly_name}")
|
726 |
self.enabled = True
|
727 |
+
|
728 |
except Exception as e:
|
729 |
print(f"β Failed to initialize Twilio: {e}")
|
730 |
+
print(f" Please check your Account SID and Auth Token")
|
731 |
self.client = None
|
732 |
self.enabled = False
|
733 |
else:
|
734 |
+
print("β Twilio credentials not configured or Twilio not installed")
|
735 |
+
print(" Please set TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN environment variables")
|
736 |
+
print(" Or modify the credentials directly in the code")
|
737 |
self.client = None
|
738 |
self.enabled = False
|
739 |
|
740 |
def send_whatsapp(self, phone, message):
|
741 |
+
"""Send WhatsApp message with proper error handling"""
|
742 |
if not self.enabled or not self.client:
|
743 |
print(f"π± [DEMO MODE] WhatsApp to {phone}: {message}")
|
744 |
return False
|
745 |
|
746 |
try:
|
747 |
+
# Ensure phone number has correct format
|
748 |
+
if not phone.startswith('+'):
|
749 |
+
phone = '+' + phone
|
750 |
+
|
751 |
to_whatsapp = f"whatsapp:{phone}"
|
752 |
+
|
753 |
+
print(f"π€ Attempting to send WhatsApp message:")
|
754 |
+
print(f" From: {self.whatsapp_number}")
|
755 |
+
print(f" To: {to_whatsapp}")
|
756 |
+
print(f" Message: {message[:50]}...")
|
757 |
+
|
758 |
twilio_message = self.client.messages.create(
|
759 |
body=message,
|
760 |
from_=self.whatsapp_number,
|
761 |
to=to_whatsapp
|
762 |
)
|
763 |
|
764 |
+
print(f"β
WhatsApp sent successfully!")
|
765 |
+
print(f" Message SID: {twilio_message.sid}")
|
766 |
+
print(f" Status: {twilio_message.status}")
|
767 |
return True
|
768 |
|
769 |
except Exception as e:
|
770 |
+
print(f"β Failed to send WhatsApp to {phone}")
|
771 |
+
print(f" Error: {str(e)}")
|
772 |
+
print(f" Error Type: {type(e).__name__}")
|
773 |
+
|
774 |
+
# Common error messages and solutions
|
775 |
+
if "not a valid phone number" in str(e).lower():
|
776 |
+
print(f" π‘ Solution: Check phone number format. Should be +country_code + number")
|
777 |
+
elif "unverified" in str(e).lower():
|
778 |
+
print(f" π‘ Solution: For Twilio Sandbox, recipient must send 'join catch-manner' to +14155238886 first")
|
779 |
+
elif "forbidden" in str(e).lower():
|
780 |
+
print(f" π‘ Solution: Check your Twilio credentials and account status")
|
781 |
+
elif "unauthorized" in str(e).lower():
|
782 |
+
print(f" π‘ Solution: Verify your Account SID and Auth Token are correct")
|
783 |
+
|
784 |
+
print(f"π± [FALLBACK] Message content: {message}")
|
785 |
return False
|
786 |
|
787 |
# Initialize services
|
|
|
791 |
|
792 |
# Helper functions (unchanged)
|
793 |
def validate_phone_number(phone):
|
794 |
+
pattern = r'^\+\d{1,3}\d{6,14}
|
795 |
return re.match(pattern, phone) is not None
|
796 |
|
797 |
def validate_password(password):
|
|
|
1165 |
return "β οΈ This number is already registered"
|
1166 |
|
1167 |
msg = f"π¦ Welcome to FinGenius Pro, {name}! Your account has been created successfully. You can now track expenses, manage budgets, and receive instant financial alerts. Start by adding your first balance! π°"
|
1168 |
+
whatsapp_sent = twilio.send_whatsapp(phone, msg)
|
1169 |
|
1170 |
+
if whatsapp_sent:
|
1171 |
+
return "β
Registration complete! Check WhatsApp for confirmation and sign in to continue."
|
1172 |
+
else:
|
1173 |
+
return "β
Registration complete! WhatsApp alerts are not configured, but you can still use all features. Sign in to continue."
|
1174 |
|
1175 |
def add_balance(phone, amount_val, description=""):
|
1176 |
if not phone:
|
|
|
2218 |
)
|
2219 |
|
2220 |
if __name__ == "__main__":
|
2221 |
+
print("π Starting FinGenius Pro...")
|
2222 |
+
print("π± WhatsApp Integration Status:")
|
2223 |
+
print(f" Twilio Available: {TWILIO_AVAILABLE}")
|
2224 |
+
print(f" Service Enabled: {twilio.enabled}")
|
2225 |
+
print("π OCR Services Status:")
|
2226 |
+
print(f" Tesseract Available: {TESSERACT_AVAILABLE}")
|
2227 |
+
print(f" Google Vision Available: {VISION_API_AVAILABLE}")
|
2228 |
+
print("πΌοΈ Image Processing Status:")
|
2229 |
+
print(f" PIL/OpenCV Available: {PIL_AVAILABLE}")
|
2230 |
+
print("")
|
2231 |
+
print("π Setup Instructions:")
|
2232 |
+
if not twilio.enabled:
|
2233 |
+
print(" 1. Set TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN environment variables")
|
2234 |
+
print(" 2. Or modify credentials directly in TwilioWhatsAppService class")
|
2235 |
+
print(" 3. Users must send 'join catch-manner' to +14155238886 to activate WhatsApp")
|
2236 |
+
print(" 4. Use the same phone number for both WhatsApp activation and app registration")
|
2237 |
+
print("")
|
2238 |
+
|
2239 |
demo.launch(
|
2240 |
server_name="0.0.0.0",
|
2241 |
server_port=7860,
|