I’m experiencing issues with my telegram bot menu not functioning properly. As a beginner to telegram bots and the python-telegram-bot library, I’m seeking assistance.
While the bot starts without showing any errors, it doesn’t respond when I attempt to interact with it. There are no output messages or errors in the console, which leaves me puzzled.
I’ve tried to adapt code examples from the official documentation and other examples I’ve come across, but it seems like there’s a fundamental issue with my implementation.
from telegram.ext import CommandHandler, CallbackQueryHandler, Updater
from telegram import KeyboardButton, ReplyKeyboardMarkup
from functools import wraps
BOT_TOKEN = 'your_token_here'
ALLOWED_USERS = ["user1", "user2"]
ADMIN_LIST = ["admin1"]
def admin_only(function):
@wraps(function)
def wrapper(update, context, *args, **kwargs):
user = update.effective_user.username
if user not in ADMIN_LIST:
update.message.reply_text(f"Access denied {user}, admin required")
return
return function(update, context, *args, **kwargs)
return wrapper
def authorized_only(function):
@wraps(function)
def wrapper(update, context, *args, **kwargs):
user = update.effective_user.username
if user not in ALLOWED_USERS:
update.message.reply_text(f"Not authorized {user}!")
return
return function(update, context, *args, **kwargs)
return wrapper
@authorized_only
def handle_start(update, context):
update.message.reply_text(get_menu_text(), reply_markup=create_main_keyboard())
def create_button_grid(items, columns, top_row=None, bottom_row=None):
grid = [items[i:i + columns] for i in range(0, len(items), columns)]
if top_row:
grid.insert(0, [top_row])
if bottom_row:
grid.append([bottom_row])
return grid
@authorized_only
def show_main_menu(update, context):
query = update.callback_query
query.answer()
query.edit_message_text(
text=get_menu_text(),
reply_markup=create_main_keyboard()
)
def create_main_keyboard():
options = [[KeyboardButton('Settings', callback_data='opt1')],
[KeyboardButton('Restart', callback_data='opt2')],
[KeyboardButton('Enable', callback_data='opt3')],
[KeyboardButton('Disable', callback_data='opt4')],
[KeyboardButton('Debug 1', callback_data='opt5')],
[KeyboardButton('Debug 2', callback_data='opt6')]]
markup = ReplyKeyboardMarkup(create_button_grid(options, n_cols=2))
return markup
def get_menu_text():
return 'Choose an option from the menu below'
bot_updater = Updater(BOT_TOKEN, use_context=True)
bot_updater.dispatcher.add_handler(CommandHandler('start', handle_start))
bot_updater.dispatcher.add_handler(CallbackQueryHandler(show_main_menu, pattern='main'))
bot_updater.start_polling()
Could anyone identify what I might be doing incorrectly? Your assistance would be greatly appreciated!