|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-2019 Dan Tès <https://github.com/delivrance> |
| 3 | +# |
| 4 | +# This file is part of Pyrogram. |
| 5 | +# |
| 6 | +# Pyrogram is free software: you can redistribute it and/or modify |
| 7 | +# it under the terms of the GNU Lesser General Public License as published |
| 8 | +# by the Free Software Foundation, either version 3 of the License, or |
| 9 | +# (at your option) any later version. |
| 10 | +# |
| 11 | +# Pyrogram is distributed in the hope that it will be useful, |
| 12 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | +# GNU Lesser General Public License for more details. |
| 15 | +# |
| 16 | +# You should have received a copy of the GNU Lesser General Public License |
| 17 | +# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + |
| 19 | +import pyrogram |
| 20 | +from pyrogram.api import types |
| 21 | +from .animation import Animation |
| 22 | +from .photo import Photo |
| 23 | +from ..pyrogram_type import PyrogramType |
| 24 | + |
| 25 | + |
| 26 | +class Game(PyrogramType): |
| 27 | + """This object represents a game. |
| 28 | + Use BotFather to create and edit games, their short names will act as unique identifiers. |
| 29 | +
|
| 30 | + Args: |
| 31 | + id (``int``): |
| 32 | + Unique identifier of the game. |
| 33 | +
|
| 34 | + title (``str``): |
| 35 | + Title of the game. |
| 36 | +
|
| 37 | + short_name (``str``): |
| 38 | + Unique short name of the game. |
| 39 | +
|
| 40 | + description (``str``): |
| 41 | + Description of the game. |
| 42 | +
|
| 43 | + photo (:obj:`Photo <pyrogram.Photo>`): |
| 44 | + Photo that will be displayed in the game message in chats. |
| 45 | +
|
| 46 | + animation (:obj:`Animation <pyrogram.Animation>`, *optional*): |
| 47 | + Animation that will be displayed in the game message in chats. |
| 48 | + Upload via BotFather. |
| 49 | + """ |
| 50 | + |
| 51 | + def __init__(self, |
| 52 | + *, |
| 53 | + client: "pyrogram.client.ext.BaseClient", |
| 54 | + id: int, |
| 55 | + title: str, |
| 56 | + short_name: str, |
| 57 | + description: str, |
| 58 | + photo: Photo, |
| 59 | + animation: Animation = None): |
| 60 | + super().__init__(client) |
| 61 | + |
| 62 | + self.id = id |
| 63 | + self.title = title |
| 64 | + self.short_name = short_name |
| 65 | + self.description = description |
| 66 | + self.photo = photo |
| 67 | + self.animation = animation |
| 68 | + |
| 69 | + @staticmethod |
| 70 | + def _parse(client, message: types.Message) -> "Game": |
| 71 | + game = message.media.game # type: types.Game |
| 72 | + animation = None |
| 73 | + |
| 74 | + if game.document: |
| 75 | + attributes = {type(i): i for i in game.document.attributes} |
| 76 | + |
| 77 | + file_name = getattr( |
| 78 | + attributes.get( |
| 79 | + types.DocumentAttributeFilename, None |
| 80 | + ), "file_name", None |
| 81 | + ) |
| 82 | + |
| 83 | + animation = Animation._parse( |
| 84 | + client, |
| 85 | + game.document, |
| 86 | + attributes.get(types.DocumentAttributeVideo, None), |
| 87 | + file_name |
| 88 | + ) |
| 89 | + |
| 90 | + return Game( |
| 91 | + id=game.id, |
| 92 | + title=game.title, |
| 93 | + short_name=game.short_name, |
| 94 | + description=game.description, |
| 95 | + photo=Photo._parse(client, game.photo), |
| 96 | + animation=animation, |
| 97 | + client=client |
| 98 | + ) |
0 commit comments