From 768491d97604e9e25d53f432fe12c597121ee88e Mon Sep 17 00:00:00 2001 From: Egor Date: Mon, 22 Jun 2020 22:13:50 +0500 Subject: [PATCH] fix: rename argument to chat_type fix: rename example file name fix: str is container also lol. example fixed also --- aiogram/dispatcher/filters/builtin.py | 12 ++++++++---- .../{chat_types_filter.py => chat_type_filter.py} | 14 +++++++++++++- 2 files changed, 21 insertions(+), 5 deletions(-) rename examples/{chat_types_filter.py => chat_type_filter.py} (60%) diff --git a/aiogram/dispatcher/filters/builtin.py b/aiogram/dispatcher/filters/builtin.py index 5e462e31..25d4c029 100644 --- a/aiogram/dispatcher/filters/builtin.py +++ b/aiogram/dispatcher/filters/builtin.py @@ -1,6 +1,7 @@ import inspect import re import typing +from collections import Container from contextvars import ContextVar from dataclasses import dataclass, field from typing import Any, Dict, Iterable, Optional, Union @@ -694,14 +695,17 @@ class ForwardedMessageFilter(BoundFilter): class ChatTypeFilter(BoundFilter): - key = 'chat_types' + key = 'chat_type' - def __init__(self, chat_types: typing.List[ChatType]): - self.chat_types = chat_types + def __init__(self, chat_type: typing.Container[ChatType]): + if isinstance(chat_type, str): + chat_type = {chat_type} + + self.chat_type: typing.Set[str] = set(chat_type) async def check(self, obj: Union[Message, CallbackQuery]): if isinstance(obj, Message): obj = obj.chat if isinstance(obj, CallbackQuery): obj = obj.message.chat - return obj.type in self.chat_types + return obj.type in self.chat_type diff --git a/examples/chat_types_filter.py b/examples/chat_type_filter.py similarity index 60% rename from examples/chat_types_filter.py rename to examples/chat_type_filter.py index 0cf0580d..08bb1858 100644 --- a/examples/chat_types_filter.py +++ b/examples/chat_type_filter.py @@ -6,6 +6,7 @@ It filters incoming object based on type of its chat type import logging from aiogram import Bot, Dispatcher, executor, types +from aiogram.dispatcher.handler import SkipHandler from aiogram.types import ChatType API_TOKEN = 'BOT TOKEN HERE' @@ -18,13 +19,24 @@ bot = Bot(token=API_TOKEN) dp = Dispatcher(bot) -@dp.message_handler(chat_types=[ChatType.PRIVATE, ChatType.CHANNEL]) +@dp.message_handler(chat_type=[ChatType.PRIVATE, ChatType.CHANNEL]) async def send_welcome(message: types.Message): """ This handler will be called when user sends `/start` or `/help` command """ await message.reply("Hi!\nI'm hearing your messages in private chats and channels") + # propagate message to the next handler + raise SkipHandler + + +@dp.message_handler(chat_type=ChatType.PRIVATE) +async def send_welcome(message: types.Message): + """ + This handler will be called when user sends `/start` or `/help` command + """ + await message.reply("Hi!\nI'm hearing your messages only in private chats") + if __name__ == '__main__': executor.start_polling(dp, skip_updates=True)