1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Merge pull request #1155 from Badiboy/master

send_poll fix with PollOptions
This commit is contained in:
Badiboy 2021-05-15 11:31:02 +03:00 committed by GitHub
commit d3c2ffd422
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View File

@ -1277,7 +1277,7 @@ def send_poll(
payload = { payload = {
'chat_id': str(chat_id), 'chat_id': str(chat_id),
'question': question, 'question': question,
'options': json.dumps(options)} 'options': json.dumps(_convert_poll_options(options))}
if is_anonymous is not None: if is_anonymous is not None:
payload['is_anonymous'] = is_anonymous payload['is_anonymous'] = is_anonymous
@ -1347,6 +1347,20 @@ def _convert_entites(entites):
return entites return entites
def _convert_poll_options(poll_options):
if poll_options is None:
return None
elif len(poll_options) == 0:
return []
elif isinstance(poll_options[0], str):
# Compatibility mode with previous bug when only list of string was accepted as poll_options
return poll_options
elif isinstance(poll_options[0], types.JsonSerializable):
return [option.text for option in poll_options]
else:
return poll_options
def convert_input_media(media): def convert_input_media(media):
if isinstance(media, types.InputMedia): if isinstance(media, types.InputMedia):
return media.convert_input_media() return media.convert_input_media()

View File

@ -2527,7 +2527,8 @@ class InputMediaDocument(InputMedia):
return ret return ret
class PollOption(JsonSerializable, JsonDeserializable): class PollOption(JsonDeserializable):
#class PollOption(JsonSerializable, JsonDeserializable):
@classmethod @classmethod
def de_json(cls, json_string): def de_json(cls, json_string):
if (json_string is None): return None if (json_string is None): return None
@ -2539,10 +2540,10 @@ class PollOption(JsonSerializable, JsonDeserializable):
def __init__(self, text, voter_count = 0): def __init__(self, text, voter_count = 0):
self.text = text self.text = text
self.voter_count = voter_count self.voter_count = voter_count
# Converted in _convert_poll_options
def to_json(self): # def to_json(self):
# send_poll Option is a simple string: https://core.telegram.org/bots/api#sendpoll # # send_poll Option is a simple string: https://core.telegram.org/bots/api#sendpoll
return json.dumps(self.text) # return json.dumps(self.text)
class Poll(JsonDeserializable): class Poll(JsonDeserializable):