mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2024-12-22 22:45:09 +01:00
8406889179
* Remove usages of python-future lib * Remove python2 datetime.timezone replacement * Remove python2 workaround in InputFile.__init__ * Remove import of str necessary for python2 * Remove urllib2 import necessary for python2 * Remove a mention of python 2 in doc * Remove python 2 from travis config file * Remove python 2 from appveyor config * Remove python2 from debian build rules * Remove unnecessarry aliasing of time.perf_counter * Remove python 2 from github workflow * Remove mention of python 2 in descriptions/readme * Remove version check for queue import * Remove version checks in tests * Adjust docs to correctly mention supported version * Fix indentation * Remove unused 'sys' imports * Fix indentation * Remove references to mq.curtime in tests * Replace super calls by argumentsless version * Remove future dependency * Fix error in de_json declaration * Use python3 metaclass syntax * Use implicit inheriting from object * Remove accidentally committed .vscode folder * Use nameless f-string and raw string * Fix regex string literal syntax * Remove old style classes * Run pyupgrade * Fix leftover from automatic merge * Fix lint errors * Update telegram/files/sticker.py Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
111 lines
3.9 KiB
Python
111 lines
3.9 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""Bot that explains Telegram's "Deep Linking Parameters" functionality.
|
|
|
|
This program is dedicated to the public domain under the CC0 license.
|
|
|
|
This Bot uses the Updater class to handle the bot.
|
|
|
|
First, a few handler functions are defined. Then, those functions are passed to
|
|
the Dispatcher and registered at their respective places.
|
|
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
|
|
|
|
from telegram import ParseMode, InlineKeyboardMarkup, InlineKeyboardButton
|
|
from telegram.ext import Updater, CommandHandler, Filters
|
|
|
|
# Enable logging
|
|
from telegram.utils import helpers
|
|
|
|
logging.basicConfig(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'
|
|
|
|
|
|
def start(update, context):
|
|
"""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)
|
|
text = "Feel free to tell your friends about it:\n\n" + url
|
|
update.message.reply_text(text)
|
|
|
|
|
|
def deep_linked_level_1(update, context):
|
|
"""Reached through the CHECK_THIS_OUT payload"""
|
|
bot = context.bot
|
|
url = helpers.create_deep_linked_url(bot.get_me().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)
|
|
)
|
|
update.message.reply_text(text, reply_markup=keyboard)
|
|
|
|
|
|
def deep_linked_level_2(update, context):
|
|
"""Reached through the SO_COOL payload"""
|
|
bot = context.bot
|
|
url = helpers.create_deep_linked_url(bot.get_me().username, USING_ENTITIES)
|
|
text = "You can also mask the deep-linked URLs as links: " \
|
|
"[▶️ CLICK HERE]({}).".format(url)
|
|
update.message.reply_text(text, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True)
|
|
|
|
|
|
def deep_linked_level_3(update, context):
|
|
"""Reached through the USING_ENTITIES payload"""
|
|
payload = context.args
|
|
update.message.reply_text("Congratulations! This is as deep as it gets 👏🏻\n\n"
|
|
"The payload was: {}".format(payload))
|
|
|
|
|
|
def main():
|
|
"""Start the bot."""
|
|
# Create the Updater and pass it your bot's token.
|
|
updater = Updater("TOKEN", use_context=True)
|
|
|
|
# Get the dispatcher to register handlers
|
|
dp = updater.dispatcher
|
|
|
|
# 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
|
|
dp.add_handler(CommandHandler("start", deep_linked_level_1, Filters.regex(CHECK_THIS_OUT)))
|
|
|
|
# This one works with a textual link instead of an URL
|
|
dp.add_handler(CommandHandler("start", deep_linked_level_2, Filters.regex(SO_COOL)))
|
|
|
|
# We can also pass on the deep-linking payload
|
|
dp.add_handler(CommandHandler("start",
|
|
deep_linked_level_3,
|
|
Filters.regex(USING_ENTITIES),
|
|
pass_args=True))
|
|
|
|
# Make sure the deep-linking handlers occur *before* the normal /start handler.
|
|
dp.add_handler(CommandHandler("start", start))
|
|
|
|
# Start the Bot
|
|
updater.start_polling()
|
|
|
|
# Run the bot until you press Ctrl-C or the process receives SIGINT,
|
|
# SIGTERM or SIGABRT. This should be used most of the time, since
|
|
# start_polling() is non-blocking and will stop the bot gracefully.
|
|
updater.idle()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|