单击此处查看流程图我需要一些有关我的 Telegram 机器人的帮助。我正在尝试创建一个机器人,它允许我修改一组选定的字典值并在其末尾以某种方式显示字典键和值。如代码所示,现在我无法弄清楚如何找出用户单击了哪个按钮并将其反映在该键的相应值中。(例如,(KEY)Shop A - (VALUE)Low Crowd、Compliant...等等,对于这个选定区域的其他商店)。请注意,我目前仅编写了区域 1 的代码作为示例 -> 它还必须能够使用他们自己的一组具有空值的预定义字典键来为其他选定区域工作。参考图片以获得更好的理解
#!/usr/bin/env python
# pylint: disable=C0116
import logging
from telegram import Update, ForceReply, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext, CallbackQueryHandler
# Enable logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
############################ Keyboards #########################################
def main_area_selection_kb():
keyboard = [
[
InlineKeyboardButton("Area 1", callback_data='main_1'),
],
[
InlineKeyboardButton("Area 2", callback_data='main_2'),
],
[
InlineKeyboardButton("Area 3", callback_data='main_3'),
],
[
InlineKeyboardButton("Area 4", callback_data='main_4'),
],
[
InlineKeyboardButton("Area 5", callback_data='main_5'),
],
[
InlineKeyboardButton("Area 6", callback_data='main_6'),
],
]
return InlineKeyboardMarkup(keyboard)
def crowd_level_selection_kb():
keyboard = [
[
InlineKeyboardButton("Low", callback_data='clvl_1'),
InlineKeyboardButton("Moderate", callback_data='clvl_2'),
InlineKeyboardButton("High", callback_data='clvl_3'),
InlineKeyboardButton("Closed", callback_data='clvl_4'),
]
]
return InlineKeyboardMarkup(keyboard)
def compliance_kb():
keyboard = [
[
InlineKeyboardButton("Compliant", callback_data='com_1'),
InlineKeyboardButton("Not Compliant", callback_data='com_2'),
]
]
return InlineKeyboardMarkup(keyboard)
############################ Selection #########################################
def compliance_selection(update, _: CallbackContext) -> None:
query = update.callback_query
query.answer()
if query.data == 'com_1':
query.message.reply_text('Compliant')
elif query.data == 'com_2':
query.message.reply_text('Not Compliant')
def crowd_level_selection(update, _: CallbackContext) -> None:
query = update.callback_query
query.answer() # How do I "pass" the dictionary in/return the value of the user selection back into the respective dictionary's key value?)
if query.data == 'clvl_1':
query.message.reply_text('You choose Low Crowd')
elif query.data == 'clvl_2':
query.message.reply_text('You choose Moderate Crowd')
elif query.data == 'clvl_3':
query.message.reply_text('You choose High Crowd')
elif query.data == 'clvl_4':
query.message.reply_text('You choose Closed')
def main_area_selection(update, _: CallbackContext) -> None:
query = update.callback_query
query.answer()
# query.edit_message_text(text=f"Selected option: {query.data}")
if query.data == 'main_1':
query.message.reply_text('You choose Area 1')
areamain(query)
elif query.data == 'main_2':
query.message.reply_text('You choose Area 2')
elif query.data == 'main_3':
query.message.reply_text('You choose Area 3')
elif query.data == 'main_4':
query.message.reply_text('You choose Area 4')
elif query.data == 'main_5':
query.message.reply_text('You choose Area 5')
elif query.data == 'main_6':
query.message.reply_text('You choose Area 6')
else:
query.message.reply_text('Error')
############################ Functions #########################################
def start(update, context):
"""Send a message when the command /start is issued."""
update.message.reply_text('Please Choose an Area', reply_markup=main_area_selection_kb())
def areamain(update):
areamaindict = {'Shop A': '', 'Shop B': '', 'Shop C': '', 'Shop D': '', 'Shop E': ''}
for i in areamaindict:
update.message.reply_text(f"{i} Crowd Level:",
reply_markup=crowd_level_selection_kb())
#Next step: Add in menu for compliance
#Following that, Set this i value to <High/Medium/Low> Crowd and <Compliant/Not Compliant> once user selected both respective buttons
############################ Main #########################################
def main():
# Create the Updater and pass it your bot's token.
updater = Updater("TOKEN")
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
# on different commands - answer in Telegram
dispatcher.add_handler(CommandHandler("start", start))
############################# Handlers #########################################
updater.dispatcher.add_handler(CallbackQueryHandler(main_area_selection, pattern='main'))
updater.dispatcher.add_handler(CallbackQueryHandler(crowd_level_selection, pattern='clvl'))
updater.dispatcher.add_handler(CallbackQueryHandler(compliance_selection, pattern='com'))
# Start the Bot/Listen for user input/messages
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()