PTBUserWarning: If 'permessage=False'… · The Chat Archive
Explore o1-preview-2024-09-12 · OpenAI WildChat-4.8M (AI2)PTBUserWarning: If 'permessage=False', 'CallbackQueryHandler' will not be tracked for… Save Share Download ▾ ReportPTBUserWarning: If 'per_message=False', 'CallbackQueryHandler' will not be tracked for every message. Read this FAQ entry to learn more about the per_* settings: https://github.com/python-telegram-bot/python-telegram-bot/wiki/Frequently-Asked-Questions#what-do-the-per_-settings-in-conversationhandler-do .
project_conv_handler = ConversationHandler(
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
)
Assistant2024-09-19 10:44 UTC Copy response
Copy first prompt
Details
Model o1-preview-2024-09-12
Provider OpenAI
Model provenance As recorded by source dataset
Turns 1 (2 messages)
Language en
Conversation date Sep 19, 2024
Added to archive Nov 16, 2026
ID 01m7h41vbjp8ck7ykh34sfmy9jSource & license Imported from WildChat-4.8M (AI2) (source ). Imported records earn no Research Points.
Upstream record: 0979d0412cc57edbf5f02c7a4c719a17
Zhao et al., WildChat: 1M ChatGPT Interaction Logs in the Wild (ICLR 2024). Contains information from WildChat-4.8M, made available under the ODC Attribution License.
License: ODC-BY-1.0
Dataset releases Not yet included in a published release.
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('_')[1]
selected_date = datetime.strptime(date_str, '%Y-%m-%d').date()
user_state[user_id]['selected_date'] = selected_date
schedule = fetch_schedule(selected_date.strftime('%Y-%m-%d'))
if not schedule:
await query.edit_message_text(
text=f"На выбранную дату {selected_date.strftime('%d.%m.%Y')} нет занятий.",
reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton("Добавить домашнее задание", callback_data='add_homework')
]])
)
return
homework_subjects = [{'subject': entry[1], 'time': entry[0]} for entry in schedule]
user_state[user_id]['homework_subjects'] = homework_subjects
keyboard = [
[InlineKeyboardButton(f"{entry['subject']} ({entry['time']})", callback_data=f'subject_{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('_')
if len(data) < 2:
await query.edit_message_text(
text="Некорректный выбор предмета.",
reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton("Добавить домашнее задание", callback_data='add_homework')
]])
)
return
index = int(data[1])
homework_subjects = user_state[user_id].get('homework_subjects', [])
if index >= len(homework_subjects):
await query.edit_message_text(
text="Некорректный выбор предмета.",
reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton("Добавить домашнее задание", callback_data='add_homework')
]])
)
return
selected_entry = homework_subjects[index]
subject = selected_entry['subject']
time_slot = selected_entry['time']
user_state[user_id]['selected_subject'] = subject
user_state[user_id]['selected_time'] = time_slot
await query.edit_message_text(
text=f"Вы выбрали предмет: {subject} ({time_slot}). Введите домашнее задание."
)
context.chat_data['awaiting_homework'] = True
async def handle_homework_text(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
if context.chat_data.get('awaiting_homework'):
homework = update.message.text
date = user_state[user_id].get('selected_date')
time_slot = user_state[user_id].get('selected_time')
if date and time_slot:
update_homework(date.strftime('%Y-%m-%d'), time_slot, homework)
await update.message.reply_text(f"Домашнее задание для {date.strftime('%d.%m.%Y')} {time_slot}:\n{homework}")
await update.message.reply_text("Домашнее задание добавлено.")
# Очистить состояние ожидания
context.chat_data['awaiting_homework'] = False
del user_state[user_id]['selected_date']
del user_state[user_id]['selected_subject']
del user_state[user_id]['selected_time']
# После добавления домашнего задания показать расписание
await send_schedule(update, context, user_id)
else:
await update.message.reply_text("Ошибка. Выберите дату и предмет.")
else:
await update.message.reply_text("Вы не находитесь в процессе добавления домашнего задания.")
async def cancel(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 'selected_date' in state or 'selected_subject' in state:
# Удалить состояние выбора даты и предмета
state.pop('selected_date', None)
state.pop('selected_subject', None)
state.pop('selected_time', None)
await query.edit_message_text(
text="Процесс добавления домашнего задания отменен.",
reply_markup=InlineKeyboardMarkup([[
InlineKeyboardButton("Добавить домашнее задание", callback_data='add_homework')
]])
)
await send_schedule(update, context, user_id)
else:
await query.edit_message_text(
text="Нет активного процесса добавления домашнего задания."
)
await send_schedule(update, context, user_id)
async def projects_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
keyboard = [
[InlineKeyboardButton("Просмотреть проекты", callback_data='projects_view')],
[InlineKeyboardButton("Добавить проект", callback_data='projects_add')],
[InlineKeyboardButton("Назад", callback_data='back_to_schedule')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
await query.edit_message_text(
text="Меню проектов:",
reply_markup=reply_markup
)
async def projects_button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
data = query.data
if data == 'projects_view':
await show_projects(update, context)
elif data == 'projects_add':
await add_project_start(update, context)
return PROJECT_SUBJECT # Начало разговора по добавлению проекта
elif data == 'back_to_schedule':
user_id = query.from_user.id
await send_schedule(update, context, user_id)
elif data.startswith('project_view_'):
project_id = int(data.split('_')[-1])
await view_project(update, context, project_id)
elif data.startswith('project_download_'):
project_id = int(data.split('_')[-1])
await download_project_file(update, context, project_id)
else:
await query.edit_message_text("Неизвестная команда.")
async def show_projects(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
projects = fetch_projects()
if not projects:
await query.edit_message_text("Проекты не найдены.", reply_markup=InlineKeyboardMarkup([
[InlineKeyboardButton("Назад", callback_data='projects_menu')]
]))
else:
text = "Список проектов:\n"
keyboard = []
for project in projects:
project_id, subject, project_name, description, file_path = project
text += f"{project_id}. {project_name} ({subject})\n"
keyboard.append([InlineKeyboardButton(f"{project_name}", callback_data=f'project_view_{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$'))
# Обработчик проекта
project_conv_handler = ConversationHandler(
entry_points=[CallbackQueryHandler(add_project_start, pattern='^projects_add$')],
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)]
)
application.add_handler(project_conv_handler)
# Общий обработчик кнопок
application.add_handler(CallbackQueryHandler(projects_button_handler, pattern='^projects_'))
application.add_handler(CallbackQueryHandler(button))
# Обработчик текстовых сообщений
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_homework_text))
application.run_polling()
if name == 'main ':
main()]
Hello!
The warning you're encountering is:
PTBUserWarning: If 'per_message=False', 'CallbackQueryHandler' will not be tracked for every message. Read this FAQ entry to learn more about the per_* settings: https://github.com/python-telegram-bot/python-telegram-bot/wiki/Frequently-Asked-Questions#what-do-the-per_-settings-in-conversationhandler-do.
This warning is generated by the python-telegram-bot library to inform you about how the ConversationHandler processes updates when you have per_message=False. Let's break down what's happening and how to resolve this warning.
Understanding the Warning
In your code, you're using a ConversationHandler to manage a conversation flow for adding projects:
project_conv_handler = ConversationHandler(
entry_points=[CallbackQueryHandler(add_project_start, pattern='^projects_add$')],
states={
# your conversation states and handlers
},
fallbacks=[CommandHandler('cancel', cancel_project_addition)]
)
By default, ConversationHandler uses the following parameters:
per_chat=True
per_user=False
per_message=False
The per_message=False setting means that the conversation is tracked per chat (or per user if per_user=True) and not per individual message. As a result, ConversationHandler will only process message updates (Update.message) and not other types of updates like callback queries (Update.callback_query).
Since you're using CallbackQueryHandler both as an entry point and within the conversation states, the ConversationHandler with per_message=False won't process those callback queries as part of the conversation. This is why the warning suggests that the CallbackQueryHandler will not be tracked for every message.
How to Resolve the Warning
To ensure that your ConversationHandler processes all types of updates (messages, callback queries, etc.), you need to set per_message=True. This tells the ConversationHandler to evaluate each update individually and not group them per chat or per user.
Here's how you can modify your ConversationHandler to include per_message=True:
project_conv_handler = ConversationHandler(
entry_points=[CallbackQueryHandler(add_project_start, pattern='^projects_add$')],
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
)
By adding per_message=True, the ConversationHandler will handle each incoming update individually, including CallbackQuery updates from button presses, which should eliminate the warning and allow your handlers to function as intended.
Important Considerations
State Management : With per_message=True, the conversation state is managed for each message separately. This might affect how your conversation flows, especially if you rely on the conversation state to be consistent across multiple messages.
Per User or Per Chat : Ensure that managing the conversation per message aligns with your application's logic. If you need the conversation to be tracked per user or per chat, you may need to adjust per_user or per_chat parameters accordingly.
Understanding per_* : I highly recommend reading the FAQ entry on per_* settings to fully understand how these settings impact your conversation handling.
Alternative Solution
If you prefer to keep per_message=False (the default), you can restructure your conversation to avoid needing to process CallbackQuery updates within the ConversationHandler. For example:
Use message commands or text inputs as entry points and states within your ConversationHandler.
Handle CallbackQuery updates outside the ConversationHandler, possibly by setting or checking a flag in context.user_data or context.chat_data.
However, given your current implementation, setting per_message=True is the straightforward solution.
Summary
Add per_message=True to your ConversationHandler initialization.
Be aware of how per_message=True changes the conversation tracking.
Verify that your conversation logic still works as expected after the change.
Read the FAQ entry to understand the implications of the per_* settings.
By making this change, the warning should disappear, and your CallbackQueryHandlers within the ConversationHandler should function correctly.
Feel free to ask if you have further questions or need assistance with any other issues!
Copy first response