如何将 URL 与 InlineKeyboardButton 一起用于电报机器人

How to use URL with InlineKeyboardButton for Telegram Bot

我看到了 问题,但它对我不起作用。我不明白为什么。 我需要打开 url link 的按钮。这是我的出发点

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