脚本完成后,如何将键盘返回其原始状态?
该脚本是电报机器人的一部分。该脚本的任务是为用户提供多种选择。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过几次尝试,我找到了这个解决方案:
也许这不是最好的解决方案。您有什么想法如何改进此代码吗?
After several attempts, I found this solution:
Perhaps this is not the best solution. Do you have any ideas how to improve this code?