pyrofork: Add the parameter business_connection_id to the methods pin_chat_message() and unpin_chat_message(), allowing bots to manage pinned messages on behalf of a business account

Signed-off-by: Yasir Aris M <git@yasirdev.my.id>
This commit is contained in:
Yasir Aris M 2024-12-20 17:16:10 +07:00 committed by Yasir
parent 6dcb6bfa67
commit b21db6f931
2 changed files with 70 additions and 17 deletions

View file

@ -22,14 +22,18 @@ from typing import Union
import pyrogram import pyrogram
from pyrogram import raw, types from pyrogram import raw, types
from ..messages.inline_session import get_session
class PinChatMessage: class PinChatMessage:
async def pin_chat_message( async def pin_chat_message(
self: "pyrogram.Client", self: "pyrogram.Client",
chat_id: Union[int, str], chat_id: Union[int, str],
message_id: int, message_id: int,
disable_notification: bool = False, disable_notification: bool = False,
both_sides: bool = False, both_sides: bool = False,
business_connection_id: str = None,
) -> "types.Message": ) -> "types.Message":
"""Pin a message in a group, channel or your own chat. """Pin a message in a group, channel or your own chat.
You must be an administrator in the chat for this to work and must have the "can_pin_messages" admin right in You must be an administrator in the chat for this to work and must have the "can_pin_messages" admin right in
@ -53,6 +57,9 @@ class PinChatMessage:
Pass True to pin the message for both sides (you and recipient). Pass True to pin the message for both sides (you and recipient).
Applicable to private chats only. Defaults to False. Applicable to private chats only. Defaults to False.
business_connection_id (``str``, *optional*):
Unique identifier of the business connection on behalf of which the message will be pinned.
Returns: Returns:
:obj:`~pyrogram.types.Message`: On success, the service message is returned. :obj:`~pyrogram.types.Message`: On success, the service message is returned.
@ -65,19 +72,39 @@ class PinChatMessage:
# Pin without notification # Pin without notification
await app.pin_chat_message(chat_id, message_id, disable_notification=True) await app.pin_chat_message(chat_id, message_id, disable_notification=True)
""" """
r = await self.invoke( rpc = raw.functions.messages.UpdatePinnedMessage(
raw.functions.messages.UpdatePinnedMessage(
peer=await self.resolve_peer(chat_id), peer=await self.resolve_peer(chat_id),
id=message_id, id=message_id,
silent=disable_notification or None, silent=disable_notification or None,
pm_oneside=not both_sides or None pm_oneside=not both_sides or None
) )
session = None
business_connection = None
if business_connection_id:
business_connection = self.business_user_connection_cache[business_connection_id]
if not business_connection:
business_connection = await self.get_business_connection(business_connection_id)
session = await get_session(
self,
business_connection._raw.connection.dc_id
) )
if business_connection_id:
r = await session.invoke(
raw.functions.InvokeWithBusinessConnection(
query=rpc,
connection_id=business_connection_id
)
)
else:
r = await self.invoke(rpc)
users = {u.id: u for u in r.users} users = {u.id: u for u in r.users}
chats = {c.id: c for c in r.chats} chats = {c.id: c for c in r.chats}
for i in r.updates: for i in r.updates:
if isinstance(i, (raw.types.UpdateNewMessage, if isinstance(i, (raw.types.UpdateNewMessage,
raw.types.UpdateNewChannelMessage)): raw.types.UpdateNewChannelMessage,
return await types.Message._parse(self, i.message, users, chats) raw.types.UpdateBotNewBusinessMessage)):
return await types.Message._parse(self, i.message, users, chats, business_connection_id=business_connection_id)

View file

@ -22,12 +22,15 @@ from typing import Union
import pyrogram import pyrogram
from pyrogram import raw from pyrogram import raw
from ..messages.inline_session import get_session
class UnpinChatMessage: class UnpinChatMessage:
async def unpin_chat_message( async def unpin_chat_message(
self: "pyrogram.Client", self: "pyrogram.Client",
chat_id: Union[int, str], chat_id: Union[int, str],
message_id: int = 0 message_id: int = 0,
business_connection_id: str = None,
) -> bool: ) -> bool:
"""Unpin a message in a group, channel or your own chat. """Unpin a message in a group, channel or your own chat.
You must be an administrator in the chat for this to work and must have the "can_pin_messages" admin You must be an administrator in the chat for this to work and must have the "can_pin_messages" admin
@ -38,12 +41,15 @@ class UnpinChatMessage:
Parameters: Parameters:
chat_id (``int`` | ``str``): chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target chat. Unique identifier (int) or username (str) of the target chat.
You can also use chat public link in form of *t.me/<username>* (str).
message_id (``int``, *optional*): message_id (``int``, *optional*):
Identifier of a message to unpin. Identifier of a message to unpin.
Required if ``business_connection_id`` is specified.
If not specified, the most recent pinned message (by sending date) will be unpinned. If not specified, the most recent pinned message (by sending date) will be unpinned.
business_connection_id (``str``, *optional*):
Unique identifier of the business connection on behalf of which the message will be unpinned.
Returns: Returns:
``bool``: True on success. ``bool``: True on success.
@ -52,12 +58,32 @@ class UnpinChatMessage:
await app.unpin_chat_message(chat_id, message_id) await app.unpin_chat_message(chat_id, message_id)
""" """
await self.invoke( rpc = raw.functions.messages.UpdatePinnedMessage(
raw.functions.messages.UpdatePinnedMessage(
peer=await self.resolve_peer(chat_id), peer=await self.resolve_peer(chat_id),
id=message_id, id=message_id,
unpin=True unpin=True
) )
session = None
business_connection = None
if business_connection_id:
business_connection = self.business_user_connection_cache[business_connection_id]
if not business_connection:
business_connection = await self.get_business_connection(business_connection_id)
session = await get_session(
self,
business_connection._raw.connection.dc_id
) )
if business_connection_id:
await session.invoke(
raw.functions.InvokeWithBusinessConnection(
query=rpc,
connection_id=business_connection_id
)
)
else:
await self.invoke(rpc)
return True return True