|
|
|
import os |
|
import json |
|
import time |
|
from flask import Flask, render_template, request, jsonify, redirect, session |
|
from transformers import pipeline |
|
from gtts import gTTS |
|
from pydub import AudioSegment |
|
from pydub.silence import detect_nonsilent |
|
from waitress import serve |
|
from simple_salesforce import Salesforce |
|
from dotenv |
|
|
|
load_dotenv() |
|
|
|
|
|
app = Flask(__name__) |
|
app.secret_key = os.urandom(24) |
|
|
|
|
|
try: |
|
print("Attempting to connect to Salesforce...") |
|
sf = Salesforce( |
|
username=os.getenv('[email protected]'), |
|
password=os.getenv('Sati@1020'), |
|
security_token=os.getenv('sSSjyhInIsUohKpG8sHzty2q') |
|
) |
|
print("Connected to Salesforce successfully!") |
|
except Exception as e: |
|
print(f"Failed to connect to Salesforce: {str(e)}") |
|
|
|
|
|
def get_customer_by_email(email): |
|
"""Check if customer exists in Salesforce.""" |
|
try: |
|
query = f"SELECT Id FROM Customer_Login__c WHERE Email__c = '{email}'" |
|
result = sf.query(query) |
|
if result['records']: |
|
return result['records'][0]['Id'] |
|
return None |
|
except Exception as e: |
|
print(f"Error fetching customer: {str(e)}") |
|
return None |
|
|
|
def create_salesforce_record(name, email, phone_number): |
|
"""Create customer login record in Salesforce.""" |
|
try: |
|
existing_customer = get_customer_by_email(email) |
|
if existing_customer: |
|
return {"id": existing_customer} |
|
|
|
customer_login = sf.Customer_Login__c.create({ |
|
'Name': name, |
|
'Email__c': email, |
|
'Phone_Number__c': phone_number |
|
}) |
|
return customer_login |
|
except Exception as e: |
|
raise Exception(f"Failed to create record: {str(e)}") |
|
|
|
def create_salesforce_order(cart_items, total_price, customer_id): |
|
"""Create order in Salesforce.""" |
|
try: |
|
order = sf.Order__c.create({ |
|
'Total_Price__c': total_price, |
|
'Cart_Items__c': json.dumps(cart_items), |
|
'Customer__c': customer_id |
|
}) |
|
return order |
|
except Exception as e: |
|
raise Exception(f"Failed to create order: {str(e)}") |
|
|
|
def get_menu_items(): |
|
"""Fetch menu items from Salesforce.""" |
|
try: |
|
query = "SELECT Name, Price__c, Ingredients__c, Category__c FROM Menu_Item__c" |
|
result = sf.query(query) |
|
return result.get('records', []) |
|
except Exception as e: |
|
print(f"Error fetching menu items: {str(e)}") |
|
return [] |
|
|
|
|
|
def generate_audio_prompt(text, filename): |
|
"""Generate speech from text using gTTS.""" |
|
try: |
|
tts = gTTS(text) |
|
tts.save(os.path.join("static", filename)) |
|
except Exception as e: |
|
print(f"Error: {e}") |
|
print("Retrying after 5 seconds...") |
|
time.sleep(5) |
|
generate_audio_prompt(text, filename) |
|
|
|
|
|
@app.route("/") |
|
def index(): |
|
return render_template("index.html") |
|
|
|
@app.route("/register", methods=["POST"]) |
|
def register(): |
|
"""Handles new user registration.""" |
|
data = request.json |
|
name = data.get('name') |
|
email = data.get('email') |
|
phone_number = data.get('phone_number') |
|
|
|
if not name or not email or not phone_number: |
|
return jsonify({'error': 'Missing required fields'}), 400 |
|
|
|
try: |
|
customer = create_salesforce_record(name, email, phone_number) |
|
session['customer_id'] = customer['id'] |
|
return jsonify({'message': 'User registered successfully', 'customer_id': customer['id']}), 201 |
|
except Exception as e: |
|
return jsonify({'error': f'Failed to register user: {str(e)}'}), 500 |
|
|
|
@app.route("/login", methods=["POST"]) |
|
def login(): |
|
"""Handles customer login.""" |
|
data = request.json |
|
email = data.get('email') |
|
|
|
if not email: |
|
return jsonify({'error': 'Email is required'}), 400 |
|
|
|
customer_id = get_customer_by_email(email) |
|
if customer_id: |
|
session['customer_id'] = customer_id |
|
return jsonify({'message': 'Login successful', 'customer_id': customer_id}), 200 |
|
else: |
|
return jsonify({'error': 'User not found'}), 404 |
|
|
|
@app.route("/menu", methods=["GET"]) |
|
def menu_page(): |
|
"""Displays the menu from Salesforce.""" |
|
menu_items = get_menu_items() |
|
if not menu_items: |
|
return jsonify({'error': 'No menu items found'}), 404 |
|
|
|
menu_data = [ |
|
{"name": item['Name'], "price": item['Price__c'], "ingredients": item.get('Ingredients__c', ''), "category": item.get('Category__c', '')} |
|
for item in menu_items |
|
] |
|
return render_template("menu_page.html", menu_items=menu_data) |
|
|
|
@app.route("/cart", methods=["GET"]) |
|
def cart(): |
|
"""Displays the cart.""" |
|
cart_items = session.get('cart_items', []) |
|
return render_template("cart_page.html", cart_items=cart_items) |
|
|
|
@app.route("/order-summary", methods=["GET"]) |
|
def order_summary(): |
|
"""Displays the order summary before finalizing.""" |
|
order_details = session.get('cart_items', []) |
|
total_price = sum(item['price'] * item['quantity'] for item in order_details) |
|
return render_template("order_summary.html", order_details=order_details, total_price=total_price) |
|
|
|
@app.route("/final_order", methods=["GET"]) |
|
def final_order(): |
|
"""Handles final order placement.""" |
|
if 'cart_items' not in session or 'customer_id' not in session: |
|
return jsonify({'error': 'No order details found'}), 400 |
|
|
|
cart_items = session.get('cart_items', []) |
|
total_price = sum(item['price'] * item['quantity'] for item in cart_items) |
|
customer_id = session['customer_id'] |
|
|
|
try: |
|
order = create_salesforce_order(cart_items, total_price, customer_id) |
|
session.pop('cart_items', None) |
|
return render_template("final_order.html", order_details=cart_items, total_price=total_price) |
|
except Exception as e: |
|
return jsonify({'error': f'Failed to create order in Salesforce: {str(e)}'}), 500 |
|
|
|
@app.route("/add_to_cart", methods=["POST"]) |
|
def add_to_cart(): |
|
"""Adds an item to the cart.""" |
|
data = request.json |
|
item_name = data.get('item_name') |
|
quantity = data.get('quantity') |
|
|
|
if not item_name or not quantity: |
|
return jsonify({'error': 'Item name and quantity required'}), 400 |
|
|
|
cart_items = session.get('cart_items', []) |
|
cart_items.append({"name": item_name, "quantity": quantity, "price": 10}) |
|
session['cart_items'] = cart_items |
|
|
|
return jsonify({"success": True, "message": f"Added {item_name} to cart."}) |
|
|
|
|
|
if __name__ == "__main__": |
|
serve(app, host="0.0.0.0", port=7860) |
|
|