Skip to content

Commit d709b39

Browse files
committed
Minor lost fixes for to_dict and other methods
1 parent 62f48c3 commit d709b39

3 files changed

Lines changed: 46 additions & 24 deletions

File tree

‎telebot/types.py‎

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,8 @@ def can_send_gift(self) -> bool:
983983
return False
984984

985985

986-
class Chat(ChatFullInfo):
986+
# noinspection PyShadowingBuiltins
987+
class Chat(JsonDeserializable):
987988
"""
988989
This object represents a chat.
989990

@@ -1022,6 +1023,17 @@ def de_json(cls, json_string):
10221023
obj = cls.check_json(json_string, dict_copy=False)
10231024
return cls(**obj)
10241025

1026+
def __init__(self, id: int, type: str, title: Optional[str]=None, username: Optional[str]=None,
1027+
first_name: Optional[str]=None, last_name: Optional[str]=None,
1028+
is_forum: Optional[bool]=None, is_direct_messages: Optional[bool]=None, **kwargs) -> None:
1029+
self.id: int = id
1030+
self.type: str = type
1031+
self.title: Optional[str] = title
1032+
self.username: Optional[str] = username
1033+
self.first_name: Optional[str] = first_name
1034+
self.last_name: Optional[str] = last_name
1035+
self.is_forum: Optional[bool] = is_forum
1036+
self.is_direct_messages: Optional[bool] = is_direct_messages
10251037

10261038

10271039
class MessageID(JsonDeserializable, ABC):
@@ -2951,6 +2963,8 @@ def to_dict(self) -> dict:
29512963
data['input_field_placeholder'] = self.input_field_placeholder
29522964
if self.is_persistent is not None:
29532965
data['is_persistent'] = self.is_persistent
2966+
if self.force_reply is not None:
2967+
data['force_reply'] = self.force_reply
29542968
return data
29552969

29562970
def to_json(self):
@@ -3136,7 +3150,6 @@ def to_dict(self) -> dict:
31363150
return data
31373151

31383152

3139-
31403153
class KeyboardButton(Dictionaryable, JsonSerializable):
31413154
"""
31423155
This object represents one button of the reply keyboard. At most one of the fields other than text, icon_custom_emoji_id, and style must be used to specify the type of the button. For simple text buttons, String can be used instead of this object to specify the button text.
@@ -3353,6 +3366,8 @@ def to_dict(self) -> dict:
33533366
data = {
33543367
'inline_keyboard': [[button.to_dict() for button in row] for row in self.inline_keyboard],
33553368
}
3369+
if self.force_reply is not None:
3370+
data['force_reply'] = self.force_reply
33563371
return data
33573372

33583373
@property
@@ -3361,8 +3376,6 @@ def keyboard(self):
33613376
return self.inline_keyboard
33623377

33633378

3364-
3365-
33663379
class InlineKeyboardButton(Dictionaryable, JsonSerializable, JsonDeserializable):
33673380
"""
33683381
This object represents one button of an inline keyboard. Exactly one of the fields other than text, icon_custom_emoji_id, and style must be used to specify the type of the button.
@@ -3393,8 +3406,7 @@ class InlineKeyboardButton(Dictionaryable, JsonSerializable, JsonDeserializable)
33933406
:param switch_inline_query: Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot's username and the specified inline query in the input field. May be empty, in which case just the bot's username will be inserted. Not supported for messages sent in channel direct messages chats and on behalf of a business account.
33943407
:type switch_inline_query: :obj:`str`
33953408

3396-
:param switch_inline_query_current_chat: Optional. If set, pressing the button will insert the bot's username and the specified inline query in the current chat's input field. May be empty, in which case only the bot's username will be inserted.
3397-
This offers a quick way for the user to open your bot in inline mode in the same chat - good for selecting something from multiple options. Not supported in channels and for messages sent in channel direct messages chats and on behalf of a business account.
3409+
:param switch_inline_query_current_chat: Optional. If set, pressing the button will insert the bot's username and the specified inline query in the current chat's input field. May be empty, in which case only the bot's username will be inserted. This offers a quick way for the user to open your bot in inline mode in the same chat - good for selecting something from multiple options. Not supported in channels and for messages sent in channel direct messages chats and on behalf of a business account.
33983410
:type switch_inline_query_current_chat: :obj:`str`
33993411

34003412
:param switch_inline_query_chosen_chat: Optional. If set, pressing the button will prompt the user to select one of their chats of the specified type, open that chat and insert the bot's username and the specified inline query in the input field. Not supported for messages sent in channel direct messages chats and on behalf of a business account.
@@ -3427,6 +3439,10 @@ def de_json(cls, json_string):
34273439
obj['switch_inline_query_chosen_chat'] = SwitchInlineQueryChosenChat.de_json(obj.get('switch_inline_query_chosen_chat'))
34283440
if 'copy_text' in obj:
34293441
obj['copy_text'] = CopyTextButton.de_json(obj.get('copy_text'))
3442+
if 'disabled' in obj:
3443+
obj['disabled'] = DisabledButton.de_json(obj.get('disabled'))
3444+
if 'callback_game' in obj:
3445+
obj['callback_game'] = CallbackGame.de_json(obj.get('callback_game'))
34303446

34313447
return cls(**obj)
34323448

@@ -3470,7 +3486,7 @@ def to_dict(self) -> dict:
34703486
if self.switch_inline_query_current_chat is not None:
34713487
data['switch_inline_query_current_chat'] = self.switch_inline_query_current_chat
34723488
if self.callback_game is not None:
3473-
data['callback_game'] = self.callback_game
3489+
data['callback_game'] = self.callback_game.to_dict()
34743490
if self.pay is not None:
34753491
data['pay'] = self.pay
34763492
if self.login_url is not None:
@@ -3483,6 +3499,8 @@ def to_dict(self) -> dict:
34833499
data['icon_custom_emoji_id'] = self.icon_custom_emoji_id
34843500
if self.style is not None:
34853501
data['style'] = self.style
3502+
if self.disabled is not None:
3503+
data['disabled'] = self.disabled.to_dict()
34863504
return data
34873505

34883506

@@ -5554,6 +5572,8 @@ def to_dict(self) -> dict:
55545572
data['video_url'] = self.video_url
55555573
data['mime_type'] = self.mime_type
55565574
data['thumbnail_url'] = self.thumbnail_url
5575+
if self.video_width is not None:
5576+
data['video_width'] = self.video_width
55575577
if self.video_height is not None:
55585578
data['video_height'] = self.video_height
55595579
if self.video_duration is not None:
@@ -5623,9 +5643,9 @@ def __init__(self, id: str, audio_url: str, title: str,
56235643
def to_dict(self) -> dict:
56245644
data = super().to_dict()
56255645
data['audio_url'] = self.audio_url
5626-
if self.performer:
5646+
if self.performer is not None:
56275647
data['performer'] = self.performer
5628-
if self.audio_duration:
5648+
if self.audio_duration is not None:
56295649
data['audio_duration'] = self.audio_duration
56305650
return data
56315651

@@ -5687,7 +5707,7 @@ def __init__(self, id: str, voice_url: str, title: str,
56875707
def to_dict(self) -> dict:
56885708
data = super().to_dict()
56895709
data['voice_url'] = self.voice_url
5690-
if self.voice_duration:
5710+
if self.voice_duration is not None:
56915711
data['voice_duration'] = self.voice_duration
56925712
return data
56935713

@@ -8234,7 +8254,6 @@ def to_dict(self) -> dict:
82348254
return data
82358255

82368256

8237-
82388257
class ChatLocation(JsonSerializable, JsonDeserializable, Dictionaryable):
82398258
"""
82408259
Represents a location to which a chat is connected.
@@ -8359,6 +8378,10 @@ def to_dict(self) -> dict:
83598378
data["pending_join_request_count"] = self.pending_join_request_count
83608379
if self.name is not None:
83618380
data["name"] = self.name
8381+
if self.subscription_period is not None:
8382+
data["subscription_period"] = self.subscription_period
8383+
if self.subscription_price is not None:
8384+
data["subscription_price"] = self.subscription_price
83628385
return data
83638386

83648387

@@ -8384,6 +8407,8 @@ class ProximityAlertTriggered(JsonDeserializable):
83848407
def de_json(cls, json_string):
83858408
if json_string is None: return None
83868409
obj = cls.check_json(json_string, dict_copy=False)
8410+
obj['traveler'] = User.de_json(obj.get('traveler'))
8411+
obj['watcher'] = User.de_json(obj.get('watcher'))
83878412
return cls(**obj)
83888413

83898414
def __init__(self, traveler: User, watcher: User, distance: int, **kwargs):
@@ -8487,7 +8512,6 @@ def __init__(self, *args, **kwargs):
84878512
super().__init__(*args, **kwargs)
84888513

84898514

8490-
84918515
class VideoChatParticipantsInvited(JsonDeserializable):
84928516
"""
84938517
This object represents a service message about new members invited to a video chat.
@@ -8641,6 +8665,7 @@ def to_dict(self) -> dict:
86418665
def de_json(cls, json_string):
86428666
if json_string is None: return None
86438667
obj = cls.check_json(json_string)
8668+
obj['web_app'] = WebAppInfo.de_json(obj.get('web_app'))
86448669
return cls(**obj)
86458670

86468671

@@ -8803,7 +8828,6 @@ def to_json(self):
88038828
return json.dumps(self.to_dict())
88048829

88058830

8806-
88078831
class InputFile:
88088832
"""
88098833
A class to send files through Telegram Bot API.
@@ -8994,7 +9018,6 @@ def __init__(self) -> None:
89949018
pass
89959019

89969020

8997-
89989021
class ForumTopic(JsonDeserializable):
89999022
"""
90009023
This object represents a forum topic.
@@ -9215,7 +9238,6 @@ def convert_input_sticker(self) -> Tuple[str, Optional[dict]]:
92159238
return self.to_json(), {self._sticker_name: self.sticker}
92169239

92179240

9218-
92199241
class SwitchInlineQueryChosenChat(JsonDeserializable, Dictionaryable, JsonSerializable):
92209242
"""
92219243
This object represents an inline button that switches the current user to inline mode in a chosen chat, with an optional default inline query.
@@ -9559,7 +9581,6 @@ def __init__(self, chat: Chat, message_id: int, date: int,
95599581
self.new_reaction: List[ReactionType] = new_reaction
95609582

95619583

9562-
95639584
class MessageReactionCountUpdated(JsonDeserializable):
95649585
"""
95659586
This object represents reaction changes on a message with anonymous reactions.
@@ -10824,7 +10845,6 @@ def can_reply(self) -> bool:
1082410845
return self.rights is not None and self.rights.can_reply
1082510846

1082610847

10827-
1082810848
class BusinessMessagesDeleted(JsonDeserializable):
1082910849
"""
1083010850
This object is received when messages are deleted from a connected business account.
@@ -12026,7 +12046,6 @@ def to_dict(self) -> dict:
1202612046
return data
1202712047

1202812048

12029-
1203012049
class InputPaidMediaVideo(InputPaidMedia):
1203112050
"""
1203212051
The paid media to send is a video.
@@ -17727,6 +17746,8 @@ def to_dict(self) -> dict:
1772717746
data['is_bordered'] = self.is_bordered
1772817747
if self.is_striped is not None:
1772917748
data['is_striped'] = self.is_striped
17749+
if self.is_compact is not None:
17750+
data['is_compact'] = self.is_compact
1773017751
if self.caption is not None:
1773117752
data['caption'] = RichText.richtext_to_dict(self.caption)
1773217753
return data
@@ -18036,7 +18057,7 @@ def to_dict(self) -> dict:
1803618057
if hasattr(self, 'document'):
1803718058
data['document'] = self.document.to_dict() if hasattr(self.document, 'to_dict') else self.document
1803818059
if self.caption is not None:
18039-
data['caption'] = RichBlockCaption.to_dict(self.caption) if hasattr(self.caption, 'to_dict') else self.caption
18060+
data['caption'] = self.caption.to_dict()
1804018061
return data
1804118062

1804218063

‎telebot/util.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
'checklist_tasks_done', 'checklist_tasks_added', 'direct_message_price_changed', 'suggested_post_refunded',
4545
'suggested_post_info', 'suggested_post_approved', 'suggested_post_approval_failed', 'suggested_post_declined',
4646
'suggested_post_paid', 'gift_upgrade_sent', 'chat_owner_left', 'chat_owner_changed', 'managed_bot_created',
47-
'poll_option_added', 'poll_option_deleted', 'community_chat_added', 'community_chat_removed'
47+
'poll_option_added', 'poll_option_deleted', 'community_chat_added', 'community_chat_removed',
48+
'community_chat_joined'
4849
]
4950

5051
#: All update types, should be used for allowed_updates parameter in polling.

‎tests/test_types.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ def test_json_inlinekeyboardbutton():
837837
assert result.switch_inline_query == 'query'
838838
assert result.switch_inline_query_current_chat == 'query'
839839
assert isinstance(result.switch_inline_query_chosen_chat, types.SwitchInlineQueryChosenChat)
840-
assert isinstance(result.callback_game, dict)
840+
assert isinstance(result.callback_game, types.CallbackGame)
841841
assert result.pay == True
842842
assert isinstance(result.login_url, types.LoginUrl)
843843
assert isinstance(result.copy_text, types.CopyTextButton)
@@ -865,7 +865,7 @@ def test_json_menubuttonwebapp():
865865
assert isinstance(result, types.MenuButtonWebApp)
866866
assert result.type == 'web_app'
867867
assert result.text == 'Open App'
868-
assert isinstance(result.web_app, dict)
868+
assert isinstance(result.web_app, types.WebAppInfo)
869869

870870

871871
def test_json_pollanswer():
@@ -2143,8 +2143,8 @@ def test_json_proximityalerttriggered():
21432143
json_str = r'{"traveler": {"id": 1, "is_bot": false, "first_name": "Test"}, "watcher": {"id": 1, "is_bot": false, "first_name": "Test"}, "distance": "test"}'
21442144
result = types.ProximityAlertTriggered.de_json(json_str)
21452145
assert isinstance(result, types.ProximityAlertTriggered)
2146-
assert isinstance(result.traveler, dict)
2147-
assert isinstance(result.watcher, dict)
2146+
assert isinstance(result.traveler, types.User)
2147+
assert isinstance(result.watcher, types.User)
21482148
assert result.distance == 'test'
21492149

21502150

0 commit comments

Comments
 (0)