Spaces:
Running
Running
Update app/app.py
Browse files- app/app.py +65 -25
app/app.py
CHANGED
@@ -1,8 +1,10 @@
|
|
1 |
-
# app/app.py
|
2 |
from flask import Flask, request, jsonify
|
3 |
import os
|
4 |
import logging
|
5 |
from waitress import serve
|
|
|
|
|
|
|
6 |
|
7 |
# Logging konfigurieren
|
8 |
logging.basicConfig(
|
@@ -11,45 +13,83 @@ logging.basicConfig(
|
|
11 |
)
|
12 |
logger = logging.getLogger(__name__)
|
13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
@app.route("/", methods=["GET"])
|
17 |
def health_check():
|
18 |
logger.info("Health check endpoint called")
|
19 |
return jsonify({
|
20 |
"status": "healthy",
|
21 |
-
"message": "Discord bot is running on Hugging Face Spaces!"
|
|
|
|
|
22 |
})
|
23 |
|
24 |
@app.route("/interactions", methods=["POST"])
|
25 |
def interactions():
|
|
|
|
|
|
|
|
|
|
|
26 |
if not request.is_json:
|
27 |
logger.warning("Received non-JSON request")
|
28 |
return jsonify({"error": "Expected JSON data"}), 400
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
"
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
if __name__ == "__main__":
|
53 |
-
logger.info("Starting Discord bot
|
54 |
-
|
55 |
serve(app, host="0.0.0.0", port=8080)
|
|
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
import os
|
3 |
import logging
|
4 |
from waitress import serve
|
5 |
+
from nacl.signing import VerifyKey
|
6 |
+
from nacl.exceptions import BadSignatureError
|
7 |
+
import json
|
8 |
|
9 |
# Logging konfigurieren
|
10 |
logging.basicConfig(
|
|
|
13 |
)
|
14 |
logger = logging.getLogger(__name__)
|
15 |
|
16 |
+
# Konfiguration aus Umgebungsvariablen
|
17 |
+
PUBLIC_KEY = os.getenv('Public_Key')
|
18 |
+
APPLICATION_ID = os.getenv('Application_ID')
|
19 |
+
|
20 |
+
if not PUBLIC_KEY or not APPLICATION_ID:
|
21 |
+
logger.error("Missing required environment variables!")
|
22 |
+
logger.error("Please set Public_Key and Application_ID in Hugging Face Spaces variables")
|
23 |
+
raise ValueError("Missing required environment variables")
|
24 |
+
|
25 |
app = Flask(__name__)
|
26 |
+
verify_key = VerifyKey(bytes.fromhex(PUBLIC_KEY))
|
27 |
+
|
28 |
+
def verify_discord_request():
|
29 |
+
signature = request.headers.get('X-Signature-Ed25519')
|
30 |
+
timestamp = request.headers.get('X-Signature-Timestamp')
|
31 |
+
|
32 |
+
if not signature or not timestamp:
|
33 |
+
logger.warning("Missing Discord signature headers")
|
34 |
+
return False
|
35 |
+
|
36 |
+
try:
|
37 |
+
body = request.data.decode('utf-8')
|
38 |
+
verify_key.verify(f"{timestamp}{body}".encode(), bytes.fromhex(signature))
|
39 |
+
return True
|
40 |
+
except (BadSignatureError, Exception) as e:
|
41 |
+
logger.warning(f"Failed to verify request: {str(e)}")
|
42 |
+
return False
|
43 |
|
44 |
@app.route("/", methods=["GET"])
|
45 |
def health_check():
|
46 |
logger.info("Health check endpoint called")
|
47 |
return jsonify({
|
48 |
"status": "healthy",
|
49 |
+
"message": "Discord bot is running on Hugging Face Spaces!",
|
50 |
+
"application_id": APPLICATION_ID[:6] + "..." if APPLICATION_ID else "Not set",
|
51 |
+
"public_key_set": bool(PUBLIC_KEY),
|
52 |
})
|
53 |
|
54 |
@app.route("/interactions", methods=["POST"])
|
55 |
def interactions():
|
56 |
+
# Verify the request is from Discord
|
57 |
+
if not verify_discord_request():
|
58 |
+
logger.warning("Failed to verify Discord request")
|
59 |
+
return "Invalid request signature", 401
|
60 |
+
|
61 |
if not request.is_json:
|
62 |
logger.warning("Received non-JSON request")
|
63 |
return jsonify({"error": "Expected JSON data"}), 400
|
64 |
|
65 |
+
try:
|
66 |
+
data = request.json
|
67 |
+
logger.info(f"Received verified interaction type: {data.get('type')}")
|
68 |
+
|
69 |
+
# Discord Ping Verification
|
70 |
+
if data.get("type") == 1:
|
71 |
+
logger.info("Responding to ping verification")
|
72 |
+
return jsonify({"type": 1})
|
73 |
+
|
74 |
+
# Slash Command Handler
|
75 |
+
if data.get("type") == 2:
|
76 |
+
command = data.get("data", {}).get("name")
|
77 |
+
logger.info(f"Received command: {command}")
|
78 |
+
if command == "settings":
|
79 |
+
return jsonify({
|
80 |
+
"type": 4,
|
81 |
+
"data": {
|
82 |
+
"content": f"🔧 Bot Einstellungen:\n• Application ID: {APPLICATION_ID[:6]}...\n• Verifizierung: ✅ Aktiv"
|
83 |
+
}
|
84 |
+
})
|
85 |
+
|
86 |
+
return jsonify({"error": "Unknown interaction type"}), 400
|
87 |
+
|
88 |
+
except Exception as e:
|
89 |
+
logger.error(f"Error processing request: {str(e)}")
|
90 |
+
return jsonify({"error": "Internal server error"}), 500
|
91 |
|
92 |
if __name__ == "__main__":
|
93 |
+
logger.info(f"Starting Discord bot (Application ID: {APPLICATION_ID[:6]}...)")
|
94 |
+
logger.info("Public Key verification is enabled")
|
95 |
serve(app, host="0.0.0.0", port=8080)
|