arbitrary_callback_data tries to match some UUID to the CallbackQueryHandler's pattern #3122
-
I'm quite new to the python-telegram-bot, so I might be missing something. When trying to send a longer than 64 bytes callback_data using arbitrary_callback_data, the string used to match the CallbackQueryHandler's pattern looks more like and UUID and has no relation to with the original content. This makes it impossible to match it. Moreover, when trying to match it against the values stored in the telegram.ext.CallbackDataCache._keyboard_data it does not match any of them. How can I recover the original calback_data and route it to the proper handler? edit: Minimal working example: import telegram
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import CallbackContext, CallbackQueryHandler, Updater
from config import settings
def process_callback(callback):
pass
def filter_callback(uuid):
print(f"Recieved uuid: {uuid}")
for k, v in bot_cache._keyboard_data.items():
print(f"key: {k}")
print(f"keyboard_uuid: {v.keyboard_uuid}")
print(f"button_Data: {v.button_data}")
bot = telegram.ext.ExtBot(settings.TELEGRAM_API_TOKEN, arbitrary_callback_data=True)
bot_cache = telegram.ext.CallbackDataCache(bot)
keyboard = [[InlineKeyboardButton("Long callback", callback_data="X" * 256)]]
reply_markup = InlineKeyboardMarkup(keyboard)
reply_markup_arbitrary = bot_cache.process_keyboard(reply_markup)
updater = Updater(settings.TELEGRAM_API_TOKEN, use_context=True)
dp = updater.dispatcher
dp.add_handler(CallbackQueryHandler(process_callback, pattern=filter_callback))
bot.send_message(
text="This has a long callback",
chat_id=settings.CHAT_ID,
reply_markup=reply_markup_arbitrary,
)
updater.start_polling()
updater.idle() When clicking in the keyboard button, the output is:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Hi. |
Beta Was this translation helpful? Give feedback.
-
The actual issue though, if I see it correctly, that you use Updater() without setting arbitrary_callback_data to True or passing your created bot instance, thus the dispatcher/updater cant resolve the arbitrary_callback_data passed by the other ExtBot instance properly and you receive the uuid in your filter instead of the long x string you expect. |
Beta Was this translation helpful? Give feedback.
-
Leaving here the final working version thanks to both @Bibo-Joshi and @Poolitzer: import telegram
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import CallbackContext, CallbackQueryHandler, Updater
from config import settings
def do_a(update: Update, context: CallbackContext):
callback_data = update.callback_query.data
update.callback_query.edit_message_reply_markup(reply_markup=None)
bot.send_message(
text=f"Doing A{callback_data['c']}",
chat_id=update.callback_query.message.chat_id,
)
def ask_a(update: Update, context: CallbackContext):
callback_data = update.callback_query.data
keyboard = [
[InlineKeyboardButton("Do a1", callback_data=dict(**callback_data, c=1))],
[InlineKeyboardButton("Do a2", callback_data=dict(**callback_data, c=2))],
[InlineKeyboardButton("Do a3", callback_data=dict(**callback_data, c=3))],
]
reply_markup = InlineKeyboardMarkup(keyboard)
update.callback_query.edit_message_reply_markup(reply_markup=reply_markup)
def pattern_a(callback_data):
return "c" in callback_data.keys()
def pattern_ask_a(callback_data):
return "c" not in callback_data.keys()
bot = telegram.ext.ExtBot(settings.TELEGRAM_API_TOKEN, arbitrary_callback_data=True)
keyboard = [
[InlineKeyboardButton("a1", callback_data=dict(a=1, b=2, c=1))],
[InlineKeyboardButton("a?", callback_data=dict(a=1, b=2))],
]
reply_markup = InlineKeyboardMarkup(keyboard)
updater = Updater(bot=bot, use_context=True)
dp = updater.dispatcher
dp.add_handler(CallbackQueryHandler(do_a, pattern=pattern_a))
dp.add_handler(CallbackQueryHandler(ask_a, pattern=pattern_ask_a))
bot.send_message(
text="These buttons have an arbitrary callback data.",
chat_id=settings.CHAT_ID,
reply_markup=reply_markup,
)
updater.start_polling()
updater.idle() |
Beta Was this translation helpful? Give feedback.
The actual issue though, if I see it correctly, that you use Updater() without setting arbitrary_callback_data to True or passing your created bot instance, thus the dispatcher/updater cant resolve the arbitrary_callback_data passed by the other ExtBot instance properly and you receive the uuid in your filter instead of the long x string you expect.