如何使用InlineKeyboardButton的URL电报Bot



我看到了那个问题,但它不适合我。我不明白为什么。我需要打开一个url链接按钮。这是我的出发点

from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext
from telegram import ReplyKeyboardMarkup, KeyboardButton

def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id,
text="I'm a bot, please talk to me!",
reply_markup=ReplyKeyboardMarkup([
[KeyboardButton('rules', url='https://google.com'), ],
])
)

updater = Updater('APIKEY', use_context=True)
dispatcher = updater.dispatcher
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
updater.start_polling()
updater.idle()

您必须使用InlineKeyboardButton以便能够从按钮打开URL。

from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import CallbackQueryHandler, CallbackContext
def start(update, context):

query = update.callback_query
chat_id = query.message.chat_id
keyboard = [[InlineKeyboardButton('rules', url = 'https://google.com')]]
reply_markup = InlineKeyboardMarkup(keyboard)
context.bot.send_message(chat_id = chat_id,
text = "I'm a bot, please talk to me!",
reply_markup = reply_markup)

另外,InlineKeyboardButton

必须有一个function
def button (update, context):
query = update.callback_context
query.answer()

buttonstart的CallbackQueryHandler:

dp.add_handler(CallbackQueryHandler(button))
dp.add_handler(CallbackQueryhandler(start))

最新更新