我不明白为什么 callback_query_handler 第一次有效,第二次无效。

huangapple go评论65阅读模式
英文:

I don't understand why the callback_query_handler works the first time and doesn't work the second time

问题

在第一个函数中,一切都正常工作,但在第二个函数中,在发送带有按钮的消息后,没有进一步的操作。按钮不会发送回调数据。

    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("🔔 Send 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="Announcement will be sent silently.", reply_markup=None)
                notification = False
                send_announcement(message, announcement, photo, notification)

我已经为您提供了代码的翻译,没有其他内容。如果您需要进一步的帮助,请告诉我。

英文:

In the first function, everything works correctly, but in the second function, after sending a message with buttons, nothing happens further. The buttons do not send the callback data.

    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)

I've tried swapping functions, putting code in another function, changing variables, etc.

The code should set the value of the notification variable depending on which button the user presses.

答案1

得分: 0

问题在于您将回调查询处理程序定义在函数get_announcement和get_notification_preference内部。您需要在全局范围内定义它。

英文:

The issue is that you are defining the callback query handlers inside the functions get_announcement and get_notification_preference.
You need to define it globally.

huangapple
  • 本文由 发表于 2023年2月27日 12:49:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576859.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定