chatcustom / app.py
Yaswanth56's picture
Update app.py
a2f1a4d verified
raw
history blame
1.93 kB
from flask import Flask, render_template, send_from_directory, request, jsonify
import requests
app = Flask(__name__, template_folder='templates', static_folder='static')
# Salesforce connection details (replace with your credentials)
SFDC_INSTANCE_URL = "https://yourinstance.my.salesforce.com" # Replace with your Salesforce instance URL
SFDC_ACCESS_TOKEN = "your_access_token" # Replace with your Salesforce access token
@app.route('/')
def index():
return render_template('index.html')
@app.route('/static/<path:filename>')
def serve_static(filename):
return send_from_directory('static', filename)
@app.route('/get_ingredients', methods=['POST'])
def get_ingredients():
dietary_preference = request.json.get('dietary_preference', '').lower()
# Salesforce SOQL query based on dietary preference
if dietary_preference == 'veg':
soql = "SELECT Sector_Detail_Name__c FROM Sector_Detail__c WHERE Category__c IN ('Veg', 'Both') LIMIT 100"
elif dietary_preference == 'non-vegetarian':
soql = "SELECT Sector_Detail_Name__c FROM Sector_Detail__c WHERE Category__c IN ('Non-Veg', 'Both') LIMIT 100"
else:
soql = "SELECT Sector_Detail_Name__c FROM Sector_Detail__c LIMIT 100" # Default case
headers = {
"Authorization": f"Bearer {SFDC_ACCESS_TOKEN}",
"Content-Type": "application/json"
}
url = f"{SFDC_INSTANCE_URL}/services/data/v57.0/query/?q={soql}"
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json()
ingredients = [record['Sector_Detail_Name__c'] for record in data['records'] if 'Sector_Detail_Name__c' in record]
return jsonify({"ingredients": ingredients})
except requests.exceptions.RequestException as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=7860)