Skip to content

Commit bcff622

Browse files
committed
Follow-Up: 88f9040
Fixes: pyrogram/pyrogram#1009 Thanks: https://t.me/pyrogramchat/247933
1 parent 7c74e15 commit bcff622

File tree

6 files changed

+107
-45
lines changed

6 files changed

+107
-45
lines changed

compiler/docs/compiler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ def get_title_list(s: str) -> list:
512512
PollOption
513513
InputPollOption
514514
Poll
515+
PollAnswer
515516
Location
516517
Venue
517518
Gift

pyrogram/dispatcher.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ class Dispatcher:
7777
CHAT_MEMBER_UPDATES = (UpdateChatParticipant, UpdateChannelParticipant, UpdateBotStopped,)
7878
USER_STATUS_UPDATES = (UpdateUserStatus,)
7979
BOT_INLINE_QUERY_UPDATES = (UpdateBotInlineQuery,)
80-
POLL_UPDATES = (UpdateMessagePoll, UpdateMessagePollVote,)
80+
POLL_UPDATES = (UpdateMessagePoll,)
81+
POLL_ANSWER_UPDATES = (UpdateMessagePollVote,)
8182
CHOSEN_INLINE_RESULT_UPDATES = (UpdateBotInlineSend,)
8283
CHAT_JOIN_REQUEST_UPDATES = (UpdateBotChatInviteRequester,)
8384
MESSAGE_BOT_NA_REACTION_UPDATES = (UpdateBotMessageReaction,)
@@ -152,6 +153,14 @@ async def poll_parser(update, users, chats):
152153
),
153154
PollHandler
154155
)
156+
157+
async def poll_answer_parser(update, users, chats):
158+
return (
159+
pyrogram.types.PollAnswer._parse_update(
160+
self.client, update, users, chats
161+
),
162+
PollHandler
163+
)
155164

156165
async def chosen_inline_result_parser(update, users, chats):
157166
return (
@@ -217,6 +226,7 @@ async def story_parser(update, users, chats):
217226
Dispatcher.USER_STATUS_UPDATES: user_status_parser,
218227
Dispatcher.BOT_INLINE_QUERY_UPDATES: inline_query_parser,
219228
Dispatcher.POLL_UPDATES: poll_parser,
229+
Dispatcher.POLL_ANSWER_UPDATES: poll_answer_parser,
220230
Dispatcher.CHOSEN_INLINE_RESULT_UPDATES: chosen_inline_result_parser,
221231
Dispatcher.CHAT_MEMBER_UPDATES: chat_member_updated_parser,
222232
Dispatcher.CHAT_JOIN_REQUEST_UPDATES: chat_join_request_parser,

pyrogram/handlers/poll_handler.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,9 @@ class PollHandler(Handler):
4242
client (:obj:`~pyrogram.Client`):
4343
The Client itself, useful when you want to call other API methods inside the poll handler.
4444
45-
poll (:obj:`~pyrogram.types.Poll`):
46-
The received poll.
45+
poll (:obj:`~pyrogram.types.Poll` | :obj:`~pyrogram.types.PollAnswer`):
46+
New poll state. Bots receive only updates about manually stopped polls and polls, which are sent by the bot.
47+
A user changed their answer in a non-anonymous poll. Bots receive new votes only in polls that were sent by the bot itself.
4748
"""
4849

4950
def __init__(self, callback: Callable, filters=None):

pyrogram/types/messages_and_media/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
from .message_entity import MessageEntity
3030
from .photo import Photo
3131
from .poll import Poll
32+
from .poll_answer import PollAnswer
3233
from .poll_option import PollOption
3334
from .reaction import (
3435
Reaction,
@@ -105,6 +106,7 @@
105106
"Thumbnail",
106107
"StrippedThumbnail",
107108
"Poll",
109+
"PollAnswer",
108110
"PollOption",
109111
"SponsoredMessage",
110112
"Gift",

pyrogram/types/messages_and_media/poll.py

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,6 @@ class Poll(Object, Update):
7777
7878
close_date (:py:obj:`~datetime.datetime`, *optional*):
7979
Point in time when the poll will be automatically closed.
80-
81-
user (:obj:`~pyrogram.types.User`, *optional*):
82-
The user that changed the answer to the poll, if the voter isn't anonymous.
83-
84-
voter_chat (:obj:`~pyrogram.types.Chat`, *optional*):
85-
The chat that changed the answer to the poll, if the voter is anonymous.
8680
8781
"""
8882

@@ -105,8 +99,6 @@ def __init__(
10599
explanation_entities: Optional[List["types.MessageEntity"]] = None,
106100
open_period: Optional[int] = None,
107101
close_date: Optional[datetime] = None,
108-
user: Optional["types.User"] = None,
109-
voter_chat: Optional["types.Chat"] = None,
110102
):
111103
super().__init__(client)
112104

@@ -125,9 +117,6 @@ def __init__(
125117
self.explanation_entities = explanation_entities
126118
self.open_period = open_period
127119
self.close_date = close_date
128-
# TODO: spit this out
129-
self.user = user
130-
self.voter_chat = voter_chat
131120

132121
@staticmethod
133122
def _parse(client, media_poll: Union["raw.types.MessageMediaPoll", "raw.types.UpdateMessagePoll"]) -> "Poll":
@@ -250,37 +239,6 @@ def _parse_update(
250239
client=client
251240
)
252241

253-
if isinstance(update, raw.types.UpdateMessagePollVote):
254-
user = None
255-
voter_chat = None
256-
if isinstance(update.peer, raw.types.PeerUser):
257-
user = types.Chat._parse_user_chat(client, users[update.peer.user_id])
258-
259-
elif isinstance(update.peer, raw.types.PeerChat):
260-
voter_chat = types.Chat._parse_chat_chat(client, chats[update.peer.chat_id])
261-
262-
else:
263-
voter_chat = types.Chat._parse_channel_chat(client, chats[update.peer.channel_id])
264-
265-
return Poll(
266-
id=str(update.poll_id),
267-
question="",
268-
options=[
269-
types.PollOption(
270-
text="",
271-
text_entities=[],
272-
voter_count=None,
273-
data=option,
274-
client=client
275-
) for option in update.options
276-
],
277-
total_voter_count=None,
278-
is_closed=False,
279-
user=user,
280-
voter_chat=voter_chat,
281-
client=client
282-
)
283-
284242
async def stop(
285243
self,
286244
reply_markup: "types.InlineKeyboardMarkup" = None,
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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

Comments
 (0)