I’m working on a physics formula assistant bot for Telegram. The bot works fine when users click the ‘Mechanics’ button, but it doesn’t respond to the ‘Electrical and Magnetic Phenomena’ button. I’m not sure what’s causing this issue.
Here’s a simplified version of my code:
import telebot
from telebot import types
bot = telebot.TeleBot('my_token')
@bot.message_handler(commands=['start'])
def greet(message):
bot.reply_to(message, 'Hi! I'm your physics helper. Use /formulas to see options.')
@bot.message_handler(commands=['formulas'])
def show_topics(message):
kb = types.ReplyKeyboardMarkup(resize_keyboard=True)
kb.add(types.KeyboardButton('Mechanics'), types.KeyboardButton('Electromagnetism'))
bot.send_message(message.chat.id, 'Choose a topic:', reply_markup=kb)
@bot.message_handler(func=lambda msg: msg.text == 'Mechanics')
def mechanics_menu(message):
kb = types.ReplyKeyboardMarkup(resize_keyboard=True)
kb.add(types.KeyboardButton('Kinematics'), types.KeyboardButton('Dynamics'))
bot.send_message(message.chat.id, 'Select a subtopic:', reply_markup=kb)
@bot.message_handler(func=lambda msg: msg.text == 'Electromagnetism')
def em_menu(message):
kb = types.ReplyKeyboardMarkup(resize_keyboard=True)
kb.add(types.KeyboardButton('Electrostatics'), types.KeyboardButton('Magnetism'))
bot.send_message(message.chat.id, 'Choose a subtopic:', reply_markup=kb)
bot.polling()
I’m new to bot development. Can anyone help me figure out why the ‘Electromagnetism’ button isn’t working?
hey there! i’ve worked with telegram bots before and i think i know whats up. the problem is probably with how ur handling the button press. try using @bot.callback_query_handler instead of @bot.message_handler for the electromagnetism button. that should fix it. good luck with ur project!
I’ve encountered a similar issue while developing my own Telegram bot. The problem might be in how you’ve named the button versus how you’re checking for it in your handler function. In your code, you’ve used ‘Electromagnetism’ in the button creation, but ‘Electrical and Magnetic Phenomena’ in your question description.
Try changing the button text and the corresponding handler function to match exactly. For example:
kb.add(types.KeyboardButton('Mechanics'), types.KeyboardButton('Electrical and Magnetic Phenomena'))
@bot.message_handler(func=lambda msg: msg.text == 'Electrical and Magnetic Phenomena')
def em_menu(message):
# Your code here
Also, ensure your bot has the necessary permissions to read messages. Sometimes, privacy settings can interfere with button responses. If the issue persists, adding some logging to verify if the handler function is activated could help pinpoint the problem.
I’ve run into this exact problem before. The issue is likely with how you’re handling the button press. Telegram sends a callback query when a button is pressed, not a regular message. You need to use the @bot.callback_query_handler decorator instead.
Try modifying your code like this:
@bot.callback_query_handler(func=lambda call: call.data == ‘Electromagnetism’)
def em_menu(call):
kb = types.ReplyKeyboardMarkup(resize_keyboard=True)
kb.add(types.KeyboardButton(‘Electrostatics’), types.KeyboardButton(‘Magnetism’))
bot.send_message(call.message.chat.id, ‘Choose a subtopic:’, reply_markup=kb)
Also, make sure you’re using InlineKeyboardMarkup and InlineKeyboardButton for inline buttons. This should resolve your issue. Let me know if you need any further clarification.