|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +"""Bot that explains Telegram's "Deep Linking Parameters" functionality. |
| 5 | +
|
| 6 | +This program is dedicated to the public domain under the CC0 license. |
| 7 | +
|
| 8 | +This Bot uses the Updater class to handle the bot. |
| 9 | +
|
| 10 | +First, a few handler functions are defined. Then, those functions are passed to |
| 11 | +the Dispatcher and registered at their respective places. |
| 12 | +Then, the bot is started and runs until we press Ctrl-C on the command line. |
| 13 | +
|
| 14 | +Usage: |
| 15 | +Deep Linking example. Send /start to get the link. |
| 16 | +Press Ctrl-C on the command line or send a signal to the process to stop the |
| 17 | +bot. |
| 18 | +""" |
| 19 | + |
| 20 | +import logging |
| 21 | + |
| 22 | +from telegram import ParseMode |
| 23 | +from telegram.ext import Updater, CommandHandler, Filters |
| 24 | +# Enable logging |
| 25 | +from telegram.utils import helpers |
| 26 | + |
| 27 | +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', |
| 28 | + level=logging.INFO) |
| 29 | + |
| 30 | +logger = logging.getLogger(__name__) |
| 31 | + |
| 32 | +# Define constants the will allow us to reuse the deep-linking parameters. |
| 33 | +CHECK_THIS_OUT = 'check-this-out' |
| 34 | +USING_ENTITIES = 'using-entities-here' |
| 35 | +SO_COOL = 'so-cool' |
| 36 | + |
| 37 | + |
| 38 | +def start(bot, update): |
| 39 | + """Send a deep-linked URL when the command /start is issued.""" |
| 40 | + url = helpers.create_deep_linked_url(bot.get_me().username, CHECK_THIS_OUT) |
| 41 | + text = "Feel free to tell your friends about it:\n\n" + url |
| 42 | + update.message.reply_text(text) |
| 43 | + |
| 44 | + |
| 45 | +def deep_linked_level_1(bot, update): |
| 46 | + """Reached through the CHECK_THIS_OUT payload""" |
| 47 | + url = helpers.create_deep_linked_url(bot.get_me().username, SO_COOL) |
| 48 | + text = "Awesome, you just accessed hidden functionality!\n\nContinue here: " + url |
| 49 | + update.message.reply_text(text) |
| 50 | + |
| 51 | + |
| 52 | +def deep_linked_level_2(bot, update): |
| 53 | + """Reached through the SO_COOL payload""" |
| 54 | + url = helpers.create_deep_linked_url(bot.get_me().username, USING_ENTITIES) |
| 55 | + text = "You can also mask the deep-linked URLs as links: " \ |
| 56 | + "[▶️ CLICK HERE]({0}).".format(url) |
| 57 | + update.message.reply_text(text, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True) |
| 58 | + |
| 59 | + |
| 60 | +def deep_linked_level_3(bot, update, args): |
| 61 | + """Reached through the USING_ENTITIES payload""" |
| 62 | + payload = args |
| 63 | + update.message.reply_text("Congratulations! This is as deep as it gets 👏🏻\n\n" |
| 64 | + "The payload was: {0}".format(payload)) |
| 65 | + |
| 66 | + |
| 67 | +def error(bot, update, error): |
| 68 | + """Log Errors caused by Updates.""" |
| 69 | + logger.warning('Update "%s" caused error "%s"', update, error) |
| 70 | + |
| 71 | + |
| 72 | +def main(): |
| 73 | + """Start the bot.""" |
| 74 | + # Create the Updater and pass it your bot's token. |
| 75 | + updater = Updater("TOKEN") |
| 76 | + |
| 77 | + # Get the dispatcher to register handlers |
| 78 | + dp = updater.dispatcher |
| 79 | + |
| 80 | + # More info on what deep linking actually is (read this first if it's unclear to you): |
| 81 | + # https://core.telegram.org/bots#deep-linking |
| 82 | + |
| 83 | + # Register a deep-linking handler |
| 84 | + dp.add_handler(CommandHandler("start", deep_linked_level_1, Filters.regex(CHECK_THIS_OUT))) |
| 85 | + |
| 86 | + # This one works with a textual link instead of an URL |
| 87 | + dp.add_handler(CommandHandler("start", deep_linked_level_2, Filters.regex(SO_COOL))) |
| 88 | + |
| 89 | + # We can also pass on the deep-linking payload |
| 90 | + dp.add_handler(CommandHandler("start", |
| 91 | + deep_linked_level_3, |
| 92 | + Filters.regex(USING_ENTITIES), |
| 93 | + pass_args=True)) |
| 94 | + |
| 95 | + # Make sure the deep-linking handlers occur *before* the normal /start handler. |
| 96 | + dp.add_handler(CommandHandler("start", start)) |
| 97 | + |
| 98 | + # log all errors |
| 99 | + dp.add_error_handler(error) |
| 100 | + |
| 101 | + # Start the Bot |
| 102 | + updater.start_polling() |
| 103 | + |
| 104 | + # Run the bot until you press Ctrl-C or the process receives SIGINT, |
| 105 | + # SIGTERM or SIGABRT. This should be used most of the time, since |
| 106 | + # start_polling() is non-blocking and will stop the bot gracefully. |
| 107 | + updater.idle() |
| 108 | + |
| 109 | + |
| 110 | +if __name__ == '__main__': |
| 111 | + main() |
0 commit comments