Make single elements ContentType object and ContentTypes class for listed elements

This commit is contained in:
Alex Root Junior 2018-08-13 23:53:37 +03:00
parent e31e08a072
commit b16963d4a9
5 changed files with 99 additions and 35 deletions

View file

@ -6,7 +6,7 @@ from typing import Any, Dict, Iterable, Optional, Union
from aiogram import types from aiogram import types
from aiogram.dispatcher.filters.filters import BoundFilter, Filter from aiogram.dispatcher.filters.filters import BoundFilter, Filter
from aiogram.types import CallbackQuery, ContentType, Message from aiogram.types import CallbackQuery, Message
class Command(Filter): class Command(Filter):
@ -233,13 +233,13 @@ class ContentTypeFilter(BoundFilter):
key = 'content_types' key = 'content_types'
required = True required = True
default = types.ContentType.TEXT default = types.ContentTypes.TEXT
def __init__(self, content_types): def __init__(self, content_types):
self.content_types = content_types self.content_types = content_types
async def check(self, message): 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 message.content_type in self.content_types

View file

@ -34,7 +34,7 @@ from .invoice import Invoice
from .labeled_price import LabeledPrice from .labeled_price import LabeledPrice
from .location import Location from .location import Location
from .mask_position import MaskPosition 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 .message_entity import MessageEntity, MessageEntityType
from .order_info import OrderInfo from .order_info import OrderInfo
from .passport_data import PassportData from .passport_data import PassportData
@ -77,6 +77,7 @@ __all__ = (
'ChosenInlineResult', 'ChosenInlineResult',
'Contact', 'Contact',
'ContentType', 'ContentType',
'ContentTypes',
'Document', 'Document',
'EncryptedCredentials', 'EncryptedCredentials',
'EncryptedPassportElement', 'EncryptedPassportElement',

View file

@ -84,59 +84,59 @@ class Message(base.TelegramObject):
@functools.lru_cache() @functools.lru_cache()
def content_type(self): def content_type(self):
if self.text: if self.text:
return ContentType.TEXT[0] return ContentType.TEXT
elif self.audio: elif self.audio:
return ContentType.AUDIO[0] return ContentType.AUDIO
elif self.animation: elif self.animation:
return ContentType.ANIMATION[0] return ContentType.ANIMATION
elif self.document: elif self.document:
return ContentType.DOCUMENT[0] return ContentType.DOCUMENT
elif self.game: elif self.game:
return ContentType.GAME[0] return ContentType.GAME
elif self.photo: elif self.photo:
return ContentType.PHOTO[0] return ContentType.PHOTO
elif self.sticker: elif self.sticker:
return ContentType.STICKER[0] return ContentType.STICKER
elif self.video: elif self.video:
return ContentType.VIDEO[0] return ContentType.VIDEO
elif self.video_note: elif self.video_note:
return ContentType.VIDEO_NOTE[0] return ContentType.VIDEO_NOTE
elif self.voice: elif self.voice:
return ContentType.VOICE[0] return ContentType.VOICE
elif self.contact: elif self.contact:
return ContentType.CONTACT[0] return ContentType.CONTACT
elif self.venue: elif self.venue:
return ContentType.VENUE[0] return ContentType.VENUE
elif self.location: elif self.location:
return ContentType.LOCATION[0] return ContentType.LOCATION
elif self.new_chat_members: elif self.new_chat_members:
return ContentType.NEW_CHAT_MEMBERS[0] return ContentType.NEW_CHAT_MEMBERS
elif self.left_chat_member: elif self.left_chat_member:
return ContentType.LEFT_CHAT_MEMBER[0] return ContentType.LEFT_CHAT_MEMBER
elif self.invoice: elif self.invoice:
return ContentType.INVOICE[0] return ContentType.INVOICE
elif self.successful_payment: elif self.successful_payment:
return ContentType.SUCCESSFUL_PAYMENT[0] return ContentType.SUCCESSFUL_PAYMENT
elif self.connected_website: elif self.connected_website:
return ContentType.CONNECTED_WEBSITE[0] return ContentType.CONNECTED_WEBSITE
elif self.migrate_from_chat_id: 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: elif self.migrate_to_chat_id:
return ContentType.MIGRATE_TO_CHAT_ID[0] return ContentType.MIGRATE_TO_CHAT_ID
elif self.pinned_message: elif self.pinned_message:
return ContentType.PINNED_MESSAGE[0] return ContentType.PINNED_MESSAGE
elif self.new_chat_title: elif self.new_chat_title:
return ContentType.NEW_CHAT_TITLE[0] return ContentType.NEW_CHAT_TITLE
elif self.new_chat_photo: elif self.new_chat_photo:
return ContentType.NEW_CHAT_PHOTO[0] return ContentType.NEW_CHAT_PHOTO
elif self.delete_chat_photo: elif self.delete_chat_photo:
return ContentType.DELETE_CHAT_PHOTO[0] return ContentType.DELETE_CHAT_PHOTO
elif self.group_chat_created: elif self.group_chat_created:
return ContentType.GROUP_CHAT_CREATED[0] return ContentType.GROUP_CHAT_CREATED
elif self.passport_data: elif self.passport_data:
return ContentType.PASSPORT_DATA[0] return ContentType.PASSPORT_DATA
else: else:
return ContentType.UNKNOWN[0] return ContentType.UNKNOWN
def is_command(self): def is_command(self):
""" """
@ -731,6 +731,69 @@ class ContentType(helper.Helper):
""" """
List of message content types 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: TEXT
:key: AUDIO :key: AUDIO
:key: DOCUMENT :key: DOCUMENT

View file

@ -4,7 +4,7 @@ from aiogram import Bot
from aiogram import types from aiogram import types
from aiogram.utils import executor from aiogram.utils import executor
from aiogram.dispatcher import Dispatcher from aiogram.dispatcher import Dispatcher
from aiogram.types.message import ContentType from aiogram.types.message import ContentTypes
BOT_TOKEN = 'BOT TOKEN HERE' 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.") " 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): async def got_payment(message: types.Message):
await bot.send_message(message.chat.id, await bot.send_message(message.chat.id,
'Hoooooray! Thanks for payment! We will proceed your order for `{} {}`' 'Hoooooray! Thanks for payment! We will proceed your order for `{} {}`'

View file

@ -9,7 +9,7 @@ from aiogram import Bot, types, Version
from aiogram.contrib.fsm_storage.memory import MemoryStorage from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import Dispatcher from aiogram.dispatcher import Dispatcher
from aiogram.dispatcher.webhook import get_new_configured_app, SendMessage 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 from aiogram.utils.markdown import hbold, bold, text, link
TOKEN = 'BOT TOKEN HERE' TOKEN = 'BOT TOKEN HERE'
@ -31,7 +31,7 @@ WEBHOOK_URL = f"https://{WEBHOOK_HOST}:{WEBHOOK_PORT}{WEBHOOK_URL_PATH}"
WEBAPP_HOST = 'localhost' WEBAPP_HOST = 'localhost'
WEBAPP_PORT = 3001 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() loop = asyncio.get_event_loop()
bot = Bot(TOKEN, loop=loop) bot = Bot(TOKEN, loop=loop)