Skip to content

Commit 9afe680

Browse files
committed
Added various deep linking tools
1 parent 9002fa8 commit 9afe680

File tree

16 files changed

+238
-54
lines changed

16 files changed

+238
-54
lines changed

examples/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Examples
22

3+
**BEFORE YOU START:** In order to run all of the examples, you must insert the token you received from @BotFather in the file `settings.py`!
4+
35
The examples in this folder are small bots meant to show you how a bot that is written with `python-telegram-bot` looks like. Some bots focus on one specific aspect of the Telegram Bot API while others focus on one of the mechanics of this library. Except for the [`echobot.py`](#pure-api) example, they all use the high-level framework this library provides with the [`telegram.ext`](https://python-telegram-bot.readthedocs.io/en/latest/telegram.ext.html) submodule.
46

57
All examples are licensed under the [CC0 License](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/LICENSE.txt) and are therefore fully dedicated to the public domain. You can use them as the base for your own bots without worrying about copyrights.

examples/__init__.py

Whitespace-only changes.

examples/conversationbot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
Press Ctrl-C on the command line or send a signal to the process to stop the
1717
bot.
1818
"""
19-
19+
from examples.settings import TOKEN
2020
from telegram import (ReplyKeyboardMarkup, ReplyKeyboardRemove)
2121
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler,
2222
ConversationHandler)
@@ -117,8 +117,8 @@ def error(bot, update, error):
117117

118118

119119
def main():
120-
# Create the EventHandler and pass it your bot's token.
121-
updater = Updater("TOKEN")
120+
# Create the Updater and pass it your bot's token as a string.
121+
updater = Updater(TOKEN)
122122

123123
# Get the dispatcher to register handlers
124124
dp = updater.dispatcher

examples/conversationbot2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
Press Ctrl-C on the command line or send a signal to the process to stop the
1717
bot.
1818
"""
19-
19+
from examples.settings import TOKEN
2020
from telegram import ReplyKeyboardMarkup
2121
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, RegexHandler,
2222
ConversationHandler)
@@ -103,8 +103,8 @@ def error(bot, update, error):
103103

104104

105105
def main():
106-
# Create the Updater and pass it your bot's token.
107-
updater = Updater("TOKEN")
106+
# Create the Updater and pass it your bot's token as a string.
107+
updater = Updater(TOKEN)
108108

109109
# Get the dispatcher to register handlers
110110
dp = updater.dispatcher

examples/deeplinking.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 examples.settings import TOKEN
23+
from telegram import ParseMode
24+
from telegram.ext import Updater, CommandHandler, Filters
25+
# Enable logging
26+
from telegram.utils import helpers
27+
28+
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
29+
level=logging.INFO)
30+
31+
logger = logging.getLogger(__name__)
32+
33+
# Define constants the will allow us to reuse the deep-linking parameters.
34+
CHECK_THIS_OUT = 'check-this-out'
35+
USING_ENTITIES = 'using-entities-here'
36+
SO_COOL = 'so-cool'
37+
38+
39+
def start(bot, update):
40+
"""Send a deep-linked URL when the command /start is issued."""
41+
url = helpers.create_deep_linked_url(bot.get_me().username, CHECK_THIS_OUT)
42+
text = "Feel free to tell your friends about it:\n\n" + url
43+
update.message.reply_text(text)
44+
45+
46+
def deep_linked_level_1(bot, update):
47+
"""Reached through the CHECK_THIS_OUT payload"""
48+
url = helpers.create_deep_linked_url(bot.get_me().username, SO_COOL)
49+
text = "Awesome, you just accessed hidden functionality!\n\nContinue here: " + url
50+
update.message.reply_text(text)
51+
52+
53+
def deep_linked_level_2(bot, update):
54+
"""Reached through the SO_COOL payload"""
55+
url = helpers.create_deep_linked_url(bot.get_me().username, USING_ENTITIES)
56+
text = "You can also mask the deep-linked URLs as links: " \
57+
"[▶️ CLICK HERE]({0}).".format(url)
58+
update.message.reply_text(text, parse_mode=ParseMode.MARKDOWN, disable_web_page_preview=True)
59+
60+
61+
def deep_linked_level_3(bot, update, args):
62+
"""Reached through the USING_ENTITIES payload"""
63+
payload = args
64+
update.message.reply_text("Congratulations! This is as deep as it gets 👏🏻\n\n"
65+
"The payload was: {0}".format(payload))
66+
67+
68+
def error(bot, update, error):
69+
"""Log Errors caused by Updates."""
70+
logger.warning('Update "%s" caused error "%s"', update, error)
71+
72+
73+
def main():
74+
"""Start the bot."""
75+
# Create the Updater and pass it your bot's token as a string.
76+
updater = Updater(TOKEN)
77+
78+
# Get the dispatcher to register handlers
79+
dp = updater.dispatcher
80+
81+
# Register a deep-linking handler
82+
dp.add_handler(CommandHandler("start", deep_linked_level_1, Filters.regex(CHECK_THIS_OUT)))
83+
84+
# This one works with a textual link instead of an URL
85+
dp.add_handler(CommandHandler("start", deep_linked_level_2, Filters.regex(SO_COOL)))
86+
87+
# We can also pass on the deep-linking payload
88+
dp.add_handler(CommandHandler("start",
89+
deep_linked_level_3,
90+
Filters.regex(USING_ENTITIES),
91+
pass_args=True))
92+
93+
# Make sure the deep-linking handlers occur *before* the normal /start handler.
94+
dp.add_handler(CommandHandler("start", start))
95+
96+
# log all errors
97+
dp.add_error_handler(error)
98+
99+
# Start the Bot
100+
updater.start_polling()
101+
102+
# Run the bot until you press Ctrl-C or the process receives SIGINT,
103+
# SIGTERM or SIGABRT. This should be used most of the time, since
104+
# start_polling() is non-blocking and will stop the bot gracefully.
105+
updater.idle()
106+
107+
108+
if __name__ == '__main__':
109+
main()

examples/echobot.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@
77
This program is dedicated to the public domain under the CC0 license.
88
"""
99
import logging
10-
import telegram
11-
from telegram.error import NetworkError, Unauthorized
1210
from time import sleep
1311

12+
import telegram
13+
from examples.settings import TOKEN
14+
from telegram.error import NetworkError, Unauthorized
1415

1516
update_id = None
1617

1718

1819
def main():
1920
"""Run the bot."""
2021
global update_id
21-
# Telegram Bot Authorization Token
22-
bot = telegram.Bot('TOKEN')
22+
# Telegram Bot Authorization Token as string
23+
bot = telegram.Bot(TOKEN)
2324

2425
# get the first pending update_id, this is so we can skip over it in case
2526
# we get an "Unauthorized" exception.

examples/echobot2.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
Press Ctrl-C on the command line or send a signal to the process to stop the
1717
bot.
1818
"""
19-
19+
from examples.settings import TOKEN
2020
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
2121
import logging
2222

@@ -51,8 +51,8 @@ def error(bot, update, error):
5151

5252
def main():
5353
"""Start the bot."""
54-
# Create the EventHandler and pass it your bot's token.
55-
updater = Updater("TOKEN")
54+
# Create the Updater and pass it your bot's token as a string.
55+
updater = Updater(TOKEN)
5656

5757
# Get the dispatcher to register handlers
5858
dp = updater.dispatcher

examples/inlinebot.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import re
2222

23+
from examples.settings import TOKEN
2324
from telegram.utils.helpers import escape_markdown
2425

2526
from telegram import InlineQueryResultArticle, ParseMode, \
@@ -77,8 +78,8 @@ def error(bot, update, error):
7778

7879

7980
def main():
80-
# Create the Updater and pass it your bot's token.
81-
updater = Updater("TOKEN")
81+
# Create the Updater and pass it your bot's token as a string.
82+
updater = Updater(TOKEN)
8283

8384
# Get the dispatcher to register handlers
8485
dp = updater.dispatcher

examples/inlinekeyboard.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# This program is dedicated to the public domain under the CC0 license.
66
"""
77
import logging
8+
9+
from examples.settings import TOKEN
810
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
911
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler
1012

@@ -42,8 +44,8 @@ def error(bot, update, error):
4244

4345

4446
def main():
45-
# Create the Updater and pass it your bot's token.
46-
updater = Updater("TOKEN")
47+
# Create the Updater and pass it your bot's token as a string.
48+
updater = Updater(TOKEN)
4749

4850
updater.dispatcher.add_handler(CommandHandler('start', start))
4951
updater.dispatcher.add_handler(CallbackQueryHandler(button))

examples/paymentbot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
This program is dedicated to the public domain under the CC0 license.
77
"""
8-
8+
from examples.settings import TOKEN
99
from telegram import (LabeledPrice, ShippingOption)
1010
from telegram.ext import (Updater, CommandHandler, MessageHandler,
1111
Filters, PreCheckoutQueryHandler, ShippingQueryHandler)
@@ -112,8 +112,8 @@ def successful_payment_callback(bot, update):
112112

113113

114114
def main():
115-
# Create the EventHandler and pass it your bot's token.
116-
updater = Updater(token="BOT_TOKEN")
115+
# Create the Updater and pass it your bot's token as a string.
116+
updater = Updater(TOKEN)
117117

118118
# Get the dispatcher to register handlers
119119
dp = updater.dispatcher

0 commit comments

Comments
 (0)