|
|
from flask import Flask, render_template, request, jsonify |
|
|
from dotenv import load_dotenv |
|
|
import os |
|
|
import json |
|
|
import httpx |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from RAG import answer_question |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
|
try: |
|
|
with open("private/surge_config.json", "r") as f: |
|
|
surge_config = json.load(f) |
|
|
except FileNotFoundError: |
|
|
print("Warning: surge_config.json not found. SMS functionality will be disabled.") |
|
|
surge_config = None |
|
|
|
|
|
@app.route('/') |
|
|
def index(): |
|
|
return render_template('index.html') |
|
|
|
|
|
@app.route('/chat', methods=['POST']) |
|
|
def chat(): |
|
|
try: |
|
|
data = request.get_json() |
|
|
user_question = data.get('question', '') |
|
|
|
|
|
if not user_question: |
|
|
return jsonify({'error': 'No question provided'}), 400 |
|
|
|
|
|
|
|
|
answer = answer_question(user_question) |
|
|
|
|
|
return jsonify({ |
|
|
'answer': answer |
|
|
}) |
|
|
|
|
|
except Exception as e: |
|
|
return jsonify({'error': str(e)}), 500 |
|
|
|
|
|
@app.route('/send-sms', methods=['POST']) |
|
|
def send_sms(): |
|
|
if not surge_config: |
|
|
return jsonify({'error': 'SMS functionality not configured'}), 503 |
|
|
|
|
|
try: |
|
|
data = request.get_json() |
|
|
message_content = data.get('message', '') |
|
|
|
|
|
if not message_content: |
|
|
return jsonify({'error': 'No message provided'}), 400 |
|
|
|
|
|
|
|
|
with httpx.Client() as client: |
|
|
response = client.post( |
|
|
"https://api.surgemsg.com/messages", |
|
|
headers={ |
|
|
"Authorization": f"Bearer {surge_config['api_key']}", |
|
|
"Surge-Account": surge_config["account_id"], |
|
|
"Content-Type": "application/json", |
|
|
}, |
|
|
json={ |
|
|
"body": message_content, |
|
|
"conversation": { |
|
|
"contact": { |
|
|
"first_name": surge_config["my_first_name"], |
|
|
"last_name": surge_config["my_last_name"], |
|
|
"phone_number": surge_config["my_phone_number"], |
|
|
} |
|
|
}, |
|
|
}, |
|
|
) |
|
|
response.raise_for_status() |
|
|
return jsonify({'status': 'Message sent successfully'}) |
|
|
|
|
|
except Exception as e: |
|
|
return jsonify({'error': str(e)}), 500 |
|
|
|
|
|
if __name__ == '__main__': |
|
|
app.run(debug=True) |
|
|
|