Skip to content

Commit

Permalink
pyrofork: Implement entities in Poll question and options
Browse files Browse the repository at this point in the history
Signed-off-by: wulan17 <[email protected]>

(cherry picked from commit 7328ccd)
Signed-off-by: eyMarv <[email protected]>
  • Loading branch information
wulan17 authored and eyMarv committed Jun 2, 2024
1 parent 8a32436 commit df60ab2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
25 changes: 14 additions & 11 deletions pyrogram/methods/messages/send_poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ async def send_poll(
self: "pyrogram.Client",
chat_id: Union[int, str],
question: str,
options: List[str],
options: List["types.PollOption"],
question_entities: List["types.MessageEntity"] = None,
is_anonymous: bool = True,
type: "enums.PollType" = enums.PollType.REGULAR,
allows_multiple_answers: bool = None,
Expand Down Expand Up @@ -73,8 +74,11 @@ async def send_poll(
question (``str``):
Poll question, 1-255 characters.
options (List of ``str``):
List of answer options, 2-10 strings 1-100 characters each.
options (List of :obj:`~pyrogram.types.PollOption`):
List of PollOption.
question_entities (List of :obj:`~pyrogram.types.MessageEntity`, *optional*):
List of special entities that appear in the poll question, which can be specified instead of *parse_mode*.
is_anonymous (``bool``, *optional*):
True, if the poll needs to be anonymous.
Expand Down Expand Up @@ -179,21 +183,20 @@ async def send_poll(
parse_mode=parse_mode,
)

solution, solution_entities = (
await utils.parse_text_entities(
self, explanation, explanation_parse_mode, explanation_entities
)
).values()
solution, solution_entities = (await utils.parse_text_entities(
self, explanation, explanation_parse_mode, explanation_entities
)).values()
q, q_entities = (await pyrogram.utils.parse_text_entities(self, question, None, question_entities)).values()

rpc = raw.functions.messages.SendMedia(
peer=await self.resolve_peer(chat_id),
media=raw.types.InputMediaPoll(
poll=raw.types.Poll(
id=self.rnd_id(),
question=question,
question=raw.types.TextWithEntities(text=q, entities=q_entities or []),
answers=[
raw.types.PollAnswer(text=text, option=bytes([i]))
for i, text in enumerate(options)
await types.PollOption(text=option.text,entities=option.entities).write(self,i)
for i, option in enumerate(options)
],
closed=is_closed,
public_voters=not is_anonymous,
Expand Down
15 changes: 14 additions & 1 deletion pyrogram/types/messages_and_media/poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class Poll(Object, Update):
options (List of :obj:`~pyrogram.types.PollOption`):
List of poll options.
question_entities (List of :obj:`~pyrogram.types.MessageEntity`, *optional*):
Special entities like usernames, URLs, bot commands, etc. that appear in the poll question.
total_voter_count (``int``):
Total number of users that voted in the poll.
Expand Down Expand Up @@ -85,6 +88,7 @@ def __init__(
id: str,
question: str,
options: List["types.PollOption"],
question_entities: List["types.MessageEntity"] = None,
total_voter_count: int,
is_closed: bool,
is_anonymous: bool = None,
Expand All @@ -102,6 +106,7 @@ def __init__(
self.id = id
self.question = question
self.options = options
self.question_entities = question_entities
self.total_voter_count = total_voter_count
self.is_closed = is_closed
self.is_anonymous = is_anonymous
Expand Down Expand Up @@ -140,19 +145,27 @@ def _parse(
if result.correct:
correct_option_id = i

o_entities = [types.MessageEntity._parse(client, entity, {}) for entity in answer.text.entities] if answer.text.entities else []
option_entities = types.List(filter(lambda x: x is not None, o_entities))

options.append(
types.PollOption(
text=answer.text.text,
voter_count=voter_count,
data=answer.option,
client=client,
entities=option_entities,
client=client
)
)

q_entities = [types.MessageEntity._parse(client, entity, {}) for entity in poll.question.entities] if poll.question.entities else []
question_entities = types.List(filter(lambda x: x is not None, q_entities))

return Poll(
id=str(poll.id),
question=poll.question.text,
options=options,
question_entities=question_entities,
total_voter_count=media_poll.results.total_voters,
is_closed=poll.closed,
is_anonymous=not poll.public_voters,
Expand Down
24 changes: 20 additions & 4 deletions pyrogram/types/messages_and_media/poll_option.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import pyrogram
from ..object import Object
from typing import List, Optional


class PollOption(Object):
Expand All @@ -27,24 +28,39 @@ class PollOption(Object):
text (``str``):
Option text, 1-100 characters.
voter_count (``int``):
voter_count (``int``, *optional*):
Number of users that voted for this option.
Equals to 0 until you vote.
data (``bytes``):
data (``bytes``, *optional*):
The data this poll option is holding.
entities (List of :obj:`~pyrogram.types.MessageEntity`, *optional*):
Special entities like usernames, URLs, bot commands, etc. that appear in the option text.
"""

def __init__(
self,
*,
client: "pyrogram.Client" = None,
text: str,
voter_count: int,
data: bytes,
voter_count: int = 0,
data: bytes = None,
entities: Optional[List["pyrogram.types.MessageEntity"]] = None,
):
super().__init__(client)

self.text = text
self.voter_count = voter_count
self.data = data
self.entities = entities

async def write(self, client, i):
option, entities = (await pyrogram.utils.parse_text_entities(client, self.text, None, self.entities)).values()
return pyrogram.raw.types.PollAnswer(
text=pyrogram.raw.types.TextWithEntities(
text=option,
entities=entities or []
),
option=bytes([i])
)

0 comments on commit df60ab2

Please sign in to comment.