I’m building a Telegram bot using Python-Telegram-Bot and have transitioned from text commands to using InlineKeyboardButtons for the menu. Here’s what I’m trying to achieve:
- A user opens the menu
- InlineKeyboardButtons appear, including an option for a game called ‘Jokenpo’
- On clicking ‘Jokenpo’, the bot sends a greeting along with the game rules
- The bot should then enter a game loop, waiting for the input ‘rock’, ‘paper’, or ‘scissors’
The issue I’m facing is that after the button click, only the greeting and rules message is sent, and the bot does not wait for further user input. With CommandHandlers, it worked perfectly, but now with InlineKeyboardButtons and CallbackContext, I’m not sure how to manage the flow.
Below is a simplified version of my code:
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import CommandHandler, MessageHandler, Filters, ConversationHandler
GAME_STATE = 1
def menu(update, context):
buttons = [[InlineKeyboardButton('Jokenpo', callback_data='jokenpo')]]
update.message.reply_text('Menu:', reply_markup=InlineKeyboardMarkup(buttons))
def handle_button(update, context):
query = update.callback_query
if query.data == 'jokenpo':
return jokenpo_start(update, context)
def jokenpo_start(update, context):
update.callback_query.message.reply_text('Jokenpo mode. Please type rock, paper, or scissors to proceed.')
return GAME_STATE
def jokenpo_game(update, context):
# Game logic here
pass
# In your main function
conv_handler = ConversationHandler(
entry_points=[CommandHandler('menu', menu)],
states={
GAME_STATE: [MessageHandler(Filters.text, jokenpo_game)]
},
fallbacks=[]
)
dp.add_handler(conv_handler)
How can I modify my code so it properly waits for user input after clicking the button?