pyrofork: Implement entities in Poll question and options

Signed-off-by: wulan17 <wulan17@nusantararom.org>
This commit is contained in:
wulan17 2024-06-02 00:16:05 +07:00
parent 59461adc55
commit 7328ccdf7c
No known key found for this signature in database
GPG key ID: 318CD6CD3A6AC0A5
3 changed files with 46 additions and 12 deletions

View file

@ -30,7 +30,8 @@ class SendPoll:
self: "pyrogram.Client", self: "pyrogram.Client",
chat_id: Union[int, str], chat_id: Union[int, str],
question: str, question: str,
options: List[str], options: List["types.PollOption"],
question_entities: List["types.MessageEntity"] = None,
is_anonymous: bool = True, is_anonymous: bool = True,
type: "enums.PollType" = enums.PollType.REGULAR, type: "enums.PollType" = enums.PollType.REGULAR,
allows_multiple_answers: bool = None, allows_multiple_answers: bool = None,
@ -73,8 +74,11 @@ class SendPoll:
question (``str``): question (``str``):
Poll question, 1-255 characters. Poll question, 1-255 characters.
options (List of ``str``): options (List of :obj:`~pyrogram.types.PollOption`):
List of answer options, 2-10 strings 1-100 characters each. 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*): is_anonymous (``bool``, *optional*):
True, if the poll needs to be anonymous. True, if the poll needs to be anonymous.
@ -185,16 +189,17 @@ class SendPoll:
solution, solution_entities = (await utils.parse_text_entities( solution, solution_entities = (await utils.parse_text_entities(
self, explanation, explanation_parse_mode, explanation_entities self, explanation, explanation_parse_mode, explanation_entities
)).values() )).values()
q, q_entities = (await pyrogram.utils.parse_text_entities(self, question, None, question_entities)).values()
rpc = raw.functions.messages.SendMedia( rpc = raw.functions.messages.SendMedia(
peer=await self.resolve_peer(chat_id), peer=await self.resolve_peer(chat_id),
media=raw.types.InputMediaPoll( media=raw.types.InputMediaPoll(
poll=raw.types.Poll( poll=raw.types.Poll(
id=self.rnd_id(), id=self.rnd_id(),
question=question, question=raw.types.TextWithEntities(text=q, entities=q_entities or []),
answers=[ answers=[
raw.types.PollAnswer(text=text, option=bytes([i])) await types.PollOption(text=option.text,entities=option.entities).write(self,i)
for i, text in enumerate(options) for i, option in enumerate(options)
], ],
closed=is_closed, closed=is_closed,
public_voters=not is_anonymous, public_voters=not is_anonymous,

View file

@ -40,6 +40,9 @@ class Poll(Object, Update):
options (List of :obj:`~pyrogram.types.PollOption`): options (List of :obj:`~pyrogram.types.PollOption`):
List of poll options. 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_voter_count (``int``):
Total number of users that voted in the poll. Total number of users that voted in the poll.
@ -84,6 +87,7 @@ class Poll(Object, Update):
id: str, id: str,
question: str, question: str,
options: List["types.PollOption"], options: List["types.PollOption"],
question_entities: List["types.MessageEntity"] = None,
total_voter_count: int, total_voter_count: int,
is_closed: bool, is_closed: bool,
is_anonymous: bool = None, is_anonymous: bool = None,
@ -101,6 +105,7 @@ class Poll(Object, Update):
self.id = id self.id = id
self.question = question self.question = question
self.options = options self.options = options
self.question_entities = question_entities
self.total_voter_count = total_voter_count self.total_voter_count = total_voter_count
self.is_closed = is_closed self.is_closed = is_closed
self.is_anonymous = is_anonymous self.is_anonymous = is_anonymous
@ -136,19 +141,27 @@ class Poll(Object, Update):
if result.correct: if result.correct:
correct_option_id = i 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( options.append(
types.PollOption( types.PollOption(
text=answer.text, text=answer.text.text,
voter_count=voter_count, voter_count=voter_count,
data=answer.option, data=answer.option,
entities=option_entities,
client=client 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( return Poll(
id=str(poll.id), id=str(poll.id),
question=poll.question, question=poll.question.text,
options=options, options=options,
question_entities=question_entities,
total_voter_count=media_poll.results.total_voters, total_voter_count=media_poll.results.total_voters,
is_closed=poll.closed, is_closed=poll.closed,
is_anonymous=not poll.public_voters, is_anonymous=not poll.public_voters,

View file

@ -19,6 +19,7 @@
import pyrogram import pyrogram
from ..object import Object from ..object import Object
from typing import List, Optional
class PollOption(Object): class PollOption(Object):
@ -28,12 +29,15 @@ class PollOption(Object):
text (``str``): text (``str``):
Option text, 1-100 characters. Option text, 1-100 characters.
voter_count (``int``): voter_count (``int``, *optional*):
Number of users that voted for this option. Number of users that voted for this option.
Equals to 0 until you vote. Equals to 0 until you vote.
data (``bytes``): data (``bytes``, *optional*):
The data this poll option is holding. 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__( def __init__(
@ -41,11 +45,23 @@ class PollOption(Object):
*, *,
client: "pyrogram.Client" = None, client: "pyrogram.Client" = None,
text: str, text: str,
voter_count: int, voter_count: int = 0,
data: bytes data: bytes = None,
entities: Optional[List["pyrogram.types.MessageEntity"]] = None,
): ):
super().__init__(client) super().__init__(client)
self.text = text self.text = text
self.voter_count = voter_count self.voter_count = voter_count
self.data = data 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])
)