GeezerVison is showing github.com — Add Version Check to Examples (#3036) · MSutt/python-telegram-bot@3013870
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .github/workflows/example_notifier.yml -16Lines changed: 0 additions & 16 deletions Collapse file Copy file name to clipboardExpand all lines: examples/README.md +3-2Lines changed: 3 additions & 2 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | # Examples | | 2 | +## ⚠️ The examples in this directory are subject to changes and may not work on the PTB version that you are using. You can find the examples compatible with a specific version at https://github.com/python-telegram-bot/python-telegram-bot/tree/v<X.Y>/examples | 2 | 3 |
| 3 | 4 | In this folder are small examples to show what a bot 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 [`rawapibot.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. | 4 | 5 |
| 5 | 6 | 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. | 6 | 7 |
| 7 | 8 | Do note that we ignore one pythonic convention. Best practice would dictate, in many handler callbacks function signatures, to replace the argument `context` with an underscore, since `context` is an unused local variable in those callbacks. However, since these are examples and not having a name for that argument confuses beginners, we decided to have it present. | 8 | 9 |
| 9 | | -### [`echobot.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/echobot.py) | | 10 | +### [`echobot.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/echobot.py) | 10 | 11 | This is probably the base for most of the bots made with `python-telegram-bot`. It simply replies to each text message with a message that contains the same text. | 11 | 12 |
| 12 | | -### [`timerbot.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/timerbot.py) | | 13 | +### [`timerbot.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/timerbot.py) | 13 | 14 | This bot uses the [`JobQueue`](https://python-telegram-bot.readthedocs.io/en/latest/telegram.ext.jobqueue.html) class to send timed messages. The user sets a timer by using `/set` command with a specific time, for example `/set 30`. The bot then sets up a job to send a message to that user after 30 seconds. The user can also cancel the timer by sending `/unset`. To learn more about the `JobQueue`, read [this wiki article](https://github.com/python-telegram-bot/python-telegram-bot/wiki/Extensions-%E2%80%93-JobQueue). | 14 | 15 |
| 15 | 16 | ### [`conversationbot.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/conversationbot.py) | |
Collapse file Copy file name to clipboardExpand all lines: examples/arbitrarycallbackdatabot.py +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #!/usr/bin/env python | 2 | | -# pylint: disable=unused-argument | | 2 | +# pylint: disable=unused-argument, wrong-import-position | 3 | 3 | # This program is dedicated to the public domain under the CC0 license. | 4 | 4 |
| 5 | 5 | """This example showcases how PTBs "arbitrary callback data" feature can be used. | | 10 | 10 | import logging | 11 | 11 | from typing import List, Tuple, cast | 12 | 12 |
| | 13 | +from telegram import __version__ as TG_VER | | 14 | + | | 15 | +try: | | 16 | + from telegram import __version_info__ | | 17 | +except ImportError: | | 18 | + __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment] | | 19 | + | | 20 | +if __version_info__ < (20, 0, 0, "alpha", 1): | | 21 | + raise RuntimeError( | | 22 | + f"This example is not compatible with your current PTB version {TG_VER}. To view the " | | 23 | + f"{TG_VER} version of this example, " | | 24 | + f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples" | | 25 | + ) | 13 | 26 | from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update | 14 | 27 | from telegram.ext import ( | 15 | 28 | Application, | |
Collapse file Copy file name to clipboardExpand all lines: examples/chatmemberbot.py +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #!/usr/bin/env python | 2 | | -# pylint: disable=unused-argument | | 2 | +# pylint: disable=unused-argument, wrong-import-position | 3 | 3 | # This program is dedicated to the public domain under the CC0 license. | 4 | 4 |
| 5 | 5 | """ | | 14 | 14 | import logging | 15 | 15 | from typing import Optional, Tuple | 16 | 16 |
| | 17 | +from telegram import __version__ as TG_VER | | 18 | + | | 19 | +try: | | 20 | + from telegram import __version_info__ | | 21 | +except ImportError: | | 22 | + __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment] | | 23 | + | | 24 | +if __version_info__ < (20, 0, 0, "alpha", 1): | | 25 | + raise RuntimeError( | | 26 | + f"This example is not compatible with your current PTB version {TG_VER}. To view the " | | 27 | + f"{TG_VER} version of this example, " | | 28 | + f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples" | | 29 | + ) | 17 | 30 | from telegram import Chat, ChatMember, ChatMemberUpdated, Update | 18 | 31 | from telegram.constants import ParseMode | 19 | 32 | from telegram.ext import Application, ChatMemberHandler, CommandHandler, ContextTypes | |
Collapse file Copy file name to clipboardExpand all lines: examples/contexttypesbot.py +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #!/usr/bin/env python | 2 | | -# pylint: disable=unused-argument | | 2 | +# pylint: disable=unused-argument, wrong-import-position | 3 | 3 | # This program is dedicated to the public domain under the CC0 license. | 4 | 4 |
| 5 | 5 | """ | | 14 | 14 | from collections import defaultdict | 15 | 15 | from typing import DefaultDict, Optional, Set | 16 | 16 |
| | 17 | +from telegram import __version__ as TG_VER | | 18 | + | | 19 | +try: | | 20 | + from telegram import __version_info__ | | 21 | +except ImportError: | | 22 | + __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment] | | 23 | + | | 24 | +if __version_info__ < (20, 0, 0, "alpha", 1): | | 25 | + raise RuntimeError( | | 26 | + f"This example is not compatible with your current PTB version {TG_VER}. To view the " | | 27 | + f"{TG_VER} version of this example, " | | 28 | + f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples" | | 29 | + ) | 17 | 30 | from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update | 18 | 31 | from telegram.constants import ParseMode | 19 | 32 | from telegram.ext import ( | |
Collapse file Copy file name to clipboardExpand all lines: examples/conversationbot.py +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #!/usr/bin/env python | 2 | | -# pylint: disable=unused-argument | | 2 | +# pylint: disable=unused-argument, wrong-import-position | 3 | 3 | # This program is dedicated to the public domain under the CC0 license. | 4 | 4 |
| 5 | 5 | """ | | 16 | 16 |
| 17 | 17 | import logging | 18 | 18 |
| | 19 | +from telegram import __version__ as TG_VER | | 20 | + | | 21 | +try: | | 22 | + from telegram import __version_info__ | | 23 | +except ImportError: | | 24 | + __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment] | | 25 | + | | 26 | +if __version_info__ < (20, 0, 0, "alpha", 1): | | 27 | + raise RuntimeError( | | 28 | + f"This example is not compatible with your current PTB version {TG_VER}. To view the " | | 29 | + f"{TG_VER} version of this example, " | | 30 | + f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples" | | 31 | + ) | 19 | 32 | from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update | 20 | 33 | from telegram.ext import ( | 21 | 34 | Application, | |
Collapse file Copy file name to clipboardExpand all lines: examples/conversationbot2.py +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #!/usr/bin/env python | 2 | | -# pylint: disable=unused-argument | | 2 | +# pylint: disable=unused-argument, wrong-import-position | 3 | 3 | # This program is dedicated to the public domain under the CC0 license. | 4 | 4 |
| 5 | 5 | """ | | 17 | 17 | import logging | 18 | 18 | from typing import Dict | 19 | 19 |
| | 20 | +from telegram import __version__ as TG_VER | | 21 | + | | 22 | +try: | | 23 | + from telegram import __version_info__ | | 24 | +except ImportError: | | 25 | + __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment] | | 26 | + | | 27 | +if __version_info__ < (20, 0, 0, "alpha", 1): | | 28 | + raise RuntimeError( | | 29 | + f"This example is not compatible with your current PTB version {TG_VER}. To view the " | | 30 | + f"{TG_VER} version of this example, " | | 31 | + f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples" | | 32 | + ) | 20 | 33 | from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update | 21 | 34 | from telegram.ext import ( | 22 | 35 | Application, | |
Collapse file Copy file name to clipboardExpand all lines: examples/deeplinking.py +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #!/usr/bin/env python | 2 | | -# pylint: disable=unused-argument | | 2 | +# pylint: disable=unused-argument, wrong-import-position | 3 | 3 | # This program is dedicated to the public domain under the CC0 license. | 4 | 4 |
| 5 | 5 | """Bot that explains Telegram's "Deep Linking Parameters" functionality. | | 20 | 20 |
| 21 | 21 | import logging | 22 | 22 |
| | 23 | +from telegram import __version__ as TG_VER | | 24 | + | | 25 | +try: | | 26 | + from telegram import __version_info__ | | 27 | +except ImportError: | | 28 | + __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment] | | 29 | + | | 30 | +if __version_info__ < (20, 0, 0, "alpha", 1): | | 31 | + raise RuntimeError( | | 32 | + f"This example is not compatible with your current PTB version {TG_VER}. To view the " | | 33 | + f"{TG_VER} version of this example, " | | 34 | + f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples" | | 35 | + ) | 23 | 36 | from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, helpers | 24 | 37 | from telegram.constants import ParseMode | 25 | 38 | from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, filters | |
Collapse file Copy file name to clipboardExpand all lines: examples/echobot.py +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #!/usr/bin/env python | 2 | | -# pylint: disable=unused-argument | | 2 | +# pylint: disable=unused-argument, wrong-import-position | 3 | 3 | # This program is dedicated to the public domain under the CC0 license. | 4 | 4 |
| 5 | 5 | """ | | 17 | 17 |
| 18 | 18 | import logging | 19 | 19 |
| | 20 | +from telegram import __version__ as TG_VER | | 21 | + | | 22 | +try: | | 23 | + from telegram import __version_info__ | | 24 | +except ImportError: | | 25 | + __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment] | | 26 | + | | 27 | +if __version_info__ < (20, 0, 0, "alpha", 1): | | 28 | + raise RuntimeError( | | 29 | + f"This example is not compatible with your current PTB version {TG_VER}. To view the " | | 30 | + f"{TG_VER} version of this example, " | | 31 | + f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples" | | 32 | + ) | 20 | 33 | from telegram import ForceReply, Update | 21 | 34 | from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters | 22 | 35 |
| |
Collapse file Copy file name to clipboardExpand all lines: examples/errorhandlerbot.py +14-1Lines changed: 14 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #!/usr/bin/env python | 2 | | -# pylint: disable=unused-argument | | 2 | +# pylint: disable=unused-argument, wrong-import-position | 3 | 3 | # This program is dedicated to the public domain under the CC0 license. | 4 | 4 |
| 5 | 5 | """This is a very simple example on how one could implement a custom error handler.""" | | 8 | 8 | import logging | 9 | 9 | import traceback | 10 | 10 |
| | 11 | +from telegram import __version__ as TG_VER | | 12 | + | | 13 | +try: | | 14 | + from telegram import __version_info__ | | 15 | +except ImportError: | | 16 | + __version_info__ = (0, 0, 0, 0, 0) # type: ignore[assignment] | | 17 | + | | 18 | +if __version_info__ < (20, 0, 0, "alpha", 1): | | 19 | + raise RuntimeError( | | 20 | + f"This example is not compatible with your current PTB version {TG_VER}. To view the " | | 21 | + f"{TG_VER} version of this example, " | | 22 | + f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples" | | 23 | + ) | 11 | 24 | from telegram import Update | 12 | 25 | from telegram.constants import ParseMode | 13 | 26 | from telegram.ext import Application, CommandHandler, ContextTypes | |
|