import os import time import logging import json import requests from flask import Flask, render_template, request, jsonify, session from flask_session import Session from simple_salesforce import Salesforce from gtts import gTTS from pydub import AudioSegment from pydub.silence import detect_nonsilent from waitress import serve app = Flask(__name__) # Configure Flask session app.secret_key = os.getenv("SECRET_KEY", "sSSjyhInIsUohKpG8sHzty2q") app.config["SESSION_TYPE"] = "filesystem" Session(app) # Set up logging logging.basicConfig(level=logging.INFO) # Salesforce credentials SF_USERNAME = 'diggavalli98@gmail.com' SF_PASSWORD = 'Sati@1020' SF_SECURITY_TOKEN = 'sSSjyhInIsUohKpG8sHzty2q' # Connect to Salesforce try: sf = Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=SF_SECURITY_TOKEN) print("✅ Connected to Salesforce successfully!") except Exception as e: print(f"❌ Failed to connect to Salesforce: {str(e)}") # Function to generate voice prompts def generate_audio_prompt(text, filename): try: tts = gTTS(text) tts.save(os.path.join("static", filename)) except gtts.tts.gTTSError as e: time.sleep(5) generate_audio_prompt(text, filename) # Function to check if audio contains actual speech def is_silent_audio(audio_path): audio = AudioSegment.from_wav(audio_path) nonsilent_parts = detect_nonsilent(audio, min_silence_len=500, silence_thresh=audio.dBFS-16) return len(nonsilent_parts) == 0 @app.route("/") def index(): return render_template("index.html") # ✅ REGISTRATION ENDPOINT @app.route("/register", methods=["POST"]) def register(): data = request.json name = data.get('name', '').strip() email = data.get('email', '').strip().lower() phone = data.get('phone', '').strip() if not name or not email or not phone: return jsonify({'error': 'Missing data'}), 400 try: # Query to check if the user already exists query = f"SELECT Id FROM Customer_Login__c WHERE LOWER(Email__c) = '{email}' AND Phone_Number__c = '{phone}' LIMIT 1" existing_user = sf.query(query) if existing_user['totalSize'] > 0: return jsonify({'error': 'User already exists'}), 409 # Create a new record in Salesforce Customer_Login__c object customer_login = sf.Customer_Login__c.create({ 'Name': name, 'Email__c': email, 'Phone_Number__c': phone }) # Check if the record was successfully created if customer_login.get('id'): return jsonify({'success': True, 'user_id': customer_login['id']}), 200 else: return jsonify({'error': 'Failed to create record in Salesforce'}), 500 except Exception as e: return jsonify({'error': f"Error creating user: {str(e)}"}), 500 # Start Production Server if __name__ == "__main__": serve(app, host="0.0.0.0", port=7860)