2019-09-13 21:09:05 +02:00
|
|
|
#!/usr/bin/env python
|
2022-05-15 14:08:40 +02:00
|
|
|
# pylint: disable=unused-argument, wrong-import-position
|
2020-10-31 16:33:34 +01:00
|
|
|
# This program is dedicated to the public domain under the CC0 license.
|
2019-09-13 21:09:05 +02:00
|
|
|
|
|
|
|
"""Bot that explains Telegram's "Deep Linking Parameters" functionality.
|
|
|
|
|
|
|
|
This program is dedicated to the public domain under the CC0 license.
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
This Bot uses the Application class to handle the bot.
|
2019-09-13 21:09:05 +02:00
|
|
|
|
|
|
|
First, a few handler functions are defined. Then, those functions are passed to
|
2022-04-24 12:38:09 +02:00
|
|
|
the Application and registered at their respective places.
|
2019-09-13 21:09:05 +02:00
|
|
|
Then, the bot is started and runs until we press Ctrl-C on the command line.
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
Deep Linking example. Send /start to get the link.
|
|
|
|
Press Ctrl-C on the command line or send a signal to the process to stop the
|
|
|
|
bot.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import logging
|
|
|
|
|
2022-05-15 14:08:40 +02:00
|
|
|
from telegram import __version__ as TG_VER
|
|
|
|
|
|
|
|
try:
|
|
|
|
from telegram import __version_info__
|
|
|
|
except ImportError:
|
|
|
|
__version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment]
|
|
|
|
|
|
|
|
if __version_info__ < (20, 0, 0, "alpha", 1):
|
|
|
|
raise RuntimeError(
|
|
|
|
f"This example is not compatible with your current PTB version {TG_VER}. To view the "
|
|
|
|
f"{TG_VER} version of this example, "
|
2022-06-09 17:22:32 +02:00
|
|
|
f"visit https://docs.python-telegram-bot.org/en/v{TG_VER}/examples.html"
|
2022-05-15 14:08:40 +02:00
|
|
|
)
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, helpers
|
2021-10-19 18:28:19 +02:00
|
|
|
from telegram.constants import ParseMode
|
2022-05-12 19:36:25 +02:00
|
|
|
from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, filters
|
2019-09-13 21:09:05 +02:00
|
|
|
|
|
|
|
# Enable logging
|
2020-10-09 17:22:07 +02:00
|
|
|
logging.basicConfig(
|
2021-01-28 17:12:13 +01:00
|
|
|
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2019-09-13 21:09:05 +02:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-10-11 20:10:21 +02:00
|
|
|
# Define constants that will allow us to reuse the deep-linking parameters.
|
2021-01-28 17:12:13 +01:00
|
|
|
CHECK_THIS_OUT = "check-this-out"
|
|
|
|
USING_ENTITIES = "using-entities-here"
|
|
|
|
USING_KEYBOARD = "using-keyboard-here"
|
|
|
|
SO_COOL = "so-cool"
|
|
|
|
|
2021-03-28 18:53:44 +02:00
|
|
|
# Callback data to pass in 3rd level deep-linking
|
2021-01-28 17:12:13 +01:00
|
|
|
KEYBOARD_CALLBACKDATA = "keyboard-callback-data"
|
2019-09-13 21:09:05 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2019-09-13 21:09:05 +02:00
|
|
|
"""Send a deep-linked URL when the command /start is issued."""
|
|
|
|
bot = context.bot
|
2021-01-28 17:12:13 +01:00
|
|
|
url = helpers.create_deep_linked_url(bot.username, CHECK_THIS_OUT, group=True)
|
2019-09-13 21:09:05 +02:00
|
|
|
text = "Feel free to tell your friends about it:\n\n" + url
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text(text)
|
2019-09-13 21:09:05 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def deep_linked_level_1(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2019-09-13 21:09:05 +02:00
|
|
|
"""Reached through the CHECK_THIS_OUT payload"""
|
|
|
|
bot = context.bot
|
2021-01-28 17:12:13 +01:00
|
|
|
url = helpers.create_deep_linked_url(bot.username, SO_COOL)
|
2020-10-09 17:22:07 +02:00
|
|
|
text = (
|
|
|
|
"Awesome, you just accessed hidden functionality! "
|
2021-03-28 18:53:44 +02:00
|
|
|
"Now let's get back to the private chat."
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2019-09-13 21:09:05 +02:00
|
|
|
keyboard = InlineKeyboardMarkup.from_button(
|
2021-01-28 17:12:13 +01:00
|
|
|
InlineKeyboardButton(text="Continue here!", url=url)
|
2019-09-13 21:09:05 +02:00
|
|
|
)
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text(text, reply_markup=keyboard)
|
2019-09-13 21:09:05 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def deep_linked_level_2(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2019-09-13 21:09:05 +02:00
|
|
|
"""Reached through the SO_COOL payload"""
|
|
|
|
bot = context.bot
|
2021-01-28 17:12:13 +01:00
|
|
|
url = helpers.create_deep_linked_url(bot.username, USING_ENTITIES)
|
2022-05-05 17:40:22 +02:00
|
|
|
text = f'You can also mask the deep-linked URLs as links: <a href="{url}">▶️ CLICK HERE</a>.'
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text(text, parse_mode=ParseMode.HTML, disable_web_page_preview=True)
|
2019-09-13 21:09:05 +02:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def deep_linked_level_3(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2019-09-13 21:09:05 +02:00
|
|
|
"""Reached through the USING_ENTITIES payload"""
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text(
|
2021-01-28 17:12:13 +01:00
|
|
|
"It is also possible to make deep-linking using InlineKeyboardButtons.",
|
|
|
|
reply_markup=InlineKeyboardMarkup(
|
|
|
|
[[InlineKeyboardButton(text="Like this!", callback_data=KEYBOARD_CALLBACKDATA)]]
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def deep_link_level_3_callback(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2021-01-28 17:12:13 +01:00
|
|
|
"""Answers CallbackQuery with deeplinking url."""
|
|
|
|
bot = context.bot
|
|
|
|
url = helpers.create_deep_linked_url(bot.username, USING_KEYBOARD)
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.callback_query.answer(url=url)
|
2021-01-28 17:12:13 +01:00
|
|
|
|
|
|
|
|
2022-05-12 19:36:25 +02:00
|
|
|
async def deep_linked_level_4(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
2021-01-28 17:12:13 +01:00
|
|
|
"""Reached through the USING_KEYBOARD payload"""
|
2019-09-13 21:09:05 +02:00
|
|
|
payload = context.args
|
2022-04-24 12:38:09 +02:00
|
|
|
await update.message.reply_text(
|
2020-11-23 22:09:29 +01:00
|
|
|
f"Congratulations! This is as deep as it gets 👏🏻\n\nThe payload was: {payload}"
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2019-09-13 21:09:05 +02:00
|
|
|
|
|
|
|
|
2021-03-13 16:21:03 +01:00
|
|
|
def main() -> None:
|
2019-09-13 21:09:05 +02:00
|
|
|
"""Start the bot."""
|
2022-04-24 12:38:09 +02:00
|
|
|
# Create the Application and pass it your bot's token.
|
|
|
|
application = Application.builder().token("TOKEN").build()
|
2019-09-13 21:09:05 +02:00
|
|
|
|
|
|
|
# More info on what deep linking actually is (read this first if it's unclear to you):
|
|
|
|
# https://core.telegram.org/bots#deep-linking
|
|
|
|
|
|
|
|
# Register a deep-linking handler
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(
|
2021-11-20 11:36:18 +01:00
|
|
|
CommandHandler("start", deep_linked_level_1, filters.Regex(CHECK_THIS_OUT))
|
2020-10-31 16:33:34 +01:00
|
|
|
)
|
2019-09-13 21:09:05 +02:00
|
|
|
|
|
|
|
# This one works with a textual link instead of an URL
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(CommandHandler("start", deep_linked_level_2, filters.Regex(SO_COOL)))
|
2019-09-13 21:09:05 +02:00
|
|
|
|
|
|
|
# We can also pass on the deep-linking payload
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(
|
2021-11-20 11:36:18 +01:00
|
|
|
CommandHandler("start", deep_linked_level_3, filters.Regex(USING_ENTITIES))
|
2020-10-09 17:22:07 +02:00
|
|
|
)
|
2019-09-13 21:09:05 +02:00
|
|
|
|
2021-03-28 18:53:44 +02:00
|
|
|
# Possible with inline keyboard buttons as well
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(
|
2021-11-20 11:36:18 +01:00
|
|
|
CommandHandler("start", deep_linked_level_4, filters.Regex(USING_KEYBOARD))
|
2021-01-28 17:12:13 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# register callback handler for inline keyboard button
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(
|
2021-01-28 17:12:13 +01:00
|
|
|
CallbackQueryHandler(deep_link_level_3_callback, pattern=KEYBOARD_CALLBACKDATA)
|
|
|
|
)
|
|
|
|
|
2019-09-13 21:09:05 +02:00
|
|
|
# Make sure the deep-linking handlers occur *before* the normal /start handler.
|
2022-04-24 12:38:09 +02:00
|
|
|
application.add_handler(CommandHandler("start", start))
|
2019-09-13 21:09:05 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
# Run the bot until the user presses Ctrl-C
|
|
|
|
application.run_polling()
|
2019-09-13 21:09:05 +02:00
|
|
|
|
|
|
|
|
2021-01-28 17:12:13 +01:00
|
|
|
if __name__ == "__main__":
|
2019-09-13 21:09:05 +02:00
|
|
|
main()
|