Transform your simple scheduler into a robust, user-friendly application
A complete scheduler bot MVP with:
In your current code, Period_7 has wrong times:
Also missing Periods 8-13 from your CSV template!
For our scheduler, SQLite is better because:
PERIOD_TIMES = {
'Period_1': ('09:00', '09:40'),
'Period_2': ('10:00', '10:40'),
'Period_3': ('11:00', '11:40'),
'Period_4': ('11:50', '12:30'),
'Period_5': ('12:40', '13:20'),
'Period_6': ('13:30', '14:10'),
'Period_7': ('10:00', '10:40'), # WRONG!
}
PERIODS = [
(1, "09:00-09:40"),
(2, "10:00-10:40"),
(3, "11:00-11:40"),
(4, "11:50-12:30"),
(5, "12:40-13:20"),
(6, "13:30-14:10"),
(7, "14:20-15:00"),
(8, "15:20-16:00"),
(9, "16:15-16:55"),
(10, "17:05-17:45"),
(11, "17:55-18:35"),
(12, "18:45-19:20"),
(13, "19:20-20:00")
]
Your current Period_7 (10:00-10:40) overlaps with Period_2! This causes wrong class detection.
The correct times match your CSV template with all 13 periods.
Add this database class to your code:
import sqlite3
import datetime
class SimpleDatabase:
def __init__(self, db_file="schedule_bot.db"):
self.db_file = db_file
self.init_database()
def init_database(self):
"""Initialize database with tables"""
conn = sqlite3.connect(self.db_file)
cursor = conn.cursor()
# Users table
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
user_id INTEGER PRIMARY KEY,
username TEXT,
first_name TEXT,
last_name TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Classes table - one row per class entry
cursor.execute('''
CREATE TABLE IF NOT EXISTS classes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER,
day TEXT,
period INTEGER,
period_time TEXT,
subject TEXT,
class_group TEXT,
room TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users (user_id)
)
''')
conn.commit()
conn.close()
Replace your simple commands with an interactive setup:
from telegram.ext import ConversationHandler
from telegram import ReplyKeyboardMarkup, KeyboardButton
# Conversation states
SELECT_DAY, SELECT_PERIOD, ENTER_SUBJECT, ENTER_GROUP, ENTER_ROOM = range(5)
async def setup_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Start the setup conversation"""
await update.message.reply_text(
"๐
Let's add a class to your schedule.\n"
"First, select the day:",
reply_markup=ReplyKeyboardMarkup(
[["Monday"], ["Tuesday"], ["Wednesday"],
["Thursday"], ["Friday"], ["Cancel"]],
resize_keyboard=True, one_time_keyboard=True
)
)
return SELECT_DAY
async def where_am_i(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Enhanced: Show current OR next class"""
user_id = update.effective_user.id
now = datetime.datetime.now()
current_day = now.strftime("%A")
current_time = now.strftime("%H:%M")
# Get current class from database
current_class = db.get_current_class(user_id, current_day, current_time)
if current_class:
response = f"๐ฏ You should be in class now!\n๐ {current_class['subject']}"
else:
# Find next class
next_class = db.get_next_class(user_id, current_day, current_time)
if next_class:
response = f"โ
Free period!\nโก๏ธ Next: {next_class['subject']} at {next_class['time']}"
else:
response = f"๐ No more classes today!"
import logging
# Setup logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
logger = logging.getLogger(__name__)
def save_class(user_id, day, period, subject, group, room):
"""Save class with error handling"""
try:
if not all([day, period, subject]):
raise ValueError("Missing required fields")
db.save_class(user_id, day, period, subject, group, room)
logger.info(f"Class saved for user {user_id}")
return True
except Exception as e:
logger.error(f"Error saving class: {e}")
return False
# Missing file or minimal content
python-telegram-bot==20.3
python-dotenv==1.0.0
pandas==2.1.0 # Optional for CSV import
scheduler_bot_mvp/
โโโ scheduler_bot.py # Main bot file
โโโ requirements.txt # Dependencies
โโโ .env # Environment variables
โโโ schedule_bot.db # SQLite database (auto-created)
โโโ simple_database.py # Database class (optional)
โโโ README.md # Documentation