2016-09-14 19:29:15 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2024-02-19 20:06:25 +01:00
|
|
|
# Copyright (C) 2015-2024
|
2016-09-14 19:29:15 +02:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
This module contains filters for use with :class:`telegram.ext.MessageHandler`,
|
|
|
|
:class:`telegram.ext.CommandHandler`, or :class:`telegram.ext.PrefixHandler`.
|
2018-03-15 05:59:27 +01:00
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionchanged:: 20.0
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
#. Filters are no longer callable, if you're using a custom filter and are calling an existing
|
|
|
|
filter, then switch to the new syntax: ``filters.{filter}.check_update(update)``.
|
|
|
|
#. Removed the ``Filters`` class. The filters are now directly attributes/classes of the
|
2022-02-09 17:30:16 +01:00
|
|
|
:mod:`~telegram.ext.filters` module.
|
2021-11-20 11:36:18 +01:00
|
|
|
#. The names of all filters has been updated:
|
|
|
|
|
Documentation Improvements (#3214, #3217, #3218, #3271, #3289, #3292, #3303, #3312, #3306, #3319, #3326, #3314)
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
Co-authored-by: Simon Fong <44134941+simonfongnt@users.noreply.github.com>
Co-authored-by: Piotr Rogulski <rivinek@gmail.com>
Co-authored-by: poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: Or Bin <or@raftt.io>
Co-authored-by: Sandro <j32g7f67hb@liamekaens.com>
Co-authored-by: Hatim Zahid <63000127+HatimZ@users.noreply.github.com>
Co-authored-by: Robi <53259730+RobiMez@users.noreply.github.com>
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
2022-11-15 09:06:23 +01:00
|
|
|
* Filter classes which are ready for use, e.g ``Filters.all`` are now capitalized, e.g
|
|
|
|
``filters.ALL``.
|
|
|
|
* Filters which need to be initialized are now in CamelCase. E.g. ``filters.User(...)``.
|
|
|
|
* Filters which do both (like ``Filters.text``) are now split as ready-to-use version
|
|
|
|
``filters.TEXT`` and class version ``filters.Text(...)``.
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2021-12-05 09:42:14 +01:00
|
|
|
__all__ = (
|
|
|
|
"ALL",
|
|
|
|
"ANIMATION",
|
|
|
|
"ATTACHMENT",
|
|
|
|
"AUDIO",
|
2024-03-02 10:56:15 +01:00
|
|
|
"BOOST_ADDED",
|
2021-12-05 09:42:14 +01:00
|
|
|
"CAPTION",
|
|
|
|
"CHAT",
|
|
|
|
"COMMAND",
|
|
|
|
"CONTACT",
|
|
|
|
"FORWARDED",
|
|
|
|
"GAME",
|
2024-02-08 17:12:00 +01:00
|
|
|
"GIVEAWAY",
|
|
|
|
"GIVEAWAY_WINNERS",
|
2023-01-01 17:00:49 +01:00
|
|
|
"HAS_MEDIA_SPOILER",
|
2021-12-05 09:42:14 +01:00
|
|
|
"HAS_PROTECTED_CONTENT",
|
|
|
|
"INVOICE",
|
|
|
|
"IS_AUTOMATIC_FORWARD",
|
2024-04-12 11:58:25 +02:00
|
|
|
"IS_FROM_OFFLINE",
|
2022-11-22 10:43:50 +01:00
|
|
|
"IS_TOPIC_MESSAGE",
|
2021-12-05 09:42:14 +01:00
|
|
|
"LOCATION",
|
|
|
|
"PASSPORT_DATA",
|
|
|
|
"PHOTO",
|
|
|
|
"POLL",
|
2024-01-08 18:35:32 +01:00
|
|
|
"PREMIUM_USER",
|
2021-12-05 09:42:14 +01:00
|
|
|
"REPLY",
|
2024-03-02 10:56:15 +01:00
|
|
|
"REPLY_TO_STORY",
|
|
|
|
"SENDER_BOOST_COUNT",
|
2023-09-03 13:43:44 +02:00
|
|
|
"STORY",
|
2021-12-05 09:42:14 +01:00
|
|
|
"SUCCESSFUL_PAYMENT",
|
|
|
|
"TEXT",
|
|
|
|
"USER",
|
2022-06-27 18:54:11 +02:00
|
|
|
"USER_ATTACHMENT",
|
2021-12-05 09:42:14 +01:00
|
|
|
"VENUE",
|
|
|
|
"VIA_BOT",
|
|
|
|
"VIDEO",
|
|
|
|
"VIDEO_NOTE",
|
|
|
|
"VOICE",
|
2024-01-24 20:53:36 +01:00
|
|
|
"BaseFilter",
|
|
|
|
"Caption",
|
|
|
|
"CaptionEntity",
|
|
|
|
"CaptionRegex",
|
|
|
|
"Chat",
|
|
|
|
"ChatType",
|
|
|
|
"Command",
|
|
|
|
"Dice",
|
|
|
|
"Document",
|
|
|
|
"Entity",
|
|
|
|
"ForwardedFrom",
|
|
|
|
"Language",
|
|
|
|
"Mention",
|
|
|
|
"MessageFilter",
|
|
|
|
"Regex",
|
|
|
|
"SenderChat",
|
|
|
|
"StatusUpdate",
|
|
|
|
"Sticker",
|
|
|
|
"SuccessfulPayment",
|
|
|
|
"Text",
|
|
|
|
"UpdateFilter",
|
|
|
|
"UpdateType",
|
|
|
|
"User",
|
2021-12-05 09:42:14 +01:00
|
|
|
"ViaBot",
|
|
|
|
)
|
2021-11-20 11:36:18 +01:00
|
|
|
import mimetypes
|
2018-03-15 05:59:27 +01:00
|
|
|
import re
|
2020-05-01 20:27:34 +02:00
|
|
|
from abc import ABC, abstractmethod
|
2020-11-07 08:44:45 +01:00
|
|
|
from typing import (
|
2023-02-02 18:55:07 +01:00
|
|
|
Collection,
|
2020-11-07 08:44:45 +01:00
|
|
|
Dict,
|
|
|
|
FrozenSet,
|
2023-10-23 21:11:56 +02:00
|
|
|
Iterable,
|
2020-11-07 08:44:45 +01:00
|
|
|
List,
|
|
|
|
Match,
|
2022-05-05 09:27:54 +02:00
|
|
|
NoReturn,
|
2020-11-07 08:44:45 +01:00
|
|
|
Optional,
|
|
|
|
Pattern,
|
2023-02-02 18:55:07 +01:00
|
|
|
Sequence,
|
2020-11-07 08:44:45 +01:00
|
|
|
Set,
|
|
|
|
Tuple,
|
|
|
|
Union,
|
|
|
|
cast,
|
|
|
|
)
|
2017-05-21 14:00:53 +02:00
|
|
|
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram import Chat as TGChat
|
2024-02-08 17:12:00 +01:00
|
|
|
from telegram import (
|
|
|
|
Message,
|
|
|
|
MessageEntity,
|
|
|
|
MessageOriginChannel,
|
|
|
|
MessageOriginChat,
|
|
|
|
MessageOriginUser,
|
|
|
|
Update,
|
|
|
|
)
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram import User as TGUser
|
2022-05-13 16:41:34 +02:00
|
|
|
from telegram._utils.types import SCT
|
2021-11-20 11:36:18 +01:00
|
|
|
from telegram.constants import DiceEmoji as DiceEmojiEnum
|
2024-02-08 17:12:00 +01:00
|
|
|
from telegram.ext._utils._update_parsing import parse_chat_id, parse_username
|
2023-02-02 18:55:07 +01:00
|
|
|
from telegram.ext._utils.types import FilterDataDict
|
2021-04-30 10:12:18 +02:00
|
|
|
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class BaseFilter:
|
2020-07-28 09:10:32 +02:00
|
|
|
"""Base class for all Filters.
|
2016-09-25 00:30:04 +02:00
|
|
|
|
2020-07-28 09:10:32 +02:00
|
|
|
Filters subclassing from this class can combined using bitwise operators:
|
2016-09-25 00:30:04 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
And::
|
2016-09-25 00:30:04 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
filters.TEXT & filters.Entity(MENTION)
|
2016-09-25 00:30:04 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
Or::
|
2016-09-25 00:30:04 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
filters.AUDIO | filters.VIDEO
|
2016-09-25 00:30:04 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
Exclusive Or::
|
2020-11-07 08:44:45 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
filters.Regex('To Be') ^ filters.Regex('Not 2B')
|
2020-11-07 08:44:45 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
Not::
|
2017-03-28 18:38:44 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
~ filters.COMMAND
|
2017-03-28 18:38:44 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
Also works with more than two filters::
|
2016-09-25 00:30:04 +02:00
|
|
|
|
2023-05-07 14:51:22 +02:00
|
|
|
filters.TEXT & (filters.Entity("url") | filters.Entity("text_link"))
|
2022-04-24 12:38:09 +02:00
|
|
|
filters.TEXT & (~ filters.FORWARDED)
|
2016-09-25 00:30:04 +02:00
|
|
|
|
2019-03-14 09:03:21 +01:00
|
|
|
Note:
|
2022-04-24 12:38:09 +02:00
|
|
|
Filters use the same short circuiting logic as python's :keyword:`and`, :keyword:`or` and
|
|
|
|
:keyword:`not`. This means that for example::
|
2019-03-14 09:03:21 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
filters.Regex(r'(a?x)') | filters.Regex(r'(b?x)')
|
2019-03-14 09:03:21 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
With ``message.text == 'x'``, will only ever return the matches for the first filter,
|
2019-03-14 09:03:21 +01:00
|
|
|
since the second one is never evaluated.
|
|
|
|
|
2020-07-28 09:10:32 +02:00
|
|
|
If you want to create your own filters create a class inheriting from either
|
2021-11-20 11:36:18 +01:00
|
|
|
:class:`MessageFilter` or :class:`UpdateFilter` and implement a ``filter()``
|
|
|
|
method that returns a boolean: :obj:`True` if the message should be
|
2020-07-28 09:10:32 +02:00
|
|
|
handled, :obj:`False` otherwise.
|
2021-11-20 11:36:18 +01:00
|
|
|
Note that the filters work only as class instances, not actual class objects (so remember to
|
2020-07-28 09:10:32 +02:00
|
|
|
initialize your filter classes).
|
2017-06-21 13:46:03 +02:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
By default, the filters name (what will get printed when converted to a string for display)
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
will be the class name. If you want to overwrite this assign a better name to the :attr:`name`
|
2017-06-21 13:46:03 +02:00
|
|
|
class variable.
|
2017-07-23 22:33:08 +02:00
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionadded:: 20.0
|
2021-11-20 11:36:18 +01:00
|
|
|
Added the arguments :attr:`name` and :attr:`data_filter`.
|
|
|
|
|
|
|
|
Args:
|
2017-07-23 22:33:08 +02:00
|
|
|
name (:obj:`str`): Name for this filter. Defaults to the type of filter.
|
2019-02-13 12:07:25 +01:00
|
|
|
data_filter (:obj:`bool`): Whether this filter is a data filter. A data filter should
|
|
|
|
return a dict with lists. The dict will be merged with
|
|
|
|
:class:`telegram.ext.CallbackContext`'s internal dict in most cases
|
|
|
|
(depends on the handler).
|
2016-09-25 00:30:04 +02:00
|
|
|
"""
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2024-02-05 19:24:00 +01:00
|
|
|
__slots__ = ("_data_filter", "_name")
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2023-05-18 07:57:59 +02:00
|
|
|
def __init__(self, name: Optional[str] = None, data_filter: bool = False):
|
2021-11-20 11:36:18 +01:00
|
|
|
self._name = self.__class__.__name__ if name is None else name
|
|
|
|
self._data_filter = data_filter
|
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def __and__(self, other: "BaseFilter") -> "BaseFilter":
|
2023-12-10 20:17:11 +01:00
|
|
|
"""Defines `AND` bitwise operator for :class:`BaseFilter` object.
|
|
|
|
The combined filter accepts an update only if it is accepted by both filters.
|
|
|
|
For example, ``filters.PHOTO & filters.CAPTION`` will only accept messages that contain
|
|
|
|
both a photo and a caption.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`BaseFilter`
|
|
|
|
"""
|
2021-11-20 11:36:18 +01:00
|
|
|
return _MergedFilter(self, and_filter=other)
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def __or__(self, other: "BaseFilter") -> "BaseFilter":
|
2023-12-10 20:17:11 +01:00
|
|
|
"""Defines `OR` bitwise operator for :class:`BaseFilter` object.
|
|
|
|
The combined filter accepts an update only if it is accepted by any of the filters.
|
|
|
|
For example, ``filters.PHOTO | filters.CAPTION`` will only accept messages that contain
|
|
|
|
photo or caption or both.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`BaseFilter`
|
|
|
|
"""
|
2021-11-20 11:36:18 +01:00
|
|
|
return _MergedFilter(self, or_filter=other)
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2020-11-07 08:44:45 +01:00
|
|
|
def __xor__(self, other: "BaseFilter") -> "BaseFilter":
|
2023-12-10 20:17:11 +01:00
|
|
|
"""Defines `XOR` bitwise operator for :class:`BaseFilter` object.
|
|
|
|
The combined filter accepts an update only if it is accepted by any of the filters and
|
|
|
|
not both of them. For example, ``filters.PHOTO ^ filters.CAPTION`` will only accept
|
|
|
|
messages that contain photo or caption, not both of them.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`BaseFilter`
|
|
|
|
"""
|
2021-11-20 11:36:18 +01:00
|
|
|
return _XORFilter(self, other)
|
2020-11-07 08:44:45 +01:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def __invert__(self) -> "BaseFilter":
|
2023-12-10 20:17:11 +01:00
|
|
|
"""Defines `NOT` bitwise operator for :class:`BaseFilter` object.
|
|
|
|
The combined filter accepts an update only if it is accepted by any of the filters.
|
|
|
|
For example, ``~ filters.PHOTO`` will only accept messages that do not contain photo.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`BaseFilter`
|
|
|
|
"""
|
2021-11-20 11:36:18 +01:00
|
|
|
return _InvertedFilter(self)
|
2017-03-28 18:38:44 +02:00
|
|
|
|
2023-09-15 22:19:45 +02:00
|
|
|
def __repr__(self) -> str:
|
2023-12-10 20:17:11 +01:00
|
|
|
"""Gives name for this filter.
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
:meth:`name`
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`str`:
|
|
|
|
"""
|
2023-09-15 22:19:45 +02:00
|
|
|
return self.name
|
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
@property
|
|
|
|
def data_filter(self) -> bool:
|
2023-02-02 18:55:07 +01:00
|
|
|
""":obj:`bool`: Whether this filter is a data filter."""
|
2021-05-29 16:18:16 +02:00
|
|
|
return self._data_filter
|
|
|
|
|
|
|
|
@data_filter.setter
|
|
|
|
def data_filter(self, value: bool) -> None:
|
|
|
|
self._data_filter = value
|
|
|
|
|
2020-11-07 08:44:45 +01:00
|
|
|
@property
|
2021-11-20 11:36:18 +01:00
|
|
|
def name(self) -> str:
|
2023-02-02 18:55:07 +01:00
|
|
|
""":obj:`str`: Name for this filter."""
|
2020-11-07 08:44:45 +01:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@name.setter
|
2021-11-20 11:36:18 +01:00
|
|
|
def name(self, name: str) -> None:
|
|
|
|
self._name = name
|
2020-11-07 08:44:45 +01:00
|
|
|
|
2024-02-07 20:45:57 +01:00
|
|
|
def check_update(self, update: Update) -> Optional[Union[bool, FilterDataDict]]:
|
2023-09-15 22:19:45 +02:00
|
|
|
"""Checks if the specified update should be handled by this filter.
|
|
|
|
|
2024-04-12 12:39:38 +02:00
|
|
|
.. versionchanged:: 21.1
|
2024-04-12 11:58:25 +02:00
|
|
|
This filter now also returns :obj:`True` if the update contains
|
|
|
|
:attr:`~telegram.Update.business_message`
|
|
|
|
or :attr:`~telegram.Update.edited_business_message`.
|
|
|
|
|
2023-09-15 22:19:45 +02:00
|
|
|
Args:
|
|
|
|
update (:class:`telegram.Update`): The update to check.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`bool`: :obj:`True` if the update contains one of
|
|
|
|
:attr:`~telegram.Update.channel_post`, :attr:`~telegram.Update.message`,
|
2024-04-12 11:58:25 +02:00
|
|
|
:attr:`~telegram.Update.edited_channel_post`,
|
|
|
|
:attr:`~telegram.Update.edited_message`, :attr:`telegram.Update.business_message`,
|
|
|
|
:attr:`telegram.Update.edited_business_message`, or :obj:`False` otherwise.
|
2023-09-15 22:19:45 +02:00
|
|
|
"""
|
2024-04-05 17:26:08 +02:00
|
|
|
return bool( # Only message updates should be handled.
|
2024-04-28 22:32:20 +02:00
|
|
|
update.channel_post
|
2023-09-15 22:19:45 +02:00
|
|
|
or update.message
|
|
|
|
or update.edited_channel_post
|
|
|
|
or update.edited_message
|
2024-04-12 11:58:25 +02:00
|
|
|
or update.business_message
|
|
|
|
or update.edited_business_message
|
2024-04-05 17:26:08 +02:00
|
|
|
)
|
2017-06-18 15:28:48 +02:00
|
|
|
|
2020-07-28 09:10:32 +02:00
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
class MessageFilter(BaseFilter):
|
2020-07-28 09:10:32 +02:00
|
|
|
"""Base class for all Message Filters. In contrast to :class:`UpdateFilter`, the object passed
|
2021-11-20 11:36:18 +01:00
|
|
|
to :meth:`filter` is :obj:`telegram.Update.effective_message`.
|
2020-07-28 09:10:32 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Please see :class:`BaseFilter` for details on how to create custom filters.
|
2020-07-28 09:10:32 +02:00
|
|
|
|
2023-03-25 19:18:04 +01:00
|
|
|
.. seealso:: :wiki:`Advanced Filters <Extensions---Advanced-Filters>`
|
Documentation Improvements (#3214, #3217, #3218, #3271, #3289, #3292, #3303, #3312, #3306, #3319, #3326, #3314)
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
Co-authored-by: Simon Fong <44134941+simonfongnt@users.noreply.github.com>
Co-authored-by: Piotr Rogulski <rivinek@gmail.com>
Co-authored-by: poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: Or Bin <or@raftt.io>
Co-authored-by: Sandro <j32g7f67hb@liamekaens.com>
Co-authored-by: Hatim Zahid <63000127+HatimZ@users.noreply.github.com>
Co-authored-by: Robi <53259730+RobiMez@users.noreply.github.com>
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
2022-11-15 09:06:23 +01:00
|
|
|
|
2020-07-28 09:10:32 +02:00
|
|
|
"""
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
|
|
|
|
2023-02-02 18:55:07 +01:00
|
|
|
def check_update(self, update: Update) -> Optional[Union[bool, FilterDataDict]]:
|
|
|
|
"""Checks if the specified update should be handled by this filter by passing
|
|
|
|
:attr:`~telegram.Update.effective_message` to :meth:`filter`.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
update (:class:`telegram.Update`): The update to check.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`bool` | Dict[:obj:`str`, :obj:`list`] | :obj:`None`: If the update should be
|
|
|
|
handled by this filter, returns :obj:`True` or a dict with lists, in case the filter
|
|
|
|
is a data filter. If the update should not be handled by this filter, :obj:`False` or
|
|
|
|
:obj:`None`.
|
|
|
|
"""
|
2021-11-21 12:39:04 +01:00
|
|
|
if super().check_update(update):
|
|
|
|
return self.filter(update.effective_message) # type: ignore[arg-type]
|
|
|
|
return False
|
2020-07-28 09:10:32 +02:00
|
|
|
|
2020-05-01 20:27:34 +02:00
|
|
|
@abstractmethod
|
2023-02-02 18:55:07 +01:00
|
|
|
def filter(self, message: Message) -> Optional[Union[bool, FilterDataDict]]:
|
2017-09-01 08:43:08 +02:00
|
|
|
"""This method must be overwritten.
|
2017-07-23 22:33:08 +02:00
|
|
|
|
2020-07-28 09:10:32 +02:00
|
|
|
Args:
|
|
|
|
message (:class:`telegram.Message`): The message that is tested.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`dict` or :obj:`bool`
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
class UpdateFilter(BaseFilter):
|
2021-02-19 19:27:17 +01:00
|
|
|
"""Base class for all Update Filters. In contrast to :class:`MessageFilter`, the object
|
2021-11-20 11:36:18 +01:00
|
|
|
passed to :meth:`filter` is an instance of :class:`telegram.Update`, which allows to create
|
|
|
|
filters like :attr:`telegram.ext.filters.UpdateType.EDITED_MESSAGE`.
|
2020-07-28 09:10:32 +02:00
|
|
|
|
2021-02-19 19:27:17 +01:00
|
|
|
Please see :class:`telegram.ext.filters.BaseFilter` for details on how to create custom
|
|
|
|
filters.
|
2020-07-28 09:10:32 +02:00
|
|
|
|
|
|
|
"""
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
|
|
|
|
2023-02-02 18:55:07 +01:00
|
|
|
def check_update(self, update: Update) -> Optional[Union[bool, FilterDataDict]]:
|
|
|
|
"""Checks if the specified update should be handled by this filter.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
update (:class:`telegram.Update`): The update to check.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`bool` | Dict[:obj:`str`, :obj:`list`] | :obj:`None`: If the update should be
|
|
|
|
handled by this filter, returns :obj:`True` or a dict with lists, in case the filter
|
|
|
|
is a data filter. If the update should not be handled by this filter, :obj:`False` or
|
|
|
|
:obj:`None`.
|
|
|
|
"""
|
2021-11-20 11:36:18 +01:00
|
|
|
return self.filter(update) if super().check_update(update) else False
|
2020-07-28 09:10:32 +02:00
|
|
|
|
|
|
|
@abstractmethod
|
2023-02-02 18:55:07 +01:00
|
|
|
def filter(self, update: Update) -> Optional[Union[bool, FilterDataDict]]:
|
2020-07-28 09:10:32 +02:00
|
|
|
"""This method must be overwritten.
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2017-07-23 22:33:08 +02:00
|
|
|
Args:
|
2019-02-13 12:07:25 +01:00
|
|
|
update (:class:`telegram.Update`): The update that is tested.
|
2017-07-23 22:33:08 +02:00
|
|
|
|
|
|
|
Returns:
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
:obj:`dict` or :obj:`bool`.
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2017-07-23 22:33:08 +02:00
|
|
|
"""
|
|
|
|
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _InvertedFilter(UpdateFilter):
|
2017-03-28 18:38:44 +02:00
|
|
|
"""Represents a filter that has been inverted.
|
|
|
|
|
|
|
|
Args:
|
2017-07-23 22:33:08 +02:00
|
|
|
f: The filter to invert.
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2017-03-28 18:38:44 +02:00
|
|
|
"""
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ("inv_filter",)
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def __init__(self, f: BaseFilter):
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__()
|
|
|
|
self.inv_filter = f
|
2017-03-28 18:38:44 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def filter(self, update: Update) -> bool:
|
2021-11-20 11:36:18 +01:00
|
|
|
return not bool(self.inv_filter.check_update(update))
|
2017-03-28 18:38:44 +02:00
|
|
|
|
2020-11-07 08:44:45 +01:00
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
2021-11-20 11:36:18 +01:00
|
|
|
return f"<inverted {self.inv_filter}>"
|
2017-03-28 18:38:44 +02:00
|
|
|
|
2020-11-07 08:44:45 +01:00
|
|
|
@name.setter
|
2024-05-12 09:13:20 +02:00
|
|
|
def name(self, _: str) -> NoReturn:
|
2021-11-20 11:36:18 +01:00
|
|
|
raise RuntimeError("Cannot set name for combined filters.")
|
2020-11-07 08:44:45 +01:00
|
|
|
|
2017-03-28 18:38:44 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _MergedFilter(UpdateFilter):
|
2016-10-14 10:32:12 +02:00
|
|
|
"""Represents a filter consisting of two other filters.
|
|
|
|
|
|
|
|
Args:
|
Documentation Improvements (#2008)
* Minor doc updates, following official API docs
* Fix spelling in Defaults docstrings
* Clarify Changelog of v12.7 about aware dates
* Fix typo in CHANGES.rst (#2024)
* Fix PicklePersistence.flush() with only bot_data (#2017)
* Update pylint in pre-commit to fix CI (#2018)
* Add Filters.via_bot (#2009)
* feat: via_bot filter
also fixing a small mistake in the empty parameter of the user filter and improve docs slightly
* fix: forgot to set via_bot to None
* fix: redoing subclassing to copy paste solution
* Cosmetic changes
Co-authored-by: Hinrich Mahler <hinrich.mahler@freenet.de>
* Update CHANGES.rst
Fixed Typo
Co-authored-by: Bibo-Joshi <hinrich.mahler@freenet.de>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
* Update downloads badge, add info on IRC Channel to Getting Help section
* Remove RegexHandler from ConversationHandlers Docs (#1973)
Replaced RegexHandler with MessageHandler, since the former is deprecated
* Fix Filters.via_bot docstrings
* Add notes on Markdown v1 being legacy mode
* Fixed typo in the Regex doc.. (#2036)
* Typo: Spelling
* Minor cleanup from #2043
* Document CommandHandler ignoring channel posts
* Doc fixes for a few telegram.ext classes
* Doc fixes for most `telegram` classes.
* pep-8
forgot the hard wrap is at 99 chars, not 100!
fixed a few spelling mistakes too.
* Address review and made rendering of booleans consistent
True, False, None are now rendered with ``bool`` wherever they weren't in telegram and telegram.ext classes.
* Few doc fixes for inline* classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram/files classes
As usual, docs were cross-checked with official tg api docs.
* Doc fixes for telegram.Game
Mostly just added hyperlinks. And fixed message length doc.
As usual, docs were cross-checked with official tg api docs.
* Very minor doc fix for passportfile.py and passportelementerrors.py
Didn't bother changing too much since this seems to be a custom implementation.
* Doc fixes for telegram.payments
As usual, cross-checked with official bot api docs.
* Address review 2
Few tiny other fixes too.
* Changed from ``True/False/None`` to :obj:`True/False/None` project-wide.
Few tiny other doc fixes too.
Co-authored-by: Robert Geislinger <mitachundkrach@gmail.com>
Co-authored-by: Poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: GauthamramRavichandran <30320759+GauthamramRavichandran@users.noreply.github.com>
Co-authored-by: Mahesh19 <maheshvagicherla99438@gmail.com>
Co-authored-by: hoppingturtles <ilovebhagwan@gmail.com>
2020-08-24 19:35:57 +02:00
|
|
|
base_filter: Filter 1 of the merged filter.
|
2016-10-14 10:32:12 +02:00
|
|
|
and_filter: Optional filter to "and" with base_filter. Mutually exclusive with or_filter.
|
|
|
|
or_filter: Optional filter to "or" with base_filter. Mutually exclusive with and_filter.
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2016-10-14 10:32:12 +02:00
|
|
|
"""
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2024-02-05 19:24:00 +01:00
|
|
|
__slots__ = ("and_filter", "base_filter", "or_filter")
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def __init__(
|
2023-05-18 07:57:59 +02:00
|
|
|
self,
|
|
|
|
base_filter: BaseFilter,
|
|
|
|
and_filter: Optional[BaseFilter] = None,
|
|
|
|
or_filter: Optional[BaseFilter] = None,
|
2020-10-06 19:28:40 +02:00
|
|
|
):
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__()
|
2016-09-14 19:29:15 +02:00
|
|
|
self.base_filter = base_filter
|
2019-02-13 12:07:25 +01:00
|
|
|
if self.base_filter.data_filter:
|
|
|
|
self.data_filter = True
|
2016-09-14 19:29:15 +02:00
|
|
|
self.and_filter = and_filter
|
2019-02-13 12:07:25 +01:00
|
|
|
if (
|
|
|
|
self.and_filter
|
|
|
|
and not isinstance(self.and_filter, bool)
|
|
|
|
and self.and_filter.data_filter
|
|
|
|
):
|
|
|
|
self.data_filter = True
|
2016-09-14 19:29:15 +02:00
|
|
|
self.or_filter = or_filter
|
2019-02-13 12:07:25 +01:00
|
|
|
if self.or_filter and not isinstance(self.and_filter, bool) and self.or_filter.data_filter:
|
|
|
|
self.data_filter = True
|
|
|
|
|
2020-10-31 16:33:34 +01:00
|
|
|
@staticmethod
|
2023-02-02 18:55:07 +01:00
|
|
|
def _merge(base_output: Union[bool, Dict], comp_output: Union[bool, Dict]) -> FilterDataDict:
|
2019-02-13 12:07:25 +01:00
|
|
|
base = base_output if isinstance(base_output, dict) else {}
|
|
|
|
comp = comp_output if isinstance(comp_output, dict) else {}
|
2023-03-25 19:18:04 +01:00
|
|
|
for k in comp:
|
2019-02-13 12:07:25 +01:00
|
|
|
# Make sure comp values are lists
|
|
|
|
comp_value = comp[k] if isinstance(comp[k], list) else []
|
|
|
|
try:
|
|
|
|
# If base is a list then merge
|
|
|
|
if isinstance(base[k], list):
|
|
|
|
base[k] += comp_value
|
|
|
|
else:
|
2023-03-25 19:18:04 +01:00
|
|
|
base[k] = [base[k], *comp_value]
|
2019-02-13 12:07:25 +01:00
|
|
|
except KeyError:
|
|
|
|
base[k] = comp_value
|
|
|
|
return base
|
|
|
|
|
2021-10-08 08:17:00 +02:00
|
|
|
# pylint: disable=too-many-return-statements
|
2023-02-02 18:55:07 +01:00
|
|
|
def filter(self, update: Update) -> Union[bool, FilterDataDict]:
|
2021-11-20 11:36:18 +01:00
|
|
|
base_output = self.base_filter.check_update(update)
|
2019-02-13 12:07:25 +01:00
|
|
|
# We need to check if the filters are data filters and if so return the merged data.
|
|
|
|
# If it's not a data filter or an or_filter but no matches return bool
|
2016-09-14 19:29:15 +02:00
|
|
|
if self.and_filter:
|
2021-11-20 11:36:18 +01:00
|
|
|
# And filter needs to short circuit if base is falsy
|
2019-03-14 09:03:21 +01:00
|
|
|
if base_output:
|
2021-11-20 11:36:18 +01:00
|
|
|
comp_output = self.and_filter.check_update(update)
|
2019-03-14 09:03:21 +01:00
|
|
|
if comp_output:
|
|
|
|
if self.data_filter:
|
|
|
|
merged = self._merge(base_output, comp_output)
|
|
|
|
if merged:
|
|
|
|
return merged
|
|
|
|
return True
|
2016-09-14 19:29:15 +02:00
|
|
|
elif self.or_filter:
|
2021-11-20 11:36:18 +01:00
|
|
|
# Or filter needs to short circuit if base is truthy
|
2019-03-14 09:03:21 +01:00
|
|
|
if base_output:
|
2019-02-13 12:07:25 +01:00
|
|
|
if self.data_filter:
|
2019-03-14 09:03:21 +01:00
|
|
|
return base_output
|
2019-02-13 12:07:25 +01:00
|
|
|
return True
|
2020-10-31 16:33:34 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
comp_output = self.or_filter.check_update(update)
|
2020-10-31 16:33:34 +01:00
|
|
|
if comp_output:
|
|
|
|
if self.data_filter:
|
|
|
|
return comp_output
|
|
|
|
return True
|
2019-02-13 12:07:25 +01:00
|
|
|
return False
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2020-11-07 08:44:45 +01:00
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
2020-11-23 22:09:29 +01:00
|
|
|
return (
|
|
|
|
f"<{self.base_filter} {'and' if self.and_filter else 'or'} "
|
|
|
|
f"{self.and_filter or self.or_filter}>"
|
2017-06-18 15:28:48 +02:00
|
|
|
)
|
2016-09-25 16:31:06 +02:00
|
|
|
|
2020-11-07 08:44:45 +01:00
|
|
|
@name.setter
|
2024-05-12 09:13:20 +02:00
|
|
|
def name(self, _: str) -> NoReturn:
|
2021-11-20 11:36:18 +01:00
|
|
|
raise RuntimeError("Cannot set name for combined filters.")
|
2020-11-07 08:44:45 +01:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _XORFilter(UpdateFilter):
|
2020-11-07 08:44:45 +01:00
|
|
|
"""Convenience filter acting as wrapper for :class:`MergedFilter` representing the an XOR gate
|
2021-02-19 19:27:17 +01:00
|
|
|
for two filters.
|
2020-11-07 08:44:45 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
base_filter: Filter 1 of the merged filter.
|
|
|
|
xor_filter: Filter 2 of the merged filter.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2024-02-05 19:24:00 +01:00
|
|
|
__slots__ = ("base_filter", "merged_filter", "xor_filter")
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2020-11-07 08:44:45 +01:00
|
|
|
def __init__(self, base_filter: BaseFilter, xor_filter: BaseFilter):
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__()
|
2020-11-07 08:44:45 +01:00
|
|
|
self.base_filter = base_filter
|
|
|
|
self.xor_filter = xor_filter
|
|
|
|
self.merged_filter = (base_filter & ~xor_filter) | (~base_filter & xor_filter)
|
|
|
|
|
2023-02-02 18:55:07 +01:00
|
|
|
def filter(self, update: Update) -> Optional[Union[bool, FilterDataDict]]:
|
2021-11-20 11:36:18 +01:00
|
|
|
return self.merged_filter.check_update(update)
|
2020-11-07 08:44:45 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
return f"<{self.base_filter} xor {self.xor_filter}>"
|
|
|
|
|
|
|
|
@name.setter
|
2024-05-12 09:13:20 +02:00
|
|
|
def name(self, _: str) -> NoReturn:
|
2021-11-20 11:36:18 +01:00
|
|
|
raise RuntimeError("Cannot set name for combined filters.")
|
2020-11-07 08:44:45 +01:00
|
|
|
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _All(MessageFilter):
|
|
|
|
__slots__ = ()
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2024-05-12 09:13:20 +02:00
|
|
|
def filter(self, message: Message) -> bool: # noqa: ARG002
|
2021-11-20 11:36:18 +01:00
|
|
|
return True
|
2020-05-02 11:56:52 +02:00
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
ALL = _All(name="filters.ALL")
|
|
|
|
"""All Messages."""
|
2020-05-02 11:56:52 +02:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Animation(MessageFilter):
|
|
|
|
__slots__ = ()
|
2020-05-02 11:56:52 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def filter(self, message: Message) -> bool:
|
2021-11-20 11:36:18 +01:00
|
|
|
return bool(message.animation)
|
2020-05-02 11:56:52 +02:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
ANIMATION = _Animation(name="filters.ANIMATION")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.animation`."""
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Attachment(MessageFilter):
|
2021-08-19 22:01:10 +02:00
|
|
|
__slots__ = ()
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.effective_attachment)
|
2016-10-15 22:58:55 +02:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
ATTACHMENT = _Attachment(name="filters.ATTACHMENT")
|
|
|
|
"""Messages that contain :meth:`telegram.Message.effective_attachment`.
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
.. versionadded:: 13.6"""
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2019-11-29 13:09:44 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Audio(MessageFilter):
|
|
|
|
__slots__ = ()
|
2019-11-29 13:09:44 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.audio)
|
2019-11-29 13:09:44 +01:00
|
|
|
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
AUDIO = _Audio(name="filters.AUDIO")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.audio`."""
|
2019-11-29 13:09:44 +01:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class Caption(MessageFilter):
|
|
|
|
"""Messages with a caption. If a list of strings is passed, it filters messages to only
|
|
|
|
allow those whose caption is appearing in the given list.
|
2019-11-29 13:09:44 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Examples:
|
2023-05-07 14:51:22 +02:00
|
|
|
``MessageHandler(filters.Caption(['PTB rocks!', 'PTB']), callback_method_2)``
|
2019-11-29 13:09:44 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
.. seealso::
|
|
|
|
:attr:`telegram.ext.filters.CAPTION`
|
2020-04-10 19:22:45 +02:00
|
|
|
|
2019-11-29 13:09:44 +01:00
|
|
|
Args:
|
2021-11-20 11:36:18 +01:00
|
|
|
strings (List[:obj:`str`] | Tuple[:obj:`str`], optional): Which captions to allow. Only
|
|
|
|
exact matches are allowed. If not specified, will allow any message with a caption.
|
2019-11-29 13:09:44 +01:00
|
|
|
"""
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ("strings",)
|
2019-11-29 13:09:44 +01:00
|
|
|
|
2023-05-18 07:57:59 +02:00
|
|
|
def __init__(self, strings: Optional[Union[List[str], Tuple[str, ...]]] = None):
|
2023-02-02 18:55:07 +01:00
|
|
|
self.strings: Optional[Sequence[str]] = strings
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__(name=f"filters.Caption({strings})" if strings else "filters.CAPTION")
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
if self.strings is None:
|
|
|
|
return bool(message.caption)
|
|
|
|
return message.caption in self.strings if message.caption else False
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2019-11-29 13:09:44 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
CAPTION = Caption()
|
|
|
|
"""Shortcut for :class:`telegram.ext.filters.Caption()`.
|
2019-11-29 13:09:44 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Examples:
|
|
|
|
To allow any caption, simply use ``MessageHandler(filters.CAPTION, callback_method)``.
|
|
|
|
"""
|
2019-11-29 13:09:44 +01:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class CaptionEntity(MessageFilter):
|
|
|
|
"""
|
|
|
|
Filters media messages to only allow those which have a :class:`telegram.MessageEntity`
|
|
|
|
where their :class:`~telegram.MessageEntity.type` matches `entity_type`.
|
2019-11-29 13:09:44 +01:00
|
|
|
|
|
|
|
Examples:
|
2021-11-20 11:36:18 +01:00
|
|
|
``MessageHandler(filters.CaptionEntity("hashtag"), callback_method)``
|
2019-11-29 13:09:44 +01:00
|
|
|
|
|
|
|
Args:
|
2021-11-20 11:36:18 +01:00
|
|
|
entity_type (:obj:`str`): Caption Entity type to check for. All types can be found as
|
|
|
|
constants in :class:`telegram.MessageEntity`.
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ("entity_type",)
|
2020-02-08 13:54:21 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def __init__(self, entity_type: str):
|
2023-02-02 18:55:07 +01:00
|
|
|
self.entity_type: str = entity_type
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__(name=f"filters.CaptionEntity({self.entity_type})")
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return any(entity.type == self.entity_type for entity in message.caption_entities)
|
2020-02-08 13:54:21 +01:00
|
|
|
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class CaptionRegex(MessageFilter):
|
2020-02-08 13:54:21 +01:00
|
|
|
"""
|
2022-10-23 14:59:27 +02:00
|
|
|
Filters updates by searching for an occurrence of :paramref:`~CaptionRegex.pattern` in the
|
|
|
|
message caption.
|
2020-02-08 13:54:21 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
This filter works similarly to :class:`Regex`, with the only exception being that
|
|
|
|
it applies to the message caption instead of the text.
|
2020-02-08 13:54:21 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Examples:
|
|
|
|
Use ``MessageHandler(filters.PHOTO & filters.CaptionRegex(r'help'), callback)``
|
|
|
|
to capture all photos with caption containing the word 'help'.
|
2020-02-08 13:54:21 +01:00
|
|
|
|
2020-04-18 12:16:14 +02:00
|
|
|
Note:
|
2021-11-20 11:36:18 +01:00
|
|
|
This filter will not work on simple text messages, but only on media with caption.
|
2020-04-18 12:16:14 +02:00
|
|
|
|
2020-02-08 13:54:21 +01:00
|
|
|
Args:
|
2022-02-09 17:30:16 +01:00
|
|
|
pattern (:obj:`str` | :func:`re.Pattern <re.compile>`): The regex pattern.
|
2020-02-08 13:54:21 +01:00
|
|
|
"""
|
2017-06-18 15:28:48 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ("pattern",)
|
2019-02-08 11:12:49 +01:00
|
|
|
|
2023-02-02 18:55:07 +01:00
|
|
|
def __init__(self, pattern: Union[str, Pattern[str]]):
|
2021-11-20 11:36:18 +01:00
|
|
|
if isinstance(pattern, str):
|
|
|
|
pattern = re.compile(pattern)
|
2023-02-02 18:55:07 +01:00
|
|
|
self.pattern: Pattern[str] = pattern
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__(name=f"filters.CaptionRegex({self.pattern})", data_filter=True)
|
2018-03-15 05:59:27 +01:00
|
|
|
|
2023-02-02 18:55:07 +01:00
|
|
|
def filter(self, message: Message) -> Optional[Dict[str, List[Match[str]]]]:
|
2023-06-29 18:17:47 +02:00
|
|
|
if message.caption and (match := self.pattern.search(message.caption)):
|
|
|
|
return {"matches": [match]}
|
2021-11-20 11:36:18 +01:00
|
|
|
return {}
|
2018-03-15 05:59:27 +01:00
|
|
|
|
2019-02-08 11:12:49 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _ChatUserBaseFilter(MessageFilter, ABC):
|
|
|
|
__slots__ = (
|
|
|
|
"_chat_id_name",
|
|
|
|
"_chat_ids",
|
2024-02-05 19:24:00 +01:00
|
|
|
"_username_name",
|
2021-11-20 11:36:18 +01:00
|
|
|
"_usernames",
|
2024-02-05 19:24:00 +01:00
|
|
|
"allow_empty",
|
2021-11-20 11:36:18 +01:00
|
|
|
)
|
2019-03-14 09:03:21 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
2023-05-18 07:57:59 +02:00
|
|
|
chat_id: Optional[SCT[int]] = None,
|
|
|
|
username: Optional[SCT[str]] = None,
|
2021-11-20 11:36:18 +01:00
|
|
|
allow_empty: bool = False,
|
|
|
|
):
|
|
|
|
super().__init__()
|
2023-02-02 18:55:07 +01:00
|
|
|
self._chat_id_name: str = "chat_id"
|
|
|
|
self._username_name: str = "username"
|
|
|
|
self.allow_empty: bool = allow_empty
|
2019-03-14 09:03:21 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
self._chat_ids: Set[int] = set()
|
|
|
|
self._usernames: Set[str] = set()
|
2019-03-14 09:03:21 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
self._set_chat_ids(chat_id)
|
|
|
|
self._set_usernames(username)
|
|
|
|
|
|
|
|
@abstractmethod
|
2024-02-05 19:24:00 +01:00
|
|
|
def _get_chat_or_user(self, message: Message) -> Union[TGChat, TGUser, None]: ...
|
2021-11-20 11:36:18 +01:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def _set_chat_ids(self, chat_id: Optional[SCT[int]]) -> None:
|
2022-04-24 12:38:09 +02:00
|
|
|
if chat_id and self._usernames:
|
|
|
|
raise RuntimeError(
|
|
|
|
f"Can't set {self._chat_id_name} in conjunction with (already set) "
|
|
|
|
f"{self._username_name}s."
|
|
|
|
)
|
2024-02-08 17:12:00 +01:00
|
|
|
self._chat_ids = set(parse_chat_id(chat_id))
|
2021-11-20 11:36:18 +01:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def _set_usernames(self, username: Optional[SCT[str]]) -> None:
|
2022-04-24 12:38:09 +02:00
|
|
|
if username and self._chat_ids:
|
|
|
|
raise RuntimeError(
|
|
|
|
f"Can't set {self._username_name} in conjunction with (already set) "
|
|
|
|
f"{self._chat_id_name}s."
|
|
|
|
)
|
2024-02-08 17:12:00 +01:00
|
|
|
self._usernames = set(parse_username(username))
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
@property
|
|
|
|
def chat_ids(self) -> FrozenSet[int]:
|
2022-04-24 12:38:09 +02:00
|
|
|
return frozenset(self._chat_ids)
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
@chat_ids.setter
|
2022-05-13 16:41:34 +02:00
|
|
|
def chat_ids(self, chat_id: SCT[int]) -> None:
|
2021-11-20 11:36:18 +01:00
|
|
|
self._set_chat_ids(chat_id)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def usernames(self) -> FrozenSet[str]:
|
|
|
|
"""Which username(s) to allow through.
|
|
|
|
|
|
|
|
Warning:
|
|
|
|
:attr:`usernames` will give a *copy* of the saved usernames as :obj:`frozenset`. This
|
|
|
|
is to ensure thread safety. To add/remove a user, you should use :meth:`add_usernames`,
|
|
|
|
and :meth:`remove_usernames`. Only update the entire set by
|
|
|
|
``filter.usernames = new_set``, if you are entirely sure that it is not causing race
|
|
|
|
conditions, as this will complete replace the current set of allowed users.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
frozenset(:obj:`str`)
|
|
|
|
"""
|
2022-04-24 12:38:09 +02:00
|
|
|
return frozenset(self._usernames)
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
@usernames.setter
|
2022-05-13 16:41:34 +02:00
|
|
|
def usernames(self, username: SCT[str]) -> None:
|
2021-11-20 11:36:18 +01:00
|
|
|
self._set_usernames(username)
|
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def add_usernames(self, username: SCT[str]) -> None:
|
2018-03-15 05:59:27 +01:00
|
|
|
"""
|
2021-11-20 11:36:18 +01:00
|
|
|
Add one or more chats to the allowed usernames.
|
2018-03-15 05:59:27 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
username(:obj:`str` | Collection[:obj:`str`]): Which username(s) to
|
2021-11-20 11:36:18 +01:00
|
|
|
allow through. Leading ``'@'`` s in usernames will be discarded.
|
2020-11-04 20:54:24 +01:00
|
|
|
"""
|
2022-04-24 12:38:09 +02:00
|
|
|
if self._chat_ids:
|
|
|
|
raise RuntimeError(
|
|
|
|
f"Can't set {self._username_name} in conjunction with (already set) "
|
|
|
|
f"{self._chat_id_name}s."
|
|
|
|
)
|
2021-11-20 11:36:18 +01:00
|
|
|
|
2024-02-08 17:12:00 +01:00
|
|
|
parsed_username = set(parse_username(username))
|
2022-04-24 12:38:09 +02:00
|
|
|
self._usernames |= parsed_username
|
2020-11-04 20:54:24 +01:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def _add_chat_ids(self, chat_id: SCT[int]) -> None:
|
2022-04-24 12:38:09 +02:00
|
|
|
if self._usernames:
|
|
|
|
raise RuntimeError(
|
|
|
|
f"Can't set {self._chat_id_name} in conjunction with (already set) "
|
|
|
|
f"{self._username_name}s."
|
|
|
|
)
|
2020-11-04 20:54:24 +01:00
|
|
|
|
2024-02-08 17:12:00 +01:00
|
|
|
parsed_chat_id = set(parse_chat_id(chat_id))
|
2020-11-04 20:54:24 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
self._chat_ids |= parsed_chat_id
|
2021-11-20 11:36:18 +01:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def remove_usernames(self, username: SCT[str]) -> None:
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
Remove one or more chats from allowed usernames.
|
2020-11-04 20:54:24 +01:00
|
|
|
|
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
username(:obj:`str` | Collection[:obj:`str`]): Which username(s) to
|
2021-11-20 11:36:18 +01:00
|
|
|
disallow through. Leading ``'@'`` s in usernames will be discarded.
|
2020-11-04 20:54:24 +01:00
|
|
|
"""
|
2022-04-24 12:38:09 +02:00
|
|
|
if self._chat_ids:
|
|
|
|
raise RuntimeError(
|
|
|
|
f"Can't set {self._username_name} in conjunction with (already set) "
|
|
|
|
f"{self._chat_id_name}s."
|
|
|
|
)
|
2020-11-04 20:54:24 +01:00
|
|
|
|
2024-02-08 17:12:00 +01:00
|
|
|
parsed_username = set(parse_username(username))
|
2022-04-24 12:38:09 +02:00
|
|
|
self._usernames -= parsed_username
|
2016-12-11 22:45:51 +01:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def _remove_chat_ids(self, chat_id: SCT[int]) -> None:
|
2022-04-24 12:38:09 +02:00
|
|
|
if self._usernames:
|
|
|
|
raise RuntimeError(
|
|
|
|
f"Can't set {self._chat_id_name} in conjunction with (already set) "
|
|
|
|
f"{self._username_name}s."
|
|
|
|
)
|
2024-02-08 17:12:00 +01:00
|
|
|
parsed_chat_id = set(parse_chat_id(chat_id))
|
2022-04-24 12:38:09 +02:00
|
|
|
self._chat_ids -= parsed_chat_id
|
2016-12-11 22:45:51 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
2023-02-02 18:55:07 +01:00
|
|
|
chat_or_user = self._get_chat_or_user(message)
|
2021-11-20 11:36:18 +01:00
|
|
|
if chat_or_user:
|
|
|
|
if self.chat_ids:
|
|
|
|
return chat_or_user.id in self.chat_ids
|
|
|
|
if self.usernames:
|
|
|
|
return bool(chat_or_user.username and chat_or_user.username in self.usernames)
|
|
|
|
return self.allow_empty
|
|
|
|
return False
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
|
|
|
return (
|
|
|
|
f"filters.{self.__class__.__name__}("
|
2023-09-22 18:19:21 +02:00
|
|
|
f"{', '.join(str(s) for s in (self.usernames or self.chat_ids))})"
|
2021-11-20 11:36:18 +01:00
|
|
|
)
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
@name.setter
|
2024-05-12 09:13:20 +02:00
|
|
|
def name(self, _: str) -> NoReturn:
|
2021-11-20 11:36:18 +01:00
|
|
|
raise RuntimeError(f"Cannot set name for filters.{self.__class__.__name__}")
|
2016-09-14 19:29:15 +02:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class Chat(_ChatUserBaseFilter):
|
|
|
|
"""Filters messages to allow only those which are from a specified chat ID or username.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
``MessageHandler(filters.Chat(-1234), callback_method)``
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Warning:
|
|
|
|
:attr:`chat_ids` will give a *copy* of the saved chat ids as :class:`frozenset`. This
|
|
|
|
is to ensure thread safety. To add/remove a chat, you should use :meth:`add_chat_ids`, and
|
|
|
|
:meth:`remove_chat_ids`. Only update the entire set by ``filter.chat_ids = new_set``,
|
|
|
|
if you are entirely sure that it is not causing race conditions, as this will complete
|
|
|
|
replace the current set of allowed chats.
|
2018-03-16 23:41:48 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
chat_id(:obj:`int` | Collection[:obj:`int`], optional):
|
2021-11-20 11:36:18 +01:00
|
|
|
Which chat ID(s) to allow through.
|
2022-05-13 16:41:34 +02:00
|
|
|
username(:obj:`str` | Collection[:obj:`str`], optional):
|
2021-11-20 11:36:18 +01:00
|
|
|
Which username(s) to allow through.
|
|
|
|
Leading ``'@'`` s in usernames will be discarded.
|
|
|
|
allow_empty(:obj:`bool`, optional): Whether updates should be processed, if no chat
|
|
|
|
is specified in :attr:`chat_ids` and :attr:`usernames`. Defaults to :obj:`False`.
|
2018-03-16 23:41:48 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Attributes:
|
|
|
|
chat_ids (set(:obj:`int`)): Which chat ID(s) to allow through.
|
|
|
|
allow_empty (:obj:`bool`): Whether updates should be processed, if no chat
|
|
|
|
is specified in :attr:`chat_ids` and :attr:`usernames`.
|
2018-03-16 23:41:48 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Raises:
|
|
|
|
RuntimeError: If ``chat_id`` and ``username`` are both present.
|
|
|
|
"""
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2023-02-02 18:55:07 +01:00
|
|
|
def _get_chat_or_user(self, message: Message) -> Optional[TGChat]:
|
2021-11-20 11:36:18 +01:00
|
|
|
return message.chat
|
2018-03-16 23:41:48 +01:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def add_chat_ids(self, chat_id: SCT[int]) -> None:
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
Add one or more chats to the allowed chat ids.
|
2018-03-16 23:41:48 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
chat_id(:obj:`int` | Collection[:obj:`int`]): Which chat ID(s) to allow
|
2021-11-20 11:36:18 +01:00
|
|
|
through.
|
|
|
|
"""
|
|
|
|
return super()._add_chat_ids(chat_id)
|
2018-03-16 23:41:48 +01:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def remove_chat_ids(self, chat_id: SCT[int]) -> None:
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
Remove one or more chats from allowed chat ids.
|
2018-03-16 23:41:48 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
chat_id(:obj:`int` | Collection[:obj:`int`]): Which chat ID(s) to
|
2021-11-20 11:36:18 +01:00
|
|
|
disallow through.
|
|
|
|
"""
|
|
|
|
return super()._remove_chat_ids(chat_id)
|
2018-03-16 23:41:48 +01:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Chat(MessageFilter):
|
|
|
|
__slots__ = ()
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.chat)
|
2018-03-16 23:41:48 +01:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
CHAT = _Chat(name="filters.CHAT")
|
2024-01-29 13:13:41 +01:00
|
|
|
"""This filter filters *any* message that has a :attr:`telegram.Message.chat`.
|
|
|
|
|
2024-02-08 18:36:28 +01:00
|
|
|
.. deprecated:: 20.8
|
2024-01-29 13:13:41 +01:00
|
|
|
This filter has no effect since :attr:`telegram.Message.chat` is always present.
|
|
|
|
"""
|
2020-11-06 18:41:54 +01:00
|
|
|
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class ChatType: # A convenience namespace for Chat types.
|
|
|
|
"""Subset for filtering the type of chat.
|
2019-02-18 20:04:52 +01:00
|
|
|
|
|
|
|
Examples:
|
2021-11-20 11:36:18 +01:00
|
|
|
Use these filters like: ``filters.ChatType.CHANNEL`` or
|
|
|
|
``filters.ChatType.SUPERGROUP`` etc.
|
2019-02-18 20:04:52 +01:00
|
|
|
|
2022-04-24 13:47:35 +02:00
|
|
|
Caution:
|
2021-11-20 11:36:18 +01:00
|
|
|
``filters.ChatType`` itself is *not* a filter, but just a convenience namespace.
|
2019-02-18 20:04:52 +01:00
|
|
|
"""
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Channel(MessageFilter):
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def filter(self, message: Message) -> bool:
|
2021-11-20 11:36:18 +01:00
|
|
|
return message.chat.type == TGChat.CHANNEL
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
CHANNEL = _Channel(name="filters.ChatType.CHANNEL")
|
|
|
|
"""Updates from channel."""
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Group(MessageFilter):
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def filter(self, message: Message) -> bool:
|
2021-11-20 11:36:18 +01:00
|
|
|
return message.chat.type == TGChat.GROUP
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
GROUP = _Group(name="filters.ChatType.GROUP")
|
|
|
|
"""Updates from group."""
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Groups(MessageFilter):
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def filter(self, message: Message) -> bool:
|
2021-11-20 11:36:18 +01:00
|
|
|
return message.chat.type in [TGChat.GROUP, TGChat.SUPERGROUP]
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
GROUPS = _Groups(name="filters.ChatType.GROUPS")
|
|
|
|
"""Update from group *or* supergroup."""
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Private(MessageFilter):
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def filter(self, message: Message) -> bool:
|
2021-11-20 11:36:18 +01:00
|
|
|
return message.chat.type == TGChat.PRIVATE
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
PRIVATE = _Private(name="filters.ChatType.PRIVATE")
|
|
|
|
"""Update from private chats."""
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _SuperGroup(MessageFilter):
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
2018-04-14 21:53:54 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def filter(self, message: Message) -> bool:
|
2021-11-20 11:36:18 +01:00
|
|
|
return message.chat.type == TGChat.SUPERGROUP
|
2018-04-14 21:53:54 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
SUPERGROUP = _SuperGroup(name="filters.ChatType.SUPERGROUP")
|
|
|
|
"""Updates from supergroup."""
|
2018-04-14 21:53:54 +02:00
|
|
|
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class Command(MessageFilter):
|
|
|
|
"""
|
2022-04-24 12:38:09 +02:00
|
|
|
Messages with a :attr:`telegram.MessageEntity.BOT_COMMAND`. By default, only allows
|
2021-11-20 11:36:18 +01:00
|
|
|
messages `starting` with a bot command. Pass :obj:`False` to also allow messages that contain a
|
|
|
|
bot command `anywhere` in the text.
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Examples:
|
|
|
|
``MessageHandler(filters.Command(False), command_anywhere_callback)``
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
.. seealso::
|
|
|
|
:attr:`telegram.ext.filters.COMMAND`.
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Note:
|
|
|
|
:attr:`telegram.ext.filters.TEXT` also accepts messages containing a command.
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
|
|
|
only_start (:obj:`bool`, optional): Whether to only allow messages that `start` with a bot
|
|
|
|
command. Defaults to :obj:`True`.
|
|
|
|
"""
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ("only_start",)
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def __init__(self, only_start: bool = True):
|
2023-02-02 18:55:07 +01:00
|
|
|
self.only_start: bool = only_start
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__(f"filters.Command({only_start})" if not only_start else "filters.COMMAND")
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
if not message.entities:
|
|
|
|
return False
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
first = message.entities[0]
|
2017-07-23 22:33:08 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
if self.only_start:
|
|
|
|
return bool(first.type == MessageEntity.BOT_COMMAND and first.offset == 0)
|
|
|
|
return bool(any(e.type == MessageEntity.BOT_COMMAND for e in message.entities))
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
COMMAND = Command()
|
|
|
|
"""Shortcut for :class:`telegram.ext.filters.Command()`.
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Examples:
|
|
|
|
To allow messages starting with a command use
|
|
|
|
``MessageHandler(filters.COMMAND, command_at_start_callback)``.
|
|
|
|
"""
|
2017-06-01 04:09:30 +02:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Contact(MessageFilter):
|
|
|
|
__slots__ = ()
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.contact)
|
2017-06-01 04:09:30 +02:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
CONTACT = _Contact(name="filters.CONTACT")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.contact`."""
|
2017-06-01 04:09:30 +02:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Dice(MessageFilter):
|
|
|
|
__slots__ = ("emoji", "values")
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2023-05-18 07:57:59 +02:00
|
|
|
def __init__(self, values: Optional[SCT[int]] = None, emoji: Optional[DiceEmojiEnum] = None):
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__()
|
2023-02-02 18:55:07 +01:00
|
|
|
self.emoji: Optional[DiceEmojiEnum] = emoji
|
|
|
|
self.values: Optional[Collection[int]] = [values] if isinstance(values, int) else values
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
if emoji: # for filters.Dice.BASKETBALL
|
|
|
|
self.name = f"filters.Dice.{emoji.name}"
|
|
|
|
if self.values and emoji: # for filters.Dice.Dice(4) SLOT_MACHINE -> SlotMachine
|
|
|
|
self.name = f"filters.Dice.{emoji.name.title().replace('_', '')}({self.values})"
|
|
|
|
elif values: # for filters.Dice(4)
|
|
|
|
self.name = f"filters.Dice({self.values})"
|
|
|
|
else:
|
|
|
|
self.name = "filters.Dice.ALL"
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
2023-06-29 18:17:47 +02:00
|
|
|
if not (dice := message.dice): # no dice
|
2021-11-20 11:36:18 +01:00
|
|
|
return False
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
if self.emoji:
|
2023-06-29 18:17:47 +02:00
|
|
|
emoji_match = dice.emoji == self.emoji
|
2021-11-20 11:36:18 +01:00
|
|
|
if self.values:
|
2023-06-29 18:17:47 +02:00
|
|
|
return dice.value in self.values and emoji_match # emoji and value
|
2021-11-20 11:36:18 +01:00
|
|
|
return emoji_match # emoji, no value
|
2023-06-29 18:17:47 +02:00
|
|
|
return dice.value in self.values if self.values else True # no emoji, only value
|
2017-06-01 04:09:30 +02:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class Dice(_Dice):
|
|
|
|
"""Dice Messages. If an integer or a list of integers is passed, it filters messages to only
|
|
|
|
allow those whose dice value is appearing in the given list.
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
.. versionadded:: 13.4
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Examples:
|
|
|
|
To allow any dice message, simply use
|
|
|
|
``MessageHandler(filters.Dice.ALL, callback_method)``.
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
To allow any dice message, but with value 3 `or` 4, use
|
|
|
|
``MessageHandler(filters.Dice([3, 4]), callback_method)``
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
To allow only dice messages with the emoji 🎲, but any value, use
|
|
|
|
``MessageHandler(filters.Dice.DICE, callback_method)``.
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
To allow only dice messages with the emoji 🎯 and with value 6, use
|
|
|
|
``MessageHandler(filters.Dice.Darts(6), callback_method)``.
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
To allow only dice messages with the emoji ⚽ and with value 5 `or` 6, use
|
|
|
|
``MessageHandler(filters.Dice.Football([5, 6]), callback_method)``.
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Note:
|
|
|
|
Dice messages don't have text. If you want to filter either text or dice messages, use
|
|
|
|
``filters.TEXT | filters.Dice.ALL``.
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
values (:obj:`int` | Collection[:obj:`int`], optional):
|
2021-11-20 11:36:18 +01:00
|
|
|
Which values to allow. If not specified, will allow the specified dice message.
|
|
|
|
"""
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
ALL = _Dice()
|
|
|
|
"""Dice messages with any value and any emoji."""
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class Basketball(_Dice):
|
|
|
|
"""Dice messages with the emoji 🏀. Supports passing a list of integers.
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
values (:obj:`int` | Collection[:obj:`int`]): Which values to allow.
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
2017-06-21 13:46:03 +02:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def __init__(self, values: SCT[int]):
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__(values, emoji=DiceEmojiEnum.BASKETBALL)
|
2018-02-18 16:11:04 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
BASKETBALL = _Dice(emoji=DiceEmojiEnum.BASKETBALL)
|
|
|
|
"""Dice messages with the emoji 🏀. Matches any dice value."""
|
2018-02-18 16:11:04 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class Bowling(_Dice):
|
|
|
|
"""Dice messages with the emoji 🎳. Supports passing a list of integers.
|
2018-02-18 16:11:04 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
values (:obj:`int` | Collection[:obj:`int`]): Which values to allow.
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def __init__(self, values: SCT[int]):
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__(values, emoji=DiceEmojiEnum.BOWLING)
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
BOWLING = _Dice(emoji=DiceEmojiEnum.BOWLING)
|
|
|
|
"""Dice messages with the emoji 🎳. Matches any dice value."""
|
2021-04-30 10:09:21 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class Darts(_Dice):
|
|
|
|
"""Dice messages with the emoji 🎯. Supports passing a list of integers.
|
2021-04-30 10:09:21 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
values (:obj:`int` | Collection[:obj:`int`]): Which values to allow.
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
2021-04-30 10:09:21 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def __init__(self, values: SCT[int]):
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__(values, emoji=DiceEmojiEnum.DARTS)
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
DARTS = _Dice(emoji=DiceEmojiEnum.DARTS)
|
|
|
|
"""Dice messages with the emoji 🎯. Matches any dice value."""
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class Dice(_Dice):
|
|
|
|
"""Dice messages with the emoji 🎲. Supports passing a list of integers.
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
values (:obj:`int` | Collection[:obj:`int`]): Which values to allow.
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def __init__(self, values: SCT[int]):
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__(values, emoji=DiceEmojiEnum.DICE)
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2024-02-07 20:45:57 +01:00
|
|
|
DICE = _Dice(emoji=DiceEmojiEnum.DICE)
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Dice messages with the emoji 🎲. Matches any dice value."""
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class Football(_Dice):
|
|
|
|
"""Dice messages with the emoji ⚽. Supports passing a list of integers.
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
values (:obj:`int` | Collection[:obj:`int`]): Which values to allow.
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
2017-06-01 04:09:30 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def __init__(self, values: SCT[int]):
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__(values, emoji=DiceEmojiEnum.FOOTBALL)
|
2017-09-01 08:41:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
FOOTBALL = _Dice(emoji=DiceEmojiEnum.FOOTBALL)
|
|
|
|
"""Dice messages with the emoji ⚽. Matches any dice value."""
|
2017-09-01 08:41:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class SlotMachine(_Dice):
|
|
|
|
"""Dice messages with the emoji 🎰. Supports passing a list of integers.
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
values (:obj:`int` | Collection[:obj:`int`]): Which values to allow.
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def __init__(self, values: SCT[int]):
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__(values, emoji=DiceEmojiEnum.SLOT_MACHINE)
|
2016-09-14 19:29:15 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
SLOT_MACHINE = _Dice(emoji=DiceEmojiEnum.SLOT_MACHINE)
|
|
|
|
"""Dice messages with the emoji 🎰. Matches any dice value."""
|
2016-09-24 18:30:58 +02:00
|
|
|
|
2016-10-19 12:35:50 +02:00
|
|
|
|
2022-04-24 13:47:35 +02:00
|
|
|
class Document:
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
Subset for messages containing a document/file.
|
2016-10-19 12:35:50 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Examples:
|
|
|
|
Use these filters like: ``filters.Document.MP3``,
|
2022-04-24 13:47:35 +02:00
|
|
|
``filters.Document.MimeType("text/plain")`` etc. Or just use ``filters.Document.ALL`` for
|
|
|
|
all document messages.
|
|
|
|
|
|
|
|
Caution:
|
|
|
|
``filters.Document`` itself is *not* a filter, but just a convenience namespace.
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
2016-10-19 12:35:50 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
2016-09-24 18:30:58 +02:00
|
|
|
|
2022-04-24 13:47:35 +02:00
|
|
|
class _All(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.document)
|
|
|
|
|
|
|
|
ALL = _All(name="filters.Document.ALL")
|
|
|
|
"""Messages that contain a :attr:`telegram.Message.document`."""
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class Category(MessageFilter):
|
|
|
|
"""Filters documents by their category in the mime-type attribute.
|
2017-07-23 22:33:08 +02:00
|
|
|
|
2016-09-24 18:30:58 +02:00
|
|
|
Args:
|
2021-11-20 11:36:18 +01:00
|
|
|
category (:obj:`str`): Category of the media you want to filter.
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Example:
|
|
|
|
``filters.Document.Category('audio/')`` returns :obj:`True` for all types
|
|
|
|
of audio sent as a file, for example ``'audio/mpeg'`` or ``'audio/x-wav'``.
|
|
|
|
|
|
|
|
Note:
|
|
|
|
This Filter only filters by the mime_type of the document, it doesn't check the
|
|
|
|
validity of the document. The user can manipulate the mime-type of a message and
|
|
|
|
send media with wrong types that don't fit to this handler.
|
2016-09-24 18:30:58 +02:00
|
|
|
"""
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ("_category",)
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def __init__(self, category: str):
|
|
|
|
self._category = category
|
|
|
|
super().__init__(name=f"filters.Document.Category('{self._category}')")
|
2016-09-24 18:30:58 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def filter(self, message: Message) -> bool:
|
2021-11-21 12:39:04 +01:00
|
|
|
if message.document and message.document.mime_type:
|
2021-11-20 11:36:18 +01:00
|
|
|
return message.document.mime_type.startswith(self._category)
|
|
|
|
return False
|
2018-04-20 13:24:40 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
APPLICATION = Category("application/")
|
|
|
|
"""Use as ``filters.Document.APPLICATION``."""
|
|
|
|
AUDIO = Category("audio/")
|
|
|
|
"""Use as ``filters.Document.AUDIO``."""
|
|
|
|
IMAGE = Category("image/")
|
|
|
|
"""Use as ``filters.Document.IMAGE``."""
|
|
|
|
VIDEO = Category("video/")
|
|
|
|
"""Use as ``filters.Document.VIDEO``."""
|
|
|
|
TEXT = Category("text/")
|
|
|
|
"""Use as ``filters.Document.TEXT``."""
|
2018-04-20 13:24:40 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class FileExtension(MessageFilter):
|
|
|
|
"""This filter filters documents by their file ending/extension.
|
2018-04-20 13:24:40 +02:00
|
|
|
|
|
|
|
Args:
|
2021-11-20 11:36:18 +01:00
|
|
|
file_extension (:obj:`str` | :obj:`None`): Media file extension you want to filter.
|
|
|
|
case_sensitive (:obj:`bool`, optional): Pass :obj:`True` to make the filter case
|
|
|
|
sensitive. Default: :obj:`False`.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
* ``filters.Document.FileExtension("jpg")``
|
|
|
|
filters files with extension ``".jpg"``.
|
|
|
|
* ``filters.Document.FileExtension(".jpg")``
|
|
|
|
filters files with extension ``"..jpg"``.
|
|
|
|
* ``filters.Document.FileExtension("Dockerfile", case_sensitive=True)``
|
|
|
|
filters files with extension ``".Dockerfile"`` minding the case.
|
|
|
|
* ``filters.Document.FileExtension(None)``
|
|
|
|
filters files without a dot in the filename.
|
2018-04-20 13:24:40 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Note:
|
|
|
|
* This Filter only filters by the file ending/extension of the document,
|
|
|
|
it doesn't check the validity of document.
|
|
|
|
* The user can manipulate the file extension of a document and
|
|
|
|
send media with wrong types that don't fit to this handler.
|
|
|
|
* Case insensitive by default,
|
|
|
|
you may change this with the flag ``case_sensitive=True``.
|
|
|
|
* Extension should be passed without leading dot
|
|
|
|
unless it's a part of the extension.
|
|
|
|
* Pass :obj:`None` to filter files with no extension,
|
|
|
|
i.e. without a dot in the filename.
|
2018-04-20 13:24:40 +02:00
|
|
|
"""
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ("_file_extension", "is_case_sensitive")
|
|
|
|
|
|
|
|
def __init__(self, file_extension: Optional[str], case_sensitive: bool = False):
|
|
|
|
super().__init__()
|
2023-02-02 18:55:07 +01:00
|
|
|
self.is_case_sensitive: bool = case_sensitive
|
2021-11-20 11:36:18 +01:00
|
|
|
if file_extension is None:
|
|
|
|
self._file_extension = None
|
|
|
|
self.name = "filters.Document.FileExtension(None)"
|
|
|
|
elif self.is_case_sensitive:
|
|
|
|
self._file_extension = f".{file_extension}"
|
|
|
|
self.name = (
|
|
|
|
f"filters.Document.FileExtension({file_extension!r}, case_sensitive=True)"
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
self._file_extension = f".{file_extension}".lower()
|
|
|
|
self.name = f"filters.Document.FileExtension({file_extension.lower()!r})"
|
2018-04-20 13:24:40 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def filter(self, message: Message) -> bool:
|
2021-11-21 12:39:04 +01:00
|
|
|
if message.document is None or message.document.file_name is None:
|
2021-11-20 11:36:18 +01:00
|
|
|
return False
|
|
|
|
if self._file_extension is None:
|
|
|
|
return "." not in message.document.file_name
|
|
|
|
if self.is_case_sensitive:
|
|
|
|
filename = message.document.file_name
|
|
|
|
else:
|
|
|
|
filename = message.document.file_name.lower()
|
|
|
|
return filename.endswith(self._file_extension)
|
2017-04-23 23:22:05 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class MimeType(MessageFilter):
|
|
|
|
"""This Filter filters documents by their mime-type attribute.
|
2020-10-29 19:42:08 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
|
|
|
mimetype (:obj:`str`): The mimetype to filter.
|
2020-10-29 19:42:08 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Example:
|
|
|
|
``filters.Document.MimeType('audio/mpeg')`` filters all audio in `.mp3` format.
|
2020-10-29 19:42:08 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Note:
|
|
|
|
This Filter only filters by the mime_type of the document, it doesn't check the
|
|
|
|
validity of document. The user can manipulate the mime-type of a message and
|
|
|
|
send media with wrong types that don't fit to this handler.
|
|
|
|
"""
|
2020-10-29 19:42:08 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ("mimetype",)
|
2020-10-29 19:42:08 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def __init__(self, mimetype: str):
|
2024-02-07 20:45:57 +01:00
|
|
|
self.mimetype: str = mimetype
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__(name=f"filters.Document.MimeType('{self.mimetype}')")
|
2020-10-29 19:42:08 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
if message.document:
|
|
|
|
return message.document.mime_type == self.mimetype
|
|
|
|
return False
|
2020-10-29 19:42:08 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
APK = MimeType("application/vnd.android.package-archive")
|
|
|
|
"""Use as ``filters.Document.APK``."""
|
2021-11-21 12:39:04 +01:00
|
|
|
DOC = MimeType(mimetypes.types_map[".doc"])
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Use as ``filters.Document.DOC``."""
|
|
|
|
DOCX = MimeType("application/vnd.openxmlformats-officedocument.wordprocessingml.document")
|
|
|
|
"""Use as ``filters.Document.DOCX``."""
|
2021-11-21 12:39:04 +01:00
|
|
|
EXE = MimeType(mimetypes.types_map[".exe"])
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Use as ``filters.Document.EXE``."""
|
2021-11-21 12:39:04 +01:00
|
|
|
MP4 = MimeType(mimetypes.types_map[".mp4"])
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Use as ``filters.Document.MP4``."""
|
2021-11-21 12:39:04 +01:00
|
|
|
GIF = MimeType(mimetypes.types_map[".gif"])
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Use as ``filters.Document.GIF``."""
|
2021-11-21 12:39:04 +01:00
|
|
|
JPG = MimeType(mimetypes.types_map[".jpg"])
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Use as ``filters.Document.JPG``."""
|
2021-11-21 12:39:04 +01:00
|
|
|
MP3 = MimeType(mimetypes.types_map[".mp3"])
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Use as ``filters.Document.MP3``."""
|
2021-11-21 12:39:04 +01:00
|
|
|
PDF = MimeType(mimetypes.types_map[".pdf"])
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Use as ``filters.Document.PDF``."""
|
2021-11-21 12:39:04 +01:00
|
|
|
PY = MimeType(mimetypes.types_map[".py"])
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Use as ``filters.Document.PY``."""
|
2021-11-21 12:39:04 +01:00
|
|
|
SVG = MimeType(mimetypes.types_map[".svg"])
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Use as ``filters.Document.SVG``."""
|
2021-11-21 12:39:04 +01:00
|
|
|
TXT = MimeType(mimetypes.types_map[".txt"])
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Use as ``filters.Document.TXT``."""
|
|
|
|
TARGZ = MimeType("application/x-compressed-tar")
|
|
|
|
"""Use as ``filters.Document.TARGZ``."""
|
2021-11-21 12:39:04 +01:00
|
|
|
WAV = MimeType(mimetypes.types_map[".wav"])
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Use as ``filters.Document.WAV``."""
|
2021-11-21 12:39:04 +01:00
|
|
|
XML = MimeType(mimetypes.types_map[".xml"])
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Use as ``filters.Document.XML``."""
|
2021-11-21 12:39:04 +01:00
|
|
|
ZIP = MimeType(mimetypes.types_map[".zip"])
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Use as ``filters.Document.ZIP``."""
|
2020-10-29 19:42:08 +01:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class Entity(MessageFilter):
|
|
|
|
"""
|
|
|
|
Filters messages to only allow those which have a :class:`telegram.MessageEntity`
|
|
|
|
where their :class:`~telegram.MessageEntity.type` matches `entity_type`.
|
2020-10-29 19:42:08 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Examples:
|
|
|
|
``MessageHandler(filters.Entity("hashtag"), callback_method)``
|
2020-10-29 19:42:08 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
|
|
|
entity_type (:obj:`str`): Entity type to check for. All types can be found as constants
|
|
|
|
in :class:`telegram.MessageEntity`.
|
2020-10-29 19:42:08 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
2020-10-29 19:42:08 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ("entity_type",)
|
2020-10-29 19:42:08 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def __init__(self, entity_type: str):
|
2023-02-02 18:55:07 +01:00
|
|
|
self.entity_type: str = entity_type
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__(name=f"filters.Entity({self.entity_type})")
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return any(entity.type == self.entity_type for entity in message.entities)
|
|
|
|
|
|
|
|
|
|
|
|
class _Forwarded(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
2024-02-08 17:12:00 +01:00
|
|
|
return bool(message.forward_origin)
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
FORWARDED = _Forwarded(name="filters.FORWARDED")
|
2024-02-08 17:12:00 +01:00
|
|
|
"""Messages that contain :attr:`telegram.Message.forward_origin`.
|
|
|
|
|
2024-02-08 18:36:28 +01:00
|
|
|
.. versionchanged:: 20.8
|
2024-02-08 17:12:00 +01:00
|
|
|
Now based on :attr:`telegram.Message.forward_origin` instead of
|
2024-02-25 10:34:47 +01:00
|
|
|
``telegram.Message.forward_date``.
|
2024-02-08 17:12:00 +01:00
|
|
|
"""
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ForwardedFrom(_ChatUserBaseFilter):
|
|
|
|
"""Filters messages to allow only those which are forwarded from the specified chat ID(s)
|
2024-02-08 17:12:00 +01:00
|
|
|
or username(s) based on :attr:`telegram.Message.forward_origin` and in particular
|
|
|
|
|
|
|
|
* :attr:`telegram.MessageOriginUser.sender_user`
|
|
|
|
* :attr:`telegram.MessageOriginChat.sender_chat`
|
|
|
|
* :attr:`telegram.MessageOriginChannel.chat`
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
.. versionadded:: 13.5
|
2020-10-29 19:42:08 +01:00
|
|
|
|
2024-02-08 18:36:28 +01:00
|
|
|
.. versionchanged:: 20.8
|
2024-02-25 10:34:47 +01:00
|
|
|
Was previously based on ``telegram.Message.forward_from`` and
|
|
|
|
``telegram.Message.forward_from_chat``.
|
2024-02-08 17:12:00 +01:00
|
|
|
|
2020-10-29 19:42:08 +01:00
|
|
|
Examples:
|
2021-11-20 11:36:18 +01:00
|
|
|
``MessageHandler(filters.ForwardedFrom(chat_id=1234), callback_method)``
|
|
|
|
|
|
|
|
Note:
|
|
|
|
When a user has disallowed adding a link to their account while forwarding their
|
2024-02-08 17:12:00 +01:00
|
|
|
messages, this filter will *not* work since
|
|
|
|
:attr:`telegram.Message.forward_origin` will be of type
|
|
|
|
:class:`telegram.MessageOriginHiddenUser`. However, this behaviour
|
2021-11-20 11:36:18 +01:00
|
|
|
is undocumented and might be changed by Telegram.
|
|
|
|
|
|
|
|
Warning:
|
|
|
|
:attr:`chat_ids` will give a *copy* of the saved chat ids as :class:`frozenset`. This
|
|
|
|
is to ensure thread safety. To add/remove a chat, you should use :meth:`add_chat_ids`, and
|
|
|
|
:meth:`remove_chat_ids`. Only update the entire set by ``filter.chat_ids = new_set``, if
|
|
|
|
you are entirely sure that it is not causing race conditions, as this will complete replace
|
|
|
|
the current set of allowed chats.
|
|
|
|
|
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
chat_id(:obj:`int` | Collection[:obj:`int`], optional):
|
2021-11-20 11:36:18 +01:00
|
|
|
Which chat/user ID(s) to allow through.
|
2022-05-13 16:41:34 +02:00
|
|
|
username(:obj:`str` | Collection[:obj:`str`], optional):
|
2021-11-20 11:36:18 +01:00
|
|
|
Which username(s) to allow through. Leading ``'@'`` s in usernames will be
|
|
|
|
discarded.
|
|
|
|
allow_empty(:obj:`bool`, optional): Whether updates should be processed, if no chat
|
|
|
|
is specified in :attr:`chat_ids` and :attr:`usernames`. Defaults to :obj:`False`.
|
2020-10-29 19:42:08 +01:00
|
|
|
|
|
|
|
Attributes:
|
2021-11-20 11:36:18 +01:00
|
|
|
chat_ids (set(:obj:`int`)): Which chat/user ID(s) to allow through.
|
|
|
|
allow_empty (:obj:`bool`): Whether updates should be processed, if no chat
|
|
|
|
is specified in :attr:`chat_ids` and :attr:`usernames`.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
RuntimeError: If both ``chat_id`` and ``username`` are present.
|
2020-10-29 19:42:08 +01:00
|
|
|
"""
|
2017-05-22 12:13:00 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
|
|
|
|
2023-02-02 18:55:07 +01:00
|
|
|
def _get_chat_or_user(self, message: Message) -> Union[TGUser, TGChat, None]:
|
2024-02-08 17:12:00 +01:00
|
|
|
if (forward_origin := message.forward_origin) is None:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if isinstance(forward_origin, MessageOriginUser):
|
|
|
|
return forward_origin.sender_user
|
|
|
|
if isinstance(forward_origin, MessageOriginChat):
|
|
|
|
return forward_origin.sender_chat
|
|
|
|
if isinstance(forward_origin, MessageOriginChannel):
|
|
|
|
return forward_origin.chat
|
|
|
|
|
|
|
|
return None
|
2021-11-20 11:36:18 +01:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def add_chat_ids(self, chat_id: SCT[int]) -> None:
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
Add one or more chats to the allowed chat ids.
|
|
|
|
|
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
chat_id(:obj:`int` | Collection[:obj:`int`]): Which chat/user ID(s) to
|
2021-11-20 11:36:18 +01:00
|
|
|
allow through.
|
|
|
|
"""
|
|
|
|
return super()._add_chat_ids(chat_id)
|
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def remove_chat_ids(self, chat_id: SCT[int]) -> None:
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
Remove one or more chats from allowed chat ids.
|
|
|
|
|
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
chat_id(:obj:`int` | Collection[:obj:`int`]): Which chat/user ID(s) to
|
2021-11-20 11:36:18 +01:00
|
|
|
disallow through.
|
|
|
|
"""
|
|
|
|
return super()._remove_chat_ids(chat_id)
|
|
|
|
|
|
|
|
|
|
|
|
class _Game(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.game)
|
|
|
|
|
|
|
|
|
|
|
|
GAME = _Game(name="filters.GAME")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.game`."""
|
|
|
|
|
|
|
|
|
2024-02-08 17:12:00 +01:00
|
|
|
class _Giveaway(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.giveaway)
|
|
|
|
|
|
|
|
|
|
|
|
GIVEAWAY = _Giveaway(name="filters.GIVEAWAY")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.giveaway`."""
|
|
|
|
|
|
|
|
|
|
|
|
class _GiveawayWinners(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.giveaway_winners)
|
|
|
|
|
|
|
|
|
|
|
|
GIVEAWAY_WINNERS = _GiveawayWinners(name="filters.GIVEAWAY_WINNERS")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.giveaway_winners`."""
|
|
|
|
|
|
|
|
|
2023-01-01 17:00:49 +01:00
|
|
|
class _HasMediaSpoiler(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.has_media_spoiler)
|
|
|
|
|
|
|
|
|
|
|
|
HAS_MEDIA_SPOILER = _HasMediaSpoiler(name="filters.HAS_MEDIA_SPOILER")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.has_media_spoiler`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _HasProtectedContent(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.has_protected_content)
|
|
|
|
|
|
|
|
|
2022-02-09 17:30:16 +01:00
|
|
|
HAS_PROTECTED_CONTENT = _HasProtectedContent(name="filters.HAS_PROTECTED_CONTENT")
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Messages that contain :attr:`telegram.Message.has_protected_content`.
|
|
|
|
|
|
|
|
.. versionadded:: 13.9
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class _Invoice(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.invoice)
|
|
|
|
|
|
|
|
|
|
|
|
INVOICE = _Invoice(name="filters.INVOICE")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.invoice`."""
|
|
|
|
|
|
|
|
|
|
|
|
class _IsAutomaticForward(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.is_automatic_forward)
|
|
|
|
|
|
|
|
|
2022-02-09 17:30:16 +01:00
|
|
|
IS_AUTOMATIC_FORWARD = _IsAutomaticForward(name="filters.IS_AUTOMATIC_FORWARD")
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Messages that contain :attr:`telegram.Message.is_automatic_forward`.
|
|
|
|
|
|
|
|
.. versionadded:: 13.9
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2022-11-22 10:43:50 +01:00
|
|
|
class _IsTopicMessage(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.is_topic_message)
|
|
|
|
|
|
|
|
|
|
|
|
IS_TOPIC_MESSAGE = _IsTopicMessage(name="filters.IS_TOPIC_MESSAGE")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.is_topic_message`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2024-04-12 11:58:25 +02:00
|
|
|
class _IsFromOffline(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.is_from_offline)
|
|
|
|
|
|
|
|
|
|
|
|
IS_FROM_OFFLINE = _IsFromOffline(name="filters.IS_FROM_OFFLINE")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.is_from_offline`.
|
|
|
|
|
2024-04-12 12:39:38 +02:00
|
|
|
.. versionadded:: 21.1
|
2024-04-12 11:58:25 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class Language(MessageFilter):
|
|
|
|
"""Filters messages to only allow those which are from users with a certain language code.
|
|
|
|
|
|
|
|
Note:
|
|
|
|
According to official Telegram Bot API documentation, not every single user has the
|
|
|
|
`language_code` attribute. Do not count on this filter working on all users.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
``MessageHandler(filters.Language("en"), callback_method)``
|
|
|
|
|
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
lang (:obj:`str` | Collection[:obj:`str`]):
|
2021-11-20 11:36:18 +01:00
|
|
|
Which language code(s) to allow through.
|
|
|
|
This will be matched using :obj:`str.startswith` meaning that
|
|
|
|
'en' will match both 'en_US' and 'en_GB'.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
__slots__ = ("lang",)
|
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def __init__(self, lang: SCT[str]):
|
2021-11-20 11:36:18 +01:00
|
|
|
if isinstance(lang, str):
|
|
|
|
lang = cast(str, lang)
|
2023-02-02 18:55:07 +01:00
|
|
|
self.lang: Sequence[str] = [lang]
|
2021-11-20 11:36:18 +01:00
|
|
|
else:
|
|
|
|
lang = cast(List[str], lang)
|
|
|
|
self.lang = lang
|
|
|
|
super().__init__(name=f"filters.Language({self.lang})")
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(
|
2021-11-21 12:39:04 +01:00
|
|
|
message.from_user
|
|
|
|
and message.from_user.language_code
|
2021-11-20 11:36:18 +01:00
|
|
|
and any(message.from_user.language_code.startswith(x) for x in self.lang)
|
2021-05-29 16:18:16 +02:00
|
|
|
)
|
|
|
|
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Location(MessageFilter):
|
|
|
|
__slots__ = ()
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.location)
|
2020-11-29 16:20:46 +01:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
LOCATION = _Location(name="filters.LOCATION")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.location`."""
|
2020-11-29 16:20:46 +01:00
|
|
|
|
|
|
|
|
2023-10-23 21:11:56 +02:00
|
|
|
class Mention(MessageFilter):
|
|
|
|
"""Messages containing mentions of specified users or chats.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
MessageHandler(filters.Mention("username"), callback)
|
|
|
|
MessageHandler(filters.Mention(["@username", 123456]), callback)
|
|
|
|
|
2023-11-27 19:01:57 +01:00
|
|
|
.. versionadded:: 20.7
|
2023-10-23 21:11:56 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
mentions (:obj:`int` | :obj:`str` | :class:`telegram.User` | Collection[:obj:`int` | \
|
|
|
|
:obj:`str` | :class:`telegram.User`]):
|
|
|
|
Specifies the users and chats to filter for. Messages that do not mention at least one
|
|
|
|
of the specified users or chats will not be handled. Leading ``'@'`` s in usernames
|
|
|
|
will be discarded.
|
|
|
|
"""
|
|
|
|
|
|
|
|
__slots__ = ("_mentions",)
|
|
|
|
|
|
|
|
def __init__(self, mentions: SCT[Union[int, str, TGUser]]):
|
|
|
|
super().__init__(name=f"filters.Mention({mentions})")
|
|
|
|
if isinstance(mentions, Iterable) and not isinstance(mentions, str):
|
|
|
|
self._mentions = {self._fix_mention_username(mention) for mention in mentions}
|
|
|
|
else:
|
|
|
|
self._mentions = {self._fix_mention_username(mentions)}
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _fix_mention_username(mention: Union[int, str, TGUser]) -> Union[int, str, TGUser]:
|
|
|
|
if not isinstance(mention, str):
|
|
|
|
return mention
|
|
|
|
return mention.lstrip("@")
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def _check_mention(cls, message: Message, mention: Union[int, str, TGUser]) -> bool:
|
|
|
|
if not message.entities:
|
|
|
|
return False
|
|
|
|
|
|
|
|
entity_texts = message.parse_entities(
|
|
|
|
types=[MessageEntity.MENTION, MessageEntity.TEXT_MENTION]
|
|
|
|
)
|
|
|
|
|
|
|
|
if isinstance(mention, TGUser):
|
|
|
|
return any(
|
|
|
|
mention.id == entity.user.id
|
|
|
|
or mention.username == entity.user.username
|
|
|
|
or mention.username == cls._fix_mention_username(entity_texts[entity])
|
|
|
|
for entity in message.entities
|
|
|
|
if entity.user
|
|
|
|
) or any(
|
|
|
|
mention.username == cls._fix_mention_username(entity_text)
|
|
|
|
for entity_text in entity_texts.values()
|
|
|
|
)
|
|
|
|
if isinstance(mention, int):
|
|
|
|
return bool(
|
|
|
|
any(mention == entity.user.id for entity in message.entities if entity.user)
|
|
|
|
)
|
|
|
|
return any(
|
|
|
|
mention == cls._fix_mention_username(entity_text)
|
|
|
|
for entity_text in entity_texts.values()
|
|
|
|
)
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return any(self._check_mention(message, mention) for mention in self._mentions)
|
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _PassportData(MessageFilter):
|
|
|
|
__slots__ = ()
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.passport_data)
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2017-06-22 10:35:59 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
PASSPORT_DATA = _PassportData(name="filters.PASSPORT_DATA")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.passport_data`."""
|
2017-06-19 19:50:44 +02:00
|
|
|
|
2020-05-10 12:15:11 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Photo(MessageFilter):
|
|
|
|
__slots__ = ()
|
2020-12-30 15:59:50 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.photo)
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
PHOTO = _Photo("filters.PHOTO")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.photo`."""
|
2020-07-14 21:51:36 +02:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Poll(MessageFilter):
|
|
|
|
__slots__ = ()
|
2020-12-30 15:59:50 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.poll)
|
2020-10-09 17:22:07 +02:00
|
|
|
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
POLL = _Poll(name="filters.POLL")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.poll`."""
|
2017-06-19 19:50:44 +02:00
|
|
|
|
2020-05-10 12:15:11 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class Regex(MessageFilter):
|
|
|
|
"""
|
2022-10-23 14:59:27 +02:00
|
|
|
Filters updates by searching for an occurrence of :paramref:`~Regex.pattern` in the message
|
|
|
|
text.
|
2022-02-09 17:30:16 +01:00
|
|
|
The :func:`re.search` function is used to determine whether an update should be filtered.
|
2020-12-30 15:59:50 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Refer to the documentation of the :obj:`re` module for more information.
|
|
|
|
|
|
|
|
To get the groups and groupdict matched, see :attr:`telegram.ext.CallbackContext.matches`.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
Use ``MessageHandler(filters.Regex(r'help'), callback)`` to capture all messages that
|
|
|
|
contain the word 'help'. You can also use
|
|
|
|
``MessageHandler(filters.Regex(re.compile(r'help', re.IGNORECASE)), callback)`` if
|
|
|
|
you want your pattern to be case insensitive. This approach is recommended
|
|
|
|
if you need to specify flags on your pattern.
|
|
|
|
|
|
|
|
Note:
|
2022-04-24 12:38:09 +02:00
|
|
|
Filters use the same short circuiting logic as python's :keyword:`and`, :keyword:`or` and
|
|
|
|
:keyword:`not`.
|
2021-11-20 11:36:18 +01:00
|
|
|
This means that for example:
|
|
|
|
|
|
|
|
>>> filters.Regex(r'(a?x)') | filters.Regex(r'(b?x)')
|
|
|
|
|
|
|
|
With a :attr:`telegram.Message.text` of `x`, will only ever return the matches for the
|
|
|
|
first filter, since the second one is never evaluated.
|
|
|
|
|
2023-01-01 16:24:00 +01:00
|
|
|
.. seealso:: :wiki:`Types of Handlers <Types-of-Handlers>`
|
Documentation Improvements (#3214, #3217, #3218, #3271, #3289, #3292, #3303, #3312, #3306, #3319, #3326, #3314)
Co-authored-by: Harshil <37377066+harshil21@users.noreply.github.com>
Co-authored-by: Simon Fong <44134941+simonfongnt@users.noreply.github.com>
Co-authored-by: Piotr Rogulski <rivinek@gmail.com>
Co-authored-by: poolitzer <25934244+Poolitzer@users.noreply.github.com>
Co-authored-by: Or Bin <or@raftt.io>
Co-authored-by: Sandro <j32g7f67hb@liamekaens.com>
Co-authored-by: Hatim Zahid <63000127+HatimZ@users.noreply.github.com>
Co-authored-by: Robi <53259730+RobiMez@users.noreply.github.com>
Co-authored-by: Dmitry Kolomatskiy <58207913+lemontree210@users.noreply.github.com>
2022-11-15 09:06:23 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-02-09 17:30:16 +01:00
|
|
|
pattern (:obj:`str` | :func:`re.Pattern <re.compile>`): The regex pattern.
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
__slots__ = ("pattern",)
|
|
|
|
|
2023-02-02 18:55:07 +01:00
|
|
|
def __init__(self, pattern: Union[str, Pattern[str]]):
|
2021-11-20 11:36:18 +01:00
|
|
|
if isinstance(pattern, str):
|
|
|
|
pattern = re.compile(pattern)
|
2023-02-02 18:55:07 +01:00
|
|
|
self.pattern: Pattern[str] = pattern
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__(name=f"filters.Regex({self.pattern})", data_filter=True)
|
|
|
|
|
2023-02-02 18:55:07 +01:00
|
|
|
def filter(self, message: Message) -> Optional[Dict[str, List[Match[str]]]]:
|
2023-06-29 18:17:47 +02:00
|
|
|
if message.text and (match := self.pattern.search(message.text)):
|
|
|
|
return {"matches": [match]}
|
2021-11-20 11:36:18 +01:00
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
class _Reply(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.reply_to_message)
|
|
|
|
|
|
|
|
|
|
|
|
REPLY = _Reply(name="filters.REPLY")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.reply_to_message`."""
|
|
|
|
|
|
|
|
|
|
|
|
class _SenderChat(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.sender_chat)
|
|
|
|
|
|
|
|
|
|
|
|
class SenderChat(_ChatUserBaseFilter):
|
|
|
|
"""Filters messages to allow only those which are from a specified sender chat's chat ID or
|
|
|
|
username.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
* To filter for messages sent to a group by a channel with ID
|
|
|
|
``-1234``, use ``MessageHandler(filters.SenderChat(-1234), callback_method)``.
|
|
|
|
* To filter for messages of anonymous admins in a super group with username
|
|
|
|
``@anonymous``, use
|
|
|
|
``MessageHandler(filters.SenderChat(username='anonymous'), callback_method)``.
|
|
|
|
* To filter for messages sent to a group by *any* channel, use
|
|
|
|
``MessageHandler(filters.SenderChat.CHANNEL, callback_method)``.
|
|
|
|
* To filter for messages of anonymous admins in *any* super group, use
|
|
|
|
``MessageHandler(filters.SenderChat.SUPERGROUP, callback_method)``.
|
|
|
|
* To filter for messages forwarded to a discussion group from *any* channel or of anonymous
|
|
|
|
admins in *any* super group, use ``MessageHandler(filters.SenderChat.ALL, callback)``
|
|
|
|
|
|
|
|
Note:
|
|
|
|
Remember, ``sender_chat`` is also set for messages in a channel as the channel itself,
|
|
|
|
so when your bot is an admin in a channel and the linked discussion group, you would
|
|
|
|
receive the message twice (once from inside the channel, once inside the discussion
|
|
|
|
group). Since v13.9, the field :attr:`telegram.Message.is_automatic_forward` will be
|
2022-01-03 09:09:03 +01:00
|
|
|
:obj:`True` for the discussion group message.
|
2021-11-20 11:36:18 +01:00
|
|
|
|
2022-02-09 17:30:16 +01:00
|
|
|
.. seealso:: :attr:`telegram.ext.filters.IS_AUTOMATIC_FORWARD`
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
Warning:
|
|
|
|
:attr:`chat_ids` will return a *copy* of the saved chat ids as :obj:`frozenset`. This
|
|
|
|
is to ensure thread safety. To add/remove a chat, you should use :meth:`add_chat_ids`, and
|
|
|
|
:meth:`remove_chat_ids`. Only update the entire set by ``filter.chat_ids = new_set``, if
|
|
|
|
you are entirely sure that it is not causing race conditions, as this will complete replace
|
|
|
|
the current set of allowed chats.
|
|
|
|
|
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
chat_id(:obj:`int` | Collection[:obj:`int`], optional):
|
2021-11-20 11:36:18 +01:00
|
|
|
Which sender chat chat ID(s) to allow through.
|
2022-05-13 16:41:34 +02:00
|
|
|
username(:obj:`str` | Collection[:obj:`str`], optional):
|
2021-11-20 11:36:18 +01:00
|
|
|
Which sender chat username(s) to allow through.
|
|
|
|
Leading ``'@'`` s in usernames will be discarded.
|
|
|
|
allow_empty(:obj:`bool`, optional): Whether updates should be processed, if no sender
|
|
|
|
chat is specified in :attr:`chat_ids` and :attr:`usernames`. Defaults to :obj:`False`.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
chat_ids (set(:obj:`int`)): Which sender chat chat ID(s) to allow through.
|
|
|
|
allow_empty (:obj:`bool`): Whether updates should be processed, if no sender chat is
|
|
|
|
specified in :attr:`chat_ids` and :attr:`usernames`.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
RuntimeError: If both ``chat_id`` and ``username`` are present.
|
|
|
|
"""
|
|
|
|
|
|
|
|
__slots__ = ()
|
2017-06-19 19:50:44 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _CHANNEL(MessageFilter):
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
if message.sender_chat:
|
|
|
|
return message.sender_chat.type == TGChat.CHANNEL
|
|
|
|
return False
|
2021-03-28 11:50:45 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _SUPERGROUP(MessageFilter):
|
|
|
|
__slots__ = ()
|
2021-03-28 11:50:45 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
if message.sender_chat:
|
|
|
|
return message.sender_chat.type == TGChat.SUPERGROUP
|
|
|
|
return False
|
|
|
|
|
|
|
|
ALL = _SenderChat(name="filters.SenderChat.ALL")
|
|
|
|
"""All messages with a :attr:`telegram.Message.sender_chat`."""
|
|
|
|
SUPER_GROUP = _SUPERGROUP(name="filters.SenderChat.SUPER_GROUP")
|
|
|
|
"""Messages whose sender chat is a super group."""
|
|
|
|
CHANNEL = _CHANNEL(name="filters.SenderChat.CHANNEL")
|
|
|
|
"""Messages whose sender chat is a channel."""
|
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def add_chat_ids(self, chat_id: SCT[int]) -> None:
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
Add one or more sender chats to the allowed chat ids.
|
2021-03-28 11:50:45 +02:00
|
|
|
|
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
chat_id(:obj:`int` | Collection[:obj:`int`]): Which sender chat ID(s) to
|
2021-03-28 11:50:45 +02:00
|
|
|
allow through.
|
|
|
|
"""
|
2021-11-20 11:36:18 +01:00
|
|
|
return super()._add_chat_ids(chat_id)
|
2021-03-28 11:50:45 +02:00
|
|
|
|
2023-02-02 18:55:07 +01:00
|
|
|
def _get_chat_or_user(self, message: Message) -> Optional[TGChat]:
|
2021-11-20 11:36:18 +01:00
|
|
|
return message.sender_chat
|
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def remove_chat_ids(self, chat_id: SCT[int]) -> None:
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
Remove one or more sender chats from allowed chat ids.
|
|
|
|
|
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
chat_id(:obj:`int` | Collection[:obj:`int`]): Which sender chat ID(s) to
|
2021-11-20 11:36:18 +01:00
|
|
|
disallow through.
|
|
|
|
"""
|
|
|
|
return super()._remove_chat_ids(chat_id)
|
|
|
|
|
|
|
|
|
|
|
|
class StatusUpdate:
|
|
|
|
"""Subset for messages containing a status update.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
Use these filters like: ``filters.StatusUpdate.NEW_CHAT_MEMBERS`` etc. Or use just
|
|
|
|
``filters.StatusUpdate.ALL`` for all status update messages.
|
|
|
|
|
2022-04-24 13:47:35 +02:00
|
|
|
Caution:
|
2021-11-20 11:36:18 +01:00
|
|
|
``filters.StatusUpdate`` itself is *not* a filter, but just a convenience namespace.
|
|
|
|
"""
|
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
class _All(UpdateFilter):
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, update: Update) -> bool:
|
|
|
|
return bool(
|
2024-02-08 17:12:00 +01:00
|
|
|
# keep this alphabetically sorted for easier maintenance
|
|
|
|
StatusUpdate.CHAT_CREATED.check_update(update)
|
|
|
|
or StatusUpdate.CHAT_SHARED.check_update(update)
|
|
|
|
or StatusUpdate.CONNECTED_WEBSITE.check_update(update)
|
2021-11-20 11:36:18 +01:00
|
|
|
or StatusUpdate.DELETE_CHAT_PHOTO.check_update(update)
|
2024-02-08 17:12:00 +01:00
|
|
|
or StatusUpdate.FORUM_TOPIC_CLOSED.check_update(update)
|
|
|
|
or StatusUpdate.FORUM_TOPIC_CREATED.check_update(update)
|
|
|
|
or StatusUpdate.FORUM_TOPIC_EDITED.check_update(update)
|
|
|
|
or StatusUpdate.FORUM_TOPIC_REOPENED.check_update(update)
|
|
|
|
or StatusUpdate.GENERAL_FORUM_TOPIC_HIDDEN.check_update(update)
|
|
|
|
or StatusUpdate.GENERAL_FORUM_TOPIC_UNHIDDEN.check_update(update)
|
|
|
|
or StatusUpdate.GIVEAWAY_COMPLETED.check_update(update)
|
|
|
|
or StatusUpdate.GIVEAWAY_CREATED.check_update(update)
|
|
|
|
or StatusUpdate.LEFT_CHAT_MEMBER.check_update(update)
|
2021-11-20 11:36:18 +01:00
|
|
|
or StatusUpdate.MESSAGE_AUTO_DELETE_TIMER_CHANGED.check_update(update)
|
|
|
|
or StatusUpdate.MIGRATE.check_update(update)
|
2024-02-08 17:12:00 +01:00
|
|
|
or StatusUpdate.NEW_CHAT_MEMBERS.check_update(update)
|
|
|
|
or StatusUpdate.NEW_CHAT_PHOTO.check_update(update)
|
|
|
|
or StatusUpdate.NEW_CHAT_TITLE.check_update(update)
|
2021-11-20 11:36:18 +01:00
|
|
|
or StatusUpdate.PINNED_MESSAGE.check_update(update)
|
|
|
|
or StatusUpdate.PROXIMITY_ALERT_TRIGGERED.check_update(update)
|
2024-02-08 17:12:00 +01:00
|
|
|
or StatusUpdate.USERS_SHARED.check_update(update)
|
|
|
|
or StatusUpdate.USER_SHARED.check_update(update)
|
2022-05-03 18:21:50 +02:00
|
|
|
or StatusUpdate.VIDEO_CHAT_ENDED.check_update(update)
|
|
|
|
or StatusUpdate.VIDEO_CHAT_PARTICIPANTS_INVITED.check_update(update)
|
2024-02-08 17:12:00 +01:00
|
|
|
or StatusUpdate.VIDEO_CHAT_SCHEDULED.check_update(update)
|
|
|
|
or StatusUpdate.VIDEO_CHAT_STARTED.check_update(update)
|
2022-05-03 18:21:50 +02:00
|
|
|
or StatusUpdate.WEB_APP_DATA.check_update(update)
|
2023-01-01 17:00:49 +01:00
|
|
|
or StatusUpdate.WRITE_ACCESS_ALLOWED.check_update(update)
|
2021-11-20 11:36:18 +01:00
|
|
|
)
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
ALL = _All(name="filters.StatusUpdate.ALL")
|
|
|
|
"""Messages that contain any of the below."""
|
2021-12-11 15:21:56 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _ChatCreated(MessageFilter):
|
|
|
|
__slots__ = ()
|
2021-03-14 16:46:37 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(
|
|
|
|
message.group_chat_created
|
|
|
|
or message.supergroup_chat_created
|
|
|
|
or message.channel_chat_created
|
|
|
|
)
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
CHAT_CREATED = _ChatCreated(name="filters.StatusUpdate.CHAT_CREATED")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.group_chat_created`,
|
|
|
|
:attr:`telegram.Message.supergroup_chat_created` or
|
|
|
|
:attr:`telegram.Message.channel_chat_created`."""
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2023-02-08 11:47:21 +01:00
|
|
|
class _ChatShared(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.chat_shared)
|
|
|
|
|
|
|
|
CHAT_SHARED = _ChatShared(name="filters.StatusUpdate.CHAT_SHARED")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.chat_shared`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.1
|
|
|
|
"""
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _ConnectedWebsite(MessageFilter):
|
|
|
|
__slots__ = ()
|
2020-11-29 16:20:46 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.connected_website)
|
|
|
|
|
|
|
|
CONNECTED_WEBSITE = _ConnectedWebsite(name="filters.StatusUpdate.CONNECTED_WEBSITE")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.connected_website`."""
|
|
|
|
|
|
|
|
class _DeleteChatPhoto(MessageFilter):
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.delete_chat_photo)
|
2017-06-19 19:50:44 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
DELETE_CHAT_PHOTO = _DeleteChatPhoto(name="filters.StatusUpdate.DELETE_CHAT_PHOTO")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.delete_chat_photo`."""
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2023-01-01 16:24:00 +01:00
|
|
|
class _ForumTopicClosed(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.forum_topic_closed)
|
|
|
|
|
|
|
|
FORUM_TOPIC_CLOSED = _ForumTopicClosed(name="filters.StatusUpdate.FORUM_TOPIC_CLOSED")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.forum_topic_closed`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
"""
|
|
|
|
|
|
|
|
class _ForumTopicCreated(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.forum_topic_created)
|
|
|
|
|
|
|
|
FORUM_TOPIC_CREATED = _ForumTopicCreated(name="filters.StatusUpdate.FORUM_TOPIC_CREATED")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.forum_topic_created`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
"""
|
|
|
|
|
2023-01-01 17:00:49 +01:00
|
|
|
class _ForumTopicEdited(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.forum_topic_edited)
|
|
|
|
|
|
|
|
FORUM_TOPIC_EDITED = _ForumTopicEdited(name="filters.StatusUpdate.FORUM_TOPIC_EDITED")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.forum_topic_edited`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
"""
|
|
|
|
|
2023-01-01 16:24:00 +01:00
|
|
|
class _ForumTopicReopened(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.forum_topic_reopened)
|
|
|
|
|
|
|
|
FORUM_TOPIC_REOPENED = _ForumTopicReopened(name="filters.StatusUpdate.FORUM_TOPIC_REOPENED")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.forum_topic_reopened`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
"""
|
|
|
|
|
2023-01-01 17:00:49 +01:00
|
|
|
class _GeneralForumTopicHidden(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.general_forum_topic_hidden)
|
|
|
|
|
|
|
|
GENERAL_FORUM_TOPIC_HIDDEN = _GeneralForumTopicHidden(
|
|
|
|
name="filters.StatusUpdate.GENERAL_FORUM_TOPIC_HIDDEN"
|
|
|
|
)
|
|
|
|
"""Messages that contain :attr:`telegram.Message.general_forum_topic_hidden`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
"""
|
|
|
|
|
|
|
|
class _GeneralForumTopicUnhidden(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.general_forum_topic_unhidden)
|
|
|
|
|
|
|
|
GENERAL_FORUM_TOPIC_UNHIDDEN = _GeneralForumTopicUnhidden(
|
|
|
|
name="filters.StatusUpdate.GENERAL_FORUM_TOPIC_UNHIDDEN"
|
|
|
|
)
|
|
|
|
"""Messages that contain :attr:`telegram.Message.general_forum_topic_unhidden`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
"""
|
|
|
|
|
2024-02-08 17:12:00 +01:00
|
|
|
class _GiveawayCreated(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.giveaway_created)
|
|
|
|
|
|
|
|
GIVEAWAY_CREATED = _GiveawayCreated(name="filters.StatusUpdate.GIVEAWAY_CREATED")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.giveaway_created`.
|
|
|
|
|
2024-02-08 18:36:28 +01:00
|
|
|
.. versionadded:: 20.8
|
2024-02-08 17:12:00 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
class _GiveawayCompleted(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.giveaway_completed)
|
|
|
|
|
|
|
|
GIVEAWAY_COMPLETED = _GiveawayCompleted(name="filters.StatusUpdate.GIVEAWAY_COMPLETED")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.giveaway_completed`.
|
2024-02-08 18:36:28 +01:00
|
|
|
.. versionadded:: 20.8
|
2024-02-08 17:12:00 +01:00
|
|
|
"""
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _LeftChatMember(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.left_chat_member)
|
2020-11-07 08:44:45 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
LEFT_CHAT_MEMBER = _LeftChatMember(name="filters.StatusUpdate.LEFT_CHAT_MEMBER")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.left_chat_member`."""
|
2020-11-07 08:44:45 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _MessageAutoDeleteTimerChanged(MessageFilter):
|
2021-12-11 15:21:56 +01:00
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
2021-11-20 11:36:18 +01:00
|
|
|
return bool(message.message_auto_delete_timer_changed)
|
2021-12-11 15:21:56 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
MESSAGE_AUTO_DELETE_TIMER_CHANGED = _MessageAutoDeleteTimerChanged(
|
|
|
|
"filters.StatusUpdate.MESSAGE_AUTO_DELETE_TIMER_CHANGED"
|
|
|
|
)
|
|
|
|
"""Messages that contain :attr:`telegram.Message.message_auto_delete_timer_changed`
|
2021-12-11 15:21:56 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
.. versionadded:: 13.4
|
2021-12-11 15:21:56 +01:00
|
|
|
"""
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Migrate(MessageFilter):
|
2021-12-11 15:21:56 +01:00
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
2021-11-20 11:36:18 +01:00
|
|
|
return bool(message.migrate_from_chat_id or message.migrate_to_chat_id)
|
2021-12-11 15:21:56 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
MIGRATE = _Migrate(name="filters.StatusUpdate.MIGRATE")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.migrate_from_chat_id` or
|
|
|
|
:attr:`telegram.Message.migrate_to_chat_id`."""
|
2021-12-11 15:21:56 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _NewChatMembers(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.new_chat_members)
|
2021-12-11 15:21:56 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
NEW_CHAT_MEMBERS = _NewChatMembers(name="filters.StatusUpdate.NEW_CHAT_MEMBERS")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.new_chat_members`."""
|
|
|
|
|
|
|
|
class _NewChatPhoto(MessageFilter):
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
2017-05-22 12:13:00 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def filter(self, message: Message) -> bool:
|
2021-11-20 11:36:18 +01:00
|
|
|
return bool(message.new_chat_photo)
|
2017-05-22 12:13:00 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
NEW_CHAT_PHOTO = _NewChatPhoto(name="filters.StatusUpdate.NEW_CHAT_PHOTO")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.new_chat_photo`."""
|
2017-05-22 12:13:00 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _NewChatTitle(MessageFilter):
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
2017-05-22 12:13:00 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def filter(self, message: Message) -> bool:
|
2021-11-20 11:36:18 +01:00
|
|
|
return bool(message.new_chat_title)
|
2017-05-22 12:13:00 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
NEW_CHAT_TITLE = _NewChatTitle(name="filters.StatusUpdate.NEW_CHAT_TITLE")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.new_chat_title`."""
|
2017-06-01 21:01:04 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _PinnedMessage(MessageFilter):
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def filter(self, message: Message) -> bool:
|
2021-11-20 11:36:18 +01:00
|
|
|
return bool(message.pinned_message)
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
PINNED_MESSAGE = _PinnedMessage(name="filters.StatusUpdate.PINNED_MESSAGE")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.pinned_message`."""
|
Bot API 4.0 (#1168)
Telegram Passport (#1174):
- Add full support for telegram passport.
- New types: PassportData, PassportFile, EncryptedPassportElement, EncryptedCredentials, PassportElementError, PassportElementErrorDataField, PassportElementErrorFrontSide, PassportElementErrorReverseSide, PassportElementErrorSelfie, PassportElementErrorFile and PassportElementErrorFiles.
- New bot method: set_passport_data_errors
- New filter: Filters.passport_data
- Field passport_data field on Message
- PassportData is automagically decrypted when you specify your private key when creating Updater or Bot.
- PassportFiles is also automagically decrypted as you download/retrieve them.
- See new passportbot.py example for details on how to use, or go to our telegram passport wiki page for more info
- NOTE: Passport decryption requires new dependency `cryptography`.
Inputfile rework (#1184):
- Change how Inputfile is handled internally
- This allows support for specifying the thumbnails of photos and videos using the thumb= argument in the different send_ methods.
- Also allows Bot.send_media_group to actually finally send more than one media.
- Add thumb to Audio, Video and Videonote
- Add Bot.edit_message_media together with InputMediaAnimation, InputMediaAudio, and inputMediaDocument.
Other Bot API 4.0 changes:
- Add forusquare_type to Venue, InlineQueryResultVenue, InputVenueMessageContent, and Bot.send_venue. (#1170)
- Add vCard support by adding vcard field to Contact, InlineQueryResultContact, InputContactMessageContent, and Bot.send_contact. (#1166)
- Support new message entities: CASHTAG and PHONE_NUMBER. (#1179)
- Cashtag seems to be things like $USD and $GBP, but it seems telegram doesn't currently send them to bots.
- Phone number also seems to have limited support for now
- Add Bot.send_animation, add width, height, and duration to Animation, and add Filters.animation. (#1172)
Co-authored-by: Jasmin Bom <jsmnbom@gmail.com>
Co-authored-by: code1mountain <32801117+code1mountain@users.noreply.github.com>
Co-authored-by: Eldinnie <pieter.schutz+github@gmail.com>
Co-authored-by: mathefreak1 <mathefreak@hi2.in>
2018-08-29 14:18:58 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _ProximityAlertTriggered(MessageFilter):
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
2020-01-26 21:57:48 +01:00
|
|
|
|
2020-10-06 19:28:40 +02:00
|
|
|
def filter(self, message: Message) -> bool:
|
2021-11-20 11:36:18 +01:00
|
|
|
return bool(message.proximity_alert_triggered)
|
2020-01-26 21:57:48 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
PROXIMITY_ALERT_TRIGGERED = _ProximityAlertTriggered(
|
|
|
|
"filters.StatusUpdate.PROXIMITY_ALERT_TRIGGERED"
|
|
|
|
)
|
|
|
|
"""Messages that contain :attr:`telegram.Message.proximity_alert_triggered`."""
|
2020-01-26 21:57:48 +01:00
|
|
|
|
2023-02-08 11:47:21 +01:00
|
|
|
class _UserShared(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
2024-02-25 10:34:47 +01:00
|
|
|
return bool(message.api_kwargs.get("user_shared"))
|
2023-02-08 11:47:21 +01:00
|
|
|
|
|
|
|
USER_SHARED = _UserShared(name="filters.StatusUpdate.USER_SHARED")
|
2024-02-25 10:34:47 +01:00
|
|
|
"""Messages that contain ``"user_shared"`` in :attr:`telegram.TelegramObject.api_kwargs`.
|
2023-02-08 11:47:21 +01:00
|
|
|
|
2024-02-08 17:12:00 +01:00
|
|
|
Warning:
|
2024-02-25 10:34:47 +01:00
|
|
|
This will only catch the legacy ``user_shared`` field, not the
|
2024-02-08 17:12:00 +01:00
|
|
|
new :attr:`telegram.Message.users_shared` attribute!
|
|
|
|
|
2024-03-06 21:09:47 +01:00
|
|
|
.. versionchanged:: 21.0
|
2024-02-25 10:34:47 +01:00
|
|
|
Now relies on :attr:`telegram.TelegramObject.api_kwargs` as the native attribute
|
|
|
|
``Message.user_shared`` was removed.
|
|
|
|
|
2023-02-08 11:47:21 +01:00
|
|
|
.. versionadded:: 20.1
|
2024-02-08 18:36:28 +01:00
|
|
|
.. deprecated:: 20.8
|
2024-02-08 17:12:00 +01:00
|
|
|
Use :attr:`USERS_SHARED` instead.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class _UsersShared(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.users_shared)
|
|
|
|
|
|
|
|
USERS_SHARED = _UsersShared(name="filters.StatusUpdate.USERS_SHARED")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.users_shared`.
|
|
|
|
|
2024-02-08 18:36:28 +01:00
|
|
|
.. versionadded:: 20.8
|
2023-02-08 11:47:21 +01:00
|
|
|
"""
|
|
|
|
|
2022-05-03 18:21:50 +02:00
|
|
|
class _VideoChatEnded(MessageFilter):
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
2022-05-03 18:21:50 +02:00
|
|
|
return bool(message.video_chat_ended)
|
2021-11-20 11:36:18 +01:00
|
|
|
|
2022-05-03 18:21:50 +02:00
|
|
|
VIDEO_CHAT_ENDED = _VideoChatEnded(name="filters.StatusUpdate.VIDEO_CHAT_ENDED")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.video_chat_ended`.
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
.. versionadded:: 13.4
|
2022-05-03 18:21:50 +02:00
|
|
|
.. versionchanged:: 20.0
|
|
|
|
This filter was formerly named ``VOICE_CHAT_ENDED``
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
|
2022-05-03 18:21:50 +02:00
|
|
|
class _VideoChatScheduled(MessageFilter):
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
2022-05-03 18:21:50 +02:00
|
|
|
return bool(message.video_chat_scheduled)
|
2021-11-20 11:36:18 +01:00
|
|
|
|
2022-05-03 18:21:50 +02:00
|
|
|
VIDEO_CHAT_SCHEDULED = _VideoChatScheduled(name="filters.StatusUpdate.VIDEO_CHAT_SCHEDULED")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.video_chat_scheduled`.
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
.. versionadded:: 13.5
|
2022-05-03 18:21:50 +02:00
|
|
|
.. versionchanged:: 20.0
|
|
|
|
This filter was formerly named ``VOICE_CHAT_SCHEDULED``
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
|
2022-05-03 18:21:50 +02:00
|
|
|
class _VideoChatStarted(MessageFilter):
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
2022-05-03 18:21:50 +02:00
|
|
|
return bool(message.video_chat_started)
|
2021-11-20 11:36:18 +01:00
|
|
|
|
2022-05-03 18:21:50 +02:00
|
|
|
VIDEO_CHAT_STARTED = _VideoChatStarted(name="filters.StatusUpdate.VIDEO_CHAT_STARTED")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.video_chat_started`.
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
.. versionadded:: 13.4
|
2022-05-03 18:21:50 +02:00
|
|
|
.. versionchanged:: 20.0
|
|
|
|
This filter was formerly named ``VOICE_CHAT_STARTED``
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
|
2022-05-03 18:21:50 +02:00
|
|
|
class _VideoChatParticipantsInvited(MessageFilter):
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
2022-05-03 18:21:50 +02:00
|
|
|
return bool(message.video_chat_participants_invited)
|
2021-11-20 11:36:18 +01:00
|
|
|
|
2022-05-03 18:21:50 +02:00
|
|
|
VIDEO_CHAT_PARTICIPANTS_INVITED = _VideoChatParticipantsInvited(
|
|
|
|
"filters.StatusUpdate.VIDEO_CHAT_PARTICIPANTS_INVITED"
|
2021-11-20 11:36:18 +01:00
|
|
|
)
|
2022-05-03 18:21:50 +02:00
|
|
|
"""Messages that contain :attr:`telegram.Message.video_chat_participants_invited`.
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
.. versionadded:: 13.4
|
2022-05-03 18:21:50 +02:00
|
|
|
.. versionchanged:: 20.0
|
|
|
|
This filter was formerly named ``VOICE_CHAT_PARTICIPANTS_INVITED``
|
|
|
|
"""
|
|
|
|
|
|
|
|
class _WebAppData(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.web_app_data)
|
|
|
|
|
|
|
|
WEB_APP_DATA = _WebAppData(name="filters.StatusUpdate.WEB_APP_DATA")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.web_app_data`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
2023-01-01 17:00:49 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
class _WriteAccessAllowed(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.write_access_allowed)
|
|
|
|
|
|
|
|
WRITE_ACCESS_ALLOWED = _WriteAccessAllowed(name="filters.StatusUpdate.WRITE_ACCESS_ALLOWED")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.write_access_allowed`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2022-04-24 13:47:35 +02:00
|
|
|
class Sticker:
|
|
|
|
"""Filters messages which contain a sticker.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
Use this filter like: ``filters.Sticker.VIDEO``. Or, just use ``filters.Sticker.ALL`` for
|
|
|
|
any type of sticker.
|
|
|
|
|
|
|
|
Caution:
|
|
|
|
``filters.Sticker`` itself is *not* a filter, but just a convenience namespace.
|
|
|
|
"""
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
|
|
|
|
2022-04-24 13:47:35 +02:00
|
|
|
class _All(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.sticker)
|
|
|
|
|
|
|
|
ALL = _All(name="filters.Sticker.ALL")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.sticker`."""
|
|
|
|
|
|
|
|
class _Animated(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.sticker) and bool(message.sticker.is_animated) # type: ignore
|
|
|
|
|
|
|
|
ANIMATED = _Animated(name="filters.Sticker.ANIMATED")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.sticker` and
|
|
|
|
:attr:`is animated <telegram.Sticker.is_animated>`.
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionadded:: 20.0
|
2022-04-24 13:47:35 +02:00
|
|
|
"""
|
2021-11-20 11:36:18 +01:00
|
|
|
|
2022-04-24 13:47:35 +02:00
|
|
|
class _Static(MessageFilter):
|
|
|
|
__slots__ = ()
|
2021-11-20 11:36:18 +01:00
|
|
|
|
2022-04-24 13:47:35 +02:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.sticker) and (
|
|
|
|
not bool(message.sticker.is_animated) # type: ignore[union-attr]
|
|
|
|
and not bool(message.sticker.is_video) # type: ignore[union-attr]
|
|
|
|
)
|
|
|
|
|
|
|
|
STATIC = _Static(name="filters.Sticker.STATIC")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.sticker` and is a static sticker, i.e. does
|
|
|
|
not contain :attr:`telegram.Sticker.is_animated` or :attr:`telegram.Sticker.is_video`.
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionadded:: 20.0
|
2022-04-24 13:47:35 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
class _Video(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.sticker) and bool(message.sticker.is_video) # type: ignore
|
|
|
|
|
|
|
|
VIDEO = _Video(name="filters.Sticker.VIDEO")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.sticker` and is a
|
|
|
|
:attr:`video sticker <telegram.Sticker.is_video>`.
|
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionadded:: 20.0
|
2022-04-24 13:47:35 +02:00
|
|
|
"""
|
2021-11-20 11:36:18 +01:00
|
|
|
|
2022-06-27 18:54:11 +02:00
|
|
|
class _Premium(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.sticker) and bool(
|
|
|
|
message.sticker.premium_animation # type: ignore
|
|
|
|
)
|
|
|
|
|
|
|
|
PREMIUM = _Premium(name="filters.Sticker.PREMIUM")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.sticker` and have a
|
|
|
|
:attr:`premium animation <telegram.Sticker.premium_animation>`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
"""
|
2022-08-25 19:36:55 +02:00
|
|
|
# neither mask nor emoji can be a message.sticker, so no filters for them
|
2022-06-27 18:54:11 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
|
2023-09-03 13:43:44 +02:00
|
|
|
class _Story(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.story)
|
|
|
|
|
|
|
|
|
|
|
|
STORY = _Story(name="filters.STORY")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.story`.
|
|
|
|
|
2023-09-03 14:46:28 +02:00
|
|
|
.. versionadded:: 20.5
|
2023-09-03 13:43:44 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
|
2024-01-02 18:35:38 +01:00
|
|
|
class SuccessfulPayment(MessageFilter):
|
|
|
|
"""Successful Payment Messages. If a list of invoice payloads is passed, it filters
|
|
|
|
messages to only allow those whose `invoice_payload` is appearing in the given list.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
`MessageHandler(filters.SuccessfulPayment(['Custom-Payload']), callback_method)`
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
:attr:`telegram.ext.filters.SUCCESSFUL_PAYMENT`
|
|
|
|
|
|
|
|
Args:
|
|
|
|
invoice_payloads (List[:obj:`str`] | Tuple[:obj:`str`], optional): Which
|
|
|
|
invoice payloads to allow. Only exact matches are allowed. If not
|
|
|
|
specified, will allow any invoice payload.
|
|
|
|
|
2024-02-08 18:36:28 +01:00
|
|
|
.. versionadded:: 20.8
|
2024-01-02 18:35:38 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
__slots__ = ("invoice_payloads",)
|
|
|
|
|
|
|
|
def __init__(self, invoice_payloads: Optional[Union[List[str], Tuple[str, ...]]] = None):
|
|
|
|
self.invoice_payloads: Optional[Sequence[str]] = invoice_payloads
|
|
|
|
super().__init__(
|
2024-02-05 19:24:00 +01:00
|
|
|
name=(
|
|
|
|
f"filters.SuccessfulPayment({invoice_payloads})"
|
|
|
|
if invoice_payloads
|
|
|
|
else "filters.SUCCESSFUL_PAYMENT"
|
|
|
|
)
|
2024-01-02 18:35:38 +01:00
|
|
|
)
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
2024-01-02 18:35:38 +01:00
|
|
|
if self.invoice_payloads is None:
|
|
|
|
return bool(message.successful_payment)
|
|
|
|
return (
|
|
|
|
payment.invoice_payload in self.invoice_payloads
|
|
|
|
if (payment := message.successful_payment)
|
|
|
|
else False
|
|
|
|
)
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
|
2024-01-02 18:35:38 +01:00
|
|
|
SUCCESSFUL_PAYMENT = SuccessfulPayment()
|
2021-11-20 11:36:18 +01:00
|
|
|
"""Messages that contain :attr:`telegram.Message.successful_payment`."""
|
|
|
|
|
|
|
|
|
|
|
|
class Text(MessageFilter):
|
|
|
|
"""Text Messages. If a list of strings is passed, it filters messages to only allow those
|
|
|
|
whose text is appearing in the given list.
|
2020-04-10 19:22:45 +02:00
|
|
|
|
|
|
|
Examples:
|
2021-11-20 11:36:18 +01:00
|
|
|
A simple use case for passing a list is to allow only messages that were sent by a
|
|
|
|
custom :class:`telegram.ReplyKeyboardMarkup`::
|
2021-06-06 12:16:23 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
buttons = ['Start', 'Settings', 'Back']
|
|
|
|
markup = ReplyKeyboardMarkup.from_column(buttons)
|
|
|
|
...
|
|
|
|
MessageHandler(filters.Text(buttons), callback_method)
|
2021-06-06 12:16:23 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
.. seealso::
|
|
|
|
:attr:`telegram.ext.filters.TEXT`
|
2021-06-06 12:16:23 +02:00
|
|
|
|
2020-04-10 19:22:45 +02:00
|
|
|
|
2020-04-18 12:16:14 +02:00
|
|
|
Note:
|
2021-11-20 11:36:18 +01:00
|
|
|
* Dice messages don't have text. If you want to filter either text or dice messages, use
|
|
|
|
``filters.TEXT | filters.Dice.ALL``.
|
|
|
|
* Messages containing a command are accepted by this filter. Use
|
|
|
|
``filters.TEXT & (~filters.COMMAND)``, if you want to filter only text messages without
|
|
|
|
commands.
|
2020-05-02 11:56:52 +02:00
|
|
|
|
2020-12-30 15:59:50 +01:00
|
|
|
Args:
|
2021-11-20 11:36:18 +01:00
|
|
|
strings (List[:obj:`str`] | Tuple[:obj:`str`], optional): Which messages to allow. Only
|
|
|
|
exact matches are allowed. If not specified, will allow any text message.
|
|
|
|
"""
|
2020-12-30 15:59:50 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ("strings",)
|
|
|
|
|
2023-05-18 07:57:59 +02:00
|
|
|
def __init__(self, strings: Optional[Union[List[str], Tuple[str, ...]]] = None):
|
2023-02-02 18:55:07 +01:00
|
|
|
self.strings: Optional[Sequence[str]] = strings
|
2021-11-20 11:36:18 +01:00
|
|
|
super().__init__(name=f"filters.Text({strings})" if strings else "filters.TEXT")
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
if self.strings is None:
|
|
|
|
return bool(message.text)
|
|
|
|
return message.text in self.strings if message.text else False
|
2021-03-14 16:41:35 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
TEXT = Text()
|
|
|
|
"""
|
|
|
|
Shortcut for :class:`telegram.ext.filters.Text()`.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
To allow any text message, simply use ``MessageHandler(filters.TEXT, callback_method)``.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class UpdateType:
|
2020-04-10 19:22:45 +02:00
|
|
|
"""
|
2021-11-20 11:36:18 +01:00
|
|
|
Subset for filtering the type of update.
|
2020-04-10 19:22:45 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Examples:
|
|
|
|
Use these filters like: ``filters.UpdateType.MESSAGE`` or
|
|
|
|
``filters.UpdateType.CHANNEL_POSTS`` etc.
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2022-04-24 13:47:35 +02:00
|
|
|
Caution:
|
2021-11-20 11:36:18 +01:00
|
|
|
``filters.UpdateType`` itself is *not* a filter, but just a convenience namespace.
|
|
|
|
"""
|
2017-05-21 14:00:53 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
2017-07-23 22:33:08 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _ChannelPost(UpdateFilter):
|
|
|
|
__slots__ = ()
|
2017-09-01 08:43:08 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, update: Update) -> bool:
|
|
|
|
return update.channel_post is not None
|
2017-05-21 14:00:53 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
CHANNEL_POST = _ChannelPost(name="filters.UpdateType.CHANNEL_POST")
|
|
|
|
"""Updates with :attr:`telegram.Update.channel_post`."""
|
2021-05-29 16:18:16 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _ChannelPosts(UpdateFilter):
|
|
|
|
__slots__ = ()
|
2017-05-21 14:00:53 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, update: Update) -> bool:
|
|
|
|
return update.channel_post is not None or update.edited_channel_post is not None
|
|
|
|
|
|
|
|
CHANNEL_POSTS = _ChannelPosts(name="filters.UpdateType.CHANNEL_POSTS")
|
|
|
|
"""Updates with either :attr:`telegram.Update.channel_post` or
|
|
|
|
:attr:`telegram.Update.edited_channel_post`."""
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Edited(UpdateFilter):
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, update: Update) -> bool:
|
2024-04-12 11:58:25 +02:00
|
|
|
return (
|
|
|
|
update.edited_message is not None
|
|
|
|
or update.edited_channel_post is not None
|
|
|
|
or update.edited_business_message is not None
|
|
|
|
)
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
EDITED = _Edited(name="filters.UpdateType.EDITED")
|
2024-04-12 11:58:25 +02:00
|
|
|
"""Updates with :attr:`telegram.Update.edited_message`,
|
|
|
|
:attr:`telegram.Update.edited_channel_post`, or
|
|
|
|
:attr:`telegram.Update.edited_business_message`.
|
2022-04-24 12:38:09 +02:00
|
|
|
|
2022-05-06 17:15:23 +02:00
|
|
|
.. versionadded:: 20.0
|
2024-04-12 11:58:25 +02:00
|
|
|
|
2024-04-12 12:39:38 +02:00
|
|
|
.. versionchanged:: 21.1
|
2024-04-12 11:58:25 +02:00
|
|
|
Added :attr:`telegram.Update.edited_business_message` to the filter.
|
2022-04-24 12:38:09 +02:00
|
|
|
"""
|
2021-05-19 10:39:10 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _EditedChannelPost(UpdateFilter):
|
|
|
|
__slots__ = ()
|
2021-05-19 10:39:10 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, update: Update) -> bool:
|
|
|
|
return update.edited_channel_post is not None
|
|
|
|
|
|
|
|
EDITED_CHANNEL_POST = _EditedChannelPost(name="filters.UpdateType.EDITED_CHANNEL_POST")
|
|
|
|
"""Updates with :attr:`telegram.Update.edited_channel_post`."""
|
2021-05-19 10:39:10 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _EditedMessage(UpdateFilter):
|
|
|
|
__slots__ = ()
|
2021-05-19 10:39:10 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, update: Update) -> bool:
|
|
|
|
return update.edited_message is not None
|
2021-05-19 10:39:10 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
EDITED_MESSAGE = _EditedMessage(name="filters.UpdateType.EDITED_MESSAGE")
|
|
|
|
"""Updates with :attr:`telegram.Update.edited_message`."""
|
|
|
|
|
|
|
|
class _Message(UpdateFilter):
|
2021-05-29 16:18:16 +02:00
|
|
|
__slots__ = ()
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, update: Update) -> bool:
|
|
|
|
return update.message is not None
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
MESSAGE = _Message(name="filters.UpdateType.MESSAGE")
|
|
|
|
"""Updates with :attr:`telegram.Update.message`."""
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Messages(UpdateFilter):
|
|
|
|
__slots__ = ()
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, update: Update) -> bool:
|
|
|
|
return update.message is not None or update.edited_message is not None
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
MESSAGES = _Messages(name="filters.UpdateType.MESSAGES")
|
|
|
|
"""Updates with either :attr:`telegram.Update.message` or
|
2024-04-12 11:58:25 +02:00
|
|
|
:attr:`telegram.Update.edited_message`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
class _BusinessMessage(UpdateFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, update: Update) -> bool:
|
|
|
|
return update.business_message is not None
|
|
|
|
|
|
|
|
BUSINESS_MESSAGE = _BusinessMessage(name="filters.UpdateType.BUSINESS_MESSAGE")
|
|
|
|
"""Updates with :attr:`telegram.Update.business_message`.
|
|
|
|
|
2024-04-12 12:39:38 +02:00
|
|
|
.. versionadded:: 21.1"""
|
2024-04-12 11:58:25 +02:00
|
|
|
|
|
|
|
class _EditedBusinessMessage(UpdateFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, update: Update) -> bool:
|
|
|
|
return update.edited_business_message is not None
|
|
|
|
|
|
|
|
EDITED_BUSINESS_MESSAGE = _EditedBusinessMessage(
|
|
|
|
name="filters.UpdateType.EDITED_BUSINESS_MESSAGE"
|
|
|
|
)
|
|
|
|
"""Updates with :attr:`telegram.Update.edited_business_message`.
|
|
|
|
|
2024-04-12 12:39:38 +02:00
|
|
|
.. versionadded:: 21.1
|
2024-04-12 11:58:25 +02:00
|
|
|
"""
|
|
|
|
|
|
|
|
class _BusinessMessages(UpdateFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, update: Update) -> bool:
|
|
|
|
return (
|
|
|
|
update.business_message is not None or update.edited_business_message is not None
|
|
|
|
)
|
|
|
|
|
|
|
|
BUSINESS_MESSAGES = _BusinessMessages(name="filters.UpdateType.BUSINESS_MESSAGES")
|
|
|
|
"""Updates with either :attr:`telegram.Update.business_message` or
|
|
|
|
:attr:`telegram.Update.edited_business_message`.
|
|
|
|
|
2024-04-12 12:39:38 +02:00
|
|
|
.. versionadded:: 21.1
|
2024-04-12 11:58:25 +02:00
|
|
|
"""
|
2019-02-13 12:07:25 +01:00
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class User(_ChatUserBaseFilter):
|
|
|
|
"""Filters messages to allow only those which are from specified user ID(s) or
|
|
|
|
username(s).
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Examples:
|
|
|
|
``MessageHandler(filters.User(1234), callback_method)``
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
user_id(:obj:`int` | Collection[:obj:`int`], optional): Which user ID(s) to
|
2021-11-20 11:36:18 +01:00
|
|
|
allow through.
|
2022-05-13 16:41:34 +02:00
|
|
|
username(:obj:`str` | Collection[:obj:`str`], optional):
|
2021-11-20 11:36:18 +01:00
|
|
|
Which username(s) to allow through. Leading ``'@'`` s in usernames will be discarded.
|
|
|
|
allow_empty(:obj:`bool`, optional): Whether updates should be processed, if no user is
|
|
|
|
specified in :attr:`user_ids` and :attr:`usernames`. Defaults to :obj:`False`.
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Raises:
|
|
|
|
RuntimeError: If ``user_id`` and ``username`` are both present.
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Attributes:
|
|
|
|
allow_empty (:obj:`bool`): Whether updates should be processed, if no user is specified in
|
|
|
|
:attr:`user_ids` and :attr:`usernames`.
|
|
|
|
"""
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
__slots__ = ()
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
2023-05-18 07:57:59 +02:00
|
|
|
user_id: Optional[SCT[int]] = None,
|
|
|
|
username: Optional[SCT[str]] = None,
|
2021-11-20 11:36:18 +01:00
|
|
|
allow_empty: bool = False,
|
|
|
|
):
|
|
|
|
super().__init__(chat_id=user_id, username=username, allow_empty=allow_empty)
|
|
|
|
self._chat_id_name = "user_id"
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2023-02-02 18:55:07 +01:00
|
|
|
def _get_chat_or_user(self, message: Message) -> Optional[TGUser]:
|
2021-11-20 11:36:18 +01:00
|
|
|
return message.from_user
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
@property
|
|
|
|
def user_ids(self) -> FrozenSet[int]:
|
|
|
|
"""
|
|
|
|
Which user ID(s) to allow through.
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Warning:
|
|
|
|
:attr:`user_ids` will give a *copy* of the saved user ids as :obj:`frozenset`. This
|
|
|
|
is to ensure thread safety. To add/remove a user, you should use :meth:`add_user_ids`,
|
|
|
|
and :meth:`remove_user_ids`. Only update the entire set by
|
|
|
|
``filter.user_ids = new_set``, if you are entirely sure that it is not causing race
|
|
|
|
conditions, as this will complete replace the current set of allowed users.
|
2021-10-07 16:13:53 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Returns:
|
|
|
|
frozenset(:obj:`int`)
|
|
|
|
"""
|
|
|
|
return self.chat_ids
|
2021-10-07 16:13:53 +02:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
@user_ids.setter
|
2022-05-13 16:41:34 +02:00
|
|
|
def user_ids(self, user_id: SCT[int]) -> None:
|
2021-11-20 11:36:18 +01:00
|
|
|
self.chat_ids = user_id # type: ignore[assignment]
|
2021-10-07 16:13:53 +02:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def add_user_ids(self, user_id: SCT[int]) -> None:
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
Add one or more users to the allowed user ids.
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
user_id(:obj:`int` | Collection[:obj:`int`]): Which user ID(s) to allow
|
2021-11-20 11:36:18 +01:00
|
|
|
through.
|
|
|
|
"""
|
|
|
|
return super()._add_chat_ids(user_id)
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def remove_user_ids(self, user_id: SCT[int]) -> None:
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
Remove one or more users from allowed user ids.
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
user_id(:obj:`int` | Collection[:obj:`int`]): Which user ID(s) to
|
2021-11-20 11:36:18 +01:00
|
|
|
disallow through.
|
|
|
|
"""
|
|
|
|
return super()._remove_chat_ids(user_id)
|
|
|
|
|
|
|
|
|
|
|
|
class _User(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.from_user)
|
|
|
|
|
|
|
|
|
|
|
|
USER = _User(name="filters.USER")
|
|
|
|
"""This filter filters *any* message that has a :attr:`telegram.Message.from_user`."""
|
|
|
|
|
|
|
|
|
2022-06-27 18:54:11 +02:00
|
|
|
class _UserAttachment(UpdateFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, update: Update) -> bool:
|
|
|
|
return bool(update.effective_user) and bool(
|
|
|
|
update.effective_user.added_to_attachment_menu # type: ignore
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
USER_ATTACHMENT = _UserAttachment(name="filters.USER_ATTACHMENT")
|
|
|
|
"""This filter filters *any* message that have a user who added the bot to their
|
|
|
|
:attr:`attachment menu <telegram.User.added_to_attachment_menu>` as
|
|
|
|
:attr:`telegram.Update.effective_user`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class _UserPremium(UpdateFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, update: Update) -> bool:
|
|
|
|
return bool(update.effective_user) and bool(
|
|
|
|
update.effective_user.is_premium # type: ignore
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
PREMIUM_USER = _UserPremium(name="filters.PREMIUM_USER")
|
|
|
|
"""This filter filters *any* message from a
|
|
|
|
:attr:`Telegram Premium user <telegram.User.is_premium>` as :attr:`telegram.Update.effective_user`.
|
|
|
|
|
|
|
|
.. versionadded:: 20.0
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
class _Venue(MessageFilter):
|
|
|
|
__slots__ = ()
|
2019-02-13 12:07:25 +01:00
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.venue)
|
|
|
|
|
|
|
|
|
|
|
|
VENUE = _Venue(name="filters.VENUE")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.venue`."""
|
|
|
|
|
|
|
|
|
|
|
|
class ViaBot(_ChatUserBaseFilter):
|
|
|
|
"""Filters messages to allow only those which are from specified via_bot ID(s) or username(s).
|
2019-02-13 12:07:25 +01:00
|
|
|
|
|
|
|
Examples:
|
2021-11-20 11:36:18 +01:00
|
|
|
``MessageHandler(filters.ViaBot(1234), callback_method)``
|
|
|
|
|
2024-04-12 11:58:25 +02:00
|
|
|
.. seealso:: :attr:`~telegram.ext.filters.VIA_BOT`
|
|
|
|
|
2021-11-20 11:36:18 +01:00
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
bot_id(:obj:`int` | Collection[:obj:`int`], optional): Which bot ID(s) to
|
2021-11-20 11:36:18 +01:00
|
|
|
allow through.
|
2022-05-13 16:41:34 +02:00
|
|
|
username(:obj:`str` | Collection[:obj:`str`], optional):
|
2021-11-20 11:36:18 +01:00
|
|
|
Which username(s) to allow through. Leading ``'@'`` s in usernames will be
|
|
|
|
discarded.
|
|
|
|
allow_empty(:obj:`bool`, optional): Whether updates should be processed, if no user
|
|
|
|
is specified in :attr:`bot_ids` and :attr:`usernames`. Defaults to :obj:`False`.
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
RuntimeError: If ``bot_id`` and ``username`` are both present.
|
2019-02-13 12:07:25 +01:00
|
|
|
|
|
|
|
Attributes:
|
2021-11-20 11:36:18 +01:00
|
|
|
allow_empty (:obj:`bool`): Whether updates should be processed, if no bot is specified in
|
|
|
|
:attr:`bot_ids` and :attr:`usernames`.
|
2019-02-13 12:07:25 +01:00
|
|
|
"""
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2023-05-18 07:57:59 +02:00
|
|
|
bot_id: Optional[SCT[int]] = None,
|
|
|
|
username: Optional[SCT[str]] = None,
|
2021-11-20 11:36:18 +01:00
|
|
|
allow_empty: bool = False,
|
|
|
|
):
|
|
|
|
super().__init__(chat_id=bot_id, username=username, allow_empty=allow_empty)
|
|
|
|
self._chat_id_name = "bot_id"
|
|
|
|
|
2023-02-02 18:55:07 +01:00
|
|
|
def _get_chat_or_user(self, message: Message) -> Optional[TGUser]:
|
2021-11-20 11:36:18 +01:00
|
|
|
return message.via_bot
|
|
|
|
|
|
|
|
@property
|
|
|
|
def bot_ids(self) -> FrozenSet[int]:
|
|
|
|
"""
|
|
|
|
Which bot ID(s) to allow through.
|
|
|
|
|
|
|
|
Warning:
|
|
|
|
:attr:`bot_ids` will give a *copy* of the saved bot ids as :obj:`frozenset`. This
|
|
|
|
is to ensure thread safety. To add/remove a bot, you should use :meth:`add_bot_ids`,
|
|
|
|
and :meth:`remove_bot_ids`. Only update the entire set by ``filter.bot_ids = new_set``,
|
|
|
|
if you are entirely sure that it is not causing race conditions, as this will complete
|
|
|
|
replace the current set of allowed bots.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
frozenset(:obj:`int`)
|
|
|
|
"""
|
|
|
|
return self.chat_ids
|
|
|
|
|
|
|
|
@bot_ids.setter
|
2022-05-13 16:41:34 +02:00
|
|
|
def bot_ids(self, bot_id: SCT[int]) -> None:
|
2021-11-20 11:36:18 +01:00
|
|
|
self.chat_ids = bot_id # type: ignore[assignment]
|
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def add_bot_ids(self, bot_id: SCT[int]) -> None:
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
Add one or more bots to the allowed bot ids.
|
|
|
|
|
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
bot_id(:obj:`int` | Collection[:obj:`int`]): Which bot ID(s) to allow
|
2021-11-20 11:36:18 +01:00
|
|
|
through.
|
|
|
|
"""
|
|
|
|
return super()._add_chat_ids(bot_id)
|
|
|
|
|
2022-05-13 16:41:34 +02:00
|
|
|
def remove_bot_ids(self, bot_id: SCT[int]) -> None:
|
2021-11-20 11:36:18 +01:00
|
|
|
"""
|
|
|
|
Remove one or more bots from allowed bot ids.
|
|
|
|
|
|
|
|
Args:
|
2022-05-13 16:41:34 +02:00
|
|
|
bot_id(:obj:`int` | Collection[:obj:`int`], optional): Which bot ID(s) to
|
2021-11-20 11:36:18 +01:00
|
|
|
disallow through.
|
|
|
|
"""
|
|
|
|
return super()._remove_chat_ids(bot_id)
|
|
|
|
|
|
|
|
|
|
|
|
class _ViaBot(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.via_bot)
|
|
|
|
|
|
|
|
|
|
|
|
VIA_BOT = _ViaBot(name="filters.VIA_BOT")
|
2024-04-12 11:58:25 +02:00
|
|
|
"""This filter filters for message that were sent via *any* bot.
|
|
|
|
|
|
|
|
.. seealso:: :class:`~telegram.ext.filters.ViaBot`"""
|
2021-11-20 11:36:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
class _Video(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.video)
|
|
|
|
|
|
|
|
|
|
|
|
VIDEO = _Video(name="filters.VIDEO")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.video`."""
|
|
|
|
|
|
|
|
|
|
|
|
class _VideoNote(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.video_note)
|
|
|
|
|
|
|
|
|
|
|
|
VIDEO_NOTE = _VideoNote(name="filters.VIDEO_NOTE")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.video_note`."""
|
|
|
|
|
|
|
|
|
|
|
|
class _Voice(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.voice)
|
|
|
|
|
|
|
|
|
|
|
|
VOICE = _Voice("filters.VOICE")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.voice`."""
|
2024-03-02 10:56:15 +01:00
|
|
|
|
|
|
|
|
|
|
|
class _ReplyToStory(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.reply_to_story)
|
|
|
|
|
|
|
|
|
|
|
|
REPLY_TO_STORY = _ReplyToStory(name="filters.REPLY_TO_STORY")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.reply_to_story`."""
|
|
|
|
|
|
|
|
|
|
|
|
class _BoostAdded(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.boost_added)
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_ADDED = _BoostAdded(name="filters.BOOST_ADDED")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.boost_added`."""
|
|
|
|
|
|
|
|
|
|
|
|
class _SenderBoostCount(MessageFilter):
|
|
|
|
__slots__ = ()
|
|
|
|
|
|
|
|
def filter(self, message: Message) -> bool:
|
|
|
|
return bool(message.sender_boost_count)
|
|
|
|
|
|
|
|
|
|
|
|
SENDER_BOOST_COUNT = _SenderBoostCount(name="filters.SENDER_BOOST_COUNT")
|
|
|
|
"""Messages that contain :attr:`telegram.Message.sender_boost_count`."""
|