From 0586c33b1d79b9552312ecac696ea693a2bdefcf Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Wed, 18 Oct 2017 23:25:22 +0300 Subject: [PATCH] Check all types. --- aiogram/types/__init__.py | 132 ++++++++++++++++++++++++++- aiogram/types/response_parameters.py | 2 - aiogram/types/shipping_address.py | 2 - aiogram/types/shipping_option.py | 18 +++- aiogram/types/shipping_query.py | 4 +- aiogram/types/sticker.py | 4 +- aiogram/types/sticker_set.py | 4 +- aiogram/types/successful_payment.py | 2 - aiogram/types/update.py | 31 ++++++- aiogram/types/user.py | 59 +++++++++++- aiogram/types/user_profile_photos.py | 6 +- aiogram/types/venue.py | 2 - aiogram/types/video.py | 2 - aiogram/types/video_note.py | 2 - aiogram/types/voice.py | 2 - aiogram/types/webhook_info.py | 7 +- 16 files changed, 239 insertions(+), 40 deletions(-) diff --git a/aiogram/types/__init__.py b/aiogram/types/__init__.py index 7e93ceb9..235d7615 100644 --- a/aiogram/types/__init__.py +++ b/aiogram/types/__init__.py @@ -1,9 +1,135 @@ from . import base from . import fields from .animation import Animation +from .audio import Audio +from .callback_game import CallbackGame +from .callback_query import CallbackQuery +from .chat import Chat, ChatActions, ChatType +from .chat_member import ChatMember, ChatMemberStatus +from .chat_photo import ChatPhoto +from .chosen_inline_result import ChosenInlineResult +from .contact import Contact +from .document import Document +from .file import File +from .force_reply import ForceReply +from .game import Game +from .game_high_score import GameHighScore +from .inline_keyboard import InlineKeyboardButton, InlineKeyboardMarkup +from .inline_query import InlineQuery +from .inline_query_result import InlineQueryResult, InlineQueryResultArticle, InlineQueryResultAudio, \ + InlineQueryResultCachedAudio, InlineQueryResultCachedDocument, InlineQueryResultCachedGif, \ + InlineQueryResultCachedMpeg4Gif, InlineQueryResultCachedPhoto, InlineQueryResultCachedSticker, \ + InlineQueryResultCachedVideo, InlineQueryResultCachedVoice, InlineQueryResultContact, InlineQueryResultDocument, \ + InlineQueryResultGame, InlineQueryResultGif, InlineQueryResultLocation, InlineQueryResultMpeg4Gif, \ + InlineQueryResultPhoto, InlineQueryResultVenue, InlineQueryResultVideo, InlineQueryResultVoice +from .input_file import InputFile +from .input_message_content import InputContactMessageContent, InputLocationMessageContent, InputMessageContent, \ + InputTextMessageContent, InputVenueMessageContent +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_entity import MessageEntity, MessageEntityType +from .order_info import OrderInfo +from .photo_size import PhotoSize +from .pre_checkout_query import PreCheckoutQuery +from .reply_keyboard import KeyboardButton, ReplyKeyboardMarkup, ReplyKeyboardRemove +from .response_parameters import ResponseParameters +from .shipping_address import ShippingAddress +from .shipping_option import ShippingOption +from .shipping_query import ShippingQuery +from .sticker import Sticker +from .sticker_set import StickerSet +from .successful_payment import SuccessfulPayment +from .update import AllowedUpdates, Update +from .user import User +from .user_profile_photos import UserProfilePhotos +from .venue import Venue +from .video import Video +from .video_note import VideoNote +from .voice import Voice +from .webhook_info import WebhookInfo __all__ = ( - 'base', 'fields', - 'InputFile', 'String', 'Integer', 'Float', 'Boolean', - 'Animation' + 'AllowedUpdates', + 'Animation', + 'Audio', + 'CallbackGame', + 'CallbackQuery', + 'Chat', + 'ChatActions', + 'ChatMember', + 'ChatMemberStatus', + 'ChatPhoto', + 'ChatType', + 'ChosenInlineResult', + 'Contact', + 'ContentType', + 'Document', + 'File', + 'ForceReply', + 'Game', + 'GameHighScore', + 'InlineKeyboardButton', + 'InlineKeyboardMarkup', + 'InlineQuery', + 'InlineQueryResult', + 'InlineQueryResultArticle', + 'InlineQueryResultAudio', + 'InlineQueryResultCachedAudio', + 'InlineQueryResultCachedDocument', + 'InlineQueryResultCachedGif', + 'InlineQueryResultCachedMpeg4Gif', + 'InlineQueryResultCachedPhoto', + 'InlineQueryResultCachedSticker', + 'InlineQueryResultCachedVideo', + 'InlineQueryResultCachedVoice', + 'InlineQueryResultContact', + 'InlineQueryResultDocument', + 'InlineQueryResultGame', + 'InlineQueryResultGif', + 'InlineQueryResultLocation', + 'InlineQueryResultMpeg4Gif', + 'InlineQueryResultPhoto', + 'InlineQueryResultVenue', + 'InlineQueryResultVideo', + 'InlineQueryResultVoice', + 'InputContactMessageContent', + 'InputFile', + 'InputLocationMessageContent', + 'InputMessageContent', + 'InputTextMessageContent', + 'InputVenueMessageContent', + 'Invoice', + 'KeyboardButton', + 'LabeledPrice', + 'Location', + 'MaskPosition', + 'Message', + 'MessageEntity', + 'MessageEntityType', + 'OrderInfo', + 'ParseMode', + 'PhotoSize', + 'PreCheckoutQuery', + 'ReplyKeyboardMarkup', + 'ReplyKeyboardRemove', + 'ResponseParameters', + 'ShippingAddress', + 'ShippingOption', + 'ShippingQuery', + 'Sticker', + 'StickerSet', + 'SuccessfulPayment', + 'Update', + 'User', + 'UserProfilePhotos', + 'Venue', + 'Video', + 'VideoNote', + 'Voice', + 'WebhookInfo', + 'base', + 'fields' ) diff --git a/aiogram/types/response_parameters.py b/aiogram/types/response_parameters.py index db3fe9c0..8f40cc11 100644 --- a/aiogram/types/response_parameters.py +++ b/aiogram/types/response_parameters.py @@ -1,6 +1,5 @@ from . import base from . import fields -import typing class ResponseParameters(base.TelegramObject): @@ -11,4 +10,3 @@ class ResponseParameters(base.TelegramObject): """ migrate_to_chat_id: base.Integer = fields.Field() retry_after: base.Integer = fields.Field() - diff --git a/aiogram/types/shipping_address.py b/aiogram/types/shipping_address.py index 4d9f263e..8c8d3418 100644 --- a/aiogram/types/shipping_address.py +++ b/aiogram/types/shipping_address.py @@ -1,6 +1,5 @@ from . import base from . import fields -import typing class ShippingAddress(base.TelegramObject): @@ -15,4 +14,3 @@ class ShippingAddress(base.TelegramObject): street_line1: base.String = fields.Field() street_line2: base.String = fields.Field() post_code: base.String = fields.Field() - diff --git a/aiogram/types/shipping_option.py b/aiogram/types/shipping_option.py index 83f117f8..4ec915c5 100644 --- a/aiogram/types/shipping_option.py +++ b/aiogram/types/shipping_option.py @@ -1,6 +1,7 @@ +import typing + from . import base from . import fields -import typing from .labeled_price import LabeledPrice @@ -14,3 +15,18 @@ class ShippingOption(base.TelegramObject): title: base.String = fields.Field() prices: typing.List[LabeledPrice] = fields.ListField(base=LabeledPrice) + def __init__(self, id: base.String, title: base.String, prices: typing.List[LabeledPrice] = None): + if prices is None: + prices = [] + + super(ShippingOption, self).__init__(id=id, title=title, prices=prices) + + def add(self, price: LabeledPrice): + """ + Add price + + :param price: + :return: + """ + self.prices.append(price) + return self diff --git a/aiogram/types/shipping_query.py b/aiogram/types/shipping_query.py index e1754ef1..18b99b4d 100644 --- a/aiogram/types/shipping_query.py +++ b/aiogram/types/shipping_query.py @@ -1,8 +1,7 @@ from . import base from . import fields -import typing -from .user import User from .shipping_address import ShippingAddress +from .user import User class ShippingQuery(base.TelegramObject): @@ -15,4 +14,3 @@ class ShippingQuery(base.TelegramObject): from_user: User = fields.Field(alias='from', base=User) invoice_payload: base.String = fields.Field() shipping_address: ShippingAddress = fields.Field(base=ShippingAddress) - diff --git a/aiogram/types/sticker.py b/aiogram/types/sticker.py index 5e3d3e93..710704d4 100644 --- a/aiogram/types/sticker.py +++ b/aiogram/types/sticker.py @@ -1,8 +1,7 @@ from . import base from . import fields -import typing -from .photo_size import PhotoSize from .mask_position import MaskPosition +from .photo_size import PhotoSize class Sticker(base.TelegramObject): @@ -19,4 +18,3 @@ class Sticker(base.TelegramObject): set_name: base.String = fields.Field() mask_position: MaskPosition = fields.Field(base=MaskPosition) file_size: base.Integer = fields.Field() - diff --git a/aiogram/types/sticker_set.py b/aiogram/types/sticker_set.py index 42655f7e..9d302bae 100644 --- a/aiogram/types/sticker_set.py +++ b/aiogram/types/sticker_set.py @@ -1,6 +1,7 @@ +import typing + from . import base from . import fields -import typing from .sticker import Sticker @@ -14,4 +15,3 @@ class StickerSet(base.TelegramObject): title: base.String = fields.Field() contains_masks: base.Boolean = fields.Field() stickers: typing.List[Sticker] = fields.ListField(base=Sticker) - diff --git a/aiogram/types/successful_payment.py b/aiogram/types/successful_payment.py index e39cca29..41cd32da 100644 --- a/aiogram/types/successful_payment.py +++ b/aiogram/types/successful_payment.py @@ -1,6 +1,5 @@ from . import base from . import fields -import typing from .order_info import OrderInfo @@ -17,4 +16,3 @@ class SuccessfulPayment(base.TelegramObject): order_info: OrderInfo = fields.Field(base=OrderInfo) telegram_payment_charge_id: base.String = fields.Field() provider_payment_charge_id: base.String = fields.Field() - diff --git a/aiogram/types/update.py b/aiogram/types/update.py index 1969cbf2..ea5a2779 100644 --- a/aiogram/types/update.py +++ b/aiogram/types/update.py @@ -1,12 +1,12 @@ from . import base from . import fields -import typing -from .message import Message -from .inline_query import InlineQuery -from .chosen_inline_result import ChosenInlineResult from .callback_query import CallbackQuery -from .shipping_query import ShippingQuery +from .chosen_inline_result import ChosenInlineResult +from .inline_query import InlineQuery +from .message import Message from .pre_checkout_query import PreCheckoutQuery +from .shipping_query import ShippingQuery +from ..utils import helper class Update(base.TelegramObject): @@ -27,3 +27,24 @@ class Update(base.TelegramObject): shipping_query: ShippingQuery = fields.Field(base=ShippingQuery) pre_checkout_query: PreCheckoutQuery = fields.Field(base=PreCheckoutQuery) + +class AllowedUpdates(helper.Helper): + """ + Helper for allowed_updates parameter in getUpdates and setWebhook methods. + + You can use &, + or | operators for make combination of allowed updates. + + Example: + >>> bot.get_updates(allowed_updates=AllowedUpdates.MESSAGE + AllowedUpdates.EDITED_MESSAGE) + """ + mode = helper.HelperMode.snake_case + + MESSAGE = helper.ListItem() # message + EDITED_MESSAGE = helper.ListItem() # edited_message + CHANNEL_POST = helper.ListItem() # channel_post + EDITED_CHANNEL_POST = helper.ListItem() # edited_channel_post + INLINE_QUERY = helper.ListItem() # inline_query + CHOSEN_INLINE_QUERY = helper.ListItem() # chosen_inline_result + CALLBACK_QUERY = helper.ListItem() # callback_query + SHIPPING_QUERY = helper.ListItem() # shipping_query + PRE_CHECKOUT_QUERY = helper.ListItem() # pre_checkout_query diff --git a/aiogram/types/user.py b/aiogram/types/user.py index 2d246f69..a0c99356 100644 --- a/aiogram/types/user.py +++ b/aiogram/types/user.py @@ -1,6 +1,11 @@ from . import base from . import fields -import typing +from ..utils import markdown + +try: + import babel +except ImportError: + babel = None class User(base.TelegramObject): @@ -16,3 +21,55 @@ class User(base.TelegramObject): username: base.String = fields.Field() language_code: base.String = fields.Field() + @property + def full_name(self): + """ + You can get full name of user. + + :return: str + """ + full_name = self.first_name + if self.last_name: + full_name += ' ' + self.last_name + return full_name + + @property + def mention(self): + """ + You can get mention to user (If user have username, otherwise return full name) + + :return: str + """ + if self.username: + return '@' + self.username + return self.full_name + + @property + def locale(self) -> 'babel.core.Locale' or None: + """ + This property require `Babel `_ module + + :return: :class:`babel.core.Locale` + :raise: ImportError: when babel is not installed. + """ + if not babel: + raise ImportError('Babel is not installed!') + if not self.language_code: + return None + if not hasattr(self, '_locale'): + setattr(self, '_locale', babel.core.Locale.parse(self.language_code, sep='-')) + return getattr(self, '_locale') + + @property + def url(self): + return f"tg://user?id={self.id}" + + def get_mention(self, name=None, as_html=False): + if name is None: + name = self.mention + if as_html: + return markdown.hlink(name, self.url) + return markdown.link(name, self.url) + + async def get_user_profile_photos(self, offset=None, limit=None): + return await self.bot.get_user_profile_photos(self.id, offset, limit) diff --git a/aiogram/types/user_profile_photos.py b/aiogram/types/user_profile_photos.py index 2c7f2157..259f669c 100644 --- a/aiogram/types/user_profile_photos.py +++ b/aiogram/types/user_profile_photos.py @@ -1,6 +1,7 @@ +import typing + from . import base from . import fields -import typing from .photo_size import PhotoSize @@ -11,5 +12,4 @@ class UserProfilePhotos(base.TelegramObject): https://core.telegram.org/bots/api#userprofilephotos """ total_count: base.Integer = fields.Field() - photos: typing.List[typing.List[PhotoSize]] = fields.ListField(base=PhotoSize) - + photos: typing.List[typing.List[PhotoSize]] = fields.ListOfLists(base=PhotoSize) diff --git a/aiogram/types/venue.py b/aiogram/types/venue.py index e3c5b4e8..38f28cf9 100644 --- a/aiogram/types/venue.py +++ b/aiogram/types/venue.py @@ -1,6 +1,5 @@ from . import base from . import fields -import typing from .location import Location @@ -14,4 +13,3 @@ class Venue(base.TelegramObject): title: base.String = fields.Field() address: base.String = fields.Field() foursquare_id: base.String = fields.Field() - diff --git a/aiogram/types/video.py b/aiogram/types/video.py index e62dcad2..636d6295 100644 --- a/aiogram/types/video.py +++ b/aiogram/types/video.py @@ -1,6 +1,5 @@ from . import base from . import fields -import typing from .photo_size import PhotoSize @@ -17,4 +16,3 @@ class Video(base.TelegramObject): thumb: PhotoSize = fields.Field(base=PhotoSize) mime_type: base.String = fields.Field() file_size: base.Integer = fields.Field() - diff --git a/aiogram/types/video_note.py b/aiogram/types/video_note.py index 975409fc..69fb11a4 100644 --- a/aiogram/types/video_note.py +++ b/aiogram/types/video_note.py @@ -1,6 +1,5 @@ from . import base from . import fields -import typing from .photo_size import PhotoSize @@ -15,4 +14,3 @@ class VideoNote(base.TelegramObject): duration: base.Integer = fields.Field() thumb: PhotoSize = fields.Field(base=PhotoSize) file_size: base.Integer = fields.Field() - diff --git a/aiogram/types/voice.py b/aiogram/types/voice.py index 0c7009f8..192be48f 100644 --- a/aiogram/types/voice.py +++ b/aiogram/types/voice.py @@ -1,6 +1,5 @@ from . import base from . import fields -import typing class Voice(base.TelegramObject): @@ -13,4 +12,3 @@ class Voice(base.TelegramObject): duration: base.Integer = fields.Field() mime_type: base.String = fields.Field() file_size: base.Integer = fields.Field() - diff --git a/aiogram/types/webhook_info.py b/aiogram/types/webhook_info.py index 9b6e7e92..995d3aaa 100644 --- a/aiogram/types/webhook_info.py +++ b/aiogram/types/webhook_info.py @@ -1,14 +1,12 @@ +import typing + from . import base from . import fields -import typing class WebhookInfo(base.TelegramObject): """ Contains information about the current status of a webhook. - All types used in the Bot API responses are represented as JSON-objects. - It is safe to use 32-bit signed integers for storing all Integer fields unless otherwise noted. - Optional fields may be not returned when irrelevant. https://core.telegram.org/bots/api#webhookinfo """ @@ -19,4 +17,3 @@ class WebhookInfo(base.TelegramObject): last_error_message: base.String = fields.Field() max_connections: base.Integer = fields.Field() allowed_updates: typing.List[base.String] = fields.ListField() -