Skip to content

Commit 046312a

Browse files
Add support for public polls
1 parent 055d125 commit 046312a

File tree

2 files changed

+56
-34
lines changed

2 files changed

+56
-34
lines changed

pyrogram/dispatcher.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
from collections import OrderedDict
2323

2424
import pyrogram
25-
from pyrogram import errors
2625
from pyrogram import utils
27-
from pyrogram import raw
2826
from pyrogram.handlers import (
2927
CallbackQueryHandler, MessageHandler, EditedMessageHandler, DeletedMessagesHandler,
3028
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler, PreCheckoutQueryHandler,
@@ -41,7 +39,7 @@
4139
UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant,
4240
UpdateBotChatInviteRequester, UpdateStory, UpdateBotShippingQuery, UpdateBotMessageReaction,
4341
UpdateBotMessageReactions, UpdateBotChatBoost, UpdateBusinessBotCallbackQuery,
44-
UpdateBotPurchasedPaidMedia
42+
UpdateBotPurchasedPaidMedia, UpdateMessagePollVote
4543
)
4644

4745
log = logging.getLogger(__name__)
@@ -55,7 +53,7 @@ class Dispatcher:
5553
CHAT_MEMBER_UPDATES = (UpdateChatParticipant, UpdateChannelParticipant)
5654
USER_STATUS_UPDATES = (UpdateUserStatus,)
5755
BOT_INLINE_QUERY_UPDATES = (UpdateBotInlineQuery,)
58-
POLL_UPDATES = (UpdateMessagePoll,)
56+
POLL_UPDATES = (UpdateMessagePoll, UpdateMessagePollVote)
5957
CHOSEN_INLINE_RESULT_UPDATES = (UpdateBotInlineSend,)
6058
CHAT_JOIN_REQUEST_UPDATES = (UpdateBotChatInviteRequester,)
6159
NEW_STORY_UPDATES = (UpdateStory,)
@@ -128,7 +126,7 @@ async def inline_query_parser(update, users, chats):
128126

129127
async def poll_parser(update, users, chats):
130128
return (
131-
pyrogram.types.Poll._parse_update(self.client, update),
129+
pyrogram.types.Poll._parse_update(self.client, update, users),
132130
PollHandler
133131
)
134132

pyrogram/types/messages_and_media/poll.py

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ 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+
voter (:obj:`~pyrogram.types.User`, *optional*):
82+
The user that voted in the poll.
8083
"""
8184

8285
def __init__(
@@ -97,7 +100,8 @@ def __init__(
97100
explanation: Optional[str] = None,
98101
explanation_entities: Optional[List["types.MessageEntity"]] = None,
99102
open_period: Optional[int] = None,
100-
close_date: Optional[datetime] = None
103+
close_date: Optional[datetime] = None,
104+
voter: Optional["types.User"] = None
101105
):
102106
super().__init__(client)
103107

@@ -116,6 +120,7 @@ def __init__(
116120
self.explanation_entities = explanation_entities
117121
self.open_period = open_period
118122
self.close_date = close_date
123+
self.voter = voter
119124

120125
@staticmethod
121126
def _parse(client, media_poll: Union["raw.types.MessageMediaPoll", "raw.types.UpdateMessagePoll"]) -> "Poll":
@@ -195,38 +200,57 @@ def _parse(client, media_poll: Union["raw.types.MessageMediaPoll", "raw.types.Up
195200
)
196201

197202
@staticmethod
198-
def _parse_update(client, update: "raw.types.UpdateMessagePoll"):
199-
if update.poll is not None:
200-
return Poll._parse(client, update)
203+
def _parse_update(client, update: Union["raw.types.UpdateMessagePoll", "raw.types.UpdateMessagePollVote"], users: dict):
204+
if isinstance(update, raw.types.UpdateMessagePoll):
205+
if update.poll is not None:
206+
return Poll._parse(client, update)
201207

202-
results = update.results.results
203-
chosen_option_id = None
204-
correct_option_id = None
205-
options = []
208+
results = update.results.results
209+
chosen_option_id = None
210+
correct_option_id = None
211+
options = []
206212

207-
for i, result in enumerate(results):
208-
if result.chosen:
209-
chosen_option_id = i
213+
for i, result in enumerate(results):
214+
if result.chosen:
215+
chosen_option_id = i
210216

211-
if result.correct:
212-
correct_option_id = i
217+
if result.correct:
218+
correct_option_id = i
213219

214-
options.append(
215-
types.PollOption(
216-
text="",
217-
voter_count=result.voters,
218-
data=result.option,
219-
client=client
220+
options.append(
221+
types.PollOption(
222+
text="",
223+
voter_count=result.voters,
224+
data=result.option,
225+
client=client
226+
)
220227
)
228+
229+
return Poll(
230+
id=str(update.poll_id),
231+
question="",
232+
options=options,
233+
total_voter_count=update.results.total_voters,
234+
is_closed=False,
235+
chosen_option_id=chosen_option_id,
236+
correct_option_id=correct_option_id,
237+
client=client
221238
)
222239

223-
return Poll(
224-
id=str(update.poll_id),
225-
question="",
226-
options=options,
227-
total_voter_count=update.results.total_voters,
228-
is_closed=False,
229-
chosen_option_id=chosen_option_id,
230-
correct_option_id=correct_option_id,
231-
client=client
232-
)
240+
if isinstance(update, raw.types.UpdateMessagePollVote):
241+
return Poll(
242+
id=str(update.poll_id),
243+
question="",
244+
options=[
245+
types.PollOption(
246+
text="",
247+
voter_count=None,
248+
data=option,
249+
client=client
250+
) for option in update.options
251+
],
252+
total_voter_count=None,
253+
is_closed=False,
254+
voter=types.User._parse(client, users[update.peer.user_id]),
255+
client=client
256+
)

0 commit comments

Comments
 (0)