mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Adding MultiRegexp filter
Adding filter to return multiple matches, instead of only first found
This commit is contained in:
parent
e70a76ff63
commit
d22a68ad0e
1 changed files with 36 additions and 0 deletions
|
|
@ -463,6 +463,42 @@ class RegexpCommandsFilter(BoundFilter):
|
|||
return False
|
||||
|
||||
|
||||
class MultiRegexp(Filter):
|
||||
"""
|
||||
Regexp filter for messages and callback query returning multiple matches
|
||||
"""
|
||||
|
||||
def __init__(self, regexp):
|
||||
if not isinstance(regexp, typing.Pattern):
|
||||
regexp = re.compile(regexp, flags=re.IGNORECASE | re.MULTILINE)
|
||||
self.regexp = regexp
|
||||
|
||||
@classmethod
|
||||
def validate(cls, full_config: typing.Dict[str, typing.Any]):
|
||||
if 'regexp' in full_config:
|
||||
return {'regexp': full_config.pop('regexp')}
|
||||
|
||||
async def check(self, obj: typing.Union[Message, CallbackQuery, InlineQuery, Poll]):
|
||||
if isinstance(obj, Message):
|
||||
content = obj.text or obj.caption or ''
|
||||
if not content and obj.poll:
|
||||
content = obj.poll.question
|
||||
elif isinstance(obj, CallbackQuery) and obj.data:
|
||||
content = obj.data
|
||||
elif isinstance(obj, InlineQuery):
|
||||
content = obj.query
|
||||
elif isinstance(obj, Poll):
|
||||
content = obj.question
|
||||
else:
|
||||
return False
|
||||
|
||||
matches = self.regexp.findall(content)
|
||||
|
||||
if matches:
|
||||
return {'regexp': matches}
|
||||
return False
|
||||
|
||||
|
||||
class ContentTypeFilter(BoundFilter):
|
||||
"""
|
||||
Check message content type
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue