Add support for ChatJoinRequest events

This commit is contained in:
Dan 2021-12-22 14:34:12 +01:00
parent 8f8c85e8f3
commit d103ae48fe
7 changed files with 205 additions and 5 deletions

View File

@ -26,7 +26,7 @@ from pyrogram import utils
from pyrogram.handlers import ( from pyrogram.handlers import (
CallbackQueryHandler, MessageHandler, DeletedMessagesHandler, CallbackQueryHandler, MessageHandler, DeletedMessagesHandler,
UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler, UserStatusHandler, RawUpdateHandler, InlineQueryHandler, PollHandler,
ChosenInlineResultHandler, ChatMemberUpdatedHandler ChosenInlineResultHandler, ChatMemberUpdatedHandler, ChatJoinRequestHandler
) )
from pyrogram.raw.types import ( from pyrogram.raw.types import (
UpdateNewMessage, UpdateNewChannelMessage, UpdateNewScheduledMessage, UpdateNewMessage, UpdateNewChannelMessage, UpdateNewScheduledMessage,
@ -34,7 +34,8 @@ from pyrogram.raw.types import (
UpdateDeleteMessages, UpdateDeleteChannelMessages, UpdateDeleteMessages, UpdateDeleteChannelMessages,
UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery, UpdateBotCallbackQuery, UpdateInlineBotCallbackQuery,
UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll, UpdateUserStatus, UpdateBotInlineQuery, UpdateMessagePoll,
UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant UpdateBotInlineSend, UpdateChatParticipant, UpdateChannelParticipant,
UpdateBotChatInviteRequester
) )
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
@ -106,6 +107,9 @@ class Dispatcher:
async def chat_member_updated_parser(update, users, chats): async def chat_member_updated_parser(update, users, chats):
return pyrogram.types.ChatMemberUpdated._parse(self.client, update, users, chats), ChatMemberUpdatedHandler 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 = { self.update_parsers = {
Dispatcher.MESSAGE_UPDATES: message_parser, Dispatcher.MESSAGE_UPDATES: message_parser,
Dispatcher.DELETE_MESSAGES_UPDATES: deleted_messages_parser, Dispatcher.DELETE_MESSAGES_UPDATES: deleted_messages_parser,
@ -114,7 +118,8 @@ class Dispatcher:
(UpdateBotInlineQuery,): inline_query_parser, (UpdateBotInlineQuery,): inline_query_parser,
(UpdateMessagePoll,): poll_parser, (UpdateMessagePoll,): poll_parser,
(UpdateBotInlineSend,): chosen_inline_result_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} self.update_parsers = {key: value for key_tuple, value in self.update_parsers.items() for key in key_tuple}

View File

@ -17,6 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from .callback_query_handler import CallbackQueryHandler from .callback_query_handler import CallbackQueryHandler
from .chat_join_request_handler import ChatJoinRequestHandler
from .chat_member_updated_handler import ChatMemberUpdatedHandler from .chat_member_updated_handler import ChatMemberUpdatedHandler
from .chosen_inline_result_handler import ChosenInlineResultHandler from .chosen_inline_result_handler import ChosenInlineResultHandler
from .deleted_messages_handler import DeletedMessagesHandler from .deleted_messages_handler import DeletedMessagesHandler

View File

@ -0,0 +1,47 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2021 Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
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)

View File

@ -17,6 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>. # along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
from .on_callback_query import OnCallbackQuery from .on_callback_query import OnCallbackQuery
from .on_chat_join_request import OnChatJoinRequest
from .on_chat_member_updated import OnChatMemberUpdated from .on_chat_member_updated import OnChatMemberUpdated
from .on_chosen_inline_result import OnChosenInlineResult from .on_chosen_inline_result import OnChosenInlineResult
from .on_deleted_messages import OnDeletedMessages from .on_deleted_messages import OnDeletedMessages
@ -38,6 +39,7 @@ class Decorators(
OnInlineQuery, OnInlineQuery,
OnPoll, OnPoll,
OnChosenInlineResult, OnChosenInlineResult,
OnChatMemberUpdated OnChatMemberUpdated,
OnChatJoinRequest
): ):
pass pass

View File

@ -0,0 +1,61 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2021 Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
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

View File

@ -21,6 +21,7 @@ from .chat_admin_with_invite_links import ChatAdminWithInviteLinks
from .chat_event import ChatEvent from .chat_event import ChatEvent
from .chat_event_filter import ChatEventFilter from .chat_event_filter import ChatEventFilter
from .chat_invite_link import ChatInviteLink from .chat_invite_link import ChatInviteLink
from .chat_join_request import ChatJoinRequest
from .chat_member import ChatMember from .chat_member import ChatMember
from .chat_member_updated import ChatMemberUpdated from .chat_member_updated import ChatMemberUpdated
from .chat_permissions import ChatPermissions from .chat_permissions import ChatPermissions
@ -53,5 +54,6 @@ __all__ = [
"VoiceChatEnded", "VoiceChatEnded",
"VoiceChatMembersInvited", "VoiceChatMembersInvited",
"ChatMemberUpdated", "ChatMemberUpdated",
"VoiceChatScheduled" "VoiceChatScheduled",
"ChatJoinRequest"
] ]

View File

@ -0,0 +1,82 @@
# Pyrogram - Telegram MTProto API Client Library for Python
# Copyright (C) 2017-2021 Dan <https://github.com/delivrance>
#
# 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 <http://www.gnu.org/licenses/>.
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
)