New types (In process...)

This commit is contained in:
Alex Root Junior 2017-10-12 16:43:23 +03:00
parent d98566bf7f
commit 84c8548ceb
48 changed files with 2206 additions and 14 deletions

View file

@ -1,4 +1,4 @@
from .bot import Bot
# from .bot import Bot
from .utils.versions import Version, Stage
VERSION = Version(1, 0, 0, stage=Stage.DEV, build=0)

View file

@ -1,7 +1,9 @@
from . import base
from . import fields
from .animation import Animation
__all__ = (
'base', 'fields',
'InputFile', 'String', 'Integer', 'Float', 'Boolean'
'InputFile', 'String', 'Integer', 'Float', 'Boolean',
'Animation'
)

View file

@ -0,0 +1,19 @@
from . import base
from . import fields
from .photo_size import PhotoSize
class Animation(base.TelegramObject):
"""
You can provide an animation for your game so that it looks stylish in chats
(check out Lumberjack for an example).
This object represents an animation file to be displayed in the message containing a game.
https://core.telegram.org/bots/api#animation
"""
file_id: base.String = fields.Field()
thumb: PhotoSize = fields.Field(base=PhotoSize)
file_name: base.String = fields.Field()
mime_type: base.String = fields.Field()
file_size: base.Integer = fields.Field()

16
aiogram/types/audio.py Normal file
View file

@ -0,0 +1,16 @@
from . import base
from . import fields
class Audio(base.TelegramObject):
"""
This object represents an audio file to be treated as music by the Telegram clients.
https://core.telegram.org/bots/api#audio
"""
file_id: base.String = fields.Field()
duration: base.Integer = fields.Field()
performer: base.String = fields.Field()
title: base.String = fields.Field()
mime_type: base.String = fields.Field()
file_size: base.Integer = fields.Field()

View file

@ -3,8 +3,8 @@ import typing
import ujson
from typing import TypeVar
from ..utils.context import get_value
from .fields import BaseField
from ..utils.context import get_value
PROPS_ATTR_NAME = '_props'
VALUES_ATTR_NAME = '_values'
@ -77,14 +77,14 @@ class TelegramObject(metaclass=MetaTelegramObject):
conf = {}
for key, value in kwargs.items():
if key in self.props:
self.props[key].set_value(self, value)
self.props[key].set_value(self, value, parent=self)
else:
self.values[key] = value
self._conf = conf
@property
def conf(self) -> typing.Dict[str, typing.Any]:
return self.conf
return self._conf
@property
def props(self) -> typing.Dict[str, BaseField]:
@ -167,6 +167,10 @@ class TelegramObject(metaclass=MetaTelegramObject):
"""
return ujson.dumps(self.to_python())
@classmethod
def create(cls, *args, **kwargs):
raise NotImplemented
def __str__(self) -> str:
"""
Return object as string. Alias for '.as_json()'
@ -186,3 +190,7 @@ class TelegramObject(metaclass=MetaTelegramObject):
setattr(self, key, value)
else:
self.values[key] = value
def __contains__(self, item):
self.clean()
return item in self.values

View file

@ -0,0 +1,10 @@
from . import base
class CallbackGame(base.TelegramObject):
"""
A placeholder, currently holds no information. Use BotFather to set up your game.
https://core.telegram.org/bots/api#callbackgame
"""
pass

View file

@ -0,0 +1,27 @@
from . import base
from . import fields
from .message import Message
from .user import User
class CallbackQuery(base.TelegramObject):
"""
This object represents an incoming callback query from a callback button in an inline keyboard.
If the button that originated the query was attached to a message sent by the bot,
the field message will be present.
If the button was attached to a message sent via the bot (in inline mode),
the field inline_message_id will be present.
Exactly one of the fields data or game_short_name will be present.
https://core.telegram.org/bots/api#callbackquery
"""
id: base.String = fields.Field()
from_user: User = fields.Field(alias='from', base=User)
message: Message = fields.Field(base=Message)
inline_message_id: base.String = fields.Field()
chat_instance: base.String = fields.Field()
data: base.String = fields.Field()
game_short_name: base.String = fields.Field()

139
aiogram/types/chat.py Normal file
View file

@ -0,0 +1,139 @@
from . import base
from . import fields
from .chat_photo import ChatPhoto
from ..utils import helper
from ..utils import markdown
class Chat(base.TelegramObject):
"""
This object represents a chat.
https://core.telegram.org/bots/api#chat
"""
id: base.Integer = fields.Field()
type: base.String = fields.Field()
title: base.String = fields.Field()
username: base.String = fields.Field()
first_name: base.String = fields.Field()
last_name: base.String = fields.Field()
all_members_are_administrators: base.Boolean = fields.Field()
photo: ChatPhoto = fields.Field(base=ChatPhoto)
description: base.String = fields.Field()
invite_link: base.String = fields.Field()
pinned_message: 'Message' = fields.Field(base='Message')
@property
def full_name(self):
if self.type == ChatType.PRIVATE:
full_name = self.first_name
if self.last_name:
full_name += ' ' + self.last_name
return full_name
return self.title
@property
def mention(self):
"""
Get mention if dialog have username or full name if this is Private dialog otherwise None
"""
if self.username:
return '@' + self.username
if self.type == ChatType.PRIVATE:
return self.full_name
return None
@property
def user_url(self):
if self.type != ChatType.PRIVATE:
raise TypeError('This property available only in private chats.')
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.user_url)
return markdown.link(name, self.user_url)
async def set_photo(self, photo):
return await self.bot.set_chat_photo(self.id, photo)
async def delete_photo(self):
return await self.bot.delete_chat_photo(self.id)
async def set_title(self, title):
return await self.bot.set_chat_title(self.id, title)
async def set_description(self, description):
return await self.bot.delete_chat_description(self.id, description)
async def pin_message(self, message_id: int, disable_notification: bool = False):
return await self.bot.pin_chat_message(self.id, message_id, disable_notification)
async def unpin_message(self):
return await self.bot.unpin_chat_message(self.id)
async def leave(self):
return await self.bot.leave_chat(self.id)
async def get_administrators(self):
return await self.bot.get_chat_administrators(self.id)
async def get_members_count(self):
return await self.bot.get_chat_members_count(self.id)
async def get_member(self, user_id):
return await self.bot.get_chat_member(self.id, user_id)
async def do(self, action):
return await self.bot.send_chat_action(self.id, action)
class ChatType(helper.Helper):
"""
List of chat types
:key: PRIVATE
:key: GROUP
:key: SUPER_GROUP
:key: CHANNEL
"""
mode = helper.HelperMode.lowercase
PRIVATE = helper.Item() # private
GROUP = helper.Item() # group
SUPER_GROUP = helper.Item() # supergroup
CHANNEL = helper.Item() # channel
class ChatActions(helper.Helper):
"""
List of chat actions
:key: TYPING
:key: UPLOAD_PHOTO
:key: RECORD_VIDEO
:key: UPLOAD_VIDEO
:key: RECORD_AUDIO
:key: UPLOAD_AUDIO
:key: UPLOAD_DOCUMENT
:key: FIND_LOCATION
:key: RECORD_VIDEO_NOTE
:key: UPLOAD_VIDEO_NOTE
"""
mode = helper.HelperMode.snake_case
TYPING = helper.Item() # typing
UPLOAD_PHOTO = helper.Item() # upload_photo
RECORD_VIDEO = helper.Item() # record_video
UPLOAD_VIDEO = helper.Item() # upload_video
RECORD_AUDIO = helper.Item() # record_audio
UPLOAD_AUDIO = helper.Item() # upload_audio
UPLOAD_DOCUMENT = helper.Item() # upload_document
FIND_LOCATION = helper.Item() # find_location
RECORD_VIDEO_NOTE = helper.Item() # record_video_note
UPLOAD_VIDEO_NOTE = helper.Item() # upload_video_note

View file

@ -0,0 +1,52 @@
import datetime
from aiogram.utils import helper
from . import base
from . import fields
from .user import User
class ChatMember(base.TelegramObject):
"""
This object contains information about one member of a chat.
https://core.telegram.org/bots/api#chatmember
"""
user: User = fields.Field(base=User)
status: base.String = fields.Field()
until_date: datetime.datetime = fields.DateTimeField()
can_be_edited: base.Boolean = fields.Field()
can_change_info: base.Boolean = fields.Field()
can_post_messages: base.Boolean = fields.Field()
can_edit_messages: base.Boolean = fields.Field()
can_delete_messages: base.Boolean = fields.Field()
can_invite_users: base.Boolean = fields.Field()
can_restrict_members: base.Boolean = fields.Field()
can_pin_messages: base.Boolean = fields.Field()
can_promote_members: base.Boolean = fields.Field()
can_send_messages: base.Boolean = fields.Field()
can_send_media_messages: base.Boolean = fields.Field()
can_send_other_messages: base.Boolean = fields.Field()
can_add_web_page_previews: base.Boolean = fields.Field()
class ChatMemberStatus(helper.Helper):
"""
Chat member status
"""
mode = helper.HelperMode.lowercase
CREATOR = helper.Item() # creator
ADMINISTRATOR = helper.Item() # administrator
MEMBER = helper.Item() # member
LEFT = helper.Item() # left
KICKED = helper.Item() # kicked
@classmethod
def is_admin(cls, role):
return role in [cls.ADMINISTRATOR, cls.CREATOR]
@classmethod
def is_member(cls, role):
return role in [cls.MEMBER, cls.ADMINISTRATOR, cls.CREATOR]

View file

@ -0,0 +1,12 @@
from . import base
from . import fields
class ChatPhoto(base.TelegramObject):
"""
This object represents a chat photo.
https://core.telegram.org/bots/api#chatphoto
"""
small_file_id: base.String = fields.Field()
big_file_id: base.String = fields.Field()

View file

@ -0,0 +1,22 @@
from . import base
from . import fields
from .location import Location
from .user import User
class ChosenInlineResult(base.TelegramObject):
"""
Represents a result of an inline query that was chosen by the user and sent to their chat partner.
Note: It is necessary to enable inline feedback via @Botfather in order to receive these objects in updates.
Your bot can accept payments from Telegram users.
Please see the introduction to payments for more details on the process and how to set up payments for your bot.
Please note that users will need Telegram v.4.0 or higher to use payments (released on May 18, 2017).
https://core.telegram.org/bots/api#choseninlineresult
"""
result_id: base.String = fields.Field()
from_user: User = fields.Field(alias='from', base=User)
location: Location = fields.Field(base=Location)
inline_message_id: base.String = fields.Field()
query: base.String = fields.Field()

14
aiogram/types/contact.py Normal file
View file

@ -0,0 +1,14 @@
from . import base
from . import fields
class Contact(base.TelegramObject):
"""
This object represents a phone contact.
https://core.telegram.org/bots/api#contact
"""
phone_number: base.String = fields.Field()
first_name: base.String = fields.Field()
last_name: base.String = fields.Field()
user_id: base.Integer = fields.Field()

16
aiogram/types/document.py Normal file
View file

@ -0,0 +1,16 @@
from . import base
from . import fields
from .photo_size import PhotoSize
class Document(base.TelegramObject):
"""
This object represents a general file (as opposed to photos, voice messages and audio files).
https://core.telegram.org/bots/api#document
"""
file_id: base.String = fields.Field()
thumb: PhotoSize = fields.Field(base=PhotoSize)
file_name: base.String = fields.Field()
mime_type: base.String = fields.Field()
file_size: base.Integer = fields.Field()

View file

@ -41,16 +41,17 @@ class BaseField(metaclass=abc.ABCMeta):
"""
return instance.values.get(self.alias)
def set_value(self, instance, value):
def set_value(self, instance, value, parent=None):
"""
Set prop value
:param instance:
:param value:
:param parent:
:return:
"""
self.resolve_base(instance)
value = self.deserialize(value)
value = self.deserialize(value, parent)
instance.values[self.alias] = value
def __get__(self, instance, owner):
@ -90,13 +91,13 @@ class Field(BaseField):
"""
def serialize(self, value):
if self.base_object is not None:
if self.base_object is not None and hasattr(value, 'to_python'):
return value.to_python()
return value
def deserialize(self, value):
if self.base_object is not None and not hasattr(value, 'base_object'):
return self.base_object(**value)
def deserialize(self, value, parent=None):
if self.base_object is not None and not hasattr(value, 'base_object') and not hasattr(value, 'to_python'):
return self.base_object(conf={'parent': parent}, **value)
return value
@ -105,6 +106,13 @@ class ListField(Field):
Field contains list ob objects
"""
def __init__(self, *args, **kwargs):
default = kwargs.pop('default', None)
if default is None:
default = []
super(ListField, self).__init__(*args, default=default, **kwargs)
def serialize(self, value):
result = []
serialize = super(ListField, self).serialize
@ -112,17 +120,39 @@ class ListField(Field):
result.append(serialize(item))
return result
def deserialize(self, value):
def deserialize(self, value, parent=None):
result = []
deserialize = super(ListField, self).deserialize
for item in value:
result.append(deserialize(item))
result.append(deserialize(item, parent=parent))
return result
class ListOfLists(Field):
def serialize(self, value):
result = []
serialize = super(ListOfLists, self).serialize
for row in value:
row_result = []
for item in row:
row_result.append(serialize(item))
result.append(row_result)
return result
def deserialize(self, value, parent=None):
result = []
deserialize = super(ListOfLists, self).deserialize
for row in value:
row_result = []
for item in row:
row_result.append(deserialize(item, parent=parent))
result.append(row_result)
return result
class DateTimeField(BaseField):
"""
In this field stored datetime
In this field st_ored datetime
in: unixtime
out: datetime

32
aiogram/types/file.py Normal file
View file

@ -0,0 +1,32 @@
from . import base
from . import fields
class File(base.TelegramObject):
"""
This object represents a file ready to be downloaded.
The file can be downloaded via the link https://api.telegram.org/file/bot<token>/<file_path>.
It is guaranteed that the link will be valid for at least 1 hour.
When the link expires, a new one can be requested by calling getFile.
Maximum file size to download is 20 MB
https://core.telegram.org/bots/api#file
"""
file_id: base.String = fields.Field()
file_size: base.Integer = fields.Field()
file_path: base.String = fields.Field()
async def download(self, destination=None, timeout=30, chunk_size=65536, seek=True):
"""
Download file by file_path to destination
:param destination: filename or instance of :class:`io.IOBase`. For e. g. :class:`io.BytesIO`
:param timeout: Integer
:param chunk_size: Integer
:param seek: Boolean - go to start of file when downloading is finished.
:return: destination
"""
return await self.bot.download_file(self.file_path, destination, timeout, chunk_size, seek)

View file

@ -0,0 +1,36 @@
import typing
from . import base
from . import fields
class ForceReply(base.TelegramObject):
"""
Upon receiving a message with this object,
Telegram clients will display a reply interface to the user
(act as if the user has selected the bots message and tapped Reply').
This can be extremely useful if you want to create user-friendly step-by-step
interfaces without having to sacrifice privacy mode.
Example: A poll bot for groups runs in privacy mode
(only receives commands, replies to its messages and mentions).
There could be two ways to create a new poll
The last option is definitely more attractive.
And if you use ForceReply in your bots questions, it will receive the users answers even
if it only receives replies, commands and mentions without any extra work for the user.
https://core.telegram.org/bots/api#forcereply
"""
force_reply: base.Boolean = fields.Field(default=True)
selective: base.Boolean = fields.Field()
@classmethod
def create(cls, selective: typing.Optional[base.Boolean] = None):
"""
Create new force reply
:param selective:
:return:
"""
return cls(selective=selective)

23
aiogram/types/game.py Normal file
View file

@ -0,0 +1,23 @@
import typing
from . import base
from . import fields
from .animation import Animation
from .message_entity import MessageEntity
from .photo_size import PhotoSize
class Game(base.TelegramObject):
"""
This object represents a game.
Use BotFather to create and edit games, their short names will act as unique identifiers.
https://core.telegram.org/bots/api#game
"""
title: base.String = fields.Field()
description: base.String = fields.Field()
photo: typing.List[PhotoSize] = fields.ListField(base=PhotoSize)
text: base.String = fields.Field()
text_entities: typing.List[MessageEntity] = fields.ListField(base=MessageEntity)
animation: Animation = fields.Field(base=Animation)

View file

@ -0,0 +1,16 @@
from . import base
from . import fields
from .user import User
class GameHighScore(base.TelegramObject):
"""
This object represents one row of the high scores table for a game.
And thats about all weve got for now.
If you've got any questions, please check out our Bot FAQ
https://core.telegram.org/bots/api#gamehighscore
"""
position: base.Integer = fields.Field()
user: User = fields.Field(base=User)
score: base.Integer = fields.Field()

View file

@ -0,0 +1,82 @@
import typing
from . import base
from . import fields
from .callback_game import CallbackGame
class InlineKeyboardMarkup(base.TelegramObject):
"""
This object represents an inline keyboard that appears right next to the message it belongs to.
Note: This will only work in Telegram versions released after 9 April, 2016.
Older clients will display unsupported message.
https://core.telegram.org/bots/api#inlinekeyboardmarkup
"""
inline_keyboard: typing.List[typing.List[InlineKeyboardButton]] = fields.ListOfLists(base=InlineKeyboardButton,
default=[])
@classmethod
def create(cls, row_width=3):
return cls(conf={'row_width': row_width})
@property
def row_width(self):
return self.conf.get('row_width', 3)
@row_width.setter
def row_width(self, value):
self.conf['row_width'] = value
def add(self, *args):
"""
Add buttons
:param args:
:return:
"""
row = []
for index, button in enumerate(args, start=1):
row.append(button)
if index % self.row_width == 0:
self.inline_keyboard.append(row)
row = []
if len(row) > 0:
self.inline_keyboard.append(row)
def row(self, *args):
"""
Add row
:param args:
:return:
"""
btn_array = []
for button in args:
btn_array.append(button.to_json())
self.inline_keyboard.append(btn_array)
return self
class InlineKeyboardButton(base.TelegramObject):
"""
This object represents one button of an inline keyboard. You must use exactly one of the optional fields.
https://core.telegram.org/bots/api#inlinekeyboardbutton
"""
text: base.String = fields.Field()
url: base.String = fields.Field()
callback_data: base.String = fields.Field()
switch_inline_query: base.String = fields.Field()
switch_inline_query_current_chat: base.String = fields.Field()
callback_game: CallbackGame = fields.Field(base=CallbackGame)
pay: base.Boolean = fields.Field()
def __init__(self, text: base.String, url: base.String = None, callback_data: base.String = None,
switch_inline_query: base.String = None, switch_inline_query_current_chat: base.String = None,
callback_game: CallbackGame = None, pay: base.Boolean = None):
super(InlineKeyboardButton, self).__init__(text=text, url=url, callback_data=callback_data,
switch_inline_query=switch_inline_query,
switch_inline_query_current_chat=switch_inline_query_current_chat,
callback_game=callback_game, pay=pay)

View file

@ -0,0 +1,19 @@
from . import base
from . import fields
from .location import Location
from .user import User
class InlineQuery(base.TelegramObject):
"""
This object represents an incoming inline query.
When the user sends an empty query, your bot could return some default or trending results.
https://core.telegram.org/bots/api#inlinequery
"""
id: base.String = fields.Field()
from_user: User = fields.Field(alias='from', base=User)
location: Location = fields.Field(base=Location)
query: base.String = fields.Field()
offset: base.String = fields.Field()

View file

@ -0,0 +1,700 @@
import typing
from . import base
from . import fields
from .inline_keyboard import InlineKeyboardMarkup
from .input_message_content import InputMessageContent
class InlineQueryResult(base.TelegramObject):
"""
This object represents one result of an inline query.
Telegram clients currently support results of the following 20 types
https://core.telegram.org/bots/api#inlinequeryresult
"""
id: base.String = fields.Field()
reply_markup: InlineKeyboardMarkup = fields.Field(base=InlineKeyboardMarkup)
class InlineQueryResultCachedAudio(InlineQueryResult):
"""
Represents a link to an mp3 audio file stored on the Telegram servers.
By default, this audio file will be sent by the user.
Alternatively, you can use input_message_content to send a message with
the specified content instead of the audio.
Note: This will only work in Telegram versions released after 9 April, 2016.
Older clients will ignore them.
https://core.telegram.org/bots/api#inlinequeryresultcachedaudio
"""
type: base.String = fields.Field(alias='type', default='audio')
audio_file_id: base.String = fields.Field()
caption: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
def __init__(self, *,
id: base.String,
audio_file_id: base.String,
caption: typing.Optional[base.String] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None):
super(InlineQueryResultCachedAudio, self).__init__(id=id, audio_file_id=audio_file_id,
caption=caption, reply_markup=reply_markup,
input_message_content=input_message_content)
class InlineQueryResultCachedDocument(InlineQueryResult):
"""
Represents a link to a file stored on the Telegram servers.
By default, this file will be sent by the user with an optional caption.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the file.
Note: This will only work in Telegram versions released after 9 April, 2016.
Older clients will ignore them.
https://core.telegram.org/bots/api#inlinequeryresultcacheddocument
"""
type: base.String = fields.Field(alias='type', default='document')
title: base.String = fields.Field()
document_file_id: base.String = fields.Field()
description: base.String = fields.Field()
caption: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
def __init__(self, *,
id: base.String,
title: base.String,
document_file_id: base.String,
description: typing.Optional[base.String] = None,
caption: typing.Optional[base.String] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None):
super(InlineQueryResultCachedDocument, self).__init__(id=id, title=title,
document_file_id=document_file_id,
description=description, caption=caption,
reply_markup=reply_markup,
input_message_content=input_message_content)
class InlineQueryResultCachedGif(InlineQueryResult):
"""
Represents a link to an animated GIF file stored on the Telegram servers.
By default, this animated GIF file will be sent by the user with an optional caption.
Alternatively, you can use input_message_content to send a message with specified content
instead of the animation.
https://core.telegram.org/bots/api#inlinequeryresultcachedgif
"""
type: base.String = fields.Field(alias='type', default='gif')
gif_file_id: base.String = fields.Field()
title: base.String = fields.Field()
caption: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
def __init__(self, *,
id: base.String,
gif_file_id: base.String,
title: typing.Optional[base.String] = None,
caption: typing.Optional[base.String] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None):
super(InlineQueryResultCachedGif, self).__init__(id=id, gif_file_id=gif_file_id, title=title,
caption=caption, reply_markup=reply_markup,
input_message_content=input_message_content)
class InlineQueryResultCachedMpeg4Gif(InlineQueryResult):
"""
Represents a link to a video animation (H.264/MPEG-4 AVC video without sound) stored on the Telegram servers.
By default, this animated MPEG-4 file will be sent by the user with an optional caption.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the animation.
https://core.telegram.org/bots/api#inlinequeryresultcachedmpeg4gif
"""
type: base.String = fields.Field(alias='type', default='mpeg4_gif')
mpeg4_file_id: base.String = fields.Field()
title: base.String = fields.Field()
caption: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
def __init__(self, *,
id: base.String,
mpeg4_file_id: base.String,
title: typing.Optional[base.String] = None,
caption: typing.Optional[base.String] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None):
super(InlineQueryResultCachedMpeg4Gif, self).__init__(id=id, mpeg4_file_id=mpeg4_file_id,
title=title, caption=caption, reply_markup=reply_markup,
input_message_content=input_message_content)
class InlineQueryResultCachedPhoto(InlineQueryResult):
"""
Represents a link to a photo stored on the Telegram servers.
By default, this photo will be sent by the user with an optional caption.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the photo.
https://core.telegram.org/bots/api#inlinequeryresultcachedphoto
"""
type: base.String = fields.Field(alias='type', default='photo')
photo_file_id: base.String = fields.Field()
title: base.String = fields.Field()
description: base.String = fields.Field()
caption: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
def __init__(self, *,
id: base.String,
photo_file_id: base.String,
title: typing.Optional[base.String] = None,
description: typing.Optional[base.String] = None,
caption: typing.Optional[base.String] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None):
super(InlineQueryResultCachedPhoto, self).__init__(id=id, photo_file_id=photo_file_id, title=title,
description=description, caption=caption,
reply_markup=reply_markup,
input_message_content=input_message_content)
class InlineQueryResultCachedSticker(InlineQueryResult):
"""
Represents a link to a sticker stored on the Telegram servers.
By default, this sticker will be sent by the user.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the sticker.
Note: This will only work in Telegram versions released after 9 April, 2016.
Older clients will ignore them.
https://core.telegram.org/bots/api#inlinequeryresultcachedsticker
"""
type: base.String = fields.Field(alias='type', default='sticker')
sticker_file_id: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
def __init__(self, *,
id: base.String,
sticker_file_id: base.String,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None):
super(InlineQueryResultCachedSticker, self).__init__(id=id, sticker_file_id=sticker_file_id,
reply_markup=reply_markup,
input_message_content=input_message_content)
class InlineQueryResultCachedVideo(InlineQueryResult):
"""
Represents a link to a video file stored on the Telegram servers.
By default, this video file will be sent by the user with an optional caption.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the video.
https://core.telegram.org/bots/api#inlinequeryresultcachedvideo
"""
type: base.String = fields.Field(alias='type', default='video')
video_file_id: base.String = fields.Field()
title: base.String = fields.Field()
description: base.String = fields.Field()
caption: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
def __init__(self, *,
id: base.String,
video_file_id: base.String,
title: base.String,
description: typing.Optional[base.String] = None,
caption: typing.Optional[base.String] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None):
super(InlineQueryResultCachedVideo, self).__init__(id=id, video_file_id=video_file_id, title=title,
description=description, caption=caption,
reply_markup=reply_markup,
input_message_content=input_message_content)
class InlineQueryResultCachedVoice(InlineQueryResult):
"""
Represents a link to a voice message stored on the Telegram servers.
By default, this voice message will be sent by the user.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the voice message.
Note: This will only work in Telegram versions released after 9 April, 2016. Older clients will ignore them.
https://core.telegram.org/bots/api#inlinequeryresultcachedvoice
"""
type: base.String = fields.Field(alias='type', default='voice')
voice_file_id: base.String = fields.Field()
title: base.String = fields.Field()
caption: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
def __init__(self, *,
id: base.String,
voice_file_id: base.String,
title: base.String,
caption: typing.Optional[base.String] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None):
super(InlineQueryResultCachedVoice, self).__init__(id=id, voice_file_id=voice_file_id, title=title,
caption=caption, reply_markup=reply_markup,
input_message_content=input_message_content)
class InlineQueryResultArticle(InlineQueryResult):
"""
Represents a link to an article or web page.
https://core.telegram.org/bots/api#inlinequeryresultarticle
"""
type: base.String = fields.Field(alias='type', default='article')
title: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
url: base.String = fields.Field()
hide_url: base.Boolean = fields.Field()
description: base.String = fields.Field()
thumb_url: base.String = fields.Field()
thumb_width: base.Integer = fields.Field()
thumb_height: base.Integer = fields.Field()
def __init__(self, *,
id: base.String,
title: base.String,
input_message_content: InputMessageContent,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
url: typing.Optional[base.String] = None,
hide_url: typing.Optional[base.Boolean] = None,
description: typing.Optional[base.String] = None,
thumb_url: typing.Optional[base.String] = None,
thumb_width: typing.Optional[base.Integer] = None,
thumb_height: typing.Optional[base.Integer] = None):
super(InlineQueryResultArticle, self).__init__(id=id, title=title,
input_message_content=input_message_content,
reply_markup=reply_markup, url=url, hide_url=hide_url,
description=description, thumb_url=thumb_url,
thumb_width=thumb_width, thumb_height=thumb_height)
class InlineQueryResultAudio(InlineQueryResult):
"""
Represents a link to an mp3 audio file. By default, this audio file will be sent by the user.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the audio.
Note: This will only work in Telegram versions released after 9 April, 2016.
Older clients will ignore them.
https://core.telegram.org/bots/api#inlinequeryresultaudio
"""
type: base.String = fields.Field(alias='type', default='audio')
audio_url: base.String = fields.Field()
title: base.String = fields.Field()
caption: base.String = fields.Field()
performer: base.String = fields.Field()
audio_duration: base.Integer = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
def __init__(self, *,
id: base.String,
audio_url: base.String,
title: base.String,
caption: typing.Optional[base.String] = None,
performer: typing.Optional[base.String] = None,
audio_duration: typing.Optional[base.Integer] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None):
super(InlineQueryResultAudio, self).__init__(id=id, audio_url=audio_url, title=title,
caption=caption, performer=performer,
audio_duration=audio_duration, reply_markup=reply_markup,
input_message_content=input_message_content)
class InlineQueryResultContact(InlineQueryResult):
"""
Represents a contact with a phone number.
By default, this contact will be sent by the user.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the contact.
Note: This will only work in Telegram versions released after 9 April, 2016. Older clients will ignore them.
https://core.telegram.org/bots/api#inlinequeryresultcontact
"""
type: base.String = fields.Field(alias='type', default='contact')
phone_number: base.String = fields.Field()
first_name: base.String = fields.Field()
last_name: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
thumb_url: base.String = fields.Field()
thumb_width: base.Integer = fields.Field()
thumb_height: base.Integer = fields.Field()
def __init__(self, *,
id: base.String,
phone_number: base.String,
first_name: base.String,
last_name: typing.Optional[base.String] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None,
thumb_url: typing.Optional[base.String] = None,
thumb_width: typing.Optional[base.Integer] = None,
thumb_height: typing.Optional[base.Integer] = None):
super(InlineQueryResultContact, self).__init__(id=id, phone_number=phone_number,
first_name=first_name, last_name=last_name,
reply_markup=reply_markup,
input_message_content=input_message_content, thumb_url=thumb_url,
thumb_width=thumb_width, thumb_height=thumb_height)
class InlineQueryResultDocument(InlineQueryResult):
"""
Represents a link to a file.
By default, this file will be sent by the user with an optional caption.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the file. Currently, only .PDF and .ZIP files can be sent using this method.
Note: This will only work in Telegram versions released after 9 April, 2016. Older clients will ignore them.
https://core.telegram.org/bots/api#inlinequeryresultdocument
"""
type: base.String = fields.Field(alias='type', default='document')
title: base.String = fields.Field()
caption: base.String = fields.Field()
document_url: base.String = fields.Field()
mime_type: base.String = fields.Field()
description: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
thumb_url: base.String = fields.Field()
thumb_width: base.Integer = fields.Field()
thumb_height: base.Integer = fields.Field()
def __init__(self, *,
id: base.String,
title: base.String,
caption: typing.Optional[base.String] = None,
document_url: typing.Optional[base.String] = None,
mime_type: typing.Optional[base.String] = None,
description: typing.Optional[base.String] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None,
thumb_url: typing.Optional[base.String] = None,
thumb_width: typing.Optional[base.Integer] = None,
thumb_height: typing.Optional[base.Integer] = None):
super(InlineQueryResultDocument, self).__init__(id=id, title=title, caption=caption,
document_url=document_url, mime_type=mime_type,
description=description, reply_markup=reply_markup,
input_message_content=input_message_content,
thumb_url=thumb_url, thumb_width=thumb_width,
thumb_height=thumb_height)
class InlineQueryResultGame(InlineQueryResult):
"""
Represents a Game.
Note: This will only work in Telegram versions released after October 1, 2016.
Older clients will not display any inline results if a game result is among them.
https://core.telegram.org/bots/api#inlinequeryresultgame
"""
type: base.String = fields.Field(alias='type', default='game')
game_short_name: base.String = fields.Field()
def __init__(self, *,
id: base.String,
game_short_name: base.String,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None):
super(InlineQueryResultGame, self).__init__(id=id, game_short_name=game_short_name,
reply_markup=reply_markup)
class InlineQueryResultGif(InlineQueryResult):
"""
Represents a link to an animated GIF file.
By default, this animated GIF file will be sent by the user with optional caption.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the animation.
https://core.telegram.org/bots/api#inlinequeryresultgif
"""
type: base.String = fields.Field(alias='type', default='gif')
gif_url: base.String = fields.Field()
gif_width: base.Integer = fields.Field()
gif_height: base.Integer = fields.Field()
gif_duration: base.Integer = fields.Field()
thumb_url: base.String = fields.Field()
title: base.String = fields.Field()
caption: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
def __init__(self, *,
id: base.String,
gif_url: base.String,
gif_width: typing.Optional[base.Integer] = None,
gif_height: typing.Optional[base.Integer] = None,
gif_duration: typing.Optional[base.Integer] = None,
thumb_url: typing.Optional[base.String] = None,
title: typing.Optional[base.String] = None,
caption: typing.Optional[base.String] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None):
super(InlineQueryResultGif, self).__init__(id=id, gif_url=gif_url, gif_width=gif_width,
gif_height=gif_height, gif_duration=gif_duration,
thumb_url=thumb_url, title=title, caption=caption,
reply_markup=reply_markup,
input_message_content=input_message_content)
class InlineQueryResultLocation(InlineQueryResult):
"""
Represents a location on a map.
By default, the location will be sent by the user.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the location.
Note: This will only work in Telegram versions released after 9 April, 2016.
Older clients will ignore them.
https://core.telegram.org/bots/api#inlinequeryresultlocation
"""
type: base.String = fields.Field(alias='type', default='location')
latitude: base.Float = fields.Field()
longitude: base.Float = fields.Field()
title: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
thumb_url: base.String = fields.Field()
thumb_width: base.Integer = fields.Field()
thumb_height: base.Integer = fields.Field()
def __init__(self, *,
id: base.String,
latitude: base.Float,
longitude: base.Float,
title: base.String,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None,
thumb_url: typing.Optional[base.String] = None,
thumb_width: typing.Optional[base.Integer] = None,
thumb_height: typing.Optional[base.Integer] = None):
super(InlineQueryResultLocation, self).__init__(id=id, latitude=latitude, longitude=longitude,
title=title, reply_markup=reply_markup,
input_message_content=input_message_content,
thumb_url=thumb_url, thumb_width=thumb_width,
thumb_height=thumb_height)
class InlineQueryResultMpeg4Gif(InlineQueryResult):
"""
Represents a link to a video animation (H.264/MPEG-4 AVC video without sound).
By default, this animated MPEG-4 file will be sent by the user with optional caption.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the animation.
https://core.telegram.org/bots/api#inlinequeryresultmpeg4gif
"""
type: base.String = fields.Field(alias='type', default='mpeg4_gif')
mpeg4_url: base.String = fields.Field()
mpeg4_width: base.Integer = fields.Field()
mpeg4_height: base.Integer = fields.Field()
mpeg4_duration: base.Integer = fields.Field()
thumb_url: base.String = fields.Field()
title: base.String = fields.Field()
caption: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
def __init__(self, *,
id: base.String,
mpeg4_url: base.String,
thumb_url: base.String,
mpeg4_width: typing.Optional[base.Integer] = None,
mpeg4_height: typing.Optional[base.Integer] = None,
mpeg4_duration: typing.Optional[base.Integer] = None,
title: typing.Optional[base.String] = None,
caption: typing.Optional[base.String] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None):
super(InlineQueryResultMpeg4Gif, self).__init__(id=id, mpeg4_url=mpeg4_url, mpeg4_width=mpeg4_width,
mpeg4_height=mpeg4_height, mpeg4_duration=mpeg4_duration,
thumb_url=thumb_url, title=title, caption=caption,
reply_markup=reply_markup,
input_message_content=input_message_content)
class InlineQueryResultPhoto(InlineQueryResult):
"""
Represents a link to a photo.
By default, this photo will be sent by the user with optional caption.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the photo.
https://core.telegram.org/bots/api#inlinequeryresultphoto
"""
type: base.String = fields.Field(alias='type', default='photo')
photo_url: base.String = fields.Field()
thumb_url: base.String = fields.Field()
photo_width: base.Integer = fields.Field()
photo_height: base.Integer = fields.Field()
title: base.String = fields.Field()
description: base.String = fields.Field()
caption: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
def __init__(self, *,
id: base.String,
photo_url: base.String,
thumb_url: base.String,
photo_width: typing.Optional[base.Integer] = None,
photo_height: typing.Optional[base.Integer] = None,
title: typing.Optional[base.String] = None,
description: typing.Optional[base.String] = None,
caption: typing.Optional[base.String] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None):
super(InlineQueryResultPhoto, self).__init__(id=id, photo_url=photo_url, thumb_url=thumb_url,
photo_width=photo_width, photo_height=photo_height, title=title,
description=description, caption=caption,
reply_markup=reply_markup,
input_message_content=input_message_content)
class InlineQueryResultVenue(InlineQueryResult):
"""
Represents a venue. By default, the venue will be sent by the user.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the venue.
Note: This will only work in Telegram versions released after 9 April, 2016.
Older clients will ignore them.
https://core.telegram.org/bots/api#inlinequeryresultvenue
"""
type: base.String = fields.Field(alias='type', default='venue')
latitude: base.Float = fields.Field()
longitude: base.Float = fields.Field()
title: base.String = fields.Field()
address: base.String = fields.Field()
foursquare_id: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
thumb_url: base.String = fields.Field()
thumb_width: base.Integer = fields.Field()
thumb_height: base.Integer = fields.Field()
def __init__(self, *,
id: base.String,
latitude: base.Float,
longitude: base.Float,
title: base.String,
address: base.String,
foursquare_id: typing.Optional[base.String] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None,
thumb_url: typing.Optional[base.String] = None,
thumb_width: typing.Optional[base.Integer] = None,
thumb_height: typing.Optional[base.Integer] = None):
super(InlineQueryResultVenue, self).__init__(id=id, latitude=latitude, longitude=longitude,
title=title, address=address, foursquare_id=foursquare_id,
reply_markup=reply_markup,
input_message_content=input_message_content, thumb_url=thumb_url,
thumb_width=thumb_width, thumb_height=thumb_height)
class InlineQueryResultVideo(InlineQueryResult):
"""
Represents a link to a page containing an embedded video player or a video file.
By default, this video file will be sent by the user with an optional caption.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the video.
If an InlineQueryResultVideo message contains an embedded video (e.g., YouTube),
you must replace its content using input_message_content.
https://core.telegram.org/bots/api#inlinequeryresultvideo
"""
type: base.String = fields.Field(alias='type', default='video')
video_url: base.String = fields.Field()
mime_type: base.String = fields.Field()
thumb_url: base.String = fields.Field()
title: base.String = fields.Field()
caption: base.String = fields.Field()
video_width: base.Integer = fields.Field()
video_height: base.Integer = fields.Field()
video_duration: base.Integer = fields.Field()
description: base.String = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
def __init__(self, *,
id: base.String,
video_url: base.String,
mime_type: base.String,
thumb_url: base.String,
title: base.String,
caption: typing.Optional[base.String] = None,
video_width: typing.Optional[base.Integer] = None,
video_height: typing.Optional[base.Integer] = None,
video_duration: typing.Optional[base.Integer] = None,
description: typing.Optional[base.String] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None):
super(InlineQueryResultVideo, self).__init__(id=id, video_url=video_url, mime_type=mime_type,
thumb_url=thumb_url, title=title, caption=caption,
video_width=video_width, video_height=video_height,
video_duration=video_duration, description=description,
reply_markup=reply_markup,
input_message_content=input_message_content)
class InlineQueryResultVoice(InlineQueryResult):
"""
Represents a link to a voice recording in an .ogg container encoded with OPUS.
By default, this voice recording will be sent by the user.
Alternatively, you can use input_message_content to send a message with the specified content
instead of the the voice message.
Note: This will only work in Telegram versions released after 9 April, 2016.
Older clients will ignore them.
https://core.telegram.org/bots/api#inlinequeryresultvoice
"""
type: base.String = fields.Field(alias='type', default='voice')
voice_url: base.String = fields.Field()
title: base.String = fields.Field()
caption: base.String = fields.Field()
voice_duration: base.Integer = fields.Field()
input_message_content: InputMessageContent = fields.Field(base=InputMessageContent)
def __init__(self, *,
id: base.String,
voice_url: base.String,
title: base.String,
caption: typing.Optional[base.String] = None,
voice_duration: typing.Optional[base.Integer] = None,
reply_markup: typing.Optional[InlineKeyboardMarkup] = None,
input_message_content: typing.Optional[InputMessageContent] = None):
super(InlineQueryResultVoice, self).__init__(id=id, voice_url=voice_url, title=title,
caption=caption, voice_duration=voice_duration,
reply_markup=reply_markup,
input_message_content=input_message_content)

View file

@ -0,0 +1,20 @@
from . import base
# TODO: Interface for sending files
class InputFile(base.TelegramObject):
"""
This object represents the contents of a file to be uploaded.
Must be posted using multipart/form-data in the usual way that files are uploaded via the browser.
https://core.telegram.org/bots/api#inputfile
"""
def __init__(self, file_id=None, path=None, url=None, filename=None):
self.file_id = file_id
self.path = path
self.url = url
self.filename = filename
super(InputFile, self).__init__()

View file

@ -0,0 +1,93 @@
import typing
from . import base
from . import fields
class InputMessageContent(base.TelegramObject):
"""
This object represents the content of a message to be sent as a result of an inline query.
Telegram clients currently support the following 4 types
https://core.telegram.org/bots/api#inputmessagecontent
"""
pass
class InputContactMessageContent(InputMessageContent):
"""
Represents the content of a contact message to be sent as the result of an inline query.
Note: This will only work in Telegram versions released after 9 April, 2016.
Older clients will ignore them.
https://core.telegram.org/bots/api#inputcontactmessagecontent
"""
phone_number: base.String = fields.Field()
first_name: base.String = fields.Field()
last_name: base.String = fields.Field()
def __init__(self, phone_number: base.String,
first_name: typing.Optional[base.String] = None,
last_name: typing.Optional[base.String] = None):
super(InputContactMessageContent, self).__init__(phone_number=phone_number, first_name=first_name,
last_name=last_name)
class InputLocationMessageContent(InputMessageContent):
"""
Represents the content of a location message to be sent as the result of an inline query.
Note: This will only work in Telegram versions released after 9 April, 2016.
Older clients will ignore them.
https://core.telegram.org/bots/api#inputlocationmessagecontent
"""
latitude: base.Float = fields.Field()
longitude: base.Float = fields.Field()
def __init__(self, latitude: base.Float,
longitude: base.Float):
super(InputLocationMessageContent, self).__init__(latitude=latitude, longitude=longitude)
class InputTextMessageContent(InputMessageContent):
"""
Represents the content of a text message to be sent as the result of an inline query.
https://core.telegram.org/bots/api#inputtextmessagecontent
"""
message_text: base.String = fields.Field()
parse_mode: base.String = fields.Field()
disable_web_page_preview: base.Boolean = fields.Field()
def __init__(self, message_text: typing.Optional[base.String] = None,
parse_mode: typing.Optional[base.String] = None,
disable_web_page_preview: typing.Optional[base.Boolean] = None):
super(InputTextMessageContent, self).__init__(message_text=message_text, parse_mode=parse_mode,
disable_web_page_preview=disable_web_page_preview)
class InputVenueMessageContent(InputMessageContent):
"""
Represents the content of a venue message to be sent as the result of an inline query.
Note: This will only work in Telegram versions released after 9 April, 2016.
Older clients will ignore them.
https://core.telegram.org/bots/api#inputvenuemessagecontent
"""
latitude: base.Float = fields.Field()
longitude: base.Float = fields.Field()
title: base.String = fields.Field()
address: base.String = fields.Field()
foursquare_id: base.String = fields.Field()
def __init__(self, latitude: typing.Optional[base.Float] = None,
longitude: typing.Optional[base.Float] = None,
title: typing.Optional[base.String] = None,
address: typing.Optional[base.String] = None,
foursquare_id: typing.Optional[base.String] = None):
super(InputVenueMessageContent, self).__init__(latitude=latitude, longitude=longitude, title=title,
address=address, foursquare_id=foursquare_id)

15
aiogram/types/invoice.py Normal file
View file

@ -0,0 +1,15 @@
from . import base
from . import fields
class Invoice(base.TelegramObject):
"""
This object contains basic information about an invoice.
https://core.telegram.org/bots/api#invoice
"""
title: base.String = fields.Field()
description: base.String = fields.Field()
start_parameter: base.String = fields.Field()
currency: base.String = fields.Field()
total_amount: base.Integer = fields.Field()

View file

@ -0,0 +1,12 @@
from . import base
from . import fields
class LabeledPrice(base.TelegramObject):
"""
This object represents a portion of the price for goods or services.
https://core.telegram.org/bots/api#labeledprice
"""
label: base.String = fields.Field()
amount: base.Integer = fields.Field()

12
aiogram/types/location.py Normal file
View file

@ -0,0 +1,12 @@
from . import base
from . import fields
class Location(base.TelegramObject):
"""
This object represents a point on the map.
https://core.telegram.org/bots/api#location
"""
longitude: base.Float = fields.Field()
latitude: base.Float = fields.Field()

View file

@ -0,0 +1,14 @@
from . import base
from . import fields
class MaskPosition(base.TelegramObject):
"""
This object describes the position on faces where a mask should be placed by default.
https://core.telegram.org/bots/api#maskposition
"""
point: base.String = fields.Field()
x_shift: base.Float = fields.Field()
y_shift: base.Float = fields.Field()
scale: base.Float = fields.Field()

230
aiogram/types/message.py Normal file
View file

@ -0,0 +1,230 @@
import typing
from aiogram.utils import helper
from aiogram.utils.exceptions import TelegramAPIError
from . import base
from . import fields
from .audio import Audio
from .chat import Chat
from .contact import Contact
from .document import Document
from .game import Game
from .invoice import Invoice
from .location import Location
from .message_entity import MessageEntity
from .photo_size import PhotoSize
from .sticker import Sticker
from .successful_payment import SuccessfulPayment
from .user import User
from .venue import Venue
from .video import Video
from .video_note import VideoNote
from .voice import Voice
class Message(base.TelegramObject):
"""
This object represents a message.
https://core.telegram.org/bots/api#message
"""
message_id: base.Integer = fields.Field()
from_user: User = fields.Field(alias='from', base=User)
date: base.Integer = fields.Field()
chat: Chat = fields.Field(base=Chat)
forward_from: User = fields.Field(base=User)
forward_from_chat: Chat = fields.Field(base=Chat)
forward_from_message_id: base.Integer = fields.Field()
forward_signature: base.String = fields.Field()
forward_date: base.Integer = fields.Field()
reply_to_message: 'Message' = fields.Field(base='Message')
edit_date: base.Integer = fields.Field()
author_signature: base.String = fields.Field()
text: base.String = fields.Field()
entities: typing.List[MessageEntity] = fields.ListField(base=MessageEntity)
audio: Audio = fields.Field(base=Audio)
document: Document = fields.Field(base=Document)
game: Game = fields.Field(base=Game)
photo: typing.List[PhotoSize] = fields.ListField(base=PhotoSize)
sticker: Sticker = fields.Field(base=Sticker)
video: Video = fields.Field(base=Video)
voice: Voice = fields.Field(base=Voice)
video_note: VideoNote = fields.Field(base=VideoNote)
caption: base.String = fields.Field()
contact: Contact = fields.Field(base=Contact)
location: Location = fields.Field(base=Location)
venue: Venue = fields.Field(base=Venue)
new_chat_members: typing.List[User] = fields.ListField(base=User)
left_chat_member: User = fields.Field(base=User)
new_chat_title: base.String = fields.Field()
new_chat_photo: typing.List[PhotoSize] = fields.ListField(base=PhotoSize)
delete_chat_photo: base.Boolean = fields.Field()
group_chat_created: base.Boolean = fields.Field()
supergroup_chat_created: base.Boolean = fields.Field()
channel_chat_created: base.Boolean = fields.Field()
migrate_to_chat_id: base.Integer = fields.Field()
migrate_from_chat_id: base.Integer = fields.Field()
pinned_message: 'Message' = fields.Field(base='Message')
invoice: Invoice = fields.Field(base=Invoice)
successful_payment: SuccessfulPayment = fields.Field(base=SuccessfulPayment)
@property
def content_type(self):
if self.text:
return ContentType.TEXT[0]
elif self.audio:
return ContentType.AUDIO[0]
elif self.document:
return ContentType.DOCUMENT[0]
elif self.game:
return ContentType.GAME[0]
elif self.photo:
return ContentType.PHOTO[0]
elif self.sticker:
return ContentType.STICKER[0]
elif self.video:
return ContentType.VIDEO[0]
elif self.voice:
return ContentType.VOICE[0]
elif self.new_chat_members:
return ContentType.NEW_CHAT_MEMBERS[0]
elif self.left_chat_member:
return ContentType.LEFT_CHAT_MEMBER[0]
elif self.invoice:
return ContentType.INVOICE[0]
elif self.successful_payment:
return ContentType.SUCCESSFUL_PAYMENT[0]
else:
return ContentType.UNKNOWN[0]
def is_command(self):
"""
Check message text is command
:return: bool
"""
return self.text and self.text.startswith('/')
def get_full_command(self):
"""
Split command and args
:return: tuple of (command, args)
"""
if self.is_command():
command, _, args = self.text.partition(' ')
return command, args
def get_command(self):
command = self.get_full_command()
if command:
return command[0]
def get_args(self):
command = self.get_full_command()
if command:
return command[1].strip()
@property
def md_text(self):
text = self.text
if self.text and self.entities:
for entity in reversed(self.entities):
text = entity.apply_md(text)
return text
@property
def html_text(self):
text = self.text
if self.text and self.entities:
for entity in reversed(self.entities):
text = entity.apply_html(text)
return text
async def reply(self, text, parse_mode=None, disable_web_page_preview=None,
disable_notification=None, reply_markup=None) -> 'Message':
"""
Reply to this message
:param text: str
:param parse_mode: str
:param disable_web_page_preview: bool
:param disable_notification: bool
:param reply_markup:
:return: :class:`aoigram.types.Message`
"""
return await self.bot.send_message(self.chat.id, text, parse_mode, disable_web_page_preview,
disable_notification, self.message_id, reply_markup)
async def forward(self, chat_id, disable_notification=None) -> 'Message':
"""
Forward this message
:param chat_id:
:param disable_notification:
:return:
"""
return await self.bot.forward_message(chat_id, self.chat.id, self.message_id, disable_notification)
async def delete(self):
"""
Delete this message
:return: bool
"""
return await self.bot.delete_message(self.chat.id, self.message_id)
async def pin(self, disable_notification: bool = False):
return await self.chat.pin_message(self.message_id, disable_notification)
class ContentType(helper.Helper):
"""
List of message content types
:key: TEXT
:key: AUDIO
:key: DOCUMENT
:key: GAME
:key: PHOTO
:key: STICKER
:key: VIDEO
:key: VOICE
:key: NEW_CHAT_MEMBERS
:key: LEFT_CHAT_MEMBER
:key: INVOICE
:key: SUCCESSFUL_PAYMENT
:key: UNKNOWN
"""
mode = helper.HelperMode.snake_case
TEXT = helper.ListItem() # text
AUDIO = helper.ListItem() # audio
DOCUMENT = helper.ListItem() # document
GAME = helper.ListItem() # game
PHOTO = helper.ListItem() # photo
STICKER = helper.ListItem() # sticker
VIDEO = helper.ListItem() # video
VOICE = helper.ListItem() # voice
NEW_CHAT_MEMBERS = helper.ListItem() # new_chat_members
LEFT_CHAT_MEMBER = helper.ListItem() # left_chat_member
INVOICE = helper.ListItem() # invoice
SUCCESSFUL_PAYMENT = helper.ListItem() # successful_payment
UNKNOWN = 'unknown'
class ParseMode(helper.Helper):
"""
Parse modes
:key: MARKDOWN
:key: HTML
"""
mode = helper.HelperMode.lowercase
MARKDOWN = helper.Item()
HTML = helper.Item()

View file

@ -0,0 +1,95 @@
from . import base
from . import fields
from .user import User
from ..utils import helper, markdown
class MessageEntity(base.TelegramObject):
"""
This object represents one special entity in a text message. For example, hashtags, usernames, URLs, etc.
https://core.telegram.org/bots/api#messageentity
"""
type: base.String = fields.Field()
offset: base.Integer = fields.Field()
length: base.Integer = fields.Field()
url: base.String = fields.Field()
user: User = fields.Field(base=User)
def _apply(self, text, func):
return text[:self.offset] + \
func(text[self.offset:self.offset + self.length]) + \
text[self.offset + self.length:]
def apply_md(self, text):
"""
Apply entity for text as Markdown
:param text:
:return:
"""
if self.type == MessageEntityType.BOLD:
return self._apply(text, markdown.bold)
elif self.type == MessageEntityType.ITALIC:
return self._apply(text, markdown.italic)
elif self.type == MessageEntityType.PRE:
return self._apply(text, markdown.pre)
elif self.type == MessageEntityType.CODE:
return self._apply(text, markdown.code)
elif self.type == MessageEntityType.URL:
return self._apply(text, lambda url: markdown.link(url, url))
elif self.type == MessageEntityType.TEXT_LINK:
return self._apply(text, lambda url: markdown.link(url, self.url))
return text
def apply_html(self, text):
"""
Apply entity for text as HTML
:param text:
:return:
"""
if self.type == MessageEntityType.BOLD:
return self._apply(text, markdown.hbold)
elif self.type == MessageEntityType.ITALIC:
return self._apply(text, markdown.hitalic)
elif self.type == MessageEntityType.PRE:
return self._apply(text, markdown.hpre)
elif self.type == MessageEntityType.CODE:
return self._apply(text, markdown.hcode)
elif self.type == MessageEntityType.URL:
return self._apply(text, lambda url: markdown.hlink(url, url))
elif self.type == MessageEntityType.TEXT_LINK:
return self._apply(text, lambda url: markdown.hlink(url, self.url))
return text
class MessageEntityType(helper.Helper):
"""
List of entity types
:key: MENTION
:key: HASHTAG
:key: BOT_COMMAND
:key: URL
:key: EMAIL
:key: BOLD
:key: ITALIC
:key: CODE
:key: PRE
:key: TEXT_LINK
:key: TEXT_MENTION
"""
mode = helper.HelperMode.snake_case
MENTION = helper.Item() # mention - @username
HASHTAG = helper.Item() # hashtag
BOT_COMMAND = helper.Item() # bot_command
URL = helper.Item() # url
EMAIL = helper.Item() # email
BOLD = helper.Item() # bold - bold text
ITALIC = helper.Item() # italic - italic text
CODE = helper.Item() # code - monowidth string
PRE = helper.Item() # pre - monowidth block
TEXT_LINK = helper.Item() # text_link - for clickable text URLs
TEXT_MENTION = helper.Item() # text_mention - for users without usernames

View file

@ -0,0 +1,15 @@
from . import base
from . import fields
from .shipping_address import ShippingAddress
class OrderInfo(base.TelegramObject):
"""
This object represents information about an order.
https://core.telegram.org/bots/api#orderinfo
"""
name: base.String = fields.Field()
phone_number: base.String = fields.Field()
email: base.String = fields.Field()
shipping_address: ShippingAddress = fields.Field(base=ShippingAddress)

View file

@ -0,0 +1,14 @@
from . import base
from . import fields
class PhotoSize(base.TelegramObject):
"""
This object represents one size of a photo or a file / sticker thumbnail.
https://core.telegram.org/bots/api#photosize
"""
file_id: base.String = fields.Field()
width: base.Integer = fields.Field()
height: base.Integer = fields.Field()
file_size: base.Integer = fields.Field()

View file

@ -0,0 +1,26 @@
from . import base
from . import fields
from .order_info import OrderInfo
from .user import User
class PreCheckoutQuery(base.TelegramObject):
"""
This object contains information about an incoming pre-checkout query.
Your bot can offer users HTML5 games to play solo or to compete against
each other in groups and one-on-one chats.
Create games via @BotFather using the /newgame command.
Please note that this kind of power requires responsibility:
you will need to accept the terms for each game that your bots will be offering.
https://core.telegram.org/bots/api#precheckoutquery
"""
id: base.String = fields.Field()
from_user: User = fields.Field(alias='from', base=User)
currency: base.String = fields.Field()
total_amount: base.Integer = fields.Field()
invoice_payload: base.String = fields.Field()
shipping_option_id: base.String = fields.Field()
order_info: OrderInfo = fields.Field(base=OrderInfo)

View file

@ -0,0 +1,91 @@
import typing
from . import base
from . import fields
class ReplyKeyboardMarkup(base.TelegramObject):
"""
This object represents a custom keyboard with reply options (see Introduction to bots for details and examples).
https://core.telegram.org/bots/api#replykeyboardmarkup
"""
keyboard: typing.List[typing.List[KeyboardButton]] = fields.ListOfLists(base=KeyboardButton, default=[])
resize_keyboard: base.Boolean = fields.Field()
one_time_keyboard: base.Boolean = fields.Field()
selective: base.Boolean = fields.Field()
def __init__(self, keyboard: typing.List[typing.List[KeyboardButton]] = None,
resize_keyboard: base.Boolean = None,
one_time_keyboard: base.Boolean = None,
selective: base.Boolean = None):
super(ReplyKeyboardMarkup, self).__init__(keyboard=keyboard, resize_keyboard=resize_keyboard,
one_time_keyboard=one_time_keyboard, selective=selective)
@classmethod
def create(cls, row_width=3):
return cls(conf={'row_width': row_width})
@property
def row_width(self):
return self.conf.get('row_width', 3)
@row_width.setter
def row_width(self, value):
self.conf['row_width'] = value
def add(self, *args):
i = 1
row = []
for button in args:
if isinstance(button, str):
row.append({'text': button})
elif isinstance(button, bytes):
row.append({'text': button.decode('utf-8')})
else:
row.append(button.to_json())
if i % self.row_width == 0:
self.keyboard.append(row)
row = []
i += 1
if len(row) > 0:
self.keyboard.append(row)
def row(self, *args):
btn_array = []
for button in args:
if isinstance(button, str):
btn_array.append({'text': button})
else:
btn_array.append(button.to_json())
self.keyboard.append(btn_array)
return self
class KeyboardButton(base.TelegramObject):
"""
This object represents one button of the reply keyboard. For simple text buttons String can be used instead of this object to specify text of the button. Optional fields are mutually exclusive.
Note: request_contact and request_location options will only work in Telegram versions released after 9 April, 2016. Older clients will ignore them.
https://core.telegram.org/bots/api#keyboardbutton
"""
text: base.String = fields.Field()
request_contact: base.Boolean = fields.Field()
request_location: base.Boolean = fields.Field()
def __init__(self, text: base.String, request_contact: base.Boolean = None, request_location: base.Boolean = None):
super(KeyboardButton, self).__init__(text=text, request_contact=request_contact,
request_location=request_location)
class ReplyKeyboardRemove(base.TelegramObject):
"""
Upon receiving a message with this object, Telegram clients will remove the current custom keyboard and display the default letter-keyboard. By default, custom keyboards are displayed until a new keyboard is sent by a bot. An exception is made for one-time keyboards that are hidden immediately after the user presses a button (see ReplyKeyboardMarkup).
https://core.telegram.org/bots/api#replykeyboardremove
"""
remove_keyboard: base.Boolean = fields.Field(default=True)
selective: base.Boolean = fields.Field()
def __init__(self, selective: base.Boolean = None):
super(ReplyKeyboardRemove, self).__init__(remove_keyboard=True, selective=selective)

View file

@ -0,0 +1,14 @@
from . import base
from . import fields
import typing
class ResponseParameters(base.TelegramObject):
"""
Contains information about why a request was unsuccessful.
https://core.telegram.org/bots/api#responseparameters
"""
migrate_to_chat_id: base.Integer = fields.Field()
retry_after: base.Integer = fields.Field()

View file

@ -0,0 +1,18 @@
from . import base
from . import fields
import typing
class ShippingAddress(base.TelegramObject):
"""
This object represents a shipping address.
https://core.telegram.org/bots/api#shippingaddress
"""
country_code: base.String = fields.Field()
state: base.String = fields.Field()
city: base.String = fields.Field()
street_line1: base.String = fields.Field()
street_line2: base.String = fields.Field()
post_code: base.String = fields.Field()

View file

@ -0,0 +1,16 @@
from . import base
from . import fields
import typing
from .labeled_price import LabeledPrice
class ShippingOption(base.TelegramObject):
"""
This object represents one shipping option.
https://core.telegram.org/bots/api#shippingoption
"""
id: base.String = fields.Field()
title: base.String = fields.Field()
prices: typing.List[LabeledPrice] = fields.ListField(base=LabeledPrice)

View file

@ -0,0 +1,18 @@
from . import base
from . import fields
import typing
from .user import User
from .shipping_address import ShippingAddress
class ShippingQuery(base.TelegramObject):
"""
This object contains information about an incoming shipping query.
https://core.telegram.org/bots/api#shippingquery
"""
id: base.String = fields.Field()
from_user: User = fields.Field(alias='from', base=User)
invoice_payload: base.String = fields.Field()
shipping_address: ShippingAddress = fields.Field(base=ShippingAddress)

22
aiogram/types/sticker.py Normal file
View file

@ -0,0 +1,22 @@
from . import base
from . import fields
import typing
from .photo_size import PhotoSize
from .mask_position import MaskPosition
class Sticker(base.TelegramObject):
"""
This object represents a sticker.
https://core.telegram.org/bots/api#sticker
"""
file_id: base.String = fields.Field()
width: base.Integer = fields.Field()
height: base.Integer = fields.Field()
thumb: PhotoSize = fields.Field(base=PhotoSize)
emoji: base.String = fields.Field()
set_name: base.String = fields.Field()
mask_position: MaskPosition = fields.Field(base=MaskPosition)
file_size: base.Integer = fields.Field()

View file

@ -0,0 +1,17 @@
from . import base
from . import fields
import typing
from .sticker import Sticker
class StickerSet(base.TelegramObject):
"""
This object represents a sticker set.
https://core.telegram.org/bots/api#stickerset
"""
name: base.String = fields.Field()
title: base.String = fields.Field()
contains_masks: base.Boolean = fields.Field()
stickers: typing.List[Sticker] = fields.ListField(base=Sticker)

View file

@ -0,0 +1,20 @@
from . import base
from . import fields
import typing
from .order_info import OrderInfo
class SuccessfulPayment(base.TelegramObject):
"""
This object contains basic information about a successful payment.
https://core.telegram.org/bots/api#successfulpayment
"""
currency: base.String = fields.Field()
total_amount: base.Integer = fields.Field()
invoice_payload: base.String = fields.Field()
shipping_option_id: base.String = fields.Field()
order_info: OrderInfo = fields.Field(base=OrderInfo)
telegram_payment_charge_id: base.String = fields.Field()
provider_payment_charge_id: base.String = fields.Field()

29
aiogram/types/update.py Normal file
View file

@ -0,0 +1,29 @@
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 .pre_checkout_query import PreCheckoutQuery
class Update(base.TelegramObject):
"""
This object represents an incoming update.
At most one of the optional parameters can be present in any given update.
https://core.telegram.org/bots/api#update
"""
update_id: base.Integer = fields.Field()
message: Message = fields.Field(base=Message)
edited_message: Message = fields.Field(base=Message)
channel_post: Message = fields.Field(base=Message)
edited_channel_post: Message = fields.Field(base=Message)
inline_query: InlineQuery = fields.Field(base=InlineQuery)
chosen_inline_result: ChosenInlineResult = fields.Field(base=ChosenInlineResult)
callback_query: CallbackQuery = fields.Field(base=CallbackQuery)
shipping_query: ShippingQuery = fields.Field(base=ShippingQuery)
pre_checkout_query: PreCheckoutQuery = fields.Field(base=PreCheckoutQuery)

18
aiogram/types/user.py Normal file
View file

@ -0,0 +1,18 @@
from . import base
from . import fields
import typing
class User(base.TelegramObject):
"""
This object represents a Telegram user or bot.
https://core.telegram.org/bots/api#user
"""
id: base.Integer = fields.Field()
is_bot: base.Boolean = fields.Field()
first_name: base.String = fields.Field()
last_name: base.String = fields.Field()
username: base.String = fields.Field()
language_code: base.String = fields.Field()

View file

@ -0,0 +1,15 @@
from . import base
from . import fields
import typing
from .photo_size import PhotoSize
class UserProfilePhotos(base.TelegramObject):
"""
This object represent a user's profile pictures.
https://core.telegram.org/bots/api#userprofilephotos
"""
total_count: base.Integer = fields.Field()
photos: typing.List[typing.List[PhotoSize]] = fields.ListField(base=PhotoSize)

17
aiogram/types/venue.py Normal file
View file

@ -0,0 +1,17 @@
from . import base
from . import fields
import typing
from .location import Location
class Venue(base.TelegramObject):
"""
This object represents a venue.
https://core.telegram.org/bots/api#venue
"""
location: Location = fields.Field(base=Location)
title: base.String = fields.Field()
address: base.String = fields.Field()
foursquare_id: base.String = fields.Field()

20
aiogram/types/video.py Normal file
View file

@ -0,0 +1,20 @@
from . import base
from . import fields
import typing
from .photo_size import PhotoSize
class Video(base.TelegramObject):
"""
This object represents a video file.
https://core.telegram.org/bots/api#video
"""
file_id: base.String = fields.Field()
width: base.Integer = fields.Field()
height: base.Integer = fields.Field()
duration: base.Integer = fields.Field()
thumb: PhotoSize = fields.Field(base=PhotoSize)
mime_type: base.String = fields.Field()
file_size: base.Integer = fields.Field()

View file

@ -0,0 +1,18 @@
from . import base
from . import fields
import typing
from .photo_size import PhotoSize
class VideoNote(base.TelegramObject):
"""
This object represents a video message (available in Telegram apps as of v.4.0).
https://core.telegram.org/bots/api#videonote
"""
file_id: base.String = fields.Field()
length: base.Integer = fields.Field()
duration: base.Integer = fields.Field()
thumb: PhotoSize = fields.Field(base=PhotoSize)
file_size: base.Integer = fields.Field()

16
aiogram/types/voice.py Normal file
View file

@ -0,0 +1,16 @@
from . import base
from . import fields
import typing
class Voice(base.TelegramObject):
"""
This object represents a voice note.
https://core.telegram.org/bots/api#voice
"""
file_id: base.String = fields.Field()
duration: base.Integer = fields.Field()
mime_type: base.String = fields.Field()
file_size: base.Integer = fields.Field()

View file

@ -0,0 +1,22 @@
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
"""
url: base.String = fields.Field()
has_custom_certificate: base.Boolean = fields.Field()
pending_update_count: base.Integer = fields.Field()
last_error_date: base.Integer = fields.Field()
last_error_message: base.String = fields.Field()
max_connections: base.Integer = fields.Field()
allowed_updates: typing.List[base.String] = fields.ListField()