Skip to content

Api 9.0 business methods #4757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changes/unreleased/4756.JT5nmUmGRG6qDEh5ScMn5f.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ features = "Full Support for Bot API 9.0"
uid = "4756"
author_uid = "Bibo-Joshi"
closes_threads = ["4754"]
[[pull_requests]]
uid = "4757"
author_uid = "Bibo-Joshi"
closes_threads = []
30 changes: 28 additions & 2 deletions docs/source/inclusions/bot_methods.rst
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,6 @@
- Used for unpinning a message
* - :meth:`~telegram.Bot.unpin_all_chat_messages`
- Used for unpinning all pinned chat messages
* - :meth:`~telegram.Bot.get_business_connection`
- Used for getting information about the business account.
* - :meth:`~telegram.Bot.get_user_profile_photos`
- Used for obtaining user's profile pictures
* - :meth:`~telegram.Bot.get_chat`
Expand Down Expand Up @@ -397,6 +395,34 @@
* - :meth:`~telegram.Bot.refund_star_payment`
- Used for refunding a payment in Telegram Stars

.. raw:: html

</details>
<br>

.. raw:: html

<details>
<summary>Business Related Methods</summary>

.. list-table::
:align: left
:widths: 1 4

* - :meth:`~telegram.Bot.get_business_connection`
- Used for getting information about the business account.
* - :meth:`~telegram.Bot.read_business_message`
- Used for marking a message as read.
* - :meth:`~telegram.Bot.delete_business_messages`
- Used for deleting business messages.
* - :meth:`~telegram.Bot.set_business_account_name`
- Used for setting the business account name.
* - :meth:`~telegram.Bot.set_business_account_username`
- Used for setting the business account username.
* - :meth:`~telegram.Bot.set_business_account_bio`
- Used for setting the business account bio.


.. raw:: html

</details>
Expand Down
237 changes: 237 additions & 0 deletions telegram/_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9401,6 +9401,233 @@ async def get_business_connection(
bot=self,
)

async def read_business_message(
self,
business_connection_id: str,
chat_id: int,
message_id: int,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""
Marks incoming message as read on behalf of a business account.
Requires the ``can_read_messages`` business bot right.

.. versionadded:: NEXT.VERSION

Args:
business_connection_id (:obj:`str`): Unique identifier of the business connection on
behalf of which to read the message.
chat_id (:obj:`int`): Unique identifier of the chat in which the message was received.
The chat must have been active in the last
:tg-const:`~telegram.constants.BusinessLimit.\
READ_BUSINESS_MESSAGE_ACTIVITY_TIMEOUT` seconds.
message_id (:obj:`int`): Unique identifier of the message to mark as read.

Returns:
:obj:`bool`: On success, :obj:`True` is returned.

Raises:
:class:`telegram.error.TelegramError`
"""
data: JSONDict = {
"business_connection_id": business_connection_id,
"chat_id": chat_id,
"message_id": message_id,
}
return await self._post(
"readBusinessMessage",
data,
read_timeout=read_timeout,
write_timeout=write_timeout,
connect_timeout=connect_timeout,
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)

async def delete_business_messages(
self,
business_connection_id: str,
message_ids: Sequence[int],
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""
Delete messages on behalf of a business account. Requires the
``can_delete_outgoing_messages`` business bot right to delete messages sent by the bot
itself, or the ``can_delete_all_messages`` business bot right to delete any message.

.. versionadded:: NEXT.VERSION

Args:
business_connection_id (:obj:`int` | :obj:`str`): Unique identifier of the business
connection on behalf of which to delete the messages
message_ids (Sequence[:obj:`int`]): A list of
:tg-const:`telegram.constants.BulkRequestLimit.MIN_LIMIT`-
:tg-const:`telegram.constants.BulkRequestLimit.MAX_LIMIT` identifiers of messages
to delete. See :meth:`delete_message` for limitations on which messages can be
deleted.

Returns:
:obj:`bool`: On success, :obj:`True` is returned.

Raises:
:class:`telegram.error.TelegramError`
"""
data: JSONDict = {
"business_connection_id": business_connection_id,
"message_ids": message_ids,
}
return await self._post(
"deleteBusinessMessages",
data,
read_timeout=read_timeout,
write_timeout=write_timeout,
connect_timeout=connect_timeout,
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)

async def set_business_account_name(
self,
business_connection_id: str,
first_name: str,
last_name: Optional[str] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""
Changes the first and last name of a managed business account. Requires the
``can_change_name`` business bot right.

.. versionadded:: NEXT.VERSION

Args:
business_connection_id (:obj:`int` | :obj:`str`): Unique identifier of the business
connection
first_name (:obj:`str`): New first name of the business account;
:tg-const:`telegram.constants.BusinessLimit.MIN_NAME_LENGTH`-
:tg-const:`telegram.constants.BusinessLimit.MAX_NAME_LENGTH` characters.
last_name (:obj:`str`, optional): New last name of the business account;
0-:tg-const:`telegram.constants.BusinessLimit.MAX_NAME_LENGTH` characters.

Returns:
:obj:`bool`: On success, :obj:`True` is returned.

Raises:
:class:`telegram.error.TelegramError`
"""
data: JSONDict = {
"business_connection_id": business_connection_id,
"first_name": first_name,
"last_name": last_name,
}
return await self._post(
"setBusinessAccountName",
data,
read_timeout=read_timeout,
write_timeout=write_timeout,
connect_timeout=connect_timeout,
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)

async def set_business_account_username(
self,
business_connection_id: str,
username: Optional[str] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""
Changes the username of a managed business account. Requires the
``can_change_username`` business bot right.

.. versionadded:: NEXT.VERSION

Args:
business_connection_id (:obj:`str`): Unique identifier of the business connection.
username (:obj:`str`, optional): New business account username;
0-:tg-const:`telegram.constants.BusinessLimit.MAX_USERNAME_LENGTH` characters.

Returns:
:obj:`bool`: On success, :obj:`True` is returned.

Raises:
:class:`telegram.error.TelegramError`
"""
data: JSONDict = {
"business_connection_id": business_connection_id,
"username": username,
}
return await self._post(
"setBusinessAccountUsername",
data,
read_timeout=read_timeout,
write_timeout=write_timeout,
connect_timeout=connect_timeout,
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)

async def set_business_account_bio(
self,
business_connection_id: str,
bio: Optional[str] = None,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""
Changes the bio of a managed business account. Requires the ``can_change_bio`` business
bot right.

.. versionadded:: NEXT.VERSION

Args:
business_connection_id (:obj:`str`): Unique identifier of the business connection.
bio (:obj:`str`, optional): The new value of the bio for the business account;
0-:tg-const:`telegram.constants.BusinessLimit.MAX_BIO_LENGTH` characters.

Returns:
:obj:`bool`: On success, :obj:`True` is returned.

Raises:
:class:`telegram.error.TelegramError`
"""
data: JSONDict = {
"business_connection_id": business_connection_id,
"bio": bio,
}
return await self._post(
"setBusinessAccountBio",
data,
read_timeout=read_timeout,
write_timeout=write_timeout,
connect_timeout=connect_timeout,
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)

async def replace_sticker_in_set(
self,
user_id: int,
Expand Down Expand Up @@ -10338,6 +10565,16 @@ def to_dict(self, recursive: bool = True) -> JSONDict: # noqa: ARG002
"""Alias for :meth:`set_message_reaction`"""
getBusinessConnection = get_business_connection
"""Alias for :meth:`get_business_connection`"""
readBusinessMessage = read_business_message
"""Alias for :meth:`read_business_message`"""
deleteBusinessMessages = delete_business_messages
"""Alias for :meth:`delete_business_messages`"""
setBusinessAccountName = set_business_account_name
"""Alias for :meth:`set_business_account_name`"""
setBusinessAccountUsername = set_business_account_username
"""Alias for :meth:`set_business_account_username`"""
setBusinessAccountBio = set_business_account_bio
"""Alias for :meth:`set_business_account_bio`"""
replaceStickerInSet = replace_sticker_in_set
"""Alias for :meth:`replace_sticker_in_set`"""
refundStarPayment = refund_star_payment
Expand Down
34 changes: 34 additions & 0 deletions telegram/_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3568,6 +3568,40 @@ async def remove_verification(
api_kwargs=api_kwargs,
)

async def read_business_message(
self,
business_connection_id: str,
message_id: int,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""Shortcut for::

await bot.read_business_message(chat_id=update.effective_chat.id, *args, **kwargs)

For the documentation of the arguments, please see
:meth:`telegram.Bot.read_business_message`.

.. versionadded:: NEXT.VERSION

Returns:
:obj:`bool`: On success, :obj:`True` is returned.
"""
return await self.get_bot().read_business_message(
chat_id=self.id,
business_connection_id=business_connection_id,
message_id=message_id,
read_timeout=read_timeout,
write_timeout=write_timeout,
connect_timeout=connect_timeout,
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)


class Chat(_ChatBase):
"""This object represents a chat.
Expand Down
37 changes: 37 additions & 0 deletions telegram/_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -4479,6 +4479,43 @@ async def set_reaction(
api_kwargs=api_kwargs,
)

async def read_business_message(
self,
*,
read_timeout: ODVInput[float] = DEFAULT_NONE,
write_timeout: ODVInput[float] = DEFAULT_NONE,
connect_timeout: ODVInput[float] = DEFAULT_NONE,
pool_timeout: ODVInput[float] = DEFAULT_NONE,
api_kwargs: Optional[JSONDict] = None,
) -> bool:
"""Shortcut for::

await bot.read_business_message(
chat_id=message.chat_id,
message_id=message.message_id,
business_connection_id=message.business_connection_id,
*args, **kwargs
)

For the documentation of the arguments, please see
:meth:`telegram.Bot.read_business_message`.

.. versionadded:: NEXT.VERSION

Returns:
:obj:`bool` On success, :obj:`True` is returned.
"""
return await self.get_bot().read_business_message(
chat_id=self.chat_id,
message_id=self.message_id,
business_connection_id=self.business_connection_id,
read_timeout=read_timeout,
write_timeout=write_timeout,
connect_timeout=connect_timeout,
pool_timeout=pool_timeout,
api_kwargs=api_kwargs,
)

def parse_entity(self, entity: MessageEntity) -> str:
"""Returns the text from a given :class:`telegram.MessageEntity`.

Expand Down
Loading
Loading