From 1d71cdc9e0bbf46766014b825b77a4feddf8fb2b Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 7 Aug 2020 16:03:50 +0200 Subject: [PATCH] Support autocast of polls into input media when possible Closes #1516. --- telethon/utils.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/telethon/utils.py b/telethon/utils.py index c8faf5a6..f74ef880 100644 --- a/telethon/utils.py +++ b/telethon/utils.py @@ -525,6 +525,27 @@ def get_input_media( if isinstance(media, types.Message): return get_input_media(media.media, is_photo=is_photo) + if isinstance(media, types.MessageMediaPoll): + if media.poll.quiz: + if not media.results.results: + # A quiz has correct answers, which we don't know until answered. + # If the quiz hasn't been answered we can't reconstruct it properly. + raise TypeError('Cannot cast unanswered quiz to any kind of InputMedia.') + + correct_answers = [r.option for r in media.results.results if r.correct] + else: + correct_answers = None + + return types.InputMediaPoll( + poll=media.poll, + correct_answers=correct_answers, + solution=media.results.solution, + solution_entities=media.results.solution_entities, + ) + + if isinstance(media, types.Poll): + return types.InputMediaPoll(media) + _raise_cast_fail(media, 'InputMedia')