From 1ae719c2529d28cf7efb29782c873882c64f759c Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Sun, 24 Apr 2022 11:56:07 +0200 Subject: [PATCH] Rework send_poll: add missing parameters --- pyrogram/methods/messages/send_poll.py | 77 +++++++++--- pyrogram/types/messages_and_media/message.py | 119 +++++++++++++------ 2 files changed, 141 insertions(+), 55 deletions(-) diff --git a/pyrogram/methods/messages/send_poll.py b/pyrogram/methods/messages/send_poll.py index 1305d2d6..3f6f35d2 100644 --- a/pyrogram/methods/messages/send_poll.py +++ b/pyrogram/methods/messages/send_poll.py @@ -31,13 +31,19 @@ class SendPoll: question: str, options: List[str], is_anonymous: bool = True, - allows_multiple_answers: bool = None, type: "enums.PollType" = enums.PollType.REGULAR, + allows_multiple_answers: bool = None, correct_option_id: int = None, + explanation: str = None, + explanation_parse_mode: "enums.ParseMode" = None, + explanation_entities: List["types.MessageEntity"] = None, + open_period: int = None, + close_date: datetime = None, + is_closed: bool = None, disable_notification: bool = None, + protect_content: bool = None, reply_to_message_id: int = None, schedule_date: datetime = None, - protect_content: bool = None, reply_markup: Union[ "types.InlineKeyboardMarkup", "types.ReplyKeyboardMarkup", @@ -69,25 +75,49 @@ class SendPoll: allows_multiple_answers (``bool``, *optional*): True, if the poll allows multiple answers, ignored for polls in quiz mode. - Defaults to False + Defaults to False. correct_option_id (``int``, *optional*): - 0-based identifier of the correct answer option (the index of the correct option) - Required for polls in quiz mode. + 0-based identifier of the correct answer option, required for polls in quiz mode. + + explanation (``str``, *optional*): + Text that is shown when a user chooses an incorrect answer or taps on the lamp icon in a quiz-style + poll, 0-200 characters with at most 2 line feeds after entities parsing. + + explanation_parse_mode (:obj:`~pyrogram.enums.ParseMode`, *optional*): + By default, texts are parsed using both Markdown and HTML styles. + You can combine both syntaxes together. + + explanation_entities (List of :obj:`~pyrogram.types.MessageEntity`): + List of special entities that appear in the poll explanation, which can be specified instead of + *parse_mode*. + + open_period (``int``, *optional*): + Amount of time in seconds the poll will be active after creation, 5-600. + Can't be used together with *close_date*. + + close_date (:py:obj:`~datetime.datetime`, *optional*): + Point in time when the poll will be automatically closed. + Must be at least 5 and no more than 600 seconds in the future. + Can't be used together with *open_period*. + + is_closed (``bool``, *optional*): + Pass True, if the poll needs to be immediately closed. + This can be useful for poll preview. disable_notification (``bool``, *optional*): Sends the message silently. Users will receive a notification with no sound. + protect_content (``bool``, *optional*): + Protects the contents of the sent message from forwarding and saving. + reply_to_message_id (``int``, *optional*): If the message is a reply, ID of the original message. schedule_date (:py:obj:`~datetime.datetime`, *optional*): Date when the message will be automatically sent. - protect_content (``bool``, *optional*): - Protects the contents of the sent message from forwarding and saving. - reply_markup (:obj:`~pyrogram.types.InlineKeyboardMarkup` | :obj:`~pyrogram.types.ReplyKeyboardMarkup` | :obj:`~pyrogram.types.ReplyKeyboardRemove` | :obj:`~pyrogram.types.ForceReply`, *optional*): Additional interface options. An object for an inline keyboard, custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user. @@ -100,25 +130,40 @@ class SendPoll: await app.send_poll(chat_id, "Is this a poll question?", ["Yes", "No", "Maybe"]) """ + + message, entities = (await utils.parse_text_entities( + self, explanation, explanation_parse_mode, explanation_entities + )).values() + + # For some reason passing None or [] as solution_entities will lead to INPUT_CONSTRUCTOR_INVALID_00 + # Add a dummy message entity with no length as workaround + solution = message or None + solution_entities = entities or ([raw.types.MessageEntityBold(offset=0, length=0)] if solution else None) + r = await self.invoke( raw.functions.messages.SendMedia( peer=await self.resolve_peer(chat_id), media=raw.types.InputMediaPoll( poll=raw.types.Poll( - id=0, + id=self.rnd_id(), question=question, answers=[ - raw.types.PollAnswer(text=o, option=bytes([i])) - for i, o in enumerate(options) + raw.types.PollAnswer(text=text, option=bytes([i])) + for i, text in enumerate(options) ], - multiple_choice=allows_multiple_answers or None, - public_voters=not is_anonymous or None, - quiz=type == enums.PollType.QUIZ or None + closed=is_closed, + public_voters=not is_anonymous, + multiple_choice=allows_multiple_answers, + quiz=type == enums.PollType.QUIZ or False, + close_period=open_period, + close_date=utils.datetime_to_timestamp(close_date) ), - correct_answers=None if correct_option_id is None else [bytes([correct_option_id])] + correct_answers=[bytes([correct_option_id])] if correct_option_id is not None else None, + solution=solution, + solution_entities=solution_entities ), message="", - silent=disable_notification or None, + silent=disable_notification, reply_to_msg_id=reply_to_message_id, random_id=self.rnd_id(), schedule_date=utils.datetime_to_timestamp(schedule_date), diff --git a/pyrogram/types/messages_and_media/message.py b/pyrogram/types/messages_and_media/message.py index 526839c9..78817f49 100644 --- a/pyrogram/types/messages_and_media/message.py +++ b/pyrogram/types/messages_and_media/message.py @@ -951,7 +951,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -1088,7 +1088,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -1227,7 +1227,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -1319,7 +1319,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -1441,7 +1441,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -1577,7 +1577,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -1655,7 +1655,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -1719,7 +1719,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -1793,7 +1793,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -1856,7 +1856,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -1972,7 +1972,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -1995,12 +1995,19 @@ class Message(Object, Update): self, question: str, options: List[str], - quote: bool = None, is_anonymous: bool = True, + type: "enums.PollType" = enums.PollType.REGULAR, allows_multiple_answers: bool = None, - type: str = "regular", correct_option_id: int = None, + explanation: str = None, + explanation_parse_mode: "enums.ParseMode" = None, + explanation_entities: List["types.MessageEntity"] = None, + open_period: int = None, + close_date: datetime = None, + is_closed: bool = None, + quote: bool = None, disable_notification: bool = None, + protect_content: bool = None, reply_to_message_id: int = None, schedule_date: datetime = None, reply_markup: Union[ @@ -2029,39 +2036,66 @@ class Message(Object, Update): Parameters: question (``str``): - The poll question, as string. + Poll question, 1-255 characters. options (List of ``str``): - The poll options, as list of strings (2 to 10 options are allowed). + List of answer options, 2-10 strings 1-100 characters each. + + is_anonymous (``bool``, *optional*): + True, if the poll needs to be anonymous. + Defaults to True. + + type (:obj`~pyrogram.enums.PollType`, *optional*): + Poll type, :obj:`~pyrogram.enums.PollType.QUIZ` or :obj:`~pyrogram.enums.PollType.REGULAR`. + Defaults to :obj:`~pyrogram.enums.PollType.REGULAR`. + + allows_multiple_answers (``bool``, *optional*): + True, if the poll allows multiple answers, ignored for polls in quiz mode. + Defaults to False. + + correct_option_id (``int``, *optional*): + 0-based identifier of the correct answer option, required for polls in quiz mode. + + explanation (``str``, *optional*): + Text that is shown when a user chooses an incorrect answer or taps on the lamp icon in a quiz-style + poll, 0-200 characters with at most 2 line feeds after entities parsing. + + explanation_parse_mode (:obj:`~pyrogram.enums.ParseMode`, *optional*): + By default, texts are parsed using both Markdown and HTML styles. + You can combine both syntaxes together. + + explanation_entities (List of :obj:`~pyrogram.types.MessageEntity`): + List of special entities that appear in the poll explanation, which can be specified instead of + *parse_mode*. + + open_period (``int``, *optional*): + Amount of time in seconds the poll will be active after creation, 5-600. + Can't be used together with *close_date*. + + close_date (:py:obj:`~datetime.datetime`, *optional*): + Point in time when the poll will be automatically closed. + Must be at least 5 and no more than 600 seconds in the future. + Can't be used together with *open_period*. + + is_closed (``bool``, *optional*): + Pass True, if the poll needs to be immediately closed. + This can be useful for poll preview. quote (``bool``, *optional*): If ``True``, the message will be sent as a reply to this message. If *reply_to_message_id* is passed, this parameter will be ignored. Defaults to ``True`` in group chats and ``False`` in private chats. - - is_anonymous (``bool``, *optional*): - True, if the poll needs to be anonymous. - Defaults to True. - - type (``str``, *optional*): - Poll type, "quiz" or "regular". - Defaults to "regular" - - allows_multiple_answers (``bool``, *optional*): - True, if the poll allows multiple answers, ignored for polls in quiz mode. - Defaults to False - - correct_option_id (``int``, *optional*): - 0-based identifier of the correct answer option (the index of the correct option) - Required for polls in quiz mode. disable_notification (``bool``, *optional*): Sends the message silently. Users will receive a notification with no sound. + protect_content (``bool``, *optional*): + Protects the contents of the sent message from forwarding and saving. + reply_to_message_id (``int``, *optional*): If the message is a reply, ID of the original message. - + schedule_date (:py:obj:`~datetime.datetime`, *optional*): Date when the message will be automatically sent. @@ -2076,7 +2110,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -2086,10 +2120,17 @@ class Message(Object, Update): question=question, options=options, is_anonymous=is_anonymous, - allows_multiple_answers=allows_multiple_answers, type=type, + allows_multiple_answers=allows_multiple_answers, correct_option_id=correct_option_id, + explanation=explanation, + explanation_parse_mode=explanation_parse_mode, + explanation_entities=explanation_entities, + open_period=open_period, + close_date=close_date, + is_closed=is_closed, disable_notification=disable_notification, + protect_content=protect_content, reply_to_message_id=reply_to_message_id, schedule_date=schedule_date, reply_markup=reply_markup @@ -2180,7 +2221,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -2275,7 +2316,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -2420,7 +2461,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -2544,7 +2585,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id @@ -2664,7 +2705,7 @@ class Message(Object, Update): RPCError: In case of a Telegram RPC error. """ if quote is None: - quote = self.chat.type != "private" + quote = self.chat.type != enums.ChatType.PRIVATE if reply_to_message_id is None and quote: reply_to_message_id = self.id