From 44c928ed47c86dc6989c29ca9d166a665673e563 Mon Sep 17 00:00:00 2001 From: Alex Root Junior Date: Fri, 19 May 2017 22:11:27 +0300 Subject: [PATCH] Add chat --- aiogram/api.py | 1 + aiogram/bot.py | 6 +++++ aiogram/types/chat.py | 51 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 aiogram/types/chat.py diff --git a/aiogram/api.py b/aiogram/api.py index 25b9fb58..5aae1c92 100644 --- a/aiogram/api.py +++ b/aiogram/api.py @@ -51,3 +51,4 @@ async def request(session, token, method, data=None): class ApiMethods: GET_ME = 'getMe' + GET_CHAT = 'getChat' diff --git a/aiogram/bot.py b/aiogram/bot.py index cbb3487b..af522a9b 100644 --- a/aiogram/bot.py +++ b/aiogram/bot.py @@ -5,6 +5,7 @@ import aiohttp from . import api from .api import ApiMethods +from .types.chat import Chat from .types.user import User @@ -47,3 +48,8 @@ class AIOGramBot: async def get_me(self) -> User: raw = await self.request(ApiMethods.GET_ME) return self._prepare_object(User.de_json(raw)) + + async def get_chat(self, chat_id) -> Chat: + payload = {'chat_id': chat_id} + raw = await self.request(ApiMethods.GET_CHAT, payload) + return self._prepare_object(Chat.de_json(raw)) diff --git a/aiogram/types/chat.py b/aiogram/types/chat.py new file mode 100644 index 00000000..69962f37 --- /dev/null +++ b/aiogram/types/chat.py @@ -0,0 +1,51 @@ +from aiogram.types import Deserializable + + +class Chat(Deserializable): + __slots__ = ('id', 'type', 'title', 'username', 'first_name', 'last_name', 'all_members_are_administrators') + + def __init__(self, data, id, type, title, username, first_name, last_name, all_members_are_administrators): + self.data = data + + self.id: int = id + self.type: str = type + self.title: str = title + self.username: str = username + self.first_name: str = first_name + self.last_name: str = last_name + self.all_members_are_administrators: bool = all_members_are_administrators + + @classmethod + def de_json(cls, data) -> 'Chat': + """ + id Integer Unique identifier for this chat. This number may be greater than 32 bits and some programming languages may have difficulty/silent defects in interpreting it. But it is smaller than 52 bits, so a signed 64 bit integer or double-precision float type are safe for storing this identifier. + type String Type of chat, can be either “private”, “group”, “supergroup” or “channel” + title String Optional. Title, for supergroups, channels and group chats + username String Optional. Username, for private chats, supergroups and channels if available + first_name String Optional. First name of the other party in a private chat + last_name String Optional. Last name of the other party in a private chat + all_members_are_administrators Boolean Optional. True if a group has ‘All Members Are Admins’ enabled. + :param data: + :return: + """ + data = cls.check_json(data) + + id: int = data.get('id') + type: str = data.get('type') + title: str = data.get('title') + username: str = data.get('username') + first_name: str = data.get('first_name') + last_name: str = data.get('last_name') + all_members_are_administrators: bool = data.get('all_members_are_administrators', False) + + return Chat(data, id, type, title, username, first_name, last_name, all_members_are_administrators) + + async def send_message(self, text): + self.bot.send_message(self.id, text) + + +class ChatType: + PRIVATE = 'private' + GROUP = 'group' + SUPER_GROUP = 'supergroup' + CHANNEL = 'channel'