-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
78 lines (64 loc) · 1.77 KB
/
main.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
67
68
69
70
71
72
73
74
75
76
77
78
from flask import (
Flask,
jsonify,
request,
)
import os
from datetime import datetime, timezone
from flask_cors import CORS
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
from dotenv import load_dotenv
import requests
load_dotenv(override=True)
WEBHOOK = os.environ["WEBHOOK_URI"]
app = Flask(__name__)
CORS(app)
limiter = Limiter(
get_remote_address,
app=app,
)
def send_webhook(url, email, subject, message):
data = {
"embeds": [
{
"title": subject,
"description": message,
"author": {
"name": email,
},
"timestamp": datetime.now(timezone.utc).isoformat(),
}
]
}
requests.post(url, json=data)
@app.route("/contact", methods=["POST"])
@limiter.limit("20/day")
@limiter.limit("10/hour")
@limiter.limit("3/minute")
def contact():
email = request.values.get("email")
subject = request.values.get("subject")
message = request.values.get("message")
if not email or not message or not subject:
return jsonify({"error": "Email, Subject and message are required"}), 400
try:
send_webhook(
WEBHOOK,
email,
subject,
message,
)
except Exception as e:
print(f"Error sending webhook: {e}")
return jsonify({"error": "Error sending message"}), 500
return jsonify({"message": "Message sent successfully"}), 200
@app.errorhandler(429)
def ratelimit_handler(e):
return jsonify({"error": f"{e}"}), 429
@app.route("/health", methods=["GET"])
@limiter.exempt
def health():
return jsonify({"status": "ok"}), 200
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True)