1

我对 Telegram 和 python-telegram-bot 很陌生。我正在尝试创建一个菜单,但即使我在启动机器人时没有收到错误,也没有任何回复。我在控制台中没有看到任何错误,只是沉默。我试图尽可能地利用 python-telegram-bot github wiki 中的代码片段以使其正确,但我一定错过了一些东西。我还登陆了使用 python-telegram-bot 1构建菜单的正确方法,但我仍然缺少一些东西。

下面是我正在使用的代码。

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()

对此的任何帮助将不胜感激。谢谢!

4

1 回答 1

0

InlineKeyboardButtons用于获取回调查询。正常KeyboardButtons对应于文本消息(单击时,将作为普通消息发送)

修改键盘功能,

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

此外,您的处理程序的正则表达式应该像这样更改以捕获所有类型的回调,

updater.dispatcher.add_handler(CallbackQueryHandler(main_menu, pattern='m*'))
于 2020-06-21T17:58:20.127 回答