无法使用pythontelegrambot创建电报机器人菜单

2024-04-18 17:07:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我对电报和python电报机器人非常陌生。我正在尝试创建一个菜单,但即使在启动bot时没有出现错误,也不会有任何响应。我在控制台中没有看到任何错误,只是保持沉默。 我试图尽可能多地利用python电报机器人github wiki中的代码片段,使其成为正确的方式,但我一定错过了什么。 我还找到了使用python Telegrame bot1构建菜单的正确方法,但我仍然缺少一些东西

下面是我正在使用的代码

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

在此方面的任何帮助都将不胜感激。谢谢


Tags: textmessagedatareturnmaindefcontextcallback
1条回答
网友
1楼 · 发布于 2024-04-18 17:07:44

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*'))

相关问题 更多 >