- Введите название предмета для проекта: - Физра - Вы не находитесь в процессе…
- User
- Введите название предмета для проекта: - Физра - Вы не находитесь в процессе добавления домашнего задания. main.py: [from datetime import datetime, timedelta import locale import os from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup from telegram.ext import ( ApplicationBuilder, CommandHandler, CallbackQueryHandler, ContextTypes, MessageHandler, filters, ConversationHandler ) from db import create_db, fetch_schedule, update_homework, insert_project, fetch_projects, fetch_project # Инициализация бота TOKEN = '7895405887:AAHPHCh8SEm8th-l3E2gajYh0-lSB5XkBlQ' # Создать базу данных и таблицы create_db() user_state = {} PROJECT_SUBJECT, PROJECT_NAME, PROJECT_DESCRIPTION, PROJECT_FILE = range(4) locale.setlocale(locale.LC_TIME, 'ru_RU.UTF-8') def format_schedule(date, schedule): day_name = date.strftime('%A') formatted = f"Расписание на {date.strftime('%d.%m.%Y')}, {day_name}\n\n" for entry in schedule: formatted += f"{entry[0]}\n" \ f"{entry[1]}\n" \ f"Преподаватель: {entry[2]}\n" \ f"Место: {entry[3]}\n" \ f"Домашнее задание: {entry[4]}\n\n" return formatted async def start(update: Update, context: ContextTypes.DEFAULT_TYPE): user_id = update.effective_user.id today = datetime.now().date() user_state[user_id] = { 'view': 'week', 'current_date': today } await send_schedule(update, context, user_id) async def send_schedule(update: Update, context: ContextTypes.DEFAULT_TYPE, user_id): state = user_state[user_id] view = state['view'] current_date = state['current_date'] if view == 'day': schedule = fetch_schedule(current_date.strftime('%Y-%m-%d')) text = format_schedule(current_date, schedule) elif view == 'week': start_of_week = current_date - timedelta(days=current_date.weekday()) end_of_week = start_of_week + timedelta(days=6) text = f"Расписание с {start_of_week.strftime('%d.%m.%Y')} по {end_of_week.strftime('%d.%m.%Y')}\n\n" for single_date in (start_of_week + timedelta(n) for n in range(7)): schedule = fetch_schedule(single_date.strftime('%Y-%m-%d')) day_name = single_date.strftime('%A') # Убедитесь, что локаль установлена на русский text += f"• {single_date.strftime('%d.%m.%Y')}, {day_name}\n" if schedule: for entry in schedule: time, subject, teacher, location, homework = entry homework_text = f"\nДомашнее задание: {homework}" if homework else "" text += f" - {time} {subject} ({teacher}){homework_text}\n" else: text += " - Нет занятий.\n" text += "\n" text = text.strip() else: text = "Неизвестный вид отображения расписания." keyboard = [ [ InlineKeyboardButton("Неделя", callback_data='view_week'), InlineKeyboardButton("День", callback_data='view_day') ], [ InlineKeyboardButton("Предыдущий", callback_data='prev'), InlineKeyboardButton("Следующий", callback_data='next') ], [ InlineKeyboardButton("Добавить домашнее задание", callback_data='add_homework') ], [ InlineKeyboardButton("Проекты", callback_data='projects_menu') ] ] reply_markup = InlineKeyboardMarkup(keyboard) if update.callback_query: await update.callback_query.edit_message_text( text=text, parse_mode='Markdown', reply_markup=reply_markup ) else: await update.message.reply_text( text=text, parse_mode='Markdown', reply_markup=reply_markup ) async def button(update: Update, context: ContextTypes.DEFAULT_TYPE): query = update.callback_query await query.answer() user_id = query.from_user.id state = user_state.get(user_id, {}) if not state: state = { 'view': 'week', 'current_date': datetime.now().date() } user_state[user_id] = state if query.data == 'view_week': state['view'] = 'week' state['current_date'] = datetime.now().date() await send_schedule(update, context, user_id) elif query.data == 'view_day': state['view'] = 'day' state['current_date'] = datetime.now().date() await send_schedule(update, context, user_id) elif query.data == 'prev': if state['view'] == 'day': state['current_date'] -= timedelta(days=1) elif state['view'] == 'week': state['current_date'] -= timedelta(weeks=1) await send_schedule(update, context, user_id) elif query.data == 'next': if state['view'] == 'day': state['current_date'] += timedelta(days=1) elif state['view'] == 'week': state['current_date'] += timedelta(weeks=1) await send_schedule(update, context, user_id) elif query.data == 'add_homework': await send_homework_selection(update, context, user_id) elif query.data == 'projects_menu': await projects_start(update, context) else: await query.edit_message_text("Неизвестная команда.") async def send_homework_selection(update: Update, context: ContextTypes.DEFAULT_TYPE, user_id): state = user_state[user_id] today = datetime.now().date() start_of_week = today - timedelta(days=today.weekday()) dates = [(start_of_week + timedelta(days=i)) for i in range(7)] keyboard = [] for date in dates: date_str_display = date.strftime('%d.%m.%Y') date_str_callback = date.strftime('%Y-%m-%d') keyboard.append([InlineKeyboardButton(date_str_display, callback_data=f'date_{date_str_callback}')]) keyboard.append([InlineKeyboardButton("Отмена", callback_data='cancel')]) reply_markup = InlineKeyboardMarkup(keyboard) await update.callback_query.edit_message_text( text="Выберите дату:", reply_markup=reply_markup ) async def select_date(update: Update, context: ContextTypes.DEFAULT_TYPE): query = update.callback_query await query.answer() user_id = query.from_user.id date_str = query.data.split('{i}')] for i, entry in enumerate(homework_subjects) ] keyboard.append([InlineKeyboardButton("Отмена", callback_data='cancel')]) reply_markup = InlineKeyboardMarkup(keyboard) await query.edit_message_text( text=f"Вы выбрали дату: {selected_date.strftime('%d.%m.%Y')}. Теперь выберите предмет:", reply_markup=reply_markup ) async def select_subject(update: Update, context: ContextTypes.DEFAULT_TYPE): query = update.callback_query await query.answer() user_id = query.from_user.id data = query.data.split(''): project_id = int(data.split(''): project_id = int(data.split('{project_id}')]) keyboard.append([InlineKeyboardButton("Назад", callback_data='projects_menu')]) reply_markup = InlineKeyboardMarkup(keyboard) await query.edit_message_text(text=text, reply_markup=reply_markup) async def view_project(update: Update, context: ContextTypes.DEFAULT_TYPE, project_id): query = update.callback_query project = fetch_project(project_id) if not project: await query.edit_message_text("Проект не найден.", reply_markup=InlineKeyboardMarkup([ [InlineKeyboardButton("Назад", callback_data='projects_view')] ])) else: project_id, subject, project_name, description, file_path = project text = f"Проект: {project_name}\nПредмет: {subject}\nОписание: {description}" keyboard = [] if file_path: keyboard.append([InlineKeyboardButton("Скачать файл", callback_data=f'project_download_{project_id}')]) keyboard.append([InlineKeyboardButton("Назад", callback_data='projects_view')]) reply_markup = InlineKeyboardMarkup(keyboard) await query.edit_message_text(text=text, reply_markup=reply_markup) async def download_project_file(update: Update, context: ContextTypes.DEFAULT_TYPE, project_id): query = update.callback_query project = fetch_project(project_id) if not project: await query.edit_message_text("Проект не найден.", reply_markup=InlineKeyboardMarkup([ [InlineKeyboardButton("Назад", callback_data='projects_view')] ])) else: file_path = project[4] if not file_path or not os.path.exists(file_path): await query.edit_message_text("Файл для этого проекта не найден.", reply_markup=InlineKeyboardMarkup([ [InlineKeyboardButton("Назад", callback_data='projects_view')] ])) else: await context.bot.send_document(chat_id=query.message.chat_id, document=open(file_path, 'rb')) await query.edit_message_text("Файл отправлен.", reply_markup=InlineKeyboardMarkup([ [InlineKeyboardButton("Назад", callback_data='projects_view')] ])) async def add_project_start(update: Update, context: ContextTypes.DEFAULT_TYPE): await update.callback_query.edit_message_text("Введите название предмета для проекта:") return PROJECT_SUBJECT async def add_project_subject(update: Update, context: ContextTypes.DEFAULT_TYPE): context.user_data['project_subject'] = update.message.text await update.message.reply_text("Введите название проекта:") return PROJECT_NAME async def add_project_name(update: Update, context: ContextTypes.DEFAULT_TYPE): context.user_data['project_name'] = update.message.text await update.message.reply_text("Введите описание проекта:") return PROJECT_DESCRIPTION async def add_project_description(update: Update, context: ContextTypes.DEFAULT_TYPE): context.user_data['project_description'] = update.message.text keyboard = [[InlineKeyboardButton("Пропустить", callback_data='skip_file')]] reply_markup = InlineKeyboardMarkup(keyboard) await update.message.reply_text("Загрузите файл проекта или нажмите 'Пропустить'.", reply_markup=reply_markup) return PROJECT_FILE async def add_project_file(update: Update, context: ContextTypes.DEFAULT_TYPE): document = update.message.document file = await context.bot.get_file(document.file_id) # Создаем директорию files, если ее нет if not os.path.exists('files'): os.makedirs('files') file_path = f"files/{document.file_name}" await file.download_to_drive(file_path) context.user_data['project_file_path'] = file_path # Сохраняем проект insert_project( context.user_data['project_subject'], context.user_data['project_name'], context.user_data['project_description'], context.user_data.get('project_file_path') ) await update.message.reply_text("Проект успешно добавлен.") return ConversationHandler.END async def skip_project_file(update: Update, context: ContextTypes.DEFAULT_TYPE): # Сохраняем проект без файла insert_project( context.user_data['project_subject'], context.user_data['project_name'], context.user_data['project_description'], None ) await update.callback_query.edit_message_text("Проект успешно добавлен без файла.") return ConversationHandler.END async def cancel_project_addition(update: Update, context: ContextTypes.DEFAULT_TYPE): await update.message.reply_text("Добавление проекта отменено.") return ConversationHandler.END def main(): application = ApplicationBuilder().token(TOKEN).build() application.add_handler(CommandHandler("start", start)) # Обработчики домашнего задания application.add_handler(CallbackQueryHandler(select_date, pattern='^date_')) application.add_handler(CallbackQueryHandler(select_subject, pattern='^subject_')) application.add_handler(CallbackQueryHandler(cancel, pattern='^cancel')], states={ PROJECT_SUBJECT: [MessageHandler(filters.TEXT & ~filters.COMMAND, add_project_subject)], PROJECT_NAME: [MessageHandler(filters.TEXT & ~filters.COMMAND, add_project_name)], PROJECT_DESCRIPTION: [MessageHandler(filters.TEXT & ~filters.COMMAND, add_project_description)], PROJECT_FILE: [ MessageHandler(filters.Document.ALL, add_project_file), CallbackQueryHandler(skip_project_file, pattern='^skip_file$') ], }, fallbacks=[CommandHandler('cancel', cancel_project_addition)], per_message=True # Add this line ) application.add_handler(project_conv_handler) # Общий обработчик кнопок application.add_handler(CallbackQueryHandler(projects_button_handler, pattern='^(projects_view|projects_add|project_view_|project_download_|back_to_schedule)')) application.add_handler(CallbackQueryHandler(button)) # Обработчик текстовых сообщений application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_homework_text), group=1) application.run_polling() if == '': main() ]
- Assistant
Проблема возникает из-за использования в вашем для добавления проекта. Опция приводит к тому, что состояние разговора не сохраняется между сообщениями, и каждый новый входящий апдейт рассматривается как начало нового разговора.