gpt4 book ai didi

python - 如何定期在 Telegram Bot 中发送 InlineKeyboardButton?

转载 作者:行者123 更新时间:2023-12-04 11:34:43 24 4
gpt4 key购买 nike

我正在尝试发送 InlineKeyboardHandler每 x 秒。为此,我使用了 updater.job_queue.run_repeating但它的行为很奇怪。
除非我先与机器人进行另一次交互,否则键盘不起作用。我写了一段简单的代码,你可以测试一下。

from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, ConversationHandler, CallbackContext, CallbackQueryHandler

user_id = '*********'
tlg_token = '******************************'
SELECTING_COMMAND=1
keyboard = [[InlineKeyboardButton('Button: Print Clicked', callback_data=1)],]
reply_markup = InlineKeyboardMarkup(keyboard)

def menu(update: Update, context: CallbackContext) -> int:
update.message.reply_text('sent by command button:', reply_markup=reply_markup)
return SELECTING_COMMAND

def InlineKeyboardHandler(update: Update, _: CallbackContext) -> None:
print('clicked')
return 1

def cancel(update: Update, context: CallbackContext) -> int:
return ConversationHandler.END

updater = Updater(tlg_token, use_context=True)
dispatcher = updater.dispatcher

conv_handler = ConversationHandler(
entry_points=[CommandHandler('request_button', menu)],
states={
SELECTING_COMMAND: [CallbackQueryHandler(InlineKeyboardHandler)],
},
fallbacks=[CommandHandler('cancel', cancel)],
)
dispatcher.add_handler(conv_handler)

j = updater.job_queue

def talker(update):
update.bot.sendMessage(chat_id=user_id, text='sent by talker:', reply_markup=reply_markup)

j.run_repeating(talker, interval=10, first=0)
updater.start_polling()
updater.bot.sendMessage(chat_id=user_id, text='/request_button')
updater.idle()
我希望我可以在单击按钮后看到“单击”打印,但除非您先单击/request_button,否则它不会起作用。为什么?我该如何解决?

最佳答案

您的代码问题为 a_guest评论中提到的是InlineKeyboardHandler只有在拨打 request_button 后才会开始工作命令。
这是一个工作版本,其中 InlineKeyboardHandler独立注册:

from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, ConversationHandler, CallbackContext, CallbackQueryHandler

#################################
user_id = 0
tlg_token = 'bot_token'
SELECTING_COMMAND = 1
keyboard = [[InlineKeyboardButton('Button: Print Clicked', callback_data=1)], ]
reply_markup = InlineKeyboardMarkup(keyboard)


#################################

def menu(update: Update, context: CallbackContext) -> int:
update.message.reply_text('sent by command button:', reply_markup=reply_markup)
return SELECTING_COMMAND


def InlineKeyboardHandler(update: Update, _: CallbackContext) -> None:
print('clicked')
return 1


def cancel(update: Update, context: CallbackContext) -> int:
return ConversationHandler.END


updater = Updater(tlg_token, use_context=True)
dispatcher = updater.dispatcher
updater.dispatcher.add_handler(CallbackQueryHandler(InlineKeyboardHandler))
updater.dispatcher.add_handler(CommandHandler('request_button', menu))
j = updater.job_queue


def talker(update):
update.bot.sendMessage(chat_id=user_id, text='sent by talker:', reply_markup=reply_markup)


j.run_repeating(talker, interval=10, first=0)
updater.start_polling()
updater.bot.sendMessage(chat_id=user_id, text='/request_button')
updater.idle()

该问题的另一个解决方案是 OP 自己在添加 CallbackQueryHandler 作为入口点的评论中提到的内容:
entry_points=[CommandHandler('request_button', menu),                   CallbackQueryHandler(InlineKeyboardHandler)]

关于python - 如何定期在 Telegram Bot 中发送 InlineKeyboardButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68973827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com