fix: rename argument to chat_type

fix: rename example file name

fix: str is container also lol. example fixed also
This commit is contained in:
Egor 2020-06-22 22:13:50 +05:00
parent 006a5ef987
commit 768491d976
2 changed files with 21 additions and 5 deletions

View file

@ -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

View file

@ -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)