Add Version Check to Examples (#3036)

This commit is contained in:
Harshil 2022-05-15 17:38:40 +05:30 committed by GitHub
parent 5e924014de
commit 3013870c1f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 257 additions and 41 deletions

View file

@ -1,16 +0,0 @@
name: Warning maintainers
on:
pull_request_target:
paths: examples/**
permissions:
pull-requests: write
jobs:
job:
runs-on: ubuntu-latest
name: about example change
steps:
- name: running the check
uses: Poolitzer/notifier-action@master
with:
notify-message: Hey there. Relax, I am just a little warning for the maintainers to release directly after merging your PR, otherwise we have broken examples and people might get confused :)
repo-token: ${{ secrets.GITHUB_TOKEN }}

View file

@ -1,4 +1,5 @@
# Examples
## ⚠️ 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
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.
@ -6,10 +7,10 @@ All examples are licensed under the [CC0 License](https://github.com/python-tele
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.
### [`echobot.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/echobot.py)
### [`echobot.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/echobot.py)
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.
### [`timerbot.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/timerbot.py)
### [`timerbot.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/timerbot.py)
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).
### [`conversationbot.py`](https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/conversationbot.py)

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""This example showcases how PTBs "arbitrary callback data" feature can be used.
@ -10,6 +10,19 @@ https://github.com/python-telegram-bot/python-telegram-bot/wiki/Arbitrary-callba
import logging
from typing import List, Tuple, cast
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import (
Application,

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""
@ -14,6 +14,19 @@ bot.
import logging
from typing import Optional, Tuple
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import Chat, ChatMember, ChatMemberUpdated, Update
from telegram.constants import ParseMode
from telegram.ext import Application, ChatMemberHandler, CommandHandler, ContextTypes

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""
@ -14,6 +14,19 @@ import logging
from collections import defaultdict
from typing import DefaultDict, Optional, Set
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.constants import ParseMode
from telegram.ext import (

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""
@ -16,6 +16,19 @@ bot.
import logging
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram.ext import (
Application,

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""
@ -17,6 +17,19 @@ bot.
import logging
from typing import Dict
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram.ext import (
Application,

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""Bot that explains Telegram's "Deep Linking Parameters" functionality.
@ -20,6 +20,19 @@ bot.
import logging
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update, helpers
from telegram.constants import ParseMode
from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes, filters

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""
@ -17,6 +17,19 @@ bot.
import logging
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import ForceReply, Update
from telegram.ext import Application, CommandHandler, ContextTypes, MessageHandler, filters

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""This is a very simple example on how one could implement a custom error handler."""
@ -8,6 +8,19 @@ import json
import logging
import traceback
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import Update
from telegram.constants import ParseMode
from telegram.ext import Application, CommandHandler, ContextTypes

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""
@ -16,6 +16,19 @@ import logging
from html import escape
from uuid import uuid4
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import InlineQueryResultArticle, InputTextMessageContent, Update
from telegram.constants import ParseMode
from telegram.ext import Application, CommandHandler, ContextTypes, InlineQueryHandler

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""
@ -8,6 +8,19 @@ Basic example for a bot that uses inline keyboards. For an in-depth explanation,
"""
import logging
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import Application, CallbackQueryHandler, CommandHandler, ContextTypes

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""Simple inline keyboard bot with multiple CallbackQueryHandlers.
@ -16,6 +16,19 @@ Press Ctrl-C on the command line to stop the bot.
"""
import logging
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import (
Application,

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""
@ -17,6 +17,19 @@ bot.
import logging
from typing import Any, Dict, Tuple
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
from telegram.ext import (
Application,

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""
@ -14,6 +14,19 @@ See https://github.com/python-telegram-bot/python-telegram-bot/wiki/Telegram-Pas
import logging
from pathlib import Path
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import Update
from telegram.ext import Application, ContextTypes, MessageHandler, filters

View file

@ -1,11 +1,24 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""Basic example for a bot that can receive payment from user."""
import logging
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import LabeledPrice, ShippingOption, Update
from telegram.ext import (
Application,

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""
@ -17,6 +17,19 @@ bot.
import logging
from typing import Dict
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove, Update
from telegram.ext import (
Application,

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""
@ -9,6 +9,19 @@ one the user sends the bot
"""
import logging
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import (
KeyboardButton,
KeyboardButtonPollType,

View file

@ -1,4 +1,5 @@
#!/usr/bin/env python
# pylint: disable=wrong-import-position
"""Simple Bot to reply to Telegram messages.
This is built on the API wrapper, see echobot.py to see the same example built
@ -9,6 +10,19 @@ import asyncio
import logging
from typing import NoReturn
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] # 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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import Bot
from telegram.error import Forbidden, NetworkError

View file

@ -1,5 +1,5 @@
#!/usr/bin/env python
# pylint: disable=unused-argument
# pylint: disable=unused-argument, wrong-import-position
# This program is dedicated to the public domain under the CC0 license.
"""
@ -20,6 +20,19 @@ bot.
import logging
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, "
f"visit https://github.com/python-telegram-bot/python-telegram-bot/tree/v{TG_VER}/examples"
)
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes

View file

@ -2292,9 +2292,7 @@ class Bot(TelegramObject, AbstractAsyncContextManager):
"Either venue or latitude, longitude, address and title must be "
"passed as arguments."
)
if not (
bool(venue) ^ any([latitude, longitude, address, title])
): # pylint: disable=superfluous-parens
if not bool(venue) ^ any([latitude, longitude, address, title]):
raise ValueError(
"Either venue or latitude, longitude, address and title must be "
"passed as arguments. Not both."
@ -2421,9 +2419,7 @@ class Bot(TelegramObject, AbstractAsyncContextManager):
raise ValueError(
"Either contact or phone_number and first_name must be passed as arguments."
)
if not (
bool(contact) ^ any([phone_number, first_name])
): # pylint: disable=superfluous-parens
if not bool(contact) ^ any([phone_number, first_name]):
raise ValueError(
"Either contact or phone_number and first_name must be passed as arguments. "
"Not both."