From b16963d4a9e3b54812e3c4db0c1d59b8c66b0a77 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Mon, 13 Aug 2018 23:53:37 +0300 Subject: [PATCH] Make single elements ContentType object and ContentTypes class for listed elements --- aiogram/dispatcher/filters/builtin.py | 6 +- aiogram/types/__init__.py | 3 +- aiogram/types/message.py | 117 ++++++++++++++++++++------ examples/payments.py | 4 +- examples/webhook_example.py | 4 +- 5 files changed, 99 insertions(+), 35 deletions(-) diff --git a/aiogram/dispatcher/filters/builtin.py b/aiogram/dispatcher/filters/builtin.py index 8930faf1..c2251b56 100644 --- a/aiogram/dispatcher/filters/builtin.py +++ b/aiogram/dispatcher/filters/builtin.py @@ -6,7 +6,7 @@ from typing import Any, Dict, Iterable, Optional, Union from aiogram import types from aiogram.dispatcher.filters.filters import BoundFilter, Filter -from aiogram.types import CallbackQuery, ContentType, Message +from aiogram.types import CallbackQuery, Message class Command(Filter): @@ -233,13 +233,13 @@ class ContentTypeFilter(BoundFilter): key = 'content_types' required = True - default = types.ContentType.TEXT + default = types.ContentTypes.TEXT def __init__(self, content_types): self.content_types = content_types async def check(self, message): - return ContentType.ANY[0] in self.content_types or \ + return types.ContentType.ANY in self.content_types or \ message.content_type in self.content_types diff --git a/aiogram/types/__init__.py b/aiogram/types/__init__.py index 943efb4b..a1e98158 100644 --- a/aiogram/types/__init__.py +++ b/aiogram/types/__init__.py @@ -34,7 +34,7 @@ from .invoice import Invoice from .labeled_price import LabeledPrice from .location import Location from .mask_position import MaskPosition -from .message import ContentType, Message, ParseMode +from .message import ContentType, ContentTypes, Message, ParseMode from .message_entity import MessageEntity, MessageEntityType from .order_info import OrderInfo from .passport_data import PassportData @@ -77,6 +77,7 @@ __all__ = ( 'ChosenInlineResult', 'Contact', 'ContentType', + 'ContentTypes', 'Document', 'EncryptedCredentials', 'EncryptedPassportElement', diff --git a/aiogram/types/message.py b/aiogram/types/message.py index b593fdb0..43e962db 100644 --- a/aiogram/types/message.py +++ b/aiogram/types/message.py @@ -84,59 +84,59 @@ class Message(base.TelegramObject): @functools.lru_cache() def content_type(self): if self.text: - return ContentType.TEXT[0] + return ContentType.TEXT elif self.audio: - return ContentType.AUDIO[0] + return ContentType.AUDIO elif self.animation: - return ContentType.ANIMATION[0] + return ContentType.ANIMATION elif self.document: - return ContentType.DOCUMENT[0] + return ContentType.DOCUMENT elif self.game: - return ContentType.GAME[0] + return ContentType.GAME elif self.photo: - return ContentType.PHOTO[0] + return ContentType.PHOTO elif self.sticker: - return ContentType.STICKER[0] + return ContentType.STICKER elif self.video: - return ContentType.VIDEO[0] + return ContentType.VIDEO elif self.video_note: - return ContentType.VIDEO_NOTE[0] + return ContentType.VIDEO_NOTE elif self.voice: - return ContentType.VOICE[0] + return ContentType.VOICE elif self.contact: - return ContentType.CONTACT[0] + return ContentType.CONTACT elif self.venue: - return ContentType.VENUE[0] + return ContentType.VENUE elif self.location: - return ContentType.LOCATION[0] + return ContentType.LOCATION elif self.new_chat_members: - return ContentType.NEW_CHAT_MEMBERS[0] + return ContentType.NEW_CHAT_MEMBERS elif self.left_chat_member: - return ContentType.LEFT_CHAT_MEMBER[0] + return ContentType.LEFT_CHAT_MEMBER elif self.invoice: - return ContentType.INVOICE[0] + return ContentType.INVOICE elif self.successful_payment: - return ContentType.SUCCESSFUL_PAYMENT[0] + return ContentType.SUCCESSFUL_PAYMENT elif self.connected_website: - return ContentType.CONNECTED_WEBSITE[0] + return ContentType.CONNECTED_WEBSITE elif self.migrate_from_chat_id: - return ContentType.MIGRATE_FROM_CHAT_ID[0] + return ContentType.MIGRATE_FROM_CHAT_ID elif self.migrate_to_chat_id: - return ContentType.MIGRATE_TO_CHAT_ID[0] + return ContentType.MIGRATE_TO_CHAT_ID elif self.pinned_message: - return ContentType.PINNED_MESSAGE[0] + return ContentType.PINNED_MESSAGE elif self.new_chat_title: - return ContentType.NEW_CHAT_TITLE[0] + return ContentType.NEW_CHAT_TITLE elif self.new_chat_photo: - return ContentType.NEW_CHAT_PHOTO[0] + return ContentType.NEW_CHAT_PHOTO elif self.delete_chat_photo: - return ContentType.DELETE_CHAT_PHOTO[0] + return ContentType.DELETE_CHAT_PHOTO elif self.group_chat_created: - return ContentType.GROUP_CHAT_CREATED[0] + return ContentType.GROUP_CHAT_CREATED elif self.passport_data: - return ContentType.PASSPORT_DATA[0] + return ContentType.PASSPORT_DATA else: - return ContentType.UNKNOWN[0] + return ContentType.UNKNOWN def is_command(self): """ @@ -731,6 +731,69 @@ class ContentType(helper.Helper): """ List of message content types + WARNING: Single elements + + :key: TEXT + :key: AUDIO + :key: DOCUMENT + :key: GAME + :key: PHOTO + :key: STICKER + :key: VIDEO + :key: VIDEO_NOTE + :key: VOICE + :key: CONTACT + :key: LOCATION + :key: VENUE + :key: NEW_CHAT_MEMBERS + :key: LEFT_CHAT_MEMBER + :key: INVOICE + :key: SUCCESSFUL_PAYMENT + :key: CONNECTED_WEBSITE + :key: MIGRATE_TO_CHAT_ID + :key: MIGRATE_FROM_CHAT_ID + :key: UNKNOWN + :key: ANY + """ + mode = helper.HelperMode.snake_case + + TEXT = helper.Item() # text + AUDIO = helper.Item() # audio + DOCUMENT = helper.Item() # document + ANIMATION = helper.Item() # animation + GAME = helper.Item() # game + PHOTO = helper.Item() # photo + STICKER = helper.Item() # sticker + VIDEO = helper.Item() # video + VIDEO_NOTE = helper.Item() # video_note + VOICE = helper.Item() # voice + CONTACT = helper.Item() # contact + LOCATION = helper.Item() # location + VENUE = helper.Item() # venue + NEW_CHAT_MEMBERS = helper.Item() # new_chat_member + LEFT_CHAT_MEMBER = helper.Item() # left_chat_member + INVOICE = helper.Item() # invoice + SUCCESSFUL_PAYMENT = helper.Item() # successful_payment + CONNECTED_WEBSITE = helper.Item() # connected_website + MIGRATE_TO_CHAT_ID = helper.Item() # migrate_to_chat_id + MIGRATE_FROM_CHAT_ID = helper.Item() # migrate_from_chat_id + PINNED_MESSAGE = helper.Item() # pinned_message + NEW_CHAT_TITLE = helper.Item() # new_chat_title + NEW_CHAT_PHOTO = helper.Item() # new_chat_photo + DELETE_CHAT_PHOTO = helper.Item() # delete_chat_photo + GROUP_CHAT_CREATED = helper.Item() # group_chat_created + PASSPORT_DATA = helper.Item() # passport_data + + UNKNOWN = helper.Item() # unknown + ANY = helper.Item() # any + + +class ContentTypes(helper.Helper): + """ + List of message content types + + WARNING: List elements. + :key: TEXT :key: AUDIO :key: DOCUMENT diff --git a/examples/payments.py b/examples/payments.py index 74b78456..d85e94ab 100644 --- a/examples/payments.py +++ b/examples/payments.py @@ -4,7 +4,7 @@ from aiogram import Bot from aiogram import types from aiogram.utils import executor from aiogram.dispatcher import Dispatcher -from aiogram.types.message import ContentType +from aiogram.types.message import ContentTypes BOT_TOKEN = 'BOT TOKEN HERE' @@ -86,7 +86,7 @@ async def checkout(pre_checkout_query: types.PreCheckoutQuery): " try to pay again in a few minutes, we need a small rest.") -@dp.message_handler(content_types=ContentType.SUCCESSFUL_PAYMENT) +@dp.message_handler(content_types=ContentTypes.SUCCESSFUL_PAYMENT) async def got_payment(message: types.Message): await bot.send_message(message.chat.id, 'Hoooooray! Thanks for payment! We will proceed your order for `{} {}`' diff --git a/examples/webhook_example.py b/examples/webhook_example.py index 1a4b8198..a1b48c0f 100644 --- a/examples/webhook_example.py +++ b/examples/webhook_example.py @@ -9,7 +9,7 @@ from aiogram import Bot, types, Version from aiogram.contrib.fsm_storage.memory import MemoryStorage from aiogram.dispatcher import Dispatcher from aiogram.dispatcher.webhook import get_new_configured_app, SendMessage -from aiogram.types import ChatType, ParseMode, ContentType +from aiogram.types import ChatType, ParseMode, ContentTypes from aiogram.utils.markdown import hbold, bold, text, link TOKEN = 'BOT TOKEN HERE' @@ -31,7 +31,7 @@ WEBHOOK_URL = f"https://{WEBHOOK_HOST}:{WEBHOOK_PORT}{WEBHOOK_URL_PATH}" WEBAPP_HOST = 'localhost' WEBAPP_PORT = 3001 -BAD_CONTENT = ContentType.PHOTO & ContentType.DOCUMENT & ContentType.STICKER & ContentType.AUDIO +BAD_CONTENT = ContentTypes.PHOTO & ContentTypes.DOCUMENT & ContentTypes.STICKER & ContentTypes.AUDIO loop = asyncio.get_event_loop() bot = Bot(TOKEN, loop=loop)