Merge pull request #210 from Arwichok/is_reply

add IsReplyFilter
This commit is contained in:
Alex Root Junior 2019-10-06 23:24:33 +03:00 committed by GitHub
commit 20e51ac84a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 1 deletions

View file

@ -10,7 +10,7 @@ from aiohttp.helpers import sentinel
from aiogram.utils.deprecated import renamed_argument
from .filters import Command, ContentTypeFilter, ExceptionsFilter, FiltersFactory, HashTag, Regexp, \
RegexpCommandsFilter, StateFilter, Text, IDFilter, AdminFilter
RegexpCommandsFilter, StateFilter, Text, IDFilter, AdminFilter, IsReplyFilter
from .handler import Handler
from .middlewares import MiddlewareManager
from .storage import BaseStorage, DELTA, DisabledStorage, EXCEEDED_COUNT, FSMContext, \
@ -145,6 +145,12 @@ class Dispatcher(DataMixin, ContextInstanceMixin):
self.callback_query_handlers,
self.inline_query_handlers,
])
filters_factory.bind(IsReplyFilter, event_handlers=[
self.message_handlers,
self.edited_message_handlers,
self.channel_post_handlers,
self.edited_channel_post_handlers,
])
def __del__(self):
self.stop_polling()

View file

@ -628,3 +628,19 @@ class AdminFilter(Filter):
admins = [member.user.id for chat_id in chat_ids for member in await obj.bot.get_chat_administrators(chat_id)]
return user_id in admins
class IsReplyFilter(BoundFilter):
"""
Check if message is replied and send reply message to handler
"""
key = 'is_reply'
def __init__(self, is_reply):
self.is_reply = is_reply
async def check(self, msg: Message):
if msg.reply_to_message and self.is_reply:
return {'reply': msg.reply_to_message}
elif not msg.reply_to_message and not self.is_reply:
return True

View file

@ -127,6 +127,14 @@ AdminFilter
:show-inheritance:
IsReplyFilter
-------------
.. autoclass:: aiogram.dispatcher.filters.filters.IsReplyFilter
:members:
:show-inheritance:
Making own filters (Custom filters)
===================================
@ -172,3 +180,4 @@ BoundFilter
dp.filters_factory.bind(ChatIdFilter, event_handlers=[dp.message_handlers])