2021-11-08 19:02:20 +01:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# A library that provides a Python interface to the Telegram Bot API
|
2024-02-19 20:06:25 +01:00
|
|
|
# Copyright (C) 2015-2024
|
2021-11-08 19:02:20 +01:00
|
|
|
# Leandro Toledo de Souza <devs@python-telegram-bot.org>
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program 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 Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser Public License
|
|
|
|
# along with this program. If not, see [http://www.gnu.org/licenses/].
|
|
|
|
"""This module contains an object that represents a Telegram ChatJoinRequest."""
|
2024-12-15 10:26:37 +01:00
|
|
|
import datetime as dtm
|
2022-10-07 11:51:53 +02:00
|
|
|
from typing import TYPE_CHECKING, Optional
|
2021-11-08 19:02:20 +01:00
|
|
|
|
2022-05-05 09:27:54 +02:00
|
|
|
from telegram._chat import Chat
|
|
|
|
from telegram._chatinvitelink import ChatInviteLink
|
|
|
|
from telegram._telegramobject import TelegramObject
|
|
|
|
from telegram._user import User
|
2023-04-18 16:16:23 +02:00
|
|
|
from telegram._utils.datetime import extract_tzinfo_from_defaults, from_timestamp
|
2021-10-10 15:10:21 +02:00
|
|
|
from telegram._utils.defaultvalue import DEFAULT_NONE
|
|
|
|
from telegram._utils.types import JSONDict, ODVInput
|
2021-11-08 19:02:20 +01:00
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from telegram import Bot
|
|
|
|
|
|
|
|
|
|
|
|
class ChatJoinRequest(TelegramObject):
|
|
|
|
"""This object represents a join request sent to a chat.
|
|
|
|
|
|
|
|
Objects of this class are comparable in terms of equality. Two objects of this class are
|
|
|
|
considered equal, if their :attr:`chat`, :attr:`from_user` and :attr:`date` are equal.
|
|
|
|
|
2021-12-11 15:21:56 +01:00
|
|
|
Note:
|
2023-02-08 11:47:21 +01:00
|
|
|
* Since Bot API 5.5, bots are allowed to contact users who sent a join request to a chat
|
|
|
|
where the bot is an administrator with the
|
2023-03-25 19:18:04 +01:00
|
|
|
:attr:`~telegram.ChatMemberAdministrator.can_invite_users` administrator right - even
|
2023-02-08 11:47:21 +01:00
|
|
|
if the user never interacted with the bot before.
|
|
|
|
* Telegram does not guarantee that :attr:`from_user.id <from_user>` coincides with the
|
|
|
|
``chat_id`` of the user. Please use :attr:`user_chat_id` to contact the user in
|
|
|
|
response to their join request.
|
2021-12-11 15:21:56 +01:00
|
|
|
|
2021-11-08 19:02:20 +01:00
|
|
|
.. versionadded:: 13.8
|
2023-02-08 11:47:21 +01:00
|
|
|
.. versionchanged:: 20.1
|
|
|
|
In Bot API 6.5 the argument :paramref:`user_chat_id` was added, which changes the position
|
|
|
|
of the optional arguments :paramref:`bio` and :paramref:`invite_link`.
|
2021-11-08 19:02:20 +01:00
|
|
|
|
|
|
|
Args:
|
|
|
|
chat (:class:`telegram.Chat`): Chat to which the request was sent.
|
|
|
|
from_user (:class:`telegram.User`): User that sent the join request.
|
|
|
|
date (:class:`datetime.datetime`): Date the request was sent.
|
2023-04-18 16:16:23 +02:00
|
|
|
|
2023-05-07 15:31:23 +02:00
|
|
|
.. versionchanged:: 20.3
|
2023-04-18 16:16:23 +02:00
|
|
|
|datetime_localization|
|
2023-02-08 11:47:21 +01:00
|
|
|
user_chat_id (:obj:`int`): Identifier of a private chat with the user who sent the join
|
|
|
|
request. This number may have more than 32 significant bits and some programming
|
|
|
|
languages may have difficulty/silent defects in interpreting it. But it has at most 52
|
|
|
|
significant bits, so a 64-bit integer or double-precision float type are safe for
|
2023-10-03 15:11:37 +02:00
|
|
|
storing this identifier. The bot can use this identifier for 5 minutes to send messages
|
2023-02-08 11:47:21 +01:00
|
|
|
until the join request is processed, assuming no other administrator contacted the
|
|
|
|
user.
|
|
|
|
|
|
|
|
.. versionadded:: 20.1
|
2021-11-08 19:02:20 +01:00
|
|
|
bio (:obj:`str`, optional): Bio of the user.
|
|
|
|
invite_link (:class:`telegram.ChatInviteLink`, optional): Chat invite link that was used
|
|
|
|
by the user to send the join request.
|
|
|
|
|
|
|
|
Attributes:
|
|
|
|
chat (:class:`telegram.Chat`): Chat to which the request was sent.
|
|
|
|
from_user (:class:`telegram.User`): User that sent the join request.
|
|
|
|
date (:class:`datetime.datetime`): Date the request was sent.
|
2023-04-18 16:16:23 +02:00
|
|
|
|
2023-05-07 15:31:23 +02:00
|
|
|
.. versionchanged:: 20.3
|
2023-04-18 16:16:23 +02:00
|
|
|
|datetime_localization|
|
2023-02-08 11:47:21 +01:00
|
|
|
user_chat_id (:obj:`int`): Identifier of a private chat with the user who sent the join
|
|
|
|
request. This number may have more than 32 significant bits and some programming
|
|
|
|
languages may have difficulty/silent defects in interpreting it. But it has at most 52
|
|
|
|
significant bits, so a 64-bit integer or double-precision float type are safe for
|
|
|
|
storing this identifier. The bot can use this identifier for 24 hours to send messages
|
|
|
|
until the join request is processed, assuming no other administrator contacted the
|
|
|
|
user.
|
|
|
|
|
|
|
|
.. versionadded:: 20.1
|
2021-11-08 19:02:20 +01:00
|
|
|
bio (:obj:`str`): Optional. Bio of the user.
|
|
|
|
invite_link (:class:`telegram.ChatInviteLink`): Optional. Chat invite link that was used
|
|
|
|
by the user to send the join request.
|
|
|
|
|
2023-07-09 11:22:08 +02:00
|
|
|
Note:
|
|
|
|
When a user joins a *public* group via an invite link, this attribute may not
|
|
|
|
be present. However, this behavior is undocument and may be subject to change.
|
|
|
|
See `this GitHub thread <https://github.com/tdlib/telegram-bot-api/issues/428>`_
|
|
|
|
for some discussion.
|
|
|
|
|
2021-11-08 19:02:20 +01:00
|
|
|
"""
|
|
|
|
|
2024-02-05 19:24:00 +01:00
|
|
|
__slots__ = ("bio", "chat", "date", "from_user", "invite_link", "user_chat_id")
|
2021-11-08 19:02:20 +01:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
chat: Chat,
|
|
|
|
from_user: User,
|
2024-12-15 10:26:37 +01:00
|
|
|
date: dtm.datetime,
|
2023-02-08 11:47:21 +01:00
|
|
|
user_chat_id: int,
|
2023-05-18 07:57:59 +02:00
|
|
|
bio: Optional[str] = None,
|
|
|
|
invite_link: Optional[ChatInviteLink] = None,
|
2022-10-07 11:51:53 +02:00
|
|
|
*,
|
2023-05-18 07:57:59 +02:00
|
|
|
api_kwargs: Optional[JSONDict] = None,
|
2021-11-08 19:02:20 +01:00
|
|
|
):
|
2022-10-07 11:51:53 +02:00
|
|
|
super().__init__(api_kwargs=api_kwargs)
|
2021-11-08 19:02:20 +01:00
|
|
|
# Required
|
2023-02-02 18:55:07 +01:00
|
|
|
self.chat: Chat = chat
|
|
|
|
self.from_user: User = from_user
|
2024-12-15 10:26:37 +01:00
|
|
|
self.date: dtm.datetime = date
|
2023-02-08 11:47:21 +01:00
|
|
|
self.user_chat_id: int = user_chat_id
|
2021-11-08 19:02:20 +01:00
|
|
|
|
|
|
|
# Optionals
|
2023-02-02 18:55:07 +01:00
|
|
|
self.bio: Optional[str] = bio
|
|
|
|
self.invite_link: Optional[ChatInviteLink] = invite_link
|
2021-11-08 19:02:20 +01:00
|
|
|
|
|
|
|
self._id_attrs = (self.chat, self.from_user, self.date)
|
|
|
|
|
2022-12-15 15:00:36 +01:00
|
|
|
self._freeze()
|
|
|
|
|
2021-11-08 19:02:20 +01:00
|
|
|
@classmethod
|
2024-07-01 19:59:54 +02:00
|
|
|
def de_json(
|
|
|
|
cls, data: Optional[JSONDict], bot: Optional["Bot"] = None
|
|
|
|
) -> Optional["ChatJoinRequest"]:
|
2021-11-08 19:02:20 +01:00
|
|
|
"""See :meth:`telegram.TelegramObject.de_json`."""
|
|
|
|
data = cls._parse_data(data)
|
|
|
|
|
|
|
|
if not data:
|
|
|
|
return None
|
|
|
|
|
2023-04-18 16:16:23 +02:00
|
|
|
# Get the local timezone from the bot if it has defaults
|
|
|
|
loc_tzinfo = extract_tzinfo_from_defaults(bot)
|
|
|
|
|
2021-11-08 19:02:20 +01:00
|
|
|
data["chat"] = Chat.de_json(data.get("chat"), bot)
|
2022-10-07 11:51:53 +02:00
|
|
|
data["from_user"] = User.de_json(data.pop("from", None), bot)
|
2023-04-18 16:16:23 +02:00
|
|
|
data["date"] = from_timestamp(data.get("date", None), tzinfo=loc_tzinfo)
|
2021-11-08 19:02:20 +01:00
|
|
|
data["invite_link"] = ChatInviteLink.de_json(data.get("invite_link"), bot)
|
|
|
|
|
2022-10-07 11:51:53 +02:00
|
|
|
return super().de_json(data=data, bot=bot)
|
2021-11-08 19:02:20 +01:00
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def approve(
|
2021-11-08 19:02:20 +01:00
|
|
|
self,
|
2022-05-18 17:18:44 +02:00
|
|
|
*,
|
2022-04-24 12:38:09 +02:00
|
|
|
read_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
write_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
connect_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
pool_timeout: ODVInput[float] = DEFAULT_NONE,
|
2023-05-18 07:57:59 +02:00
|
|
|
api_kwargs: Optional[JSONDict] = None,
|
2021-11-08 19:02:20 +01:00
|
|
|
) -> bool:
|
|
|
|
"""Shortcut for::
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
await bot.approve_chat_join_request(
|
|
|
|
chat_id=update.effective_chat.id, user_id=update.effective_user.id, *args, **kwargs
|
|
|
|
)
|
2021-11-08 19:02:20 +01:00
|
|
|
|
|
|
|
For the documentation of the arguments, please see
|
|
|
|
:meth:`telegram.Bot.approve_chat_join_request`.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`bool`: On success, :obj:`True` is returned.
|
|
|
|
|
|
|
|
"""
|
2022-04-24 12:38:09 +02:00
|
|
|
return await self.get_bot().approve_chat_join_request(
|
|
|
|
chat_id=self.chat.id,
|
|
|
|
user_id=self.from_user.id,
|
|
|
|
read_timeout=read_timeout,
|
|
|
|
write_timeout=write_timeout,
|
|
|
|
connect_timeout=connect_timeout,
|
|
|
|
pool_timeout=pool_timeout,
|
|
|
|
api_kwargs=api_kwargs,
|
2021-11-08 19:02:20 +01:00
|
|
|
)
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
async def decline(
|
2021-11-08 19:02:20 +01:00
|
|
|
self,
|
2022-05-18 17:18:44 +02:00
|
|
|
*,
|
2022-04-24 12:38:09 +02:00
|
|
|
read_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
write_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
connect_timeout: ODVInput[float] = DEFAULT_NONE,
|
|
|
|
pool_timeout: ODVInput[float] = DEFAULT_NONE,
|
2023-05-18 07:57:59 +02:00
|
|
|
api_kwargs: Optional[JSONDict] = None,
|
2021-11-08 19:02:20 +01:00
|
|
|
) -> bool:
|
|
|
|
"""Shortcut for::
|
|
|
|
|
2022-04-24 12:38:09 +02:00
|
|
|
await bot.decline_chat_join_request(
|
|
|
|
chat_id=update.effective_chat.id, user_id=update.effective_user.id, *args, **kwargs
|
|
|
|
)
|
2021-11-08 19:02:20 +01:00
|
|
|
|
|
|
|
For the documentation of the arguments, please see
|
|
|
|
:meth:`telegram.Bot.decline_chat_join_request`.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
:obj:`bool`: On success, :obj:`True` is returned.
|
|
|
|
|
|
|
|
"""
|
2022-04-24 12:38:09 +02:00
|
|
|
return await self.get_bot().decline_chat_join_request(
|
|
|
|
chat_id=self.chat.id,
|
|
|
|
user_id=self.from_user.id,
|
|
|
|
read_timeout=read_timeout,
|
|
|
|
write_timeout=write_timeout,
|
|
|
|
connect_timeout=connect_timeout,
|
|
|
|
pool_timeout=pool_timeout,
|
|
|
|
api_kwargs=api_kwargs,
|
2021-11-08 19:02:20 +01:00
|
|
|
)
|