refactored bot code

This commit is contained in:
Hosein
2025-12-30 18:57:48 +03:30
parent 67a76250ab
commit a52d62c0fc
15 changed files with 328 additions and 149 deletions
+4
View File
@@ -0,0 +1,4 @@
def register(bot):
@bot.message_handler(func=lambda message: True)
def handle_unknown(message):
bot.reply_to(message, f"You said: {message.text}")
+14
View File
@@ -0,0 +1,14 @@
from . import start, students, teachers, common
from bot.callbacks.init import register_all_callbacks
def register_all_handlers(bot):
"""Register all handlers and callbacks"""
# Message handlers
start.register(bot)
students.register(bot)
teachers.register(bot)
common.register(bot) # Must be last (catch-all)
# Callback handlers
register_all_callbacks(bot)
+29
View File
@@ -0,0 +1,29 @@
from telebot import types
from bot.utils.auth import is_authenticated
def register(bot):
@bot.message_handler(commands=["start"])
def start_handler(message):
if is_authenticated(message.from_user.id):
bot.send_message(
message.chat.id, "authorized user! Welcome.")
else:
bot.send_message(
message.chat.id, "Welcome! Use buttons to fetch from database.")
markup = types.ReplyKeyboardMarkup(
row_width=2, one_time_keyboard=True, resize_keyboard=True)
btn1 = types.KeyboardButton("Show Students")
btn2 = types.KeyboardButton("Show Teachers")
btn3 = types.KeyboardButton("Show Courses")
btn4 = types.KeyboardButton("Show Tag")
btn5 = types.KeyboardButton("Show Categories")
markup.add(btn1, btn2, btn3, btn4, btn5)
bot.reply_to(message, "Use buttons to fetch from database.",
reply_markup=markup)
@bot.message_handler(func=lambda message: message.text in ["Show Courses", "Show Tag", "Show Categories"])
def handle_test_options(message):
option = message.text
bot.send_message(message.chat.id, f"You selected: {option}")
+26
View File
@@ -0,0 +1,26 @@
import logging
from telebot import types
from database.models import Students
logger = logging.getLogger(__name__)
def register(bot):
@bot.message_handler(func=lambda message: message.text == "Show Students")
def get_students(message):
try:
data = Students.getAllStudents()
if data:
markup = types.InlineKeyboardMarkup(row_width=2)
for row in data:
btn = types.InlineKeyboardButton(
f"@{row[1]} | id#{row[0]}", callback_data=f"student_{row[0]}")
markup.add(btn)
bot.send_message(
message.chat.id, "Here is the data:", reply_markup=markup)
else:
bot.reply_to(message, "No data found.")
except Exception as e:
logger.error(f"Error in get_students handler: {e}")
bot.reply_to(message, "Sorry, an error occurred.")
+26
View File
@@ -0,0 +1,26 @@
import logging
from telebot import types
from database.models import Teachers
logger = logging.getLogger(__name__)
def register(bot):
@bot.message_handler(func=lambda message: message.text == "Show Teachers")
def get_teachers(message):
try:
data = Teachers.getAllTeachers()
if data:
markup = types.InlineKeyboardMarkup(row_width=2)
for row in data:
btn = types.InlineKeyboardButton(
f"@{row[1]} | id#{row[0]}", callback_data=f"teacher_{row[0]}")
markup.add(btn)
bot.send_message(
message.chat.id, "Here is the data:", reply_markup=markup)
else:
bot.reply_to(message, "No data found.")
except Exception as e:
logger.error(f"Error in get_teachers handler: {e}")
bot.reply_to(message, "Sorry, an error occurred.")