mirror of
https://github.com/eternnoir/pyTelegramBotAPI.git
synced 2023-08-10 21:12:57 +03:00
Added class ChatMemberUpdated
* Added class `ChatMemberUpdated` to types * Simplified `de_json` functions in `WebhookInfo` and `Update` classes (for overall more consistent code) * changed `options_ids` to ´option_id` in class `PollAnswer` * Added test for `ChatMemberUpdated` class in `test_types.py` and added the fields `my_chat_member` and `chat_member` to the `Update` class and its tests
This commit is contained in:
parent
795f7fff7f
commit
0370a9f277
@ -94,23 +94,24 @@ class Update(JsonDeserializable):
|
|||||||
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
|
||||||
obj = cls.check_json(json_string)
|
obj = cls.check_json(json_string)
|
||||||
update_id = obj['update_id']
|
obj['message'] = Message.de_json(obj.get('message'))
|
||||||
message = Message.de_json(obj.get('message'))
|
obj['edited_message'] = Message.de_json(obj.get('edited_message'))
|
||||||
edited_message = Message.de_json(obj.get('edited_message'))
|
obj['channel_post'] = Message.de_json(obj.get('channel_post'))
|
||||||
channel_post = Message.de_json(obj.get('channel_post'))
|
obj['edited_channel_post'] = Message.de_json(obj.get('edited_channel_post'))
|
||||||
edited_channel_post = Message.de_json(obj.get('edited_channel_post'))
|
obj['inline_query'] = InlineQuery.de_json(obj.get('inline_query'))
|
||||||
inline_query = InlineQuery.de_json(obj.get('inline_query'))
|
obj['chosen_inline_result'] = ChosenInlineResult.de_json(obj.get('chosen_inline_result'))
|
||||||
chosen_inline_result = ChosenInlineResult.de_json(obj.get('chosen_inline_result'))
|
obj['callback_query'] = CallbackQuery.de_json(obj.get('callback_query'))
|
||||||
callback_query = CallbackQuery.de_json(obj.get('callback_query'))
|
obj['shipping_query'] = ShippingQuery.de_json(obj.get('shipping_query'))
|
||||||
shipping_query = ShippingQuery.de_json(obj.get('shipping_query'))
|
obj['pre_checkout_query'] = PreCheckoutQuery.de_json(obj.get('pre_checkout_query'))
|
||||||
pre_checkout_query = PreCheckoutQuery.de_json(obj.get('pre_checkout_query'))
|
obj['poll'] = Poll.de_json(obj.get('poll'))
|
||||||
poll = Poll.de_json(obj.get('poll'))
|
obj['poll_answer'] = PollAnswer.de_json(obj.get('poll_answer'))
|
||||||
poll_answer = PollAnswer.de_json(obj.get('poll_answer'))
|
obj['my_chat_member'] = ChatMemberUpdated.de_json(obj.get('my_chat_member'))
|
||||||
return cls(update_id, message, edited_message, channel_post, edited_channel_post, inline_query,
|
obj['chat_member'] = ChatMemberUpdated.de_json(obj.get('chat_member'))
|
||||||
chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll, poll_answer)
|
return cls(**obj)
|
||||||
|
|
||||||
def __init__(self, update_id, message, edited_message, channel_post, edited_channel_post, inline_query,
|
def __init__(self, update_id, message, edited_message, channel_post, edited_channel_post, inline_query,
|
||||||
chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll, poll_answer):
|
chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll, poll_answer,
|
||||||
|
my_chat_member, chat_member, **kwargs):
|
||||||
self.update_id = update_id
|
self.update_id = update_id
|
||||||
self.message = message
|
self.message = message
|
||||||
self.edited_message = edited_message
|
self.edited_message = edited_message
|
||||||
@ -123,6 +124,29 @@ class Update(JsonDeserializable):
|
|||||||
self.pre_checkout_query = pre_checkout_query
|
self.pre_checkout_query = pre_checkout_query
|
||||||
self.poll = poll
|
self.poll = poll
|
||||||
self.poll_answer = poll_answer
|
self.poll_answer = poll_answer
|
||||||
|
self.my_chat_member = my_chat_member
|
||||||
|
self.chat_member = chat_member
|
||||||
|
|
||||||
|
|
||||||
|
class ChatMemberUpdated(JsonDeserializable):
|
||||||
|
@classmethod
|
||||||
|
def de_json(cls, json_string):
|
||||||
|
if json_string is None: return None
|
||||||
|
obj = cls.check_json(json_string)
|
||||||
|
obj['chat'] = Chat.de_json(obj['chat'])
|
||||||
|
obj['from_user'] = User.de_json(obj.pop('from'))
|
||||||
|
obj['old_chat_member'] = ChatMember.de_json(obj['old_chat_member'])
|
||||||
|
obj['new_chat_member'] = ChatMember.de_json(obj['new_chat_member'])
|
||||||
|
obj['invite_link'] = ChatInviteLink.de_json(obj.get('invite_link'))
|
||||||
|
return cls(**obj)
|
||||||
|
|
||||||
|
def __init__(self, chat, from_user, date, old_chat_member, new_chat_member, invite_link=None, **kwargs):
|
||||||
|
self.chat: Chat = chat
|
||||||
|
self.from_user: User = from_user
|
||||||
|
self.date: int = date
|
||||||
|
self.old_chat_member: ChatMember = old_chat_member
|
||||||
|
self.new_chat_member: ChatMember = new_chat_member
|
||||||
|
self.invite_link: Union[ChatInviteLink, None] = invite_link
|
||||||
|
|
||||||
|
|
||||||
class WebhookInfo(JsonDeserializable):
|
class WebhookInfo(JsonDeserializable):
|
||||||
@ -130,19 +154,11 @@ class WebhookInfo(JsonDeserializable):
|
|||||||
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
|
||||||
obj = cls.check_json(json_string)
|
obj = cls.check_json(json_string)
|
||||||
url = obj['url']
|
return cls(**obj)
|
||||||
has_custom_certificate = obj['has_custom_certificate']
|
|
||||||
pending_update_count = obj['pending_update_count']
|
|
||||||
ip_address = obj.get('ip_address')
|
|
||||||
last_error_date = obj.get('last_error_date')
|
|
||||||
last_error_message = obj.get('last_error_message')
|
|
||||||
max_connections = obj.get('max_connections')
|
|
||||||
allowed_updates = obj.get('allowed_updates')
|
|
||||||
return cls(url, has_custom_certificate, pending_update_count, ip_address, last_error_date,
|
|
||||||
last_error_message, max_connections, allowed_updates)
|
|
||||||
|
|
||||||
def __init__(self, url, has_custom_certificate, pending_update_count, ip_address, last_error_date,
|
def __init__(self, url, has_custom_certificate, pending_update_count, ip_address=None,
|
||||||
last_error_message, max_connections, allowed_updates):
|
last_error_date=None, last_error_message=None, max_connections=None,
|
||||||
|
allowed_updates=None, **kwargs):
|
||||||
self.url = url
|
self.url = url
|
||||||
self.has_custom_certificate = has_custom_certificate
|
self.has_custom_certificate = has_custom_certificate
|
||||||
self.pending_update_count = pending_update_count
|
self.pending_update_count = pending_update_count
|
||||||
@ -767,7 +783,7 @@ class Venue(JsonDeserializable):
|
|||||||
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
|
||||||
obj = cls.check_json(json_string)
|
obj = cls.check_json(json_string)
|
||||||
obj['location'] = Location.de_json(obj.get('location'))
|
obj['location'] = Location.de_json(obj['location'])
|
||||||
return cls(**obj)
|
return cls(**obj)
|
||||||
|
|
||||||
def __init__(self, location, title, address, foursquare_id=None, foursquare_type=None,
|
def __init__(self, location, title, address, foursquare_id=None, foursquare_type=None,
|
||||||
@ -2473,14 +2489,12 @@ class PollAnswer(JsonSerializable, JsonDeserializable, Dictionaryable):
|
|||||||
if (json_string is None): return None
|
if (json_string is None): return None
|
||||||
obj = cls.check_json(json_string)
|
obj = cls.check_json(json_string)
|
||||||
obj['user'] = User.de_json(obj['user'])
|
obj['user'] = User.de_json(obj['user'])
|
||||||
# Strange name, i think it should be `option_ids` not `options_ids` maybe replace that
|
|
||||||
obj['options_ids'] = obj.pop('option_ids')
|
|
||||||
return cls(**obj)
|
return cls(**obj)
|
||||||
|
|
||||||
def __init__(self, poll_id, user, options_ids, **kwargs):
|
def __init__(self, poll_id, user, option_ids, **kwargs):
|
||||||
self.poll_id: str = poll_id
|
self.poll_id: str = poll_id
|
||||||
self.user: User = user
|
self.user: User = user
|
||||||
self.options_ids: List[int] = options_ids
|
self.option_ids: List[int] = option_ids
|
||||||
|
|
||||||
def to_json(self):
|
def to_json(self):
|
||||||
return json.dumps(self.to_dict())
|
return json.dumps(self.to_dict())
|
||||||
@ -2488,8 +2502,7 @@ class PollAnswer(JsonSerializable, JsonDeserializable, Dictionaryable):
|
|||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return {'poll_id': self.poll_id,
|
return {'poll_id': self.poll_id,
|
||||||
'user': self.user.to_dict(),
|
'user': self.user.to_dict(),
|
||||||
#should be `option_ids` not `options_ids` could cause problems here
|
'option_ids': self.option_ids}
|
||||||
'options_ids': self.options_ids}
|
|
||||||
|
|
||||||
|
|
||||||
class ChatLocation(JsonSerializable, JsonDeserializable, Dictionaryable):
|
class ChatLocation(JsonSerializable, JsonDeserializable, Dictionaryable):
|
||||||
|
@ -62,8 +62,11 @@ def update_type(message):
|
|||||||
pre_checkout_query = None
|
pre_checkout_query = None
|
||||||
poll = None
|
poll = None
|
||||||
poll_answer = None
|
poll_answer = None
|
||||||
|
my_chat_member = None
|
||||||
|
chat_member = None
|
||||||
return types.Update(1001234038283, message, edited_message, channel_post, edited_channel_post, inline_query,
|
return types.Update(1001234038283, message, edited_message, channel_post, edited_channel_post, inline_query,
|
||||||
chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll, poll_answer)
|
chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll, poll_answer,
|
||||||
|
my_chat_member, chat_member)
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture()
|
@pytest.fixture()
|
||||||
@ -78,9 +81,11 @@ def reply_to_message_update_type(reply_to_message):
|
|||||||
pre_checkout_query = None
|
pre_checkout_query = None
|
||||||
poll = None
|
poll = None
|
||||||
poll_answer = None
|
poll_answer = None
|
||||||
|
my_chat_member = None
|
||||||
|
chat_member = None
|
||||||
return types.Update(1001234038284, reply_to_message, edited_message, channel_post, edited_channel_post,
|
return types.Update(1001234038284, reply_to_message, edited_message, channel_post, edited_channel_post,
|
||||||
inline_query,
|
inline_query, chosen_inline_result, callback_query, shipping_query, pre_checkout_query,
|
||||||
chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll, poll_answer)
|
poll, poll_answer, my_chat_member, chat_member)
|
||||||
|
|
||||||
|
|
||||||
def next_handler(message):
|
def next_handler(message):
|
||||||
|
@ -458,8 +458,11 @@ class TestTeleBot:
|
|||||||
pre_checkout_query = None
|
pre_checkout_query = None
|
||||||
poll = None
|
poll = None
|
||||||
poll_answer = None
|
poll_answer = None
|
||||||
|
my_chat_member = None
|
||||||
|
chat_member = None
|
||||||
return types.Update(-1001234038283, message, edited_message, channel_post, edited_channel_post, inline_query,
|
return types.Update(-1001234038283, message, edited_message, channel_post, edited_channel_post, inline_query,
|
||||||
chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll, poll_answer)
|
chosen_inline_result, callback_query, shipping_query, pre_checkout_query, poll, poll_answer,
|
||||||
|
my_chat_member, chat_member)
|
||||||
|
|
||||||
def test_is_string_unicode(self):
|
def test_is_string_unicode(self):
|
||||||
s1 = u'string'
|
s1 = u'string'
|
||||||
|
@ -210,7 +210,7 @@ def test_json_poll_answer():
|
|||||||
poll_answer = types.PollAnswer.de_json(jsonstring)
|
poll_answer = types.PollAnswer.de_json(jsonstring)
|
||||||
assert poll_answer.poll_id == '5895675970559410186'
|
assert poll_answer.poll_id == '5895675970559410186'
|
||||||
assert isinstance(poll_answer.user, types.User)
|
assert isinstance(poll_answer.user, types.User)
|
||||||
assert poll_answer.options_ids == [1]
|
assert poll_answer.option_ids == [1]
|
||||||
|
|
||||||
|
|
||||||
def test_KeyboardButtonPollType():
|
def test_KeyboardButtonPollType():
|
||||||
@ -229,4 +229,14 @@ def test_json_chat_invite_link():
|
|||||||
assert not invite_link.is_primary
|
assert not invite_link.is_primary
|
||||||
assert not invite_link.is_revoked
|
assert not invite_link.is_revoked
|
||||||
assert invite_link.expire_date == 1624119999
|
assert invite_link.expire_date == 1624119999
|
||||||
assert invite_link.member_limit == 10
|
assert invite_link.member_limit == 10
|
||||||
|
|
||||||
|
def test_chat_member_updated():
|
||||||
|
json_string = r'{"chat": {"id": -1234567890123, "type": "supergroup", "title": "No Real Group", "username": "NoRealGroup"}, "from": {"id": 133869498, "is_bot": false, "first_name": "Vincent"}, "date": 1624119999, "old_chat_member": {"user": {"id": 77777777, "is_bot": false, "first_name": "Pepe"}, "status": "member"}, "new_chat_member": {"user": {"id": 77777777, "is_bot": false, "first_name": "Pepe"}, "status": "administrator"}}'
|
||||||
|
cm_updated = types.ChatMemberUpdated.de_json(json_string)
|
||||||
|
assert cm_updated.chat.id == -1234567890123
|
||||||
|
assert cm_updated.from_user.id == 133869498
|
||||||
|
assert cm_updated.date == 1624119999
|
||||||
|
assert cm_updated.old_chat_member.status == "member"
|
||||||
|
assert cm_updated.new_chat_member.status == "administrator"
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user