From 06c878212704cc5e41622d9d18a2faaa2028da83 Mon Sep 17 00:00:00 2001 From: _run Date: Fri, 5 Nov 2021 23:22:03 +0500 Subject: [PATCH] Little update Allowed other handlers, checked methods and other things --- telebot/__init__.py | 63 +++++++++++++++++++++++++++++++++++++++----- telebot/apihelper.py | 12 +++++++-- telebot/types.py | 31 +++++++++++++++++++--- 3 files changed, 94 insertions(+), 12 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 64cf2c7..185d1ee 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -185,6 +185,7 @@ class TeleBot: self.poll_answer_handlers = [] self.my_chat_member_handlers = [] self.chat_member_handlers = [] + self.chat_join_request_handlers = [] self.custom_filters = {} self.state_handlers = [] @@ -205,7 +206,8 @@ class TeleBot: 'poll': [], 'poll_answer': [], 'my_chat_member': [], - 'chat_member': [] + 'chat_member': [], + 'chat_join_request': [] } self.default_middleware_handlers = [] @@ -426,6 +428,7 @@ class TeleBot: new_poll_answers = None new_my_chat_members = None new_chat_members = None + chat_join_request = None for update in updates: if apihelper.ENABLE_MIDDLEWARE: @@ -480,6 +483,9 @@ class TeleBot: if update.chat_member: if new_chat_members is None: new_chat_members = [] new_chat_members.append(update.chat_member) + if update.chat_join_request: + if chat_join_request is None: chat_join_request = [] + chat_join_request.append(update.chat_join_request) if new_messages: self.process_new_messages(new_messages) @@ -507,6 +513,9 @@ class TeleBot: self.process_new_my_chat_member(new_my_chat_members) if new_chat_members: self.process_new_chat_member(new_chat_members) + if chat_join_request: + self.process_chat_join_request(chat_join_request) + def process_new_messages(self, new_messages): self._notify_next_handlers(new_messages) @@ -550,6 +559,9 @@ class TeleBot: def process_new_chat_member(self, chat_members): self._notify_command_handlers(self.chat_member_handlers, chat_members) + def process_chat_join_request(self, chat_join_request): + self._notify_command_handlers(self.chat_join_request_handlers, chat_join_request) + def process_middlewares(self, update): for update_type, middlewares in self.typed_middleware_handlers.items(): if getattr(update, update_type) is not None: @@ -1667,9 +1679,11 @@ class TeleBot: return apihelper.set_chat_permissions(self.token, chat_id, permissions) def create_chat_invite_link( - self, chat_id: Union[int, str], + self, chat_id: Union[int, str], + name: Optional[str]=None, expire_date: Optional[Union[int, datetime]]=None, - member_limit: Optional[int]=None) -> types.ChatInviteLink: + member_limit: Optional[int]=None, + creates_join_request: Optional[bool]=None) -> types.ChatInviteLink: """ Use this method to create an additional invite link for a chat. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. @@ -1681,13 +1695,15 @@ class TeleBot: :return: """ return types.ChatInviteLink.de_json( - apihelper.create_chat_invite_link(self.token, chat_id, expire_date, member_limit) + apihelper.create_chat_invite_link(self.token, chat_id, name, expire_date, member_limit, creates_join_request) ) def edit_chat_invite_link( - self, chat_id: Union[int, str], invite_link: str, + self, chat_id: Union[int, str], name: Optional[str]=None, + invite_link: Optional[str] = None, expire_date: Optional[Union[int, datetime]]=None, - member_limit: Optional[int]=None) -> types.ChatInviteLink: + member_limit: Optional[int]=None , + creates_join_request: Optional[bool]=None) -> types.ChatInviteLink: """ Use this method to edit a non-primary invite link created by the bot. The bot must be an administrator in the chat for this to work and must have the appropriate admin rights. @@ -1701,7 +1717,7 @@ class TeleBot: :return: """ return types.ChatInviteLink.de_json( - apihelper.edit_chat_invite_link(self.token, chat_id, invite_link, expire_date, member_limit) + apihelper.edit_chat_invite_link(self.token, chat_id, name, invite_link, expire_date, member_limit, creates_join_request) ) def revoke_chat_invite_link( @@ -3148,6 +3164,39 @@ class TeleBot: handler_dict = self._build_handler_dict(callback, func=func, **kwargs) self.add_chat_member_handler(handler_dict) + def chat_join_request_handler(self, func=None, **kwargs): + """ + chat_join_request handler + :param func: + :param kwargs: + :return: + """ + + def decorator(handler): + handler_dict = self._build_handler_dict(handler, func=func, **kwargs) + self.add_chat_join_request_handler(handler_dict) + return handler + + return decorator + + def add_chat_join_request_handler(self, handler_dict): + """ + Adds a chat_join_request handler + :param handler_dict: + :return: + """ + self.chat_join_request_handlers.append(handler_dict) + + def register_chat_join_request_handler(self, callback, func=None, **kwargs): + """ + Registers chat join request handler. + :param callback: function to be called + :param func: + :return: decorated function + """ + handler_dict = self._build_handler_dict(callback, func=func, **kwargs) + self.add_chat_join_request_handler(handler_dict) + def _test_message_handler(self, message_handler, message): """ Test message handler diff --git a/telebot/apihelper.py b/telebot/apihelper.py index 7b7ee96..4d4f919 100644 --- a/telebot/apihelper.py +++ b/telebot/apihelper.py @@ -973,7 +973,7 @@ def set_chat_permissions(token, chat_id, permissions): return _make_request(token, method_url, params=payload, method='post') -def create_chat_invite_link(token, chat_id, expire_date, member_limit): +def create_chat_invite_link(token, chat_id, name, expire_date, member_limit, creates_join_request): method_url = 'createChatInviteLink' payload = { 'chat_id': chat_id @@ -986,11 +986,15 @@ def create_chat_invite_link(token, chat_id, expire_date, member_limit): payload['expire_date'] = expire_date if member_limit: payload['member_limit'] = member_limit + if creates_join_request: + payload['creates_join_request'] = creates_join_request + if name: + payload['name'] = name return _make_request(token, method_url, params=payload, method='post') -def edit_chat_invite_link(token, chat_id, invite_link, expire_date, member_limit): +def edit_chat_invite_link(token, chat_id, invite_link, name, expire_date, member_limit, creates_join_request): method_url = 'editChatInviteLink' payload = { 'chat_id': chat_id, @@ -1005,6 +1009,10 @@ def edit_chat_invite_link(token, chat_id, invite_link, expire_date, member_limit if member_limit is not None: payload['member_limit'] = member_limit + if name: + payload['name'] = name + if creates_join_request: + payload['creates_join_request'] = creates_join_request return _make_request(token, method_url, params=payload, method='post') diff --git a/telebot/types.py b/telebot/types.py index 8f68218..99b2559 100644 --- a/telebot/types.py +++ b/telebot/types.py @@ -107,6 +107,7 @@ class Update(JsonDeserializable): poll_answer = PollAnswer.de_json(obj.get('poll_answer')) my_chat_member = ChatMemberUpdated.de_json(obj.get('my_chat_member')) chat_member = ChatMemberUpdated.de_json(obj.get('chat_member')) + chat_join_request = ChatJoinRequest.de_json(obj.get('chat_join_request')) return cls(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, my_chat_member, chat_member) @@ -166,6 +167,22 @@ class ChatMemberUpdated(JsonDeserializable): dif[key] = [old[key], new[key]] return dif +class ChatJoinRequest(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.de_json(obj['from']) + + return cls(**obj) + + def __init__(self, chat, from_user, date, bio=None, invite_link=None, **kwargs): + self.chat = Chat = chat + self.from_user: User = from_user + self.date: int = date + self.bio: Optional[str] = bio + self.invite_link: Optional[ChatInviteLink] = invite_link class WebhookInfo(JsonDeserializable): @classmethod @@ -2752,14 +2769,17 @@ class ChatInviteLink(JsonSerializable, JsonDeserializable, Dictionaryable): obj['creator'] = User.de_json(obj['creator']) return cls(**obj) - def __init__(self, invite_link, creator, is_primary, is_revoked, - expire_date=None, member_limit=None, **kwargs): + def __init__(self, invite_link, creator, creates_join_request , is_primary, is_revoked, + name=None, expire_date=None, member_limit=None, pending_join_request_count=None, **kwargs): self.invite_link: str = invite_link self.creator: User = creator + self.creates_join_request: bool = creates_join_request self.is_primary: bool = is_primary self.is_revoked: bool = is_revoked + self.name: str = name self.expire_date: int = expire_date self.member_limit: int = member_limit + self.pending_join_request_count: int = pending_join_request_count def to_json(self): return json.dumps(self.to_dict()) @@ -2769,12 +2789,17 @@ class ChatInviteLink(JsonSerializable, JsonDeserializable, Dictionaryable): "invite_link": self.invite_link, "creator": self.creator.to_dict(), "is_primary": self.is_primary, - "is_revoked": self.is_revoked + "is_revoked": self.is_revoked, + "creates_join_request": self.creates_join_request } if self.expire_date: json_dict["expire_date"] = self.expire_date if self.member_limit: json_dict["member_limit"] = self.member_limit + if self.pending_join_request_count: + json_dict["pending_join_request_count"] = self.pending_join_request_count + if self.name: + json_dict["name"] = self.name return json_dict