added creating teacher functionality

This commit is contained in:
Hosein
2025-12-30 20:47:37 +03:30
parent a52d62c0fc
commit af88cbce8f
4 changed files with 123 additions and 5 deletions
+92 -2
View File
@@ -1,9 +1,14 @@
from telebot import types
from telebot import types, TeleBot
from database.models import Teachers
from bot.utils.formatters import format_teacher_info
def register(bot):
def register(bot: TeleBot):
cancelMarkup = types.InlineKeyboardMarkup()
cancelMarkup.add(types.InlineKeyboardButton(
"Cancel", callback_data="cancel"))
# Showing details of a Teacher
@bot.callback_query_handler(func=lambda call: call.data.startswith('teacher_'))
def show_teacher_details(call):
teacher_id = call.data.split('_')[1]
@@ -18,3 +23,88 @@ def register(bot):
bot.send_message(call.message.chat.id, "Teacher not found.")
bot.answer_callback_query(call.id)
# Creating a Teacher Flow
@bot.callback_query_handler(func=lambda call: call.data == 'create_teacher')
def start_teacher_creation(call):
msg = bot.send_message(call.message.chat.id,
"Please enter the teacher's name:", reply_markup=cancelMarkup)
bot.register_next_step_handler(
msg, process_name_step)
bot.answer_callback_query(call.id)
def process_name_step(message):
name = message.text
msg = bot.send_message(
message.chat.id, f"Name: {name}\n\nNow enter email:", reply_markup=cancelMarkup)
bot.register_next_step_handler(msg, process_email_step, name)
def process_email_step(message, name):
email = message.text
msg = bot.send_message(
message.chat.id, f"Email: {email}\n\nNow enter phone: (<Optional>)", reply_markup=cancelMarkup)
bot.register_next_step_handler(msg, process_phone_step, name, email)
def process_phone_step(message, name, email):
phone = message.text
msg = bot.send_message(
message.chat.id, f"Phone: {phone}\n\nNow enter password: ", reply_markup=cancelMarkup)
bot.register_next_step_handler(
msg, process_password_step, name, email, phone)
def process_password_step(message, name, email, phone):
password = message.text
msg = bot.send_message(
message.chat.id, f"password: {password}\n\nNow enter username:", reply_markup=cancelMarkup)
bot.register_next_step_handler(
msg, process_username_step, name, email, phone, password)
def process_username_step(message, name, email, phone, password):
username = message.text
msg = bot.send_message(
message.chat.id, f"username: {username}\n\nNow enter birthday: (YY/MM/DD)", reply_markup=cancelMarkup)
bot.register_next_step_handler(
msg, process_birthday_step, name, email, phone, password, username)
def process_birthday_step(message, name, email, phone, password, username):
birthday = message.text
msg = bot.send_message(
message.chat.id, f"birthday: {birthday}\n\nNow enter about me: ", reply_markup=cancelMarkup)
bot.register_next_step_handler(
msg, process_aboutme_step, name, email, phone, password, username, birthday)
def process_aboutme_step(message, name, email, phone, password, username, birthday):
about_me = message.text
msg = bot.send_message(
message.chat.id, f"about me: {about_me}\n\nNow enter job title: ", reply_markup=cancelMarkup)
bot.register_next_step_handler(
msg, process_jobtitle_step, name, email, phone, password, username, birthday, about_me)
def process_jobtitle_step(message, name, email, phone, password, username, birthday, about_me):
job_title = message.text
msg = bot.send_message(message.chat.id, "is this correct? (enter any key) (Use Cancel to Stop creating)\n"
f"Name: {name}\n"
f"Email: {email}\n"
f"Phone: {phone}\n"
f"Password: {password}\n"
f"Birthday: {birthday}\n"
f"About Me: {about_me}\n"
f"Job Title: {job_title}",
reply_markup=cancelMarkup)
bot.register_next_step_handler(
msg, confirm_teacher_creation, name, email, phone, password, username, birthday, about_me, job_title)
def confirm_teacher_creation(message, name, email, phone, password, username, birthday, about_me, job_title):
if (Teachers.createTeacher(name=name, email=email, phone_number=phone,
password=password, username=username, birthday=birthday,
about_me=about_me, job_title=job_title)):
bot.send_message(message.chat.id, "✅ Teacher created!")
else:
bot.send_message(message.chat.id, "❌ Failed to create teacher.")
@bot.callback_query_handler(func=lambda call: call.data == 'cancel')
def cancel_action(call):
bot.clear_step_handler(call.message)
bot.send_message(call.message.chat.id, "Action cancelled.")
bot.answer_callback_query(call.id)
+5 -1
View File
@@ -12,11 +12,15 @@ def register(bot):
data = Teachers.getAllTeachers()
if data:
markup = types.InlineKeyboardMarkup(row_width=2)
markup = types.InlineKeyboardMarkup(row_width=1)
for row in data:
btn = types.InlineKeyboardButton(
f"@{row[1]} | id#{row[0]}", callback_data=f"teacher_{row[0]}")
markup.add(btn)
# add button for creating a new teacher
markup.add(types.InlineKeyboardButton(
" Create New Teacher", callback_data="create_teacher"))
bot.send_message(
message.chat.id, "Here is the data:", reply_markup=markup)
else:
+1 -1
View File
@@ -89,7 +89,7 @@ def format_teacher_info(teacher):
age = calculate_age(birthday)
details = f"""
<b>👤 Student Profile</b>
<b>👤 Teacher Profile</b>
<b>Name:</b> {name}
<b>Username:</b> {username}
+25 -1
View File
@@ -77,7 +77,7 @@ class Teachers:
cursor = conn.cursor()
cursor.execute(
"SELECT id,username, name, created_at, email, phone_number, last_seen, is_verfied, birthday, about_me, job_title FROM teachers WHERE id = %s", (teacher_id))
"SELECT id, username, name, created_at, email, phone_number, last_seen, is_verfied, birthday, about_me, job_title FROM teachers WHERE id = %s", (teacher_id, ))
result = cursor.fetchall()
cursor.close()
@@ -88,3 +88,27 @@ class Teachers:
finally:
if conn:
connection.release_db_connection(conn)
def createTeacher(name, email, phone_number, password, username, birthday, about_me, job_title):
conn = None
try:
conn = connection.get_db_connection()
if not conn:
return False
cursor = conn.cursor()
cursor.execute(
"INSERT INTO teachers (name, email, phone_number, hashed_password, username, birthday, about_me, job_title) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
(name, email, phone_number, password,
username, birthday, about_me, job_title)
)
conn.commit()
cursor.close()
return True
except Exception as e:
logger.error(f"Database query error in createTeacher: {e}")
return False
finally:
if conn:
connection.release_db_connection(conn)