Repository: python-telegram-bot/python-telegram-bot

Issue:
Showcase answer_callback_query with deeplinking URL
Instead of using the deeplinking URL directly in text links or inline buttons, one can also use them as `answer_inline_query(url=deeplinking_url)`, as done e.g. by @spoilerobot . I think it would be a good idea to include this option in the `deeplinking.py` example somehow.


Discussion:

Resolving patch:
diff --git a/examples/deeplinking.py b/examples/deeplinking.py
--- a/examples/deeplinking.py
+++ b/examples/deeplinking.py
@@ -22,27 +22,37 @@
 import logging
 
 from telegram import ParseMode, InlineKeyboardMarkup, InlineKeyboardButton, Update
-from telegram.ext import Updater, CommandHandler, Filters, CallbackContext
+from telegram.ext import (
+    Updater,
+    CommandHandler,
+    CallbackQueryHandler,
+    Filters,
+    CallbackContext,
+)
 
 # Enable logging
 from telegram.utils import helpers
 
 logging.basicConfig(
-    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
+    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
 )
 
 logger = logging.getLogger(__name__)
 
 # Define constants that will allow us to reuse the deep-linking parameters.
-CHECK_THIS_OUT = 'check-this-out'
-USING_ENTITIES = 'using-entities-here'
-SO_COOL = 'so-cool'
+CHECK_THIS_OUT = "check-this-out"
+USING_ENTITIES = "using-entities-here"
+USING_KEYBOARD = "using-keyboard-here"
+SO_COOL = "so-cool"
+
+# Callback data to pass in 3rd level deeplinking
+KEYBOARD_CALLBACKDATA = "keyboard-callback-data"
 
 
 def start(update: Update, context: CallbackContext) -> None:
     """Send a deep-linked URL when the command /start is issued."""
     bot = context.bot
-    url = helpers.create_deep_linked_url(bot.get_me().username, CHECK_THIS_OUT, group=True)
+    url = helpers.create_deep_linked_url(bot.username, CHECK_THIS_OUT, group=True)
     text = "Feel free to tell your friends about it:\n\n" + url
     update.message.reply_text(text)
 
@@ -50,13 +60,13 @@ def start(update: Update, context: CallbackContext) -> None:
 def deep_linked_level_1(update: Update, context: CallbackContext) -> None:
     """Reached through the CHECK_THIS_OUT payload"""
     bot = context.bot
-    url = helpers.create_deep_linked_url(bot.get_me().username, SO_COOL)
+    url = helpers.create_deep_linked_url(bot.username, SO_COOL)
     text = (
         "Awesome, you just accessed hidden functionality! "
         " Now let's get back to the private chat."
     )
     keyboard = InlineKeyboardMarkup.from_button(
-        InlineKeyboardButton(text='Continue here!', url=url)
+        InlineKeyboardButton(text="Continue here!", url=url)
     )
     update.message.reply_text(text, reply_markup=keyboard)
 
@@ -64,13 +74,30 @@ def deep_linked_level_1(update: Update, context: CallbackContext) -> None:
 def deep_linked_level_2(update: Update, context: CallbackContext) -> None:
     """Reached through the SO_COOL payload"""
     bot = context.bot
-    url = helpers.create_deep_linked_url(bot.get_me().username, USING_ENTITIES)
+    url = helpers.create_deep_linked_url(bot.username, USING_ENTITIES)
     text = f"You can also mask the deep-linked URLs as links: [▶️ CLICK HERE]({url})."
     update.message.reply_text(text, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True)
 
 
 def deep_linked_level_3(update: Update, context: CallbackContext) -> None:
     """Reached through the USING_ENTITIES payload"""
+    update.message.reply_text(
+        "It is also possible to make deep-linking using InlineKeyboardButtons.",
+        reply_markup=InlineKeyboardMarkup(
+            [[InlineKeyboardButton(text="Like this!", callback_data=KEYBOARD_CALLBACKDATA)]]
+        ),
+    )
+
+
+def deep_link_level_3_callback(update: Update, context: CallbackContext) -> None:
+    """Answers CallbackQuery with deeplinking url."""
+    bot = context.bot
+    url = helpers.create_deep_linked_url(bot.username, USING_KEYBOARD)
+    update.callback_query.answer(url=url)
+
+
+def deep_linked_level_4(update: Update, context: CallbackContext) -> None:
+    """Reached through the USING_KEYBOARD payload"""
     payload = context.args
     update.message.reply_text(
         f"Congratulations! This is as deep as it gets 👏🏻\n\nThe payload was: {payload}"
@@ -101,6 +128,16 @@ def main():
         CommandHandler("start", deep_linked_level_3, Filters.regex(USING_ENTITIES), pass_args=True)
     )
 
+    # Possible with inline keyboard buttons aswell
+    dispatcher.add_handler(
+        CommandHandler("start", deep_linked_level_4, Filters.regex(USING_KEYBOARD))
+    )
+
+    # register callback handler for inline keyboard button
+    dispatcher.add_handler(
+        CallbackQueryHandler(deep_link_level_3_callback, pattern=KEYBOARD_CALLBACKDATA)
+    )
+
     # Make sure the deep-linking handlers occur *before* the normal /start handler.
     dispatcher.add_handler(CommandHandler("start", start))
 
@@ -113,5 +150,5 @@ def main():
     updater.idle()
 
 
-if __name__ == '__main__':
+if __name__ == "__main__":
     main()


Test patch: