Я не понимаю, почему callback_query_handler работает в первый раз и не работает во второй.Python

Программы на Python
Ответить
Anonymous
 Я не понимаю, почему callback_query_handler работает в первый раз и не работает во второй.

Сообщение Anonymous »

В первой функции все работает корректно, а во второй после отправки сообщения кнопками дальше ничего не происходит. Кнопки не отправляют данные обратного вызова.

Код: Выделить всё

    def get_announcement(message):

announcement = message.text
if not announcement:
bot.send_message(chat_id=message.chat.id, text="You have sent an empty message. Sending an announcement has been aborted because you cannot send an empty message.")
return

markup = types.InlineKeyboardMarkup(row_width=2)
item1 = types.InlineKeyboardButton("📎 Attach photo", callback_data='attach_photo')
item2 = types.InlineKeyboardButton("➡️ Skip", callback_data='skip')
markup.add(item1, item2)

bot.send_message(chat_id=message.chat.id, text="Do you want to attach a photo?", reply_markup=markup)

@bot.callback_query_handler(func=lambda call: True)
def handle_callback(call):
if call.data == "attach_photo":
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.id, text="Send the photo you want to attach.", reply_markup=None)
bot.register_next_step_handler(message, lambda message: get_photo(message, announcement))
elif call.data == "skip":
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.id, text="Announcement will be sent without a photo.", reply_markup=None)
photo=None
get_notification_preference(message, announcement, photo)

def get_notification_preference(message, announcement, photo):

markup = types.InlineKeyboardMarkup(row_width=2)
item1 = types.InlineKeyboardButton("🔈 Sand with notification", callback_data="notification_on")
item2 = types.InlineKeyboardButton("🔇 Send silently", callback_data="notification_off")
markup.add(item1, item2)

bot.send_message(chat_id=message.chat.id, text="How to send an announcement?", reply_markup=markup)

@bot.callback_query_handler(func=lambda call: True)
def handle_callback(call):
if call.data == "notification_on":
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.id, text="Announcement will be sent with notification", reply_markup=None)
notification = True
send_announcement(message, announcement, photo, notification)
elif call.data == "notification_off":
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.id, text="Аnnouncement will be sent silently.", reply_markup=None)
notification = False
send_announcement(message, announcement, photo, notification)
Я пробовал менять функции, помещать код в другую функцию, изменять переменные и т. д.
Код должен установить значение переменной уведомления в зависимости от того, какую кнопку нажимает пользователь.

Подробнее здесь: https://stackoverflow.com/questions/755 ... -and-doesn
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Python»