【问题标题】:Unable to create a telegram bot menu using python-telegram-bot无法使用 python-telegram-bot 创建电报机器人菜单
【发布时间】:2020-10-01 22:10:07
【问题描述】:

我对 Telegram 和 python-telegram-bot 非常陌生。我正在尝试创建一个菜单,但即使我在启动机器人时没有收到错误,也没有任何回复。我在控制台中没有看到任何错误,只是沉默。 我试图尽可能地利用 python-telegram-bot github wiki 中的代码 sn-ps 以使其正确,但我一定错过了一些东西。 我也登陆了使用 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()

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

【问题讨论】:

    标签: python telegram telegram-bot


    【解决方案1】:

    InlineKeyboardButtons 用于获取回调查询。普通KeyboardButtons对应短信(点击时,发送that作为普通信息)

    修改键盘功能,

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

    【讨论】:

    • 感谢 Gauthamram!我应用了您建议的更改,但当我启动它时,机器人仍然没有做任何事情。我将在今天晚些时候尝试使用调试器。也许我会找到导致这种行为的原因......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-25
    • 2017-06-16
    • 2018-12-10
    • 2021-12-06
    相关资源
    最近更新 更多