我对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()
在此方面的任何帮助将不胜感激。谢谢!
答案 0 :(得分: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*'))