Skip to content

Commit 0a82b23

Browse files
Add support for public polls, in an unstable way
Git-Original-Commit-Id: KurimuzonAkuma/pyrogram@046312a Co-authored-by: KurimuzonAkuma <[email protected]>
1 parent 12be393 commit 0a82b23

File tree

2 files changed

+89
-37
lines changed

2 files changed

+89
-37
lines changed

pyrogram/dispatcher.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@
5151
UpdateEditMessage, UpdateEditChannelMessage,
5252
UpdateDeleteMessages, UpdateDeleteChannelMessages,
5353
UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery,
54-
UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll,
54+
UpdateUserStatus, UpdateBotInlineQuery,
55+
UpdateMessagePoll, UpdateMessagePollVote,
5556
UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant, UpdateBotStopped,
5657
UpdateBotChatInviteRequester,
5758
UpdateBotMessageReaction,
@@ -76,7 +77,7 @@ class Dispatcher:
7677
CHAT_MEMBER_UPDATES = (UpdateChatParticipant, UpdateChannelParticipant, UpdateBotStopped,)
7778
USER_STATUS_UPDATES = (UpdateUserStatus,)
7879
BOT_INLINE_QUERY_UPDATES = (UpdateBotInlineQuery,)
79-
POLL_UPDATES = (UpdateMessagePoll,)
80+
POLL_UPDATES = (UpdateMessagePoll, UpdateMessagePollVote,)
8081
CHOSEN_INLINE_RESULT_UPDATES = (UpdateBotInlineSend,)
8182
CHAT_JOIN_REQUEST_UPDATES = (UpdateBotChatInviteRequester,)
8283
MESSAGE_BOT_NA_REACTION_UPDATES = (UpdateBotMessageReaction,)
@@ -146,7 +147,9 @@ async def inline_query_parser(update, users, chats):
146147

147148
async def poll_parser(update, users, chats):
148149
return (
149-
pyrogram.types.Poll._parse_update(self.client, update),
150+
pyrogram.types.Poll._parse_update(
151+
self.client, update, users, chats
152+
),
150153
PollHandler
151154
)
152155

pyrogram/types/messages_and_media/poll.py

Lines changed: 83 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ 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.
86+
8087
"""
8188

8289
def __init__(
@@ -97,7 +104,9 @@ def __init__(
97104
explanation: Optional[str] = None,
98105
explanation_entities: Optional[List["types.MessageEntity"]] = None,
99106
open_period: Optional[int] = None,
100-
close_date: Optional[datetime] = None
107+
close_date: Optional[datetime] = None,
108+
user: Optional["types.User"] = None,
109+
voter_chat: Optional["types.Chat"] = None,
101110
):
102111
super().__init__(client)
103112

@@ -116,6 +125,9 @@ def __init__(
116125
self.explanation_entities = explanation_entities
117126
self.open_period = open_period
118127
self.close_date = close_date
128+
# TODO: spit this out
129+
self.user = user
130+
self.voter_chat = voter_chat
119131

120132
@staticmethod
121133
def _parse(client, media_poll: Union["raw.types.MessageMediaPoll", "raw.types.UpdateMessagePoll"]) -> "Poll":
@@ -193,44 +205,81 @@ def _parse(client, media_poll: Union["raw.types.MessageMediaPoll", "raw.types.Up
193205
)
194206

195207
@staticmethod
196-
def _parse_update(client, update: "raw.types.UpdateMessagePoll"):
197-
if update.poll is not None:
198-
return Poll._parse(client, update)
199-
200-
# TODO: FIXME!
201-
results = update.results.results
202-
chosen_option_id = None
203-
correct_option_id = None
204-
options = []
205-
question = ""
206-
207-
for i, result in enumerate(results):
208-
if result.chosen:
209-
chosen_option_id = i
208+
def _parse_update(
209+
client,
210+
update: Union["raw.types.UpdateMessagePoll", "raw.types.UpdateMessagePollVote"],
211+
users: dict,
212+
chats: dict,
213+
):
214+
if isinstance(update, raw.types.UpdateMessagePoll):
215+
if update.poll is not None:
216+
return Poll._parse(client, update)
217+
218+
# TODO: FIXME!
219+
results = update.results.results
220+
chosen_option_id = None
221+
correct_option_id = None
222+
options = []
223+
question = ""
224+
225+
for i, result in enumerate(results):
226+
if result.chosen:
227+
chosen_option_id = i
210228

211-
if result.correct:
212-
correct_option_id = i
229+
if result.correct:
230+
correct_option_id = i
213231

214-
options.append(
215-
types.PollOption(
216-
text="",
217-
text_entities=[],
218-
voter_count=result.voters,
219-
data=result.option,
220-
client=client
232+
options.append(
233+
types.PollOption(
234+
text="",
235+
text_entities=[],
236+
voter_count=result.voters,
237+
data=result.option,
238+
client=client
239+
)
221240
)
241+
242+
return Poll(
243+
id=str(update.poll_id),
244+
question=question,
245+
options=options,
246+
total_voter_count=update.results.total_voters,
247+
is_closed=False,
248+
chosen_option_id=chosen_option_id,
249+
correct_option_id=correct_option_id,
250+
client=client
222251
)
223252

224-
return Poll(
225-
id=str(update.poll_id),
226-
question=question,
227-
options=options,
228-
total_voter_count=update.results.total_voters,
229-
is_closed=False,
230-
chosen_option_id=chosen_option_id,
231-
correct_option_id=correct_option_id,
232-
client=client
233-
)
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+
)
234283

235284
async def stop(
236285
self,

0 commit comments

Comments
 (0)