added authentication to the bot. added bot commnads. added create button for when the db is empty or no data is available to show.

This commit is contained in:
Hosein
2026-01-05 23:40:13 +03:30
parent 4b5b24c97c
commit caac6c3a08
18 changed files with 369 additions and 47 deletions
+12 -3
View File
@@ -1,5 +1,5 @@
from telebot import types, TeleBot
from database.models import Categories
from database.models import Categories, Admins
from bot.handlers.start import startMarkup
from bot.utils.crud_helpers import create_entity_markup
@@ -38,7 +38,7 @@ def register(bot: TeleBot):
<b>Description:</b> \n{category[2]}
"""
details.strip()
markup = create_entity_markup("category", category_id)
markup = create_entity_markup("category", category_id, True)
bot.send_message(call.message.chat.id, details,
reply_markup=markup, parse_mode="HTML")
@@ -91,9 +91,13 @@ def register(bot: TeleBot):
message.chat.id, "❌ Failed to create category.", reply_markup=startMarkup())
# Editing a Category Flow
@bot.callback_query_handler(func=lambda call: call.data.startswith('edit_category_'))
def start_category_editing(call):
if not Admins.is_authenticated(call.from_user.id):
bot.answer_callback_query(call.id, "⛔ Unauthorized access!")
bot.send_message(call.message.chat.id, "Please /login first.")
return
category_id = call.data.split('_')[2]
category = Categories.getCategorieById(category_id)
@@ -150,6 +154,11 @@ def register(bot: TeleBot):
# Deleting a Category
@bot.callback_query_handler(func=lambda call: call.data.startswith('delete_category_'))
def delete_category(call):
if not Admins.is_authenticated(call.from_user.id):
bot.answer_callback_query(call.id, "⛔ Unauthorized access!")
bot.send_message(call.message.chat.id, "Please /login first.")
return
category_id = call.data.split('_')[2]
if (Categories.deleteCategory(category_id)):
bot.send_message(call.message.chat.id, "✅ Category deleted.",
+12 -2
View File
@@ -1,5 +1,5 @@
from telebot import TeleBot, types
from database.models import Courses
from database.models import Courses, Admins
from bot.utils.crud_helpers import create_entity_markup
from bot.utils.formatters import format_course_info
from bot.handlers.start import startMarkup
@@ -38,7 +38,7 @@ def register(bot: TeleBot):
return
details = format_course_info(course)
markup = create_entity_markup("course", course_id)
markup = create_entity_markup("course", course_id, True)
markup.add(types.InlineKeyboardButton(
f"👨‍🏫 Teacher's Info", callback_data=f"teacher_{course[3]}"))
@@ -93,6 +93,11 @@ def register(bot: TeleBot):
# Editing a Course Flow
@bot.callback_query_handler(func=lambda call: call.data.startswith('edit_course_'))
def start_course_editing(call):
if not Admins.is_authenticated(call.from_user.id):
bot.answer_callback_query(call.id, "⛔ Unauthorized access!")
bot.send_message(call.message.chat.id, "Please /login first.")
return
course_id = call.data.split('_')[2]
course = Courses.getCourseById(course_id)
@@ -149,6 +154,11 @@ def register(bot: TeleBot):
# Deleting a Teacher
@bot.callback_query_handler(func=lambda call: call.data.startswith('delete_course_'))
def delete_course(call):
if not Admins.is_authenticated(call.from_user.id):
bot.answer_callback_query(call.id, "⛔ Unauthorized access!")
bot.send_message(call.message.chat.id, "Please /login first.")
return
course_id = call.data.split('_')[2]
if (Courses.deleteCourse(course_id)):
bot.send_message(call.message.chat.id, "✅ Course deleted.",
+22 -2
View File
@@ -1,5 +1,5 @@
from telebot import types, TeleBot
from database.models import Students
from database.models import Students, Admins
from bot.utils.formatters import format_student_info
from bot.utils.crud_helpers import create_entity_markup
from bot.handlers.start import startMarkup
@@ -30,12 +30,17 @@ def register(bot: TeleBot):
# Showing details of a Student
@bot.callback_query_handler(func=lambda call: call.data.startswith('student_'))
def show_student_details(call):
if not Admins.is_authenticated(call.from_user.id):
bot.answer_callback_query(call.id, "⛔ Unauthorized access!")
bot.send_message(call.message.chat.id, "Please /login first.")
return
student_id = call.data.split('_')[1]
student = Students.getStudentById(student_id)
if student:
details = format_student_info(student)
markup = create_entity_markup("student", student_id)
markup = create_entity_markup("student", student_id, True)
bot.send_message(call.message.chat.id, details,
reply_markup=markup, parse_mode="HTML")
@@ -47,6 +52,11 @@ def register(bot: TeleBot):
# Creating a Student Flow
@bot.callback_query_handler(func=lambda call: call.data == 'create_student')
def create_student(call):
if not Admins.is_authenticated(call.from_user.id):
bot.answer_callback_query(call.id, "⛔ Unauthorized access!")
bot.send_message(call.message.chat.id, "Please /login first.")
return
msg = bot.send_message(call.message.chat.id,
"Please enter following data: (enter any key to start)",
reply_markup=cancelMarkup)
@@ -90,6 +100,11 @@ def register(bot: TeleBot):
# Editing a Student Flow
@bot.callback_query_handler(func=lambda call: call.data.startswith('edit_student_'))
def start_student_editing(call):
if not Admins.is_authenticated(call.from_user.id):
bot.answer_callback_query(call.id, "⛔ Unauthorized access!")
bot.send_message(call.message.chat.id, "Please /login first.")
return
student_id = call.data.split('_')[2]
student = Students.getStudentById(student_id)
@@ -146,6 +161,11 @@ def register(bot: TeleBot):
# Deleting a Teacher
@bot.callback_query_handler(func=lambda call: call.data.startswith('delete_student_'))
def delete_student(call):
if not Admins.is_authenticated(call.from_user.id):
bot.answer_callback_query(call.id, "⛔ Unauthorized access!")
bot.send_message(call.message.chat.id, "Please /login first.")
return
student_id = call.data.split('_')[2]
if (Students.deleteStudent(student_id)):
bot.send_message(call.message.chat.id, "✅ Student deleted.",
+12 -3
View File
@@ -1,5 +1,5 @@
from telebot import types, TeleBot
from database.models import Tags
from database.models import Tags, Admins
from bot.handlers.start import startMarkup
from bot.utils.crud_helpers import create_entity_markup
@@ -34,7 +34,7 @@ def register(bot: TeleBot):
<b>slug:</b> {tag[2]}
"""
details.strip()
markup = create_entity_markup("tag", tag_id)
markup = create_entity_markup("tag", tag_id, True)
bot.send_message(call.message.chat.id, details,
reply_markup=markup, parse_mode="HTML")
@@ -87,9 +87,13 @@ def register(bot: TeleBot):
message.chat.id, "❌ Failed to create tag.", reply_markup=startMarkup())
# Editing a Tag Flow
@bot.callback_query_handler(func=lambda call: call.data.startswith('edit_tag_'))
def start_tag_editing(call):
if not Admins.is_authenticated(call.from_user.id):
bot.answer_callback_query(call.id, "⛔ Unauthorized access!")
bot.send_message(call.message.chat.id, "Please /login first.")
return
tag_id = call.data.split('_')[2]
tag = Tags.getTagById(tag_id)
@@ -146,6 +150,11 @@ def register(bot: TeleBot):
# Deleting a Tag
@bot.callback_query_handler(func=lambda call: call.data.startswith('delete_tag_'))
def delete_tag(call):
if not Admins.is_authenticated(call.from_user.id):
bot.answer_callback_query(call.id, "⛔ Unauthorized access!")
bot.send_message(call.message.chat.id, "Please /login first.")
return
tag_id = call.data.split('_')[2]
if (Tags.deleteTag(tag_id)):
bot.send_message(call.message.chat.id, "✅ Tag deleted.",
+18 -4
View File
@@ -1,12 +1,11 @@
from telebot import types, TeleBot
from database.models import Teachers
from database.models import Teachers, Admins
from bot.utils.formatters import format_teacher_info
from bot.handlers.start import startMarkup
from bot.utils.crud_helpers import create_entity_markup
# TODO: Add Buttons for skipping Optional Fields (Add to all entities).
# TODO: Add Option for handling all updates at once.
# TODO: Add Authentication for sensitive actions and info.
# TODO: Add Option for handling all updates at once.\
# TODO: Add Option for seeing courses taught by a teacher in Teacher Details.
@@ -45,7 +44,7 @@ def register(bot: TeleBot):
if teacher:
details = format_teacher_info(teacher)
markup = create_entity_markup("teacher", teacher_id)
markup = create_entity_markup("teacher", teacher_id, True)
bot.send_message(call.message.chat.id, details,
reply_markup=markup, parse_mode="HTML")
@@ -57,6 +56,11 @@ def register(bot: TeleBot):
# Creating a Teacher Flow
@bot.callback_query_handler(func=lambda call: call.data == 'create_teacher')
def start_teacher_creation(call):
if not Admins.is_authenticated(call.from_user.id):
bot.answer_callback_query(call.id, "⛔ Unauthorized access!")
bot.send_message(call.message.chat.id, "Please /login first.")
return
msg = bot.send_message(call.message.chat.id,
"Please enter following data: (enter any key to start)",
reply_markup=cancelMarkup)
@@ -100,6 +104,11 @@ def register(bot: TeleBot):
# Editing a Teacher Flow
@bot.callback_query_handler(func=lambda call: call.data.startswith('edit_teacher_'))
def start_teacher_editing(call):
if not Admins.is_authenticated(call.from_user.id):
bot.answer_callback_query(call.id, "⛔ Unauthorized access!")
bot.send_message(call.message.chat.id, "Please /login first.")
return
teacher_id = call.data.split('_')[2]
teacher = Teachers.getTeacherById(teacher_id)
@@ -156,6 +165,11 @@ def register(bot: TeleBot):
# Deleting a Teacher
@bot.callback_query_handler(func=lambda call: call.data.startswith('delete_teacher_'))
def delete_teacher(call):
if not Admins.is_authenticated(call.from_user.id):
bot.answer_callback_query(call.id, "⛔ Unauthorized access!")
bot.send_message(call.message.chat.id, "Please /login first.")
return
teacher_id = call.data.split('_')[2]
if (Teachers.deleteTeacher(teacher_id)):
bot.send_message(call.message.chat.id, "✅ Teacher deleted.",