okaeiz
okaeiz

Reputation: 390

How to log clicking on a URL button in InlineKeyboardMarkup (python-telegram-bot)

Summarize the problem

I have created an inline keyboard markup in a Telegram bot using python-telegram-bot as below.

keyboard = [
[InlineKeyboardButton("🔗 view website" , url='<some_url>')],
[InlineKeyboardButton("🏢 add a new organization", callback_data='addorganizations')],
[InlineKeyboardButton("👤 add a new person", callback_data='addmembers')],
[InlineKeyboardButton("❔ guide", callback_data='guide')],
[InlineKeyboardButton("🛑 exit", callback_data='kill')]
]

Callback buttons can be logged if clicked. I use the following line of code to do so.

"""Audit & Log"""
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO
)
logger = logging.getLogger(__name__)

async def help(update: Update, context: ContextTypes.DEFAULT_TYPE):
    user = update.callback_query.from_user
    logger.info("User %s ran the help command & read the guide.", user.full_name)

But what if a user clicks on a URL button? how can we log this kind of activity?

What I have tried

I know it sounds stupid but I tried to assign a callback_data to url buttons as well. No wonder it didn't work.

Upvotes: 1

Views: 2411

Answers (1)

CallMeStag
CallMeStag

Reputation: 7050

Inline buttons with a URL don't produce a CallbackQuery, i.e. your bot is not informed about the user pressing the button. Your best bet is to use a URL redirect, e.g. url=http://yourdomain.com/<some_id>, where opening http://yourdomain.com/<some_id> does two things:

  1. inform your bot that the user pressed the button
  2. redirects the user to the actual URL

Upvotes: 1

Related Questions