Bot API 3.3.

This commit is contained in:
Alex Root Junior 2017-08-23 22:51:27 +03:00
parent 6696c8e9ad
commit c8349112d0
3 changed files with 39 additions and 14 deletions

View file

@ -11,7 +11,9 @@ class Chat(Deserializable):
"""
def __init__(self, id, type, title, username, first_name, last_name, all_members_are_administrators, photo,
description, invite_link):
description, invite_link, pinned_message):
from .message import Message
self.id: int = id
self.type: str = type
self.title: str = title
@ -22,9 +24,12 @@ class Chat(Deserializable):
self.photo: ChatPhoto = photo
self.description: str = description
self.invite_link: str = invite_link
self.pinned_message: Message = pinned_message
@classmethod
def de_json(cls, raw_data) -> 'Chat':
from .message import Message
id: int = raw_data.get('id')
type: str = raw_data.get('type')
title: str = raw_data.get('title')
@ -35,9 +40,10 @@ class Chat(Deserializable):
photo = raw_data.get('photo')
description = raw_data.get('description')
invite_link = raw_data.get('invite_link')
pinned_message: Message = Message.deserialize(raw_data.get('pinned_message'))
return Chat(id, type, title, username, first_name, last_name, all_members_are_administrators, photo,
description, invite_link)
description, invite_link, pinned_message)
@property
def full_name(self):

View file

@ -29,11 +29,11 @@ class Message(Deserializable):
"""
def __init__(self, message_id, from_user, date, chat, forward_from, forward_from_chat, forward_from_message_id,
forward_date, reply_to_message, edit_date, text, entities, audio, document, game, photo, sticker,
video, voice, video_note, new_chat_members, caption, contact, location, venue, left_chat_member,
new_chat_title, new_chat_photo, delete_chat_photo, group_chat_created, supergroup_chat_created,
channel_chat_created, migrate_to_chat_id, migrate_from_chat_id, pinned_message, invoice,
successful_payment, content_type):
forward_signature, forward_date, reply_to_message, edit_date, author_signature, text, entities, audio,
document, game, photo, sticker, video, voice, video_note, new_chat_members, caption, contact, location,
venue, left_chat_member, new_chat_title, new_chat_photo, delete_chat_photo, group_chat_created,
supergroup_chat_created, channel_chat_created, migrate_to_chat_id, migrate_from_chat_id,
pinned_message, invoice, successful_payment, content_type):
self.message_id: int = message_id
self.from_user: User = from_user
self.date: datetime.datetime = date
@ -41,9 +41,11 @@ class Message(Deserializable):
self.forward_from: User = forward_from
self.forward_from_chat: Chat = forward_from_chat
self.forward_from_message_id: int = forward_from_message_id
self.forward_signature: str = forward_signature
self.forward_date: datetime.datetime = forward_date
self.reply_to_message: Message = reply_to_message
self.edit_date: datetime.datetime = edit_date
self.author_signature: str = author_signature
self.text: str = text
self.entities = entities
self.audio = audio
@ -83,9 +85,11 @@ class Message(Deserializable):
forward_from = User.deserialize(raw_data.get('forward_from', {}))
forward_from_chat = Chat.deserialize(raw_data.get('forward_from_chat', {}))
forward_from_message_id = raw_data.get('forward_from_message_id')
forward_signature = raw_data.get('forward_signature')
forward_date = cls._parse_date(raw_data.get('forward_date', 0))
reply_to_message = Message.deserialize(raw_data.get('reply_to_message', {}))
edit_date = cls._parse_date(raw_data.get('edit_date', 0))
author_signature = raw_data.get('author_signature')
text = raw_data.get('text')
entities = MessageEntity.deserialize(raw_data.get('entities'))
audio = Audio.deserialize(raw_data.get('audio'))
@ -142,11 +146,11 @@ class Message(Deserializable):
content_type = ContentType.UNKNOWN[0]
return Message(message_id, from_user, date, chat, forward_from, forward_from_chat, forward_from_message_id,
forward_date, reply_to_message, edit_date, text, entities, audio, document, game, photo, sticker,
video, voice, video_note, new_chat_members, caption, contact, location, venue, left_chat_member,
new_chat_title, new_chat_photo, delete_chat_photo, group_chat_created, supergroup_chat_created,
channel_chat_created, migrate_to_chat_id, migrate_from_chat_id, pinned_message, invoice,
successful_payment, content_type)
forward_signature, forward_date, reply_to_message, edit_date, author_signature, text, entities,
audio, document, game, photo, sticker, video, voice, video_note, new_chat_members, caption,
contact, location, venue, left_chat_member, new_chat_title, new_chat_photo, delete_chat_photo,
group_chat_created, supergroup_chat_created, channel_chat_created, migrate_to_chat_id,
migrate_from_chat_id, pinned_message, invoice, successful_payment, content_type)
def is_command(self):
"""

View file

@ -1,3 +1,5 @@
from ..utils.markdown import link, hlink
try:
import babel
except ImportError:
@ -13,8 +15,9 @@ class User(Deserializable):
https://core.telegram.org/bots/api#user
"""
def __init__(self, id, first_name, last_name, username, language_code):
def __init__(self, id, is_bot, first_name, last_name, username, language_code):
self.id: int = id
self.is_bot: bool = is_bot
self.first_name: str = first_name
self.last_name: str = last_name
self.username: str = username
@ -23,12 +26,13 @@ class User(Deserializable):
@classmethod
def de_json(cls, raw_data: str or dict) -> 'User':
id = raw_data.get('id')
is_bot = raw_data.get('is_bot')
first_name = raw_data.get('first_name')
last_name = raw_data.get('last_name')
username = raw_data.get('username')
language_code = raw_data.get('language_code')
return User(id, first_name, last_name, username, language_code)
return User(id, is_bot, first_name, last_name, username, language_code)
@property
def full_name(self):
@ -69,5 +73,16 @@ class User(Deserializable):
setattr(self, '_locale', babel.core.Locale.parse(self.language_code, sep='-'))
return getattr(self, '_locale')
@property
def url(self):
return f"tg://user?id={self.id}"
def get_mention(self, name=None, as_html=False):
if name is None:
name = self.mention
if as_html:
return hlink(name, self.url)
return link(name, self.url)
async def get_user_profile_photos(self, offset=None, limit=None):
return await self.bot.get_user_profile_photos(self.id, offset, limit)