Skip to content

Commit

Permalink
send_poll fix with PollOptions
Browse files Browse the repository at this point in the history
Now send_poll correctly operates with PollOptions passed as array of PollOption.
  • Loading branch information
Badiboy committed May 11, 2021
1 parent 3d26a0c commit 53c9832
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
16 changes: 15 additions & 1 deletion telebot/apihelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ def send_poll(
payload = {
'chat_id': str(chat_id),
'question': question,
'options': json.dumps(options)}
'options': json.dumps(_convert_poll_options(options))}

if is_anonymous is not None:
payload['is_anonymous'] = is_anonymous
Expand Down Expand Up @@ -1347,6 +1347,20 @@ def _convert_entites(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):
if isinstance(media, types.InputMedia):
return media.convert_input_media()
Expand Down
11 changes: 6 additions & 5 deletions telebot/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2527,7 +2527,8 @@ def to_dict(self):
return ret


class PollOption(JsonSerializable, JsonDeserializable):
class PollOption(JsonDeserializable):
#class PollOption(JsonSerializable, JsonDeserializable):
@classmethod
def de_json(cls, json_string):
if (json_string is None): return None
Expand All @@ -2539,10 +2540,10 @@ def de_json(cls, json_string):
def __init__(self, text, voter_count = 0):
self.text = text
self.voter_count = voter_count

def to_json(self):
# send_poll Option is a simple string: https://core.telegram.org/bots/api#sendpoll
return json.dumps(self.text)
# Converted in _convert_poll_options
# def to_json(self):
# # send_poll Option is a simple string: https://core.telegram.org/bots/api#sendpoll
# return json.dumps(self.text)


class Poll(JsonDeserializable):
Expand Down

0 comments on commit 53c9832

Please sign in to comment.