脚本完成后,如何将键盘返回其原始状态?

发布于 2025-01-21 02:46:54 字数 3018 浏览 0 评论 0原文

该脚本是电报机器人的一部分。该脚本的任务是为用户提供多种选择。

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (
    Updater,
    CommandHandler,
    CallbackQueryHandler,
    ConversationHandler)

import logging

green_check = '\u2705'
grey_check = '\u2714'

FIRST, ANSWER = range(2)
TEXT_1 = 'Click'
CHECK = grey_check
CLICK = False

KEYS = {'1': (0, 0), '2': (0, 1), '3': (1, 0), '4': (1, 1)}

keyboard = [
[
    InlineKeyboardButton(f'1 {grey_check}', callback_data='1'),
    InlineKeyboardButton(f'2 {grey_check}', callback_data='2'),
],
[
    InlineKeyboardButton(f'3 {grey_check}', callback_data='3'),
    InlineKeyboardButton(f'4 {grey_check}', callback_data='4'),
],
[
    InlineKeyboardButton('Next \u27A1', callback_data='next'),
],
]


def start(update, context):
    CHECK = grey_check
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text(TEXT_1, reply_markup=reply_markup)
    for i in range(1,5):
        context.user_data[f'BUTTON_{i}'] = False

    return FIRST


def clicked(click=None, data=None):
    global keyboard
    global CHECK
    if click == True:
        CHECK = green_check
    else:
        CHECK = grey_check

    keyboard[KEYS[data][0]][KEYS[data][1]] = InlineKeyboardButton(
        f'{data} {CHECK}', callback_data=f'{data}')

    reply_markup = InlineKeyboardMarkup(keyboard)

    return reply_markup


def press(update, context):
    data = update.callback_query.data
    global CLICK
    if context.user_data[f'BUTTON_{data}'] == False:
        context.user_data[f'BUTTON_{data}'] = True
    else:
        context.user_data[f'BUTTON_{data}'] = False

    reply_keyboard = clicked(context.user_data[f'BUTTON_{data}'], data)
    update.callback_query.edit_message_text(text=TEXT_1, reply_markup=reply_keyboard)
    return FIRST


def send(update, context):
    text = ''
    for i in range(1, 5):
        text += f'{i} - ' + str(context.user_data[f'BUTTON_{i}']) + '\n'
    context.user_data[ANSWER] = text
    update.callback_query.edit_message_text(text=context.user_data[ANSWER])
    return ConversationHandler.END


def main():
    updater = Updater(
        'TOKEN', use_context=True)
    dp = updater.dispatcher
    conv_handler = ConversationHandler(
        entry_points=[CommandHandler('start', start)],
        states={
            FIRST: [
                    CallbackQueryHandler(press, pattern='^'+str(1)+'$'),
                    CallbackQueryHandler(press, pattern='^'+str(2)+'$'),
                    CallbackQueryHandler(press, pattern='^'+str(3)+'$'),
                    CallbackQueryHandler(press, pattern='^'+str(4)+'$'),
                    CallbackQueryHandler(send, pattern='^'+'next'+'$'),
                    ],
        },
        fallbacks=[CommandHandler('start', start)]
    )

    dp.add_handler(conv_handler)
    updater.start_polling()
    updater.idle()

if __name__ == "__main__":
    main()

选择该选项后,按钮的颜色变为绿色,发送数据和下次运行 /start 脚本后,键盘不会恢复到原来的形式。用户按下的按钮保持绿色,而按下按钮时数据会按预期发生变化。

This script is part of a telegram bot. The task of this script is to provide the user with a multiple choice of options.

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (
    Updater,
    CommandHandler,
    CallbackQueryHandler,
    ConversationHandler)

import logging

green_check = '\u2705'
grey_check = '\u2714'

FIRST, ANSWER = range(2)
TEXT_1 = 'Click'
CHECK = grey_check
CLICK = False

KEYS = {'1': (0, 0), '2': (0, 1), '3': (1, 0), '4': (1, 1)}

keyboard = [
[
    InlineKeyboardButton(f'1 {grey_check}', callback_data='1'),
    InlineKeyboardButton(f'2 {grey_check}', callback_data='2'),
],
[
    InlineKeyboardButton(f'3 {grey_check}', callback_data='3'),
    InlineKeyboardButton(f'4 {grey_check}', callback_data='4'),
],
[
    InlineKeyboardButton('Next \u27A1', callback_data='next'),
],
]


def start(update, context):
    CHECK = grey_check
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text(TEXT_1, reply_markup=reply_markup)
    for i in range(1,5):
        context.user_data[f'BUTTON_{i}'] = False

    return FIRST


def clicked(click=None, data=None):
    global keyboard
    global CHECK
    if click == True:
        CHECK = green_check
    else:
        CHECK = grey_check

    keyboard[KEYS[data][0]][KEYS[data][1]] = InlineKeyboardButton(
        f'{data} {CHECK}', callback_data=f'{data}')

    reply_markup = InlineKeyboardMarkup(keyboard)

    return reply_markup


def press(update, context):
    data = update.callback_query.data
    global CLICK
    if context.user_data[f'BUTTON_{data}'] == False:
        context.user_data[f'BUTTON_{data}'] = True
    else:
        context.user_data[f'BUTTON_{data}'] = False

    reply_keyboard = clicked(context.user_data[f'BUTTON_{data}'], data)
    update.callback_query.edit_message_text(text=TEXT_1, reply_markup=reply_keyboard)
    return FIRST


def send(update, context):
    text = ''
    for i in range(1, 5):
        text += f'{i} - ' + str(context.user_data[f'BUTTON_{i}']) + '\n'
    context.user_data[ANSWER] = text
    update.callback_query.edit_message_text(text=context.user_data[ANSWER])
    return ConversationHandler.END


def main():
    updater = Updater(
        'TOKEN', use_context=True)
    dp = updater.dispatcher
    conv_handler = ConversationHandler(
        entry_points=[CommandHandler('start', start)],
        states={
            FIRST: [
                    CallbackQueryHandler(press, pattern='^'+str(1)+'

After selecting the option, the color of the button changes to green, after sending the data and the next run of the /start script, the keyboard does not return to its original form. The buttons pressed by the user remain green, while the data change, when the buttons are pressed, occurs as expected.

), CallbackQueryHandler(press, pattern='^'+str(2)+'

After selecting the option, the color of the button changes to green, after sending the data and the next run of the /start script, the keyboard does not return to its original form. The buttons pressed by the user remain green, while the data change, when the buttons are pressed, occurs as expected.

), CallbackQueryHandler(press, pattern='^'+str(3)+'

After selecting the option, the color of the button changes to green, after sending the data and the next run of the /start script, the keyboard does not return to its original form. The buttons pressed by the user remain green, while the data change, when the buttons are pressed, occurs as expected.

), CallbackQueryHandler(press, pattern='^'+str(4)+'

After selecting the option, the color of the button changes to green, after sending the data and the next run of the /start script, the keyboard does not return to its original form. The buttons pressed by the user remain green, while the data change, when the buttons are pressed, occurs as expected.

), CallbackQueryHandler(send, pattern='^'+'next'+'

After selecting the option, the color of the button changes to green, after sending the data and the next run of the /start script, the keyboard does not return to its original form. The buttons pressed by the user remain green, while the data change, when the buttons are pressed, occurs as expected.

), ], }, fallbacks=[CommandHandler('start', start)] ) dp.add_handler(conv_handler) updater.start_polling() updater.idle() if __name__ == "__main__": main()

After selecting the option, the color of the button changes to green, after sending the data and the next run of the /start script, the keyboard does not return to its original form. The buttons pressed by the user remain green, while the data change, when the buttons are pressed, occurs as expected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有伤那来痛 2025-01-28 02:46:54

经过几次尝试,我找到了这个解决方案:

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (
    Updater,
    CommandHandler,
    CallbackQueryHandler,
    ConversationHandler)

import logging

green_check = '\u2705'
grey_check = '\u2714'

FIRST, ANSWER = range(2)
TEXT_1 = 'Click'
CHECK = grey_check
CLICK = False
BUTTONS = []
KEYS = {'1': (0, 0), '2': (0, 1), '3': (1, 0), '4': (1, 1)}

keyboard = [
[
    InlineKeyboardButton(f'1 {CHECK}', callback_data='1'),
    InlineKeyboardButton(f'2 {CHECK}', callback_data='2'),
],
[
    InlineKeyboardButton(f'3 {CHECK}', callback_data='3'),
    InlineKeyboardButton(f'4 {CHECK}', callback_data='4'),
],
[
    InlineKeyboardButton('Next \u27A1', callback_data='next'),
],
]


def start(update, context):
    global CHECK
    global keyboard
    global BUTTONS
    CHECK = grey_check
    for data in BUTTONS:
        keyboard[KEYS[data][0]][KEYS[data][1]] = InlineKeyboardButton(
            f'{data} {CHECK}', callback_data=f'{data}')
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text(TEXT_1, reply_markup=reply_markup)
    for i in range(1,5):
        context.user_data[f'BUTTON_{i}'] = False
    BUTTONS = []
    return FIRST


def clicked(click=None, data=None):
    global keyboard
    global CHECK
    global BUTTONS
    if click == True:
        CHECK = green_check
    else:
        CHECK = grey_check

    BUTTONS.append(data)
    keyboard[KEYS[data][0]][KEYS[data][1]] = InlineKeyboardButton(
        f'{data} {CHECK}', callback_data=f'{data}')

    reply_markup = InlineKeyboardMarkup(keyboard)

    return reply_markup


def press(update, context):
    data = update.callback_query.data
    global CLICK
    if context.user_data[f'BUTTON_{data}'] == False:
        context.user_data[f'BUTTON_{data}'] = True
    else:
        context.user_data[f'BUTTON_{data}'] = False

    reply_keyboard = clicked(context.user_data[f'BUTTON_{data}'], data)
    update.callback_query.edit_message_text(text=TEXT_1, reply_markup=reply_keyboard)
    return FIRST


def send(update, context):
    text = ''
    for i in range(1, 5):
        text += f'{i} - ' + str(context.user_data[f'BUTTON_{i}']) + '\n'
    context.user_data[ANSWER] = text
    update.callback_query.edit_message_text(text=context.user_data[ANSWER])

    return ConversationHandler.END


def main():
    updater = Updater(
        'TOKEN', use_context=True)
    dp = updater.dispatcher
    conv_handler = ConversationHandler(
        entry_points=[CommandHandler('start', start)],
        states={
            FIRST: [
                    CallbackQueryHandler(press, pattern='^'+str(1)+'

也许这不是最好的解决方案。您有什么想法如何改进此代码吗?

), CallbackQueryHandler(press, pattern='^'+str(2)+'

也许这不是最好的解决方案。您有什么想法如何改进此代码吗?

), CallbackQueryHandler(press, pattern='^'+str(3)+'

也许这不是最好的解决方案。您有什么想法如何改进此代码吗?

), CallbackQueryHandler(press, pattern='^'+str(4)+'

也许这不是最好的解决方案。您有什么想法如何改进此代码吗?

), CallbackQueryHandler(send, pattern='^'+'next'+'

也许这不是最好的解决方案。您有什么想法如何改进此代码吗?

), ], }, fallbacks=[CommandHandler('start', start)] ) dp.add_handler(conv_handler) updater.start_polling() updater.idle() if __name__ == "__main__": main()

也许这不是最好的解决方案。您有什么想法如何改进此代码吗?

After several attempts, I found this solution:

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (
    Updater,
    CommandHandler,
    CallbackQueryHandler,
    ConversationHandler)

import logging

green_check = '\u2705'
grey_check = '\u2714'

FIRST, ANSWER = range(2)
TEXT_1 = 'Click'
CHECK = grey_check
CLICK = False
BUTTONS = []
KEYS = {'1': (0, 0), '2': (0, 1), '3': (1, 0), '4': (1, 1)}

keyboard = [
[
    InlineKeyboardButton(f'1 {CHECK}', callback_data='1'),
    InlineKeyboardButton(f'2 {CHECK}', callback_data='2'),
],
[
    InlineKeyboardButton(f'3 {CHECK}', callback_data='3'),
    InlineKeyboardButton(f'4 {CHECK}', callback_data='4'),
],
[
    InlineKeyboardButton('Next \u27A1', callback_data='next'),
],
]


def start(update, context):
    global CHECK
    global keyboard
    global BUTTONS
    CHECK = grey_check
    for data in BUTTONS:
        keyboard[KEYS[data][0]][KEYS[data][1]] = InlineKeyboardButton(
            f'{data} {CHECK}', callback_data=f'{data}')
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text(TEXT_1, reply_markup=reply_markup)
    for i in range(1,5):
        context.user_data[f'BUTTON_{i}'] = False
    BUTTONS = []
    return FIRST


def clicked(click=None, data=None):
    global keyboard
    global CHECK
    global BUTTONS
    if click == True:
        CHECK = green_check
    else:
        CHECK = grey_check

    BUTTONS.append(data)
    keyboard[KEYS[data][0]][KEYS[data][1]] = InlineKeyboardButton(
        f'{data} {CHECK}', callback_data=f'{data}')

    reply_markup = InlineKeyboardMarkup(keyboard)

    return reply_markup


def press(update, context):
    data = update.callback_query.data
    global CLICK
    if context.user_data[f'BUTTON_{data}'] == False:
        context.user_data[f'BUTTON_{data}'] = True
    else:
        context.user_data[f'BUTTON_{data}'] = False

    reply_keyboard = clicked(context.user_data[f'BUTTON_{data}'], data)
    update.callback_query.edit_message_text(text=TEXT_1, reply_markup=reply_keyboard)
    return FIRST


def send(update, context):
    text = ''
    for i in range(1, 5):
        text += f'{i} - ' + str(context.user_data[f'BUTTON_{i}']) + '\n'
    context.user_data[ANSWER] = text
    update.callback_query.edit_message_text(text=context.user_data[ANSWER])

    return ConversationHandler.END


def main():
    updater = Updater(
        'TOKEN', use_context=True)
    dp = updater.dispatcher
    conv_handler = ConversationHandler(
        entry_points=[CommandHandler('start', start)],
        states={
            FIRST: [
                    CallbackQueryHandler(press, pattern='^'+str(1)+'

Perhaps this is not the best solution. Do you have any ideas how to improve this code?

), CallbackQueryHandler(press, pattern='^'+str(2)+'

Perhaps this is not the best solution. Do you have any ideas how to improve this code?

), CallbackQueryHandler(press, pattern='^'+str(3)+'

Perhaps this is not the best solution. Do you have any ideas how to improve this code?

), CallbackQueryHandler(press, pattern='^'+str(4)+'

Perhaps this is not the best solution. Do you have any ideas how to improve this code?

), CallbackQueryHandler(send, pattern='^'+'next'+'

Perhaps this is not the best solution. Do you have any ideas how to improve this code?

), ], }, fallbacks=[CommandHandler('start', start)] ) dp.add_handler(conv_handler) updater.start_polling() updater.idle() if __name__ == "__main__": main()

Perhaps this is not the best solution. Do you have any ideas how to improve this code?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文