|
| 1 | +# Pyrogram - Telegram MTProto API Client Library for Python |
| 2 | +# Copyright (C) 2017-present Dan <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 | +from typing import List, Optional |
| 20 | + |
| 21 | +import pyrogram |
| 22 | +from pyrogram import enums, raw, types, utils |
| 23 | +from ..object import Object |
| 24 | +from ..update import Update |
| 25 | +from .message import Str |
| 26 | + |
| 27 | + |
| 28 | +class PollAnswer(Object, Update): |
| 29 | + """This object represents an answer of a user in a non-anonymous poll. |
| 30 | +
|
| 31 | + Parameters: |
| 32 | + poll_id (``str``): |
| 33 | + Unique poll identifier. |
| 34 | +
|
| 35 | + voter_chat (:obj:`~pyrogram.types.Chat`, *optional*): |
| 36 | + The chat that changed the answer to the poll, if the voter is anonymous. |
| 37 | +
|
| 38 | + user (:obj:`~pyrogram.types.User`, *optional*): |
| 39 | + The user that changed the answer to the poll, if the voter isn't anonymous. |
| 40 | +
|
| 41 | + option_ids (List of ``int``): |
| 42 | + 0-based identifiers of chosen answer options. May be empty if the vote was retracted. |
| 43 | +
|
| 44 | + """ |
| 45 | + |
| 46 | + def __init__( |
| 47 | + self, |
| 48 | + *, |
| 49 | + client: "pyrogram.Client" = None, |
| 50 | + poll_id: str, |
| 51 | + option_ids: List[int], |
| 52 | + user: Optional["types.User"] = None, |
| 53 | + voter_chat: Optional["types.Chat"] = None, |
| 54 | + ): |
| 55 | + super().__init__(client) |
| 56 | + |
| 57 | + self.poll_id = poll_id |
| 58 | + self.option_ids = option_ids |
| 59 | + self.user = user |
| 60 | + self.voter_chat = voter_chat |
| 61 | + |
| 62 | + @staticmethod |
| 63 | + def _parse_update( |
| 64 | + client, |
| 65 | + update: "raw.types.UpdateMessagePollVote", |
| 66 | + users: dict, |
| 67 | + chats: dict, |
| 68 | + ): |
| 69 | + if isinstance(update, raw.types.UpdateMessagePollVote): |
| 70 | + user = None |
| 71 | + voter_chat = None |
| 72 | + if isinstance(update.peer, raw.types.PeerUser): |
| 73 | + user = types.Chat._parse_user_chat(client, users[update.peer.user_id]) |
| 74 | + |
| 75 | + elif isinstance(update.peer, raw.types.PeerChat): |
| 76 | + voter_chat = types.Chat._parse_chat_chat(client, chats[update.peer.chat_id]) |
| 77 | + |
| 78 | + else: |
| 79 | + voter_chat = types.Chat._parse_channel_chat(client, chats[update.peer.channel_id]) |
| 80 | + |
| 81 | + return PollAnswer( |
| 82 | + poll_id=str(update.poll_id), |
| 83 | + option_ids=[ |
| 84 | + "{:0>2x}".format(option[0]) |
| 85 | + for option in update.options |
| 86 | + ], |
| 87 | + user=user, |
| 88 | + voter_chat=voter_chat, |
| 89 | + client=client |
| 90 | + ) |
0 commit comments