-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
66 lines (54 loc) · 2.45 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
from flask import Flask, request, jsonify
from twilio.rest import Client
from flask_cors import CORS
from dotenv import load_dotenv
import os
import logging
# Initialize Flask application and add origins
app = Flask(__name__)
CORS(app, resources={r"/scan": {"origins": "https://anonymous-reporting-4.onrender.com"}})
# Configure logging
logging.basicConfig(level=logging.INFO)
# Load the environment variables from .env file
load_dotenv()
# Twilio configuration using environment variables
ACCOUNT_SID = os.getenv('TWILIO_ACCOUNT_SID')
AUTH_TOKEN = os.getenv('TWILIO_AUTH_TOKEN')
TWILIO_PHONE_NUMBER = os.getenv('TWILIO_PHONE_NUMBER')
DEVELOPER_PHONE_NUMBER = os.getenv('DEVELOPER_PHONE_NUMBER')
#these things should be updated in env file
# Log environment variables to ensure they are loaded (excluding sensitive ones)
app.logger.info(f"ACCOUNT_SID: {ACCOUNT_SID}")
app.logger.info(f"AUTH_TOKEN: {'***' + AUTH_TOKEN[-4:]}") # Masking sensitive part
app.logger.info(f"TWILIO_PHONE_NUMBER: {TWILIO_PHONE_NUMBER}")
app.logger.info(f"DEVELOPER_PHONE_NUMBER: {DEVELOPER_PHONE_NUMBER}")
# Initialize Twilio client outside of route handler
client = Client(ACCOUNT_SID, AUTH_TOKEN)
@app.route('/scan', methods=['POST'])
def scan():
app.logger.info('Request received')
phone_number = request.form.get('phone_number')
location = request.form.get('location')
app.logger.info(f'Received data - Phone Number: {phone_number}, Location: {location}')
if not phone_number or not location:
app.logger.error('Phone number or location not provided')
return jsonify({"success": False, "error": "Phone number and location are required"}), 400
try:
message = f"Phone Number: {phone_number}\nLocation: {location}"
client.messages.create(
body=message,
from_=TWILIO_PHONE_NUMBER,
to=DEVELOPER_PHONE_NUMBER
)
app.logger.info('Message sent successfully')
return jsonify({"success": True}), 200
except Exception as e:
app.logger.error(f'Error processing request: {str(e)}')
return jsonify({"success": False, "error": str(e)}), 500
# Route to confirm server is running
@app.route('/')
def home():
return jsonify({"message": "Server is running!"})
# For deployment on Render.com or similar platforms
if __name__ == '__main__':
app.run(debug=False, host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))