I'm making a Telegram bot for my school project. It's meant to be a tool for math practice, but I'm only working on multiplication problems right now and I'm experiencing some issues.
Here's my current code:
import telebot
import random
from telebot import types
bot = telebot.TeleBot('bot_token_here')
@bot.message_handler(commands=['start'])
def begin(message):
keyboard = types.ReplyKeyboardMarkup(resize_keyboard=True)
mult_btn = types.KeyboardButton("Multiply")
div_btn = types.KeyboardButton("Divide")
add_btn = types.KeyboardButton("Add")
sub_btn = types.KeyboardButton("Subtract")
keyboard.add(mult_btn, div_btn, add_btn, sub_btn)
bot.send_message(message.chat.id, "Hi! Pick a mode", reply_markup=keyboard)
@bot.message_handler(content_types=['text'])
def handle_messages(message):
if message.text == "Multiply":
num1 = random.randint(1,10)
num2 = random.randint(1,10)
bot.send_message(message.chat.id, f"{num1} x {num2} =")
if message.text == str(num1 * num2):
bot.send_message(message.chat.id, "Great job! That's correct!")
bot.polling(none_stop=True, interval=0)
I can’t figure out how to compare the user’s input with the correct answer generated by the bot. My current approach doesn’t seem to work. Any suggestions on how to fix this?
I’ve encountered similar issues when working on Telegram bots. The problem in your code is that you’re trying to check the user’s answer in the same function that generates the question. This won’t work because the user hasn’t had a chance to respond yet.
Here’s what I’d suggest:
- Create a separate function to handle the user’s answer.
- Use bot.register_next_step_handler() to wait for the user’s response.
- Store the correct answer in a variable or dictionary to compare later.
Something like this:
user_answers = {}
def check_answer(message):
user_answer = message.text
correct_answer = user_answers[message.chat.id]
if user_answer == str(correct_answer):
bot.send_message(message.chat.id, 'Correct!')
else:
bot.send_message(message.chat.id, f'Sorry, the correct answer was {correct_answer}')
@bot.message_handler(content_types=['text'])
def handle_messages(message):
if message.text == 'Multiply':
num1 = random.randint(1,10)
num2 = random.randint(1,10)
user_answers[message.chat.id] = num1 * num2
bot.send_message(message.chat.id, f'{num1} x {num2} =')
bot.register_next_step_handler(message, check_answer)
This approach should solve your problem. Let me know if you need any clarification!
The issue in your code lies in the handling of user input. Your current setup doesn’t allow for capturing the user’s response to the multiplication question. To fix this, you need to implement a state management system.
Consider using a dictionary to store the current state for each user, including the correct answer. Then, use bot.register_next_step_handler() to wait for and process the user’s response.
Here’s a basic implementation:
user_state = {}
def check_answer(message):
if message.chat.id in user_state:
if message.text == str(user_state[message.chat.id]):
bot.reply_to(message, ‘Correct!’)
else:
bot.reply_to(message, f’Wrong. The answer was {user_state[message.chat.id]}')
del user_state[message.chat.id]
Modify your handle_messages function
def handle_messages(message):
if message.text == ‘Multiply’:
num1, num2 = random.randint(1,10), random.randint(1,10)
user_state[message.chat.id] = num1 * num2
bot.send_message(message.chat.id, f’{num1} x {num2} =')
bot.register_next_step_handler(message, check_answer)
This approach should resolve your issue and allow for proper answer checking.
hey mate, i faced similar probs with my bot. try using a dictionary to keep track of user states and answers. something like:
user_states = {}
def handle_answer(msg):
if msg.chat.id in user_states:
if msg.text == str(user_states[msg.chat.id]):
bot.reply_to(msg, ‘correct!’)
else:
bot.reply_to(msg, ‘wrong :(’)
del user_states[msg.chat.id]
this should help u out. lmk if u need more help!