Skip to content

Commit f9aacd8

Browse files
committed
Update poll parsing
1 parent 8490cfa commit f9aacd8

File tree

2 files changed

+64
-17
lines changed

2 files changed

+64
-17
lines changed

pyrogram/types/messages_and_media/message.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,8 @@ async def _parse(
827827
except MessageIdsEmpty:
828828
pass
829829

830-
client.message_cache[(parsed_message.chat.id, parsed_message.id)] = parsed_message
830+
if not parsed_message.poll: # Do not cache poll messages
831+
client.message_cache[(parsed_message.chat.id, parsed_message.id)] = parsed_message
831832

832833
return parsed_message
833834

pyrogram/types/messages_and_media/poll.py

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

19-
from typing import List, Union
19+
from datetime import datetime
20+
from typing import List, Union, Optional
2021

2122
import pyrogram
22-
from pyrogram import raw, enums
23+
from pyrogram import raw, enums, utils
2324
from pyrogram import types
2425
from ..object import Object
2526
from ..update import Update
@@ -53,8 +54,26 @@ class Poll(Object, Update):
5354
allows_multiple_answers (``bool``, *optional*):
5455
True, if the poll allows multiple answers.
5556
56-
chosen_option (``int``, *optional*):
57-
Index of your chosen option (0-9), None in case you haven't voted yet.
57+
chosen_option_id (``int``, *optional*):
58+
0-based index of the chosen option), None in case of no vote yet.
59+
60+
correct_option_id (``int``, *optional*):
61+
0-based identifier of the correct answer option.
62+
Available only for polls in the quiz mode, which are closed, or was sent (not forwarded) by the bot or to
63+
the private chat with the bot.
64+
65+
explanation (``str``, *optional*):
66+
Text that is shown when a user chooses an incorrect answer or taps on the lamp icon in a quiz-style poll,
67+
0-200 characters.
68+
69+
explanation_entities (List of :obj:`~pyrogram.types.MessageEntity`, *optional*):
70+
Special entities like usernames, URLs, bot commands, etc. that appear in the explanation.
71+
72+
open_period (``int``, *optional*):
73+
Amount of time in seconds the poll will be active after creation.
74+
75+
close_date (:py:obj:`~datetime.datetime`, *optional*):
76+
Point in time when the poll will be automatically closed.
5877
"""
5978

6079
def __init__(
@@ -69,8 +88,12 @@ def __init__(
6988
is_anonymous: bool = None,
7089
type: "enums.PollType" = None,
7190
allows_multiple_answers: bool = None,
72-
# correct_option_id: int,
73-
chosen_option: int = None
91+
chosen_option_id: Optional[int] = None,
92+
correct_option_id: Optional[int] = None,
93+
explanation: Optional[str] = None,
94+
explanation_entities: Optional[List["types.MessageEntity"]] = None,
95+
open_period: Optional[int] = None,
96+
close_date: Optional[datetime] = None
7497
):
7598
super().__init__(client)
7699

@@ -82,14 +105,21 @@ def __init__(
82105
self.is_anonymous = is_anonymous
83106
self.type = type
84107
self.allows_multiple_answers = allows_multiple_answers
85-
# self.correct_option_id = correct_option_id
86-
self.chosen_option = chosen_option
108+
self.chosen_option_id = chosen_option_id
109+
self.correct_option_id = correct_option_id
110+
self.explanation = explanation
111+
self.explanation_entities = explanation_entities
112+
self.open_period = open_period
113+
self.close_date = close_date
87114

88115
@staticmethod
89116
def _parse(client, media_poll: Union["raw.types.MessageMediaPoll", "raw.types.UpdateMessagePoll"]) -> "Poll":
90-
poll = media_poll.poll # type: raw.types.Poll
91-
results = media_poll.results.results
92-
chosen_option = None
117+
poll: raw.types.Poll = media_poll.poll
118+
poll_results: raw.types.PollResults = media_poll.results
119+
results: List[raw.types.PollAnswerVoters] = poll_results.results
120+
121+
chosen_option_id = None
122+
correct_option_id = None
93123
options = []
94124

95125
for i, answer in enumerate(poll.answers):
@@ -100,7 +130,10 @@ def _parse(client, media_poll: Union["raw.types.MessageMediaPoll", "raw.types.Up
100130
voter_count = result.voters
101131

102132
if result.chosen:
103-
chosen_option = i
133+
chosen_option_id = i
134+
135+
if result.correct:
136+
correct_option_id = i
104137

105138
options.append(
106139
types.PollOption(
@@ -120,7 +153,15 @@ def _parse(client, media_poll: Union["raw.types.MessageMediaPoll", "raw.types.Up
120153
is_anonymous=not poll.public_voters,
121154
type=enums.PollType.QUIZ if poll.quiz else enums.PollType.REGULAR,
122155
allows_multiple_answers=poll.multiple_choice,
123-
chosen_option=chosen_option,
156+
chosen_option_id=chosen_option_id,
157+
correct_option_id=correct_option_id,
158+
explanation=poll_results.solution,
159+
explanation_entities=[
160+
types.MessageEntity._parse(client, i, {})
161+
for i in poll_results.solution_entities
162+
] if poll_results.solution_entities else None,
163+
open_period=poll.close_period,
164+
close_date=utils.timestamp_to_datetime(poll.close_date),
124165
client=client
125166
)
126167

@@ -130,12 +171,16 @@ def _parse_update(client, update: "raw.types.UpdateMessagePoll"):
130171
return Poll._parse(client, update)
131172

132173
results = update.results.results
133-
chosen_option = None
174+
chosen_option_id = None
175+
correct_option_id = None
134176
options = []
135177

136178
for i, result in enumerate(results):
137179
if result.chosen:
138-
chosen_option = i
180+
chosen_option_id = i
181+
182+
if result.correct:
183+
correct_option_id = i
139184

140185
options.append(
141186
types.PollOption(
@@ -152,6 +197,7 @@ def _parse_update(client, update: "raw.types.UpdateMessagePoll"):
152197
options=options,
153198
total_voter_count=update.results.total_voters,
154199
is_closed=False,
155-
chosen_option=chosen_option,
200+
chosen_option_id=chosen_option_id,
201+
correct_option_id=correct_option_id,
156202
client=client
157203
)

0 commit comments

Comments
 (0)