Skip to content

Commit d103ae4

Browse files
committed
Add support for ChatJoinRequest events
1 parent 8f8c85e commit d103ae4

7 files changed

Lines changed: 205 additions & 5 deletions

File tree

pyrogram/dispatcher.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@
2626
from pyrogram.handlers import (
2727
CallbackQueryHandler, MessageHandler, DeletedMessagesHandler,
2828
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler,
29-
ChosenInlineResultHandler, ChatMemberUpdatedHandler
29+
ChosenInlineResultHandler, ChatMemberUpdatedHandler, ChatJoinRequestHandler
3030
)
3131
from pyrogram.raw.types import (
3232
UpdateNewMessage, UpdateNewChannelMessage, UpdateNewScheduledMessage,
3333
UpdateEditMessage, UpdateEditChannelMessage,
3434
UpdateDeleteMessages, UpdateDeleteChannelMessages,
3535
UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery,
3636
UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll,
37-
UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant
37+
UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant,
38+
UpdateBotChatInviteRequester
3839
)
3940

4041
log = logging.getLogger(__name__)
@@ -106,6 +107,9 @@ async def chosen_inline_result_parser(update, users, chats):
106107
async def chat_member_updated_parser(update, users, chats):
107108
return pyrogram.types.ChatMemberUpdated._parse(self.client, update, users, chats), ChatMemberUpdatedHandler
108109

110+
async def chat_join_request_parser(update, users, chats):
111+
return pyrogram.types.ChatJoinRequest._parse(self.client, update, users, chats), ChatJoinRequestHandler
112+
109113
self.update_parsers = {
110114
Dispatcher.MESSAGE_UPDATES: message_parser,
111115
Dispatcher.DELETE_MESSAGES_UPDATES: deleted_messages_parser,
@@ -114,7 +118,8 @@ async def chat_member_updated_parser(update, users, chats):
114118
(UpdateBotInlineQuery,): inline_query_parser,
115119
(UpdateMessagePoll,): poll_parser,
116120
(UpdateBotInlineSend,): chosen_inline_result_parser,
117-
Dispatcher.CHAT_MEMBER_UPDATES: chat_member_updated_parser
121+
Dispatcher.CHAT_MEMBER_UPDATES: chat_member_updated_parser,
122+
(UpdateBotChatInviteRequester,): chat_join_request_parser
118123
}
119124

120125
self.update_parsers = {key: value for key_tuple, value in self.update_parsers.items() for key in key_tuple}

pyrogram/handlers/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from .callback_query_handler import CallbackQueryHandler
20+
from .chat_join_request_handler import ChatJoinRequestHandler
2021
from .chat_member_updated_handler import ChatMemberUpdatedHandler
2122
from .chosen_inline_result_handler import ChosenInlineResultHandler
2223
from .deleted_messages_handler import DeletedMessagesHandler
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from .handler import Handler
20+
21+
22+
class ChatJoinRequestHandler(Handler):
23+
"""The ChatJoinRequest handler class. Used to handle join chat requests.
24+
It is intended to be used with :meth:`~pyrogram.Client.add_handler`.
25+
26+
For a nicer way to register this handler, have a look at the
27+
:meth:`~pyrogram.Client.on_chat_join_request` decorator.
28+
29+
Parameters:
30+
callback (``callable``):
31+
Pass a function that will be called when a new ChatJoinRequest event arrives. It takes
32+
*(client, chat_join_request)* as positional arguments (look at the section below for a detailed
33+
description).
34+
35+
filters (:obj:`Filters`):
36+
Pass one or more filters to allow only a subset of updates to be passed in your callback function.
37+
38+
Other parameters:
39+
client (:obj:`~pyrogram.Client`):
40+
The Client itself, useful when you want to call other API methods inside the handler.
41+
42+
chat_join_request (:obj:`~pyrogram.types.ChatJoinRequest`):
43+
The received chat join request.
44+
"""
45+
46+
def __init__(self, callback: callable, filters=None):
47+
super().__init__(callback, filters)

pyrogram/methods/decorators/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
1818

1919
from .on_callback_query import OnCallbackQuery
20+
from .on_chat_join_request import OnChatJoinRequest
2021
from .on_chat_member_updated import OnChatMemberUpdated
2122
from .on_chosen_inline_result import OnChosenInlineResult
2223
from .on_deleted_messages import OnDeletedMessages
@@ -38,6 +39,7 @@ class Decorators(
3839
OnInlineQuery,
3940
OnPoll,
4041
OnChosenInlineResult,
41-
OnChatMemberUpdated
42+
OnChatMemberUpdated,
43+
OnChatJoinRequest
4244
):
4345
pass
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import Callable
20+
21+
import pyrogram
22+
from pyrogram.filters import Filter
23+
from pyrogram.scaffold import Scaffold
24+
25+
26+
class OnChatJoinRequest(Scaffold):
27+
def on_chat_join_request(
28+
self=None,
29+
filters=None,
30+
group: int = 0
31+
) -> callable:
32+
"""Decorator for handling chat join requests.
33+
34+
This does the same thing as :meth:`~pyrogram.Client.add_handler` using the
35+
:obj:`~pyrogram.handlers.ChatJoinRequestHandler`.
36+
37+
Parameters:
38+
filters (:obj:`~pyrogram.filters`, *optional*):
39+
Pass one or more filters to allow only a subset of updates to be passed in your function.
40+
41+
group (``int``, *optional*):
42+
The group identifier, defaults to 0.
43+
"""
44+
45+
def decorator(func: Callable) -> Callable:
46+
if isinstance(self, pyrogram.Client):
47+
self.add_handler(pyrogram.handlers.ChatJoinRequestHandler(func, filters), group)
48+
elif isinstance(self, Filter) or self is None:
49+
if not hasattr(func, "handlers"):
50+
func.handlers = []
51+
52+
func.handlers.append(
53+
(
54+
pyrogram.handlers.ChatJoinRequestHandler(func, self),
55+
group if filters is None else filters
56+
)
57+
)
58+
59+
return func
60+
61+
return decorator

pyrogram/types/user_and_chats/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from .chat_event import ChatEvent
2222
from .chat_event_filter import ChatEventFilter
2323
from .chat_invite_link import ChatInviteLink
24+
from .chat_join_request import ChatJoinRequest
2425
from .chat_member import ChatMember
2526
from .chat_member_updated import ChatMemberUpdated
2627
from .chat_permissions import ChatPermissions
@@ -53,5 +54,6 @@
5354
"VoiceChatEnded",
5455
"VoiceChatMembersInvited",
5556
"ChatMemberUpdated",
56-
"VoiceChatScheduled"
57+
"VoiceChatScheduled",
58+
"ChatJoinRequest"
5759
]
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Pyrogram - Telegram MTProto API Client Library for Python
2+
# Copyright (C) 2017-2021 Dan <https://github.com/delivrance>
3+
#
4+
# This file is part of Pyrogram.
5+
#
6+
# Pyrogram is free software: you can redistribute it and/or modify
7+
# it under the terms of the GNU Lesser General Public License as published
8+
# by the Free Software Foundation, either version 3 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# Pyrogram is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU Lesser General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU Lesser General Public License
17+
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from typing import Dict
20+
21+
import pyrogram
22+
from pyrogram import raw, utils
23+
from pyrogram import types
24+
from ..object import Object
25+
from ..update import Update
26+
27+
28+
class ChatJoinRequest(Object, Update):
29+
"""Represents a join request sent to a chat.
30+
31+
Parameters:
32+
chat (:obj:`~pyrogram.types.Chat`):
33+
Chat to which the request was sent.
34+
35+
from_user (:obj:`~pyrogram.types.User`):
36+
User that sent the join request.
37+
38+
date (``int``):
39+
Date the request was sent in Unix time
40+
41+
bio (``str``, *optional*):
42+
Bio of the user.
43+
44+
invite_link (:obj:`~pyrogram.types.ChatInviteLink`, *optional*):
45+
Chat invite link that was used by the user to send the join request.
46+
"""
47+
48+
def __init__(
49+
self,
50+
*,
51+
client: "pyrogram.Client" = None,
52+
chat: "types.Chat",
53+
from_user: "types.User",
54+
date: int,
55+
bio: str = None,
56+
invite_link: "types.ChatInviteLink" = None
57+
):
58+
super().__init__(client)
59+
60+
self.chat = chat
61+
self.from_user = from_user
62+
self.date = date
63+
self.bio = bio
64+
self.invite_link = invite_link
65+
66+
@staticmethod
67+
def _parse(
68+
client: "pyrogram.Client",
69+
update: "raw.types.UpdateBotChatInviteRequester",
70+
users: Dict[int, "raw.types.User"],
71+
chats: Dict[int, "raw.types.Chat"]
72+
) -> "ChatJoinRequest":
73+
chat_id = utils.get_raw_peer_id(update.peer)
74+
75+
return ChatJoinRequest(
76+
chat=types.Chat._parse_chat(client, chats[chat_id]),
77+
from_user=types.User._parse(client, users[update.user_id]),
78+
date=update.date,
79+
bio=update.about,
80+
invite_link=types.ChatInviteLink._parse(client, update.invite, users),
81+
client=client
82+
)

0 commit comments

Comments
 (0)