diff --git a/pyrogram/dispatcher.py b/pyrogram/dispatcher.py index 0f46d642..ccd8b9da 100644 --- a/pyrogram/dispatcher.py +++ b/pyrogram/dispatcher.py @@ -26,7 +26,7 @@ from pyrogram import utils from pyrogram.handlers import ( CallbackQueryHandler, MessageHandler, DeletedMessagesHandler, UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler, - ChosenInlineResultHandler, ChatMemberUpdatedHandler + ChosenInlineResultHandler, ChatMemberUpdatedHandler, ChatJoinRequestHandler ) from pyrogram.raw.types import ( UpdateNewMessage, UpdateNewChannelMessage, UpdateNewScheduledMessage, @@ -34,7 +34,8 @@ from pyrogram.raw.types import ( UpdateDeleteMessages, UpdateDeleteChannelMessages, UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery, UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll, - UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant + UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant, + UpdateBotChatInviteRequester ) log = logging.getLogger(__name__) @@ -106,6 +107,9 @@ class Dispatcher: async def chat_member_updated_parser(update, users, chats): return pyrogram.types.ChatMemberUpdated._parse(self.client, update, users, chats), ChatMemberUpdatedHandler + async def chat_join_request_parser(update, users, chats): + return pyrogram.types.ChatJoinRequest._parse(self.client, update, users, chats), ChatJoinRequestHandler + self.update_parsers = { Dispatcher.MESSAGE_UPDATES: message_parser, Dispatcher.DELETE_MESSAGES_UPDATES: deleted_messages_parser, @@ -114,7 +118,8 @@ class Dispatcher: (UpdateBotInlineQuery,): inline_query_parser, (UpdateMessagePoll,): poll_parser, (UpdateBotInlineSend,): chosen_inline_result_parser, - Dispatcher.CHAT_MEMBER_UPDATES: chat_member_updated_parser + Dispatcher.CHAT_MEMBER_UPDATES: chat_member_updated_parser, + (UpdateBotChatInviteRequester,): chat_join_request_parser } self.update_parsers = {key: value for key_tuple, value in self.update_parsers.items() for key in key_tuple} diff --git a/pyrogram/handlers/__init__.py b/pyrogram/handlers/__init__.py index 71ecab72..1312c6e5 100644 --- a/pyrogram/handlers/__init__.py +++ b/pyrogram/handlers/__init__.py @@ -17,6 +17,7 @@ # along with Pyrogram. If not, see . from .callback_query_handler import CallbackQueryHandler +from .chat_join_request_handler import ChatJoinRequestHandler from .chat_member_updated_handler import ChatMemberUpdatedHandler from .chosen_inline_result_handler import ChosenInlineResultHandler from .deleted_messages_handler import DeletedMessagesHandler diff --git a/pyrogram/handlers/chat_join_request_handler.py b/pyrogram/handlers/chat_join_request_handler.py new file mode 100644 index 00000000..f26abefc --- /dev/null +++ b/pyrogram/handlers/chat_join_request_handler.py @@ -0,0 +1,47 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2021 Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from .handler import Handler + + +class ChatJoinRequestHandler(Handler): + """The ChatJoinRequest handler class. Used to handle join chat requests. + It is intended to be used with :meth:`~pyrogram.Client.add_handler`. + + For a nicer way to register this handler, have a look at the + :meth:`~pyrogram.Client.on_chat_join_request` decorator. + + Parameters: + callback (``callable``): + Pass a function that will be called when a new ChatJoinRequest event arrives. It takes + *(client, chat_join_request)* as positional arguments (look at the section below for a detailed + description). + + filters (:obj:`Filters`): + Pass one or more filters to allow only a subset of updates to be passed in your callback function. + + Other parameters: + client (:obj:`~pyrogram.Client`): + The Client itself, useful when you want to call other API methods inside the handler. + + chat_join_request (:obj:`~pyrogram.types.ChatJoinRequest`): + The received chat join request. + """ + + def __init__(self, callback: callable, filters=None): + super().__init__(callback, filters) diff --git a/pyrogram/methods/decorators/__init__.py b/pyrogram/methods/decorators/__init__.py index 01ddffce..384560fe 100644 --- a/pyrogram/methods/decorators/__init__.py +++ b/pyrogram/methods/decorators/__init__.py @@ -17,6 +17,7 @@ # along with Pyrogram. If not, see . from .on_callback_query import OnCallbackQuery +from .on_chat_join_request import OnChatJoinRequest from .on_chat_member_updated import OnChatMemberUpdated from .on_chosen_inline_result import OnChosenInlineResult from .on_deleted_messages import OnDeletedMessages @@ -38,6 +39,7 @@ class Decorators( OnInlineQuery, OnPoll, OnChosenInlineResult, - OnChatMemberUpdated + OnChatMemberUpdated, + OnChatJoinRequest ): pass diff --git a/pyrogram/methods/decorators/on_chat_join_request.py b/pyrogram/methods/decorators/on_chat_join_request.py new file mode 100644 index 00000000..93d48c10 --- /dev/null +++ b/pyrogram/methods/decorators/on_chat_join_request.py @@ -0,0 +1,61 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2021 Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from typing import Callable + +import pyrogram +from pyrogram.filters import Filter +from pyrogram.scaffold import Scaffold + + +class OnChatJoinRequest(Scaffold): + def on_chat_join_request( + self=None, + filters=None, + group: int = 0 + ) -> callable: + """Decorator for handling chat join requests. + + This does the same thing as :meth:`~pyrogram.Client.add_handler` using the + :obj:`~pyrogram.handlers.ChatJoinRequestHandler`. + + Parameters: + filters (:obj:`~pyrogram.filters`, *optional*): + Pass one or more filters to allow only a subset of updates to be passed in your function. + + group (``int``, *optional*): + The group identifier, defaults to 0. + """ + + def decorator(func: Callable) -> Callable: + if isinstance(self, pyrogram.Client): + self.add_handler(pyrogram.handlers.ChatJoinRequestHandler(func, filters), group) + elif isinstance(self, Filter) or self is None: + if not hasattr(func, "handlers"): + func.handlers = [] + + func.handlers.append( + ( + pyrogram.handlers.ChatJoinRequestHandler(func, self), + group if filters is None else filters + ) + ) + + return func + + return decorator diff --git a/pyrogram/types/user_and_chats/__init__.py b/pyrogram/types/user_and_chats/__init__.py index 39e5d786..22455486 100644 --- a/pyrogram/types/user_and_chats/__init__.py +++ b/pyrogram/types/user_and_chats/__init__.py @@ -21,6 +21,7 @@ from .chat_admin_with_invite_links import ChatAdminWithInviteLinks from .chat_event import ChatEvent from .chat_event_filter import ChatEventFilter from .chat_invite_link import ChatInviteLink +from .chat_join_request import ChatJoinRequest from .chat_member import ChatMember from .chat_member_updated import ChatMemberUpdated from .chat_permissions import ChatPermissions @@ -53,5 +54,6 @@ __all__ = [ "VoiceChatEnded", "VoiceChatMembersInvited", "ChatMemberUpdated", - "VoiceChatScheduled" + "VoiceChatScheduled", + "ChatJoinRequest" ] diff --git a/pyrogram/types/user_and_chats/chat_join_request.py b/pyrogram/types/user_and_chats/chat_join_request.py new file mode 100644 index 00000000..4ed7c1ed --- /dev/null +++ b/pyrogram/types/user_and_chats/chat_join_request.py @@ -0,0 +1,82 @@ +# Pyrogram - Telegram MTProto API Client Library for Python +# Copyright (C) 2017-2021 Dan +# +# This file is part of Pyrogram. +# +# Pyrogram is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published +# by the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Pyrogram is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with Pyrogram. If not, see . + +from typing import Dict + +import pyrogram +from pyrogram import raw, utils +from pyrogram import types +from ..object import Object +from ..update import Update + + +class ChatJoinRequest(Object, Update): + """Represents a join request sent to a chat. + + Parameters: + chat (:obj:`~pyrogram.types.Chat`): + Chat to which the request was sent. + + from_user (:obj:`~pyrogram.types.User`): + User that sent the join request. + + date (``int``): + Date the request was sent in Unix time + + bio (``str``, *optional*): + Bio of the user. + + invite_link (:obj:`~pyrogram.types.ChatInviteLink`, *optional*): + Chat invite link that was used by the user to send the join request. + """ + + def __init__( + self, + *, + client: "pyrogram.Client" = None, + chat: "types.Chat", + from_user: "types.User", + date: int, + bio: str = None, + invite_link: "types.ChatInviteLink" = None + ): + super().__init__(client) + + self.chat = chat + self.from_user = from_user + self.date = date + self.bio = bio + self.invite_link = invite_link + + @staticmethod + def _parse( + client: "pyrogram.Client", + update: "raw.types.UpdateBotChatInviteRequester", + users: Dict[int, "raw.types.User"], + chats: Dict[int, "raw.types.Chat"] + ) -> "ChatJoinRequest": + chat_id = utils.get_raw_peer_id(update.peer) + + return ChatJoinRequest( + chat=types.Chat._parse_chat(client, chats[chat_id]), + from_user=types.User._parse(client, users[update.user_id]), + date=update.date, + bio=update.about, + invite_link=types.ChatInviteLink._parse(client, update.invite, users), + client=client + )