Skip to content

Commit 4661fb0

Browse files
committed
Refactor Poll types and methods to reflect Bot API 4.2 docs
1 parent 605b5f1 commit 4661fb0

8 files changed

Lines changed: 74 additions & 66 deletions

File tree

pyrogram/client/methods/messages/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
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 .close_poll import ClosePoll
19+
from .stop_poll import StopPoll
2020
from .delete_messages import DeleteMessages
2121
from .download_media import DownloadMedia
2222
from .edit_message_caption import EditMessageCaption
@@ -72,7 +72,7 @@ class Messages(
7272
SendVoice,
7373
SendPoll,
7474
VotePoll,
75-
ClosePoll,
75+
StopPoll,
7676
RetractVote,
7777
DownloadMedia,
7878
IterHistory,

pyrogram/client/methods/messages/retract_vote.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from typing import Union
2020

21+
import pyrogram
2122
from pyrogram.api import functions
2223
from pyrogram.client.ext import BaseClient
2324

@@ -26,8 +27,8 @@ class RetractVote(BaseClient):
2627
def retract_vote(
2728
self,
2829
chat_id: Union[int, str],
29-
message_id: id
30-
) -> bool:
30+
message_id: int
31+
) -> "pyrogram.Poll":
3132
"""Use this method to retract your vote in a poll.
3233
3334
Args:
@@ -37,20 +38,20 @@ def retract_vote(
3738
For a contact that exists in your Telegram address book you can use his phone number (str).
3839
3940
message_id (``int``):
40-
Unique poll message identifier inside this chat.
41+
Identifier of the original message with the poll.
4142
4243
Returns:
43-
On success, True is returned.
44+
On success, the :obj:`Poll <pyrogram.Poll>` with the retracted vote is returned.
4445
4546
Raises:
4647
:class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error.
4748
"""
48-
self.send(
49+
r = self.send(
4950
functions.messages.SendVote(
5051
peer=self.resolve_peer(chat_id),
5152
msg_id=message_id,
5253
options=[]
5354
)
5455
)
5556

56-
return True
57+
return pyrogram.Poll._parse(self, r.updates[0])

pyrogram/client/methods/messages/send_poll.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ def send_poll(
4747
For a contact that exists in your Telegram address book you can use his phone number (str).
4848
4949
question (``str``):
50-
The poll question, as string.
50+
Poll question, 1-255 characters.
5151
5252
options (List of ``str``):
53-
The poll options, as list of strings (2 to 10 options are allowed).
53+
List of answer options, 2-10 strings 1-100 characters each.
5454
5555
disable_notification (``bool``, *optional*):
5656
Sends the message silently.

pyrogram/client/methods/messages/close_poll.py renamed to pyrogram/client/methods/messages/stop_poll.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@
1818

1919
from typing import Union
2020

21+
import pyrogram
2122
from pyrogram.api import functions, types
2223
from pyrogram.client.ext import BaseClient
2324

2425

25-
class ClosePoll(BaseClient):
26-
def close_poll(
26+
class StopPoll(BaseClient):
27+
def stop_poll(
2728
self,
2829
chat_id: Union[int, str],
29-
message_id: id
30-
) -> bool:
31-
"""Use this method to close (stop) a poll.
30+
message_id: int,
31+
reply_markup: "pyrogram.InlineKeyboardMarkup" = None
32+
) -> "pyrogram.Poll":
33+
"""Use this method to stop a poll which was sent by you.
3234
33-
Closed polls can't be reopened and nobody will be able to vote in it anymore.
35+
Stopped polls can't be reopened and nobody will be able to vote in it anymore.
3436
3537
Args:
3638
chat_id (``int`` | ``str``):
@@ -39,17 +41,20 @@ def close_poll(
3941
For a contact that exists in your Telegram address book you can use his phone number (str).
4042
4143
message_id (``int``):
42-
Unique poll message identifier inside this chat.
44+
Identifier of the original message with the poll.
45+
46+
reply_markup (:obj:`InlineKeyboardMarkup`, *optional*):
47+
An InlineKeyboardMarkup object.
4348
4449
Returns:
45-
On success, True is returned.
50+
On success, the stopped :obj:`Poll <pyrogram.Poll>` with the final results is returned.
4651
4752
Raises:
4853
:class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error.
4954
"""
5055
poll = self.get_messages(chat_id, message_id).poll
5156

52-
self.send(
57+
r = self.send(
5358
functions.messages.EditMessage(
5459
peer=self.resolve_peer(chat_id),
5560
id=message_id,
@@ -60,8 +65,9 @@ def close_poll(
6065
question="",
6166
answers=[]
6267
)
63-
)
68+
),
69+
reply_markup=reply_markup.write() if reply_markup else None
6470
)
6571
)
6672

67-
return True
73+
return pyrogram.Poll._parse(self, r.updates[0])

pyrogram/client/methods/messages/vote_poll.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
from typing import Union
2020

21+
import pyrogram
2122
from pyrogram.api import functions
2223
from pyrogram.client.ext import BaseClient
2324

@@ -28,7 +29,7 @@ def vote_poll(
2829
chat_id: Union[int, str],
2930
message_id: id,
3031
option: int
31-
) -> bool:
32+
) -> "pyrogram.Poll":
3233
"""Use this method to vote a poll.
3334
3435
Args:
@@ -38,25 +39,26 @@ def vote_poll(
3839
For a contact that exists in your Telegram address book you can use his phone number (str).
3940
4041
message_id (``int``):
41-
Unique poll message identifier inside this chat.
42+
Identifier of the original message with the poll.
4243
4344
option (``int``):
4445
Index of the poll option you want to vote for (0 to 9).
4546
4647
Returns:
47-
On success, True is returned.
48+
On success, the :obj:`Poll <pyrogram.Poll>` with the chosen option is returned.
4849
4950
Raises:
5051
:class:`RPCError <pyrogram.RPCError>` in case of a Telegram RPC error.
5152
"""
53+
5254
poll = self.get_messages(chat_id, message_id).poll
5355

54-
self.send(
56+
r = self.send(
5557
functions.messages.SendVote(
5658
peer=self.resolve_peer(chat_id),
5759
msg_id=message_id,
58-
options=[poll.options[option].data]
60+
options=[poll.options[option]._data]
5961
)
6062
)
6163

62-
return True
64+
return pyrogram.Poll._parse(self, r.updates[0])

pyrogram/client/types/messages_and_media/poll.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,78 +29,80 @@ class Poll(PyrogramType):
2929
3030
Args:
3131
id (``int``):
32-
The poll id in this chat.
33-
34-
closed (``bool``):
35-
Whether the poll is closed or not.
32+
Unique poll identifier.
3633
3734
question (``str``):
38-
Poll question.
35+
Poll question, 1-255 characters.
3936
4037
options (List of :obj:`PollOption`):
41-
The available poll options.
38+
List of poll options.
39+
40+
is_closed (``bool``):
41+
True, if the poll is closed.
4242
4343
total_voters (``int``):
44-
Total amount of voters for this poll.
44+
Total count of voters for this poll.
4545
46-
option_chosen (``int``, *optional*):
47-
The index of your chosen option (in case you voted already), None otherwise.
46+
chosen_option (``int``, *optional*):
47+
Index of your chosen option (0-9), None in case you haven't voted yet.
4848
"""
4949

50-
__slots__ = ["id", "closed", "question", "options", "total_voters", "option_chosen"]
50+
__slots__ = ["id", "question", "options", "is_closed", "total_voters", "chosen_option"]
5151

5252
def __init__(
5353
self,
5454
*,
5555
client: "pyrogram.client.ext.BaseClient",
5656
id: int,
57-
closed: bool,
5857
question: str,
5958
options: List[PollOption],
59+
is_closed: bool,
6060
total_voters: int,
61-
option_chosen: int = None
61+
chosen_option: int = None
6262
):
6363
super().__init__(client)
6464

6565
self.id = id
66-
self.closed = closed
6766
self.question = question
6867
self.options = options
68+
self.is_closed = is_closed
6969
self.total_voters = total_voters
70-
self.option_chosen = option_chosen
70+
self.chosen_option = chosen_option
7171

7272
@staticmethod
7373
def _parse(client, media_poll: types.MessageMediaPoll) -> "Poll":
7474
poll = media_poll.poll
7575
results = media_poll.results.results
7676
total_voters = media_poll.results.total_voters
77-
option_chosen = None
77+
chosen_option = None
7878

7979
options = []
8080

8181
for i, answer in enumerate(poll.answers):
82-
voters = 0
82+
voter_count = 0
8383

8484
if results:
8585
result = results[i]
86-
voters = result.voters
86+
voter_count = result.voters
8787

8888
if result.chosen:
89-
option_chosen = i
89+
chosen_option = i
9090

91-
options.append(PollOption(
92-
text=answer.text,
93-
voters=voters,
94-
data=answer.option,
95-
client=client
96-
))
91+
options.append(
92+
PollOption(
93+
text=answer.text,
94+
voter_count=voter_count,
95+
data=answer.option,
96+
client=client
97+
)
98+
)
9799

98100
return Poll(
99101
id=poll.id,
100-
closed=poll.closed,
101102
question=poll.question,
102103
options=options,
104+
is_closed=poll.closed,
103105
total_voters=total_voters,
104-
option_chosen=option_chosen,
106+
chosen_option=chosen_option,
105107
client=client
106108
)

pyrogram/client/types/messages_and_media/poll_option.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,32 +21,29 @@
2121

2222

2323
class PollOption(PyrogramType):
24-
"""This object represents a Poll Option.
24+
"""This object contains information about one answer option in a poll.
2525
2626
Args:
2727
text (``str``):
28-
Text of the poll option.
28+
Option text, 1-100 characters.
2929
30-
voters (``int``):
31-
The number of users who voted this option.
32-
It will be 0 until you vote for the poll.
33-
34-
data (``bytes``):
35-
Unique data that identifies this option among all the other options in a poll.
30+
voter_count (``int``):
31+
Number of users that voted for this option.
32+
Equals to 0 until you vote.
3633
"""
3734

38-
__slots__ = ["text", "voters", "data"]
35+
__slots__ = ["text", "voter_count", "_data"]
3936

4037
def __init__(
4138
self,
4239
*,
4340
client: "pyrogram.client.ext.BaseClient",
4441
text: str,
45-
voters: int,
42+
voter_count: int,
4643
data: bytes
4744
):
4845
super().__init__(client)
4946

5047
self.text = text
51-
self.voters = voters
52-
self.data = data
48+
self.voter_count = voter_count
49+
self._data = data # Hidden

pyrogram/client/types/pyrogram_type.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def default(o: PyrogramType):
5151
return remove_none(
5252
OrderedDict(
5353
[("_", "pyrogram." + o.__class__.__name__)]
54-
+ [i for i in content.items()]
54+
+ [i for i in content.items() if not i[0].startswith("_")]
5555
)
5656
)
5757
except AttributeError:

0 commit comments

Comments
 (0)