Merge remote-tracking branch 'origin/master' into dev

# Conflicts:
#	aiogram/bot/base.py
This commit is contained in:
Alex Root Junior 2017-07-22 19:23:48 +03:00
commit 07835b504c
16 changed files with 360 additions and 55 deletions

View file

@ -3,6 +3,7 @@ from .audio import Audio
from .callback_query import CallbackQuery
from .chat import Chat, ChatType, ChatActions
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
@ -54,6 +55,7 @@ __all__ = [
'ChatActions',
'ChatMember',
'ChatMemberStatus',
'ChatPhoto',
'ChatType',
'ChosenInlineResult',
'Contact',

View file

@ -61,7 +61,7 @@ class Deserializable:
return result
@property
def bot(self):
def bot(self) -> 'Bot':
"""
Bot instance
"""

View file

@ -1,4 +1,5 @@
from .base import Deserializable
from .chat_photo import ChatPhoto
class Chat(Deserializable):
@ -8,7 +9,8 @@ class Chat(Deserializable):
https://core.telegram.org/bots/api#chat
"""
def __init__(self, id, type, title, username, first_name, last_name, all_members_are_administrators):
def __init__(self, id, type, title, username, first_name, last_name, all_members_are_administrators, photo,
description, invite_link):
self.id: int = id
self.type: str = type
self.title: str = title
@ -16,6 +18,9 @@ class Chat(Deserializable):
self.first_name: str = first_name
self.last_name: str = last_name
self.all_members_are_administrators: bool = all_members_are_administrators
self.photo: ChatPhoto = photo
self.description: str = description
self.invite_link: str = invite_link
@classmethod
def de_json(cls, raw_data) -> 'Chat':
@ -28,8 +33,12 @@ class Chat(Deserializable):
first_name: str = raw_data.get('first_name')
last_name: str = raw_data.get('last_name')
all_members_are_administrators: bool = raw_data.get('all_members_are_administrators', False)
photo = raw_data.get('photo')
description = raw_data.get('description')
invite_link = raw_data.get('invite_link')
return Chat(id, type, title, username, first_name, last_name, all_members_are_administrators)
return Chat(id, type, title, username, first_name, last_name, all_members_are_administrators, photo,
description, invite_link)
@property
def full_name(self):
@ -51,6 +60,39 @@ class Chat(Deserializable):
return self.full_name
return None
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:
"""

View file

@ -1,3 +1,5 @@
import datetime
from .base import Deserializable
from .user import User
@ -8,10 +10,34 @@ class ChatMember(Deserializable):
https://core.telegram.org/bots/api#chatmember
"""
def __init__(self, user, status):
def __init__(self, user, status, until_date, can_be_edited, can_change_info, can_post_messages,
can_edit_messages, can_delete_messages, can_invite_users, can_restrict_members,
can_pin_messages, can_promote_members, can_send_messages, can_send_media_messages,
can_send_other_messages, can_add_web_page_previews
):
self.user: User = user
self.status: str = status
self.until_date: datetime.datetime = until_date
self.can_be_edited: bool = can_be_edited
self.can_change_info: bool = can_change_info
self.can_post_messages: bool = can_post_messages
self.can_edit_messages: bool = can_edit_messages
self.can_delete_messages: bool = can_delete_messages
self.can_invite_users: bool = can_invite_users
self.can_restrict_members: bool = can_restrict_members
self.can_pin_messages: bool = can_pin_messages
self.can_promote_members: bool = can_promote_members
self.can_send_messages: bool = can_send_messages
self.can_send_media_messages: bool = can_send_media_messages
self.can_send_other_messages: bool = can_send_other_messages
self.can_add_web_page_previews: bool = can_add_web_page_previews
@classmethod
def _parse_date(cls, unix_time):
return datetime.datetime.fromtimestamp(unix_time)
@classmethod
def de_json(cls, raw_data):
raw_data = cls.check_json(raw_data)
@ -19,7 +45,26 @@ class ChatMember(Deserializable):
user = User.deserialize(raw_data.get('user'))
status = raw_data.get('status')
return ChatMember(user, status)
until_date = cls._parse_date(raw_data.get('until_date'))
can_be_edited = raw_data.get('can_be_edited')
can_change_info = raw_data.get('can_change_info')
can_post_messages = raw_data.get('can_post_messages')
can_edit_messages = raw_data.get('can_edit_messages')
can_delete_messages = raw_data.get('can_delete_messages')
can_invite_users = raw_data.get('can_invite_users')
can_restrict_members = raw_data.get('can_restrict_members')
can_pin_messages = raw_data.get('can_pin_messages')
can_promote_members = raw_data.get('can_promote_members')
can_send_messages = raw_data.get('can_send_messages')
can_send_media_messages = raw_data.get('can_send_media_messages')
can_send_other_messages = raw_data.get('can_send_other_messages')
can_add_web_page_previews = raw_data.get('can_add_web_page_previews')
return ChatMember(user, status, until_date, can_be_edited, can_change_info, can_post_messages,
can_edit_messages, can_delete_messages, can_invite_users, can_restrict_members,
can_pin_messages, can_promote_members, can_send_messages, can_send_media_messages,
can_send_other_messages, can_add_web_page_previews
)
class ChatMemberStatus:

View file

@ -0,0 +1,22 @@
from .base import Deserializable
class ChatPhoto(Deserializable):
"""
This object represents a chat photo.
https://core.telegram.org/bots/api#chatphoto
"""
def __init__(self, small_file_id, big_file_id):
self.small_file_id: str = small_file_id
self.big_file_id: str = big_file_id
@classmethod
def de_json(cls, raw_data):
raw_data = cls.check_json(raw_data)
small_file_id = raw_data.get('small_file_id')
big_file_id = raw_data.get('big_file_id')
return ChatPhoto(small_file_id, big_file_id)

View file

@ -26,3 +26,6 @@ class File(Deserializable):
file_path = raw_data.get('file_path')
return File(file_id, file_size, file_path)
async def download(self, destination=None, timeout=30, chunk_size=65536, seek=True):
return await self.bot.download_file(self.file_path, destination, timeout, chunk_size, seek)

View file

@ -15,7 +15,7 @@ class InputMessageContent(Serializable):
"""
def to_json(self):
return {k: v.to_json() if hasattr(v, 'to_json') else v for k, v in self.__dict__.items() if
not k.startswith('_')}
v is not None and not k.startswith('_')}
class InlineQueryResult(InputMessageContent):
@ -74,10 +74,10 @@ class InlineQueryResultArticle(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultarticle
"""
def __init__(self, type: str, id: str, title: str, input_message_content: InputMessageContent,
def __init__(self, id: str, title: str, input_message_content: InputMessageContent,
reply_markup: InlineKeyboardMarkup = None, url: str = None, hide_url: bool = None,
description: str = None, thumb_url: str = None, thumb_width: int = None, thumb_height: int = None):
self.type: str = type
self.type = 'article'
self.id: str = id
self.title: str = title
self.input_message_content: InputMessageContent = input_message_content
@ -99,10 +99,10 @@ class InlineQueryResultPhoto(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultphoto
"""
def __init__(self, type: str, id: str, photo_url: str, thumb_url: str, photo_width: int = None,
def __init__(self, id: str, photo_url: str, thumb_url: str, photo_width: int = None,
photo_height: int = None, title: str = None, description: str = None, caption: str = None,
reply_markup: InlineKeyboardMarkup = None, input_message_content: InputMessageContent = None):
self.type: str = type
self.type = 'photo'
self.id: str = id
self.photo_url: str = photo_url
self.thumb_url: str = thumb_url
@ -126,10 +126,10 @@ class InlineQueryResultGif(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultgif
"""
def __init__(self, type: str, id: str, gif_url: str, thumb_url: str, gif_width: int = None, gif_height: int = None,
def __init__(self, id: str, gif_url: str, thumb_url: str, gif_width: int = None, gif_height: int = None,
gif_duration: int = None, title: str = None, caption: str = None,
reply_markup: InlineKeyboardMarkup = None, input_message_content: InputMessageContent = None):
self.type: str = type
self.type = 'gif'
self.id: str = id
self.gif_url: str = gif_url
self.gif_width: int = gif_width
@ -153,10 +153,10 @@ class InlineQueryResultMpeg4Gif(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultmpeg4gif
"""
def __init__(self, type: str, id: str, mpeg4_url: str, thumb_url: str, mpeg4_width: int = None,
def __init__(self, id: str, mpeg4_url: str, thumb_url: str, mpeg4_width: int = None,
mpeg4_height: int = None, mpeg4_duration: int = None, title: str = None, caption: str = None,
reply_markup: InlineKeyboardMarkup = None, input_message_content: InputMessageContent = None):
self.type: str = type
self.type = 'mpeg4_gif'
self.id: str = id
self.mpeg4_url: str = mpeg4_url
self.mpeg4_width: int = mpeg4_width
@ -178,11 +178,11 @@ class InlineQueryResultVideo(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultvideo
"""
def __init__(self, type: str, id: str, video_url: str, mime_type: str, thumb_url: str, title: str,
def __init__(self, id: str, video_url: str, mime_type: str, thumb_url: str, title: str,
caption: str = None, video_width: int = None, video_height: int = None, video_duration: int = None,
description: str = None, reply_markup: InlineKeyboardMarkup = None,
input_message_content: InputMessageContent = None):
self.type: str = type
self.type = 'video'
self.id: str = id
self.video_url: str = video_url
self.mime_type: str = mime_type
@ -204,10 +204,10 @@ class InlineQueryResultAudio(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultaudio
"""
def __init__(self, type: str, id: str, audio_url: str, title: str, caption: str = None, performer: str = None,
def __init__(self, id: str, audio_url: str, title: str, caption: str = None, performer: str = None,
audio_duration: int = None, reply_markup: InlineKeyboardMarkup = None,
input_message_content: InputMessageContent = None):
self.type: str = type
self.type = 'audio'
self.id: str = id
self.audio_url: str = audio_url
self.title: str = title
@ -229,9 +229,9 @@ class InlineQueryResultVoice(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultvoice
"""
def __init__(self, type: str, id: str, voice_url: str, title: str, caption: str = None, voice_duration: int = None,
def __init__(self, id: str, voice_url: str, title: str, caption: str = None, voice_duration: int = None,
reply_markup: InlineKeyboardMarkup = None, input_message_content: InputMessageContent = None):
self.type: str = type
self.type = 'voice'
self.id: str = id
self.voice_url: str = voice_url
self.title: str = title
@ -250,11 +250,11 @@ class InlineQueryResultDocument(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultdocument
"""
def __init__(self, type: str, id: str, title: str, document_url: str, mime_type: str, caption: str = None,
def __init__(self, id: str, title: str, document_url: str, mime_type: str, caption: str = None,
description: str = None, reply_markup: InlineKeyboardMarkup = None,
input_message_content: InputMessageContent = None, thumb_url: str = None, thumb_width: int = None,
thumb_height: int = None):
self.type: str = type
self.type = 'document'
self.id: str = id
self.title: str = title
self.caption: str = caption
@ -276,10 +276,10 @@ class InlineQueryResultLocation(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultlocation
"""
def __init__(self, type: str, id: str, latitude: float, longitude: float, title: str,
def __init__(self, id: str, latitude: float, longitude: float, title: str,
reply_markup: InlineKeyboardMarkup = None, input_message_content: InputMessageContent = None,
thumb_url: str = None, thumb_width: int = None, thumb_height: int = None):
self.type: str = type
self.type = 'location'
self.id: str = id
self.latitude: float = latitude
self.longitude: float = longitude
@ -299,11 +299,11 @@ class InlineQueryResultVenue(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultvenue
"""
def __init__(self, type: str, id: str, latitude: float, longitude: float, title: str, address: str,
def __init__(self, id: str, latitude: float, longitude: float, title: str, address: str,
foursquare_id: str = None, reply_markup: InlineKeyboardMarkup = None,
input_message_content: InputMessageContent = None, thumb_url: str = None, thumb_width: int = None,
thumb_height: int = None):
self.type: str = type
self.type = 'venue'
self.id: str = id
self.latitude: float = latitude
self.longitude: float = longitude
@ -328,10 +328,10 @@ class InlineQueryResultContact(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultcontact
"""
def __init__(self, type: str, id: str, phone_number: str, first_name: str, last_name: str = None,
def __init__(self, id: str, phone_number: str, first_name: str, last_name: str = None,
reply_markup: InlineKeyboardMarkup = None, input_message_content: InputMessageContent = None,
thumb_url: str = None, thumb_width: int = None, thumb_height: int = None):
self.type: str = type
self.type = 'contact'
self.id: str = id
self.phone_number: str = phone_number
self.first_name: str = first_name
@ -349,8 +349,8 @@ class InlineQueryResultGame(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultgame
"""
def __init__(self, type: str, id: str, game_short_name: str, reply_markup: InlineKeyboardMarkup = None):
self.type: str = type
def __init__(self, id: str, game_short_name: str, reply_markup: InlineKeyboardMarkup = None):
self.type = 'game'
self.id: str = id
self.game_short_name: str = game_short_name
self.reply_markup: InlineKeyboardMarkup = reply_markup
@ -367,10 +367,10 @@ class InlineQueryResultCachedPhoto(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultcachedphoto
"""
def __init__(self, type: str, id: str, photo_file_id: str, title: str = None, description: str = None,
def __init__(self, id: str, photo_file_id: str, title: str = None, description: str = None,
caption: str = None, reply_markup: InlineKeyboardMarkup = None,
input_message_content: InputMessageContent = None):
self.type: str = type
self.type = 'photo'
self.id: str = id
self.photo_file_id: str = photo_file_id
self.title: str = title
@ -391,9 +391,9 @@ class InlineQueryResultCachedGif(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultcachedgif
"""
def __init__(self, type: str, id: str, gif_file_id: str, title: str = None, caption: str = None,
def __init__(self, id: str, gif_file_id: str, title: str = None, caption: str = None,
reply_markup: InlineKeyboardMarkup = None, input_message_content: InputMessageContent = None):
self.type: str = type
self.type = 'gif'
self.id: str = id
self.gif_file_id: str = gif_file_id
self.title: str = title
@ -413,9 +413,9 @@ class InlineQueryResultCachedMpeg4Gif(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultcachedmpeg4gif
"""
def __init__(self, type: str, id: str, mpeg4_file_id: str, title: str = None, caption: str = None,
def __init__(self, id: str, mpeg4_file_id: str, title: str = None, caption: str = None,
reply_markup: InlineKeyboardMarkup = None, input_message_content: InputMessageContent = None):
self.type: str = type
self.type = 'mpeg4_gif'
self.id: str = id
self.mpeg4_file_id: str = mpeg4_file_id
self.title: str = title
@ -435,9 +435,9 @@ class InlineQueryResultCachedSticker(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultcachedsticker
"""
def __init__(self, type: str, id: str, sticker_file_id: str, reply_markup: InlineKeyboardMarkup = None,
def __init__(self, id: str, sticker_file_id: str, reply_markup: InlineKeyboardMarkup = None,
input_message_content: InputMessageContent = None):
self.type: str = type
self.type = 'sticker'
self.id: str = id
self.sticker_file_id: str = sticker_file_id
self.reply_markup: InlineKeyboardMarkup = reply_markup
@ -455,10 +455,10 @@ class InlineQueryResultCachedDocument(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultcacheddocument
"""
def __init__(self, type: str, id: str, title: str, document_file_id: str, description: str = None,
def __init__(self, id: str, title: str, document_file_id: str, description: str = None,
caption: str = None, reply_markup: InlineKeyboardMarkup = None,
input_message_content: InputMessageContent = None):
self.type: str = type
self.type = 'document'
self.id: str = id
self.title: str = title
self.document_file_id: str = document_file_id
@ -477,9 +477,9 @@ class InlineQueryResultCachedVideo(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultcachedvideo
"""
def __init__(self, type: str, id: str, video_file_id: str, title: str, description: str = None, caption: str = None,
def __init__(self, id: str, video_file_id: str, title: str, description: str = None, caption: str = None,
reply_markup: InlineKeyboardMarkup = None, input_message_content: InputMessageContent = None):
self.type: str = type
self.type = 'video'
self.id: str = id
self.video_file_id: str = video_file_id
self.title: str = title
@ -500,9 +500,9 @@ class InlineQueryResultCachedVoice(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultcachedvoice
"""
def __init__(self, type: str, id: str, voice_file_id: str, title: str, caption: str = None,
def __init__(self, id: str, voice_file_id: str, title: str, caption: str = None,
reply_markup: InlineKeyboardMarkup = None, input_message_content: InputMessageContent = None):
self.type: str = type
self.type = 'voice'
self.id: str = id
self.voice_file_id: str = voice_file_id
self.title: str = title
@ -521,9 +521,9 @@ class InlineQueryResultCachedAudio(InlineQueryResult):
https://core.telegram.org/bots/api#inlinequeryresultcachedaudio
"""
def __init__(self, type: str, id: str, audio_file_id: str, caption: str = None,
def __init__(self, id: str, audio_file_id: str, caption: str = None,
reply_markup: InlineKeyboardMarkup = None, input_message_content: InputMessageContent = None):
self.type: str = type
self.type = 'audio'
self.id: str = id
self.audio_file_id: str = audio_file_id
self.caption: str = caption

View file

@ -216,6 +216,9 @@ class Message(Deserializable):
return False
return True
async def pin(self, disable_notification: bool = False):
return await self.chat.pin_message(self.message_id, disable_notification)
class ContentType:
"""

View file

@ -69,3 +69,6 @@ class User(Deserializable):
if not hasattr(self, '_locale'):
setattr(self, '_locale', babel.core.Locale.parse(self.language_code, sep='-'))
return getattr(self, '_locale')
async def get_user_profile_photos(self, offset=None, limit=None):
return await self.bot.get_user_profile_photos(self.id, offset, limit)