1

I am very new to Telegram and the python-telegram-bot. I am trying to create a menu but even if I don't get errors when I start the bot, nothing is replied. I don't see any error in the console, just silence. I tried to leverage the code snippets from python-telegram-bot github wiki as much as possible to make it the right way but I must have missed something. I also landed to the post Proper way to build menus with python-telegram-bot 1 but I am still missing something.

Here below the code I am using.

from telegram.ext import CommandHandler, CallbackQueryHandler, Updater
from telegram import KeyboardButton, ReplyKeyboardMarkup
from functools import wraps

API_TOKEN = '#####'
AUTHORIZED_USERS = ["#####", "#####"]
LIST_OF_ADMINS = ["#####"]

def restricted_admins(func):
    @wraps(func)
    def wrapped(update, context, *args, **kwargs):
        username = update.effective_user.username
        if username not in LIST_OF_ADMINS:
            update.message.reply_text("Sorry {}, this feature is for Admins only".format(username))
            return
        return func(update, context, *args, **kwargs)
    return wrapped

def restricted_users(func):
    @wraps(func)
    def wrapped(update, context, *args, **kwargs):
        username = update.effective_user.username
        if username not in AUTHORIZED_USERS:
            update.message.reply_text("Unauthorized access denied for {}!".format(username))
            return
        return func(update, context, *args, **kwargs)
    return wrapped

@restricted_users
def start(update, context):
  update.message.reply_text(main_menu_message(), reply_markup=main_menu_keyboard())

def build_menu(buttons,
               n_cols,
               header_buttons=None,
               footer_buttons=None):
    menu = [buttons[i:i + n_cols] for i in range(0, len(buttons), n_cols)]
    if header_buttons:
        menu.insert(0, [header_buttons])
    if footer_buttons:
        menu.append([footer_buttons])
    return menu

@restricted_users
def main_menu(update,context):
  query = update.callback_query
  query.answer()
  query.edit_message_text(
    text=main_menu_message(),
    reply_markup=main_menu_keyboard()
  )

def main_menu_keyboard():
  button_list = [[KeyboardButton('Configure', callback_data='m1')],
               [KeyboardButton('Reload', callback_data='m2')],
               [KeyboardButton('Turn On', callback_data='m3')],
               [KeyboardButton('Turn Off', callback_data='m4')],
               [KeyboardButton('Test 1', callback_data='m5')],
               [KeyboardButton('Test 2', callback_data='m6')]]
  reply_markup = ReplyKeyboardMarkup(build_menu(button_list, n_cols=3))
  return reply_markup

def main_menu_message():
    return 'Please select from the menu'

updater = Updater(API_TOKEN, use_context=True)
updater.dispatcher.add_handler(CommandHandler('start', start))
updater.dispatcher.add_handler(CallbackQueryHandler(main_menu, pattern='main'))
updater.start_polling()

Any help on this will be greatly appreciated. Thanks!

Rockyjoe
  • 23
  • 3

1 Answers1

1

InlineKeyboardButtons are used to get callbackquery(s). Normal KeyboardButtons corresponds to text messages (when clicked, send that as a normal message)

Modified keyboard function,

def main_menu_keyboard():
  button_list = [[InlineKeyboardButton('Configure', callback_data='m1')],
                 [InlineKeyboardButton('Reload', callback_data='m2')],
                 [InlineKeyboardButton('Turn On', callback_data='m3')],
                 [InlineKeyboardButton('Turn Off', callback_data='m4')],
                 [InlineKeyboardButton('Test 1', callback_data='m5')],
                 [InlineKeyboardButton('Test 2', callback_data='m6')]]
  reply_markup = InlineKeyboardMarkup(build_menu(button_list, n_cols=3))
  return reply_markup

Also, your handler's regex should be changed like this to catch all types of callbacks,

updater.dispatcher.add_handler(CallbackQueryHandler(main_menu, pattern='m*'))
  • Thanks Gauthamram! I applied the changes you suggested but still the bot doesn't do anything when I start it. I will try with the debugger later today. Maybe I will find what is causing this behavior... – Rockyjoe Jun 24 '20 at 05:05