如何在python-telegram-bot中使用ForceReply?求助实现指定功能
Hey there! I totally get how frustrating it can be to piece together code snippets and not get the functionality you want—let's get this ForceReply working for your /sett command step by step. I'll keep things simple and copy-paste friendly since you mentioned you're working with snippets from Google.
First, Let's Cover the Basics
You'll need the python-telegram-bot library installed. Run this command in your terminal to get a stable, widely-used version:
pip install python-telegram-bot==20.7
Full Working Code (Async Version - Modern)
This uses the latest async syntax, which is the current standard for the library:
from telegram import Update, ForceReply from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters, ContextTypes # Handle the /sett command async def sett(update: Update, context: ContextTypes.DEFAULT_TYPE): # Send the "Enter No." message with ForceReply enabled await update.message.reply_text( "Enter No.", # selective=True ensures only the user who received this message is forced to reply reply_markup=ForceReply(selective=True) ) # Handle the user's reply to our ForceReply message async def handle_reply(update: Update, context: ContextTypes.DEFAULT_TYPE): # Check if the incoming message is a reply to our bot's ForceReply message if update.message.reply_to_message and update.message.reply_to_message.reply_markup: # Confirm it's the ForceReply markup we sent if isinstance(update.message.reply_to_message.reply_markup, ForceReply): user_input = update.message.text await update.message.reply_text(f"You entered: {user_input}") def main(): # Replace YOUR_BOT_TOKEN with the token you got from @BotFather application = ApplicationBuilder().token("YOUR_BOT_TOKEN").build() # Add the /sett command handler application.add_handler(CommandHandler("sett", sett)) # Add handler to catch text replies (ignores other commands) application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_reply)) # Start the bot application.run_polling() if __name__ == "__main__": main()
If You're Using an Older Version (v13, Sync Syntax)
If the snippets you found use the older sync style (common in older tutorials), use this code instead:
from telegram import Update, ForceReply from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext def sett(update: Update, context: CallbackContext): update.message.reply_text( "Enter No.", reply_markup=ForceReply(selective=True) ) def handle_reply(update: Update, context: CallbackContext): if update.message.reply_to_message and update.message.reply_to_message.reply_markup: if isinstance(update.message.reply_to_message.reply_markup, ForceReply): user_input = update.message.text update.message.reply_text(f"You entered: {user_input}") def main(): updater = Updater("YOUR_BOT_TOKEN") dp = updater.dispatcher dp.add_handler(CommandHandler("sett", sett)) dp.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_reply)) updater.start_polling() updater.idle() if __name__ == "__main__": main()
What You Need to Do Next
- Replace
YOUR_BOT_TOKENwith the actual token you received from Telegram's @BotFather when you created your bot. - Run the script, then send
/settto your bot in Telegram. You'll see the "Enter No." message with a prompt to reply directly to it. - When you type your response, the bot will confirm what you entered.
That's it! This should exactly match what you're trying to build. No extra complicated bits—just the core functionality you need.
内容的提问来源于stack exchange,提问作者iG0tB0lts

