mirror of
https://github.com/python-telegram-bot/python-telegram-bot.git
synced 2025-01-07 19:12:26 +01:00
Use filter method instead of __call__
__call__ is scary looking for users wanted to create their own filters. Also allows us to put additional logic in __call__ if we want in the future.
This commit is contained in:
parent
71e74da0a2
commit
2161681131
1 changed files with 18 additions and 15 deletions
|
@ -23,7 +23,7 @@ class BaseFilter(object):
|
||||||
"""Base class for all Message Filters"""
|
"""Base class for all Message Filters"""
|
||||||
|
|
||||||
def __call__(self, message):
|
def __call__(self, message):
|
||||||
raise NotImplementedError('Please implement a call method in your filter.')
|
self.filter(message)
|
||||||
|
|
||||||
def __and__(self, other):
|
def __and__(self, other):
|
||||||
return MergedFilter(self, and_filter=other)
|
return MergedFilter(self, and_filter=other)
|
||||||
|
@ -31,6 +31,9 @@ class BaseFilter(object):
|
||||||
def __or__(self, other):
|
def __or__(self, other):
|
||||||
return MergedFilter(self, or_filter=other)
|
return MergedFilter(self, or_filter=other)
|
||||||
|
|
||||||
|
def filter(self, message):
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
class MergedFilter(BaseFilter):
|
class MergedFilter(BaseFilter):
|
||||||
"""Represents a filter consisting of two other filters."""
|
"""Represents a filter consisting of two other filters."""
|
||||||
|
@ -40,7 +43,7 @@ class MergedFilter(BaseFilter):
|
||||||
self.and_filter = and_filter
|
self.and_filter = and_filter
|
||||||
self.or_filter = or_filter
|
self.or_filter = or_filter
|
||||||
|
|
||||||
def __call__(self, message):
|
def filter(self, message):
|
||||||
if self.and_filter:
|
if self.and_filter:
|
||||||
return self.base_filter(message) and self.and_filter(message)
|
return self.base_filter(message) and self.and_filter(message)
|
||||||
elif self.or_filter:
|
elif self.or_filter:
|
||||||
|
@ -55,84 +58,84 @@ class Filters(object):
|
||||||
|
|
||||||
class Text(BaseFilter):
|
class Text(BaseFilter):
|
||||||
|
|
||||||
def __call__(self, message):
|
def filter(self, message):
|
||||||
return bool(message.text and not message.text.startswith('/'))
|
return bool(message.text and not message.text.startswith('/'))
|
||||||
|
|
||||||
text = Text()
|
text = Text()
|
||||||
|
|
||||||
class Command(BaseFilter):
|
class Command(BaseFilter):
|
||||||
|
|
||||||
def __call__(self, message):
|
def filter(self, message):
|
||||||
return bool(message.text and message.text.startswith('/'))
|
return bool(message.text and message.text.startswith('/'))
|
||||||
|
|
||||||
command = Command()
|
command = Command()
|
||||||
|
|
||||||
class Audio(BaseFilter):
|
class Audio(BaseFilter):
|
||||||
|
|
||||||
def __call__(self, message):
|
def filter(self, message):
|
||||||
return bool(message.audio)
|
return bool(message.audio)
|
||||||
|
|
||||||
audio = Audio()
|
audio = Audio()
|
||||||
|
|
||||||
class Document(BaseFilter):
|
class Document(BaseFilter):
|
||||||
|
|
||||||
def __call__(self, message):
|
def filter(self, message):
|
||||||
return bool(message.document)
|
return bool(message.document)
|
||||||
|
|
||||||
document = Document()
|
document = Document()
|
||||||
|
|
||||||
class Photo(BaseFilter):
|
class Photo(BaseFilter):
|
||||||
|
|
||||||
def __call__(self, message):
|
def filter(self, message):
|
||||||
return bool(message.photo)
|
return bool(message.photo)
|
||||||
|
|
||||||
photo = Photo()
|
photo = Photo()
|
||||||
|
|
||||||
class Sticker(BaseFilter):
|
class Sticker(BaseFilter):
|
||||||
|
|
||||||
def __call__(self, message):
|
def filter(self, message):
|
||||||
return bool(message.sticker)
|
return bool(message.sticker)
|
||||||
|
|
||||||
sticker = Sticker()
|
sticker = Sticker()
|
||||||
|
|
||||||
class Video(BaseFilter):
|
class Video(BaseFilter):
|
||||||
|
|
||||||
def __call__(self, message):
|
def filter(self, message):
|
||||||
return bool(message.video)
|
return bool(message.video)
|
||||||
|
|
||||||
video = Video()
|
video = Video()
|
||||||
|
|
||||||
class Voice(BaseFilter):
|
class Voice(BaseFilter):
|
||||||
|
|
||||||
def __call__(self, message):
|
def filter(self, message):
|
||||||
return bool(message.voice)
|
return bool(message.voice)
|
||||||
|
|
||||||
voice = Voice()
|
voice = Voice()
|
||||||
|
|
||||||
class Contact(BaseFilter):
|
class Contact(BaseFilter):
|
||||||
|
|
||||||
def __call__(self, message):
|
def filter(self, message):
|
||||||
return bool(message.contact)
|
return bool(message.contact)
|
||||||
|
|
||||||
contact = Contact()
|
contact = Contact()
|
||||||
|
|
||||||
class Location(BaseFilter):
|
class Location(BaseFilter):
|
||||||
|
|
||||||
def __call__(self, message):
|
def filter(self, message):
|
||||||
return bool(message.location)
|
return bool(message.location)
|
||||||
|
|
||||||
location = Location()
|
location = Location()
|
||||||
|
|
||||||
class Venue(BaseFilter):
|
class Venue(BaseFilter):
|
||||||
|
|
||||||
def __call__(self, message):
|
def filter(self, message):
|
||||||
return bool(message.venue)
|
return bool(message.venue)
|
||||||
|
|
||||||
venue = Venue()
|
venue = Venue()
|
||||||
|
|
||||||
class StatusUpdate(BaseFilter):
|
class StatusUpdate(BaseFilter):
|
||||||
|
|
||||||
def __call__(self, message):
|
def filter(self, message):
|
||||||
return bool(message.new_chat_member or message.left_chat_member
|
return bool(message.new_chat_member or message.left_chat_member
|
||||||
or message.new_chat_title or message.new_chat_photo
|
or message.new_chat_title or message.new_chat_photo
|
||||||
or message.delete_chat_photo or message.group_chat_created
|
or message.delete_chat_photo or message.group_chat_created
|
||||||
|
@ -144,7 +147,7 @@ class Filters(object):
|
||||||
|
|
||||||
class Forwarded(BaseFilter):
|
class Forwarded(BaseFilter):
|
||||||
|
|
||||||
def __call__(self, message):
|
def filter(self, message):
|
||||||
return bool(message.forward_date)
|
return bool(message.forward_date)
|
||||||
|
|
||||||
forwarded = Forwarded()
|
forwarded = Forwarded()
|
||||||
|
|
Loading…
Reference in a new issue