diff --git a/pyrogram/methods/messages/send_poll.py b/pyrogram/methods/messages/send_poll.py index 352371eb9..73e6d8e44 100644 --- a/pyrogram/methods/messages/send_poll.py +++ b/pyrogram/methods/messages/send_poll.py @@ -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, @@ -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. @@ -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, diff --git a/pyrogram/types/messages_and_media/poll.py b/pyrogram/types/messages_and_media/poll.py index 797e9611b..f0c94d7a7 100644 --- a/pyrogram/types/messages_and_media/poll.py +++ b/pyrogram/types/messages_and_media/poll.py @@ -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. @@ -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, @@ -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 @@ -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, diff --git a/pyrogram/types/messages_and_media/poll_option.py b/pyrogram/types/messages_and_media/poll_option.py index db878bbe2..62194b8f7 100644 --- a/pyrogram/types/messages_and_media/poll_option.py +++ b/pyrogram/types/messages_and_media/poll_option.py @@ -18,6 +18,7 @@ import pyrogram from ..object import Object +from typing import List, Optional class PollOption(Object): @@ -27,12 +28,15 @@ 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__( @@ -40,11 +44,23 @@ def __init__( *, 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]) + )