mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
feat: add ChatType builtin filter (#356)
* feat: ChatTypesFilter * feat: add example of usage * feat: docs * fix: add import in filters/__init__ * fix: remove some of event_handlers * fix * fix imports * fix: rename to ChatTypeFilter * fix: rename argument to chat_type fix: rename example file name fix: str is container also lol. example fixed also * fix: respect type hints * fix: add warning with respect to type hint * fix: use warnings instead of logging
This commit is contained in:
parent
d179789ea7
commit
81b36bd192
6 changed files with 90 additions and 5 deletions
42
examples/chat_type_filter.py
Normal file
42
examples/chat_type_filter.py
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
"""
|
||||
This is an example with usage of ChatTypeFilter
|
||||
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'
|
||||
|
||||
# Configure logging
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
# Initialize bot and dispatcher
|
||||
bot = Bot(token=API_TOKEN)
|
||||
dp = Dispatcher(bot)
|
||||
|
||||
|
||||
@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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue