-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
75 lines (62 loc) · 2.08 KB
/
Copy pathapp.py
File metadata and controls
75 lines (62 loc) · 2.08 KB
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
import os
import logging
from flask import Flask, render_template, jsonify, request
# Configure logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
# Initialize Flask
app = Flask(__name__)
app.secret_key = os.environ.get("SESSION_SECRET", "default-secret-key")
# Initialize Quiz Manager
try:
from quiz_manager import QuizManager
quiz_manager = QuizManager()
logger.info("Quiz Manager initialized successfully")
except Exception as e:
logger.error(f"Failed to initialize Quiz Manager: {e}")
raise
# Setup Telegram Bot handlers
telegram_bot = None
async def init_bot():
"""Initialize and start the Telegram bot"""
global telegram_bot
try:
from bot_handlers import TelegramQuizBot
# Get bot token
token = os.environ.get("TELEGRAM_TOKEN")
if not token:
raise ValueError("TELEGRAM_TOKEN environment variable is required")
# Initialize bot
telegram_bot = TelegramQuizBot(quiz_manager)
await telegram_bot.initialize(token)
logger.info("Telegram bot initialized successfully")
return telegram_bot
except Exception as e:
logger.error(f"Failed to initialize Telegram bot: {e}")
raise
@app.route('/')
def admin_panel():
return render_template('admin.html')
@app.route('/api/questions', methods=['GET'])
def get_questions():
return jsonify(quiz_manager.get_all_questions())
@app.route('/api/questions', methods=['POST'])
def add_question():
data = request.get_json()
quiz_manager.add_question(
data['question'],
data['options'],
data['correct_answer']
)
return jsonify({"status": "success"})
@app.route('/api/questions/<int:question_id>', methods=['DELETE'])
def delete_question(question_id):
quiz_manager.delete_question(question_id)
return jsonify({"status": "success"})
if __name__ == "__main__":
try:
import asyncio
asyncio.run(init_bot())
app.run(host="0.0.0.0", port=5000, debug=True)
except Exception as e:
logger.exception(f"Application startup failed: {e}")