2
0
mirror of https://github.com/pyrogram/pyrogram synced 2025-09-01 06:45:39 +00:00

Merge pull request #527 from pyrogram/L120

Update API schema to Layer 120
This commit is contained in:
Dan
2020-10-31 16:56:27 +01:00
committed by GitHub
11 changed files with 242 additions and 72 deletions

View File

@@ -539,7 +539,7 @@ class Client(Methods, Scaffold):
getattr(
getattr(
update, "message", None
), "to_id", None
), "peer_id", None
), "channel_id", None
) or getattr(update, "channel_id", None)

View File

@@ -89,15 +89,15 @@ class GetDialogs(Scaffold):
messages = {}
for message in r.messages:
to_id = message.to_id
peer_id = message.peer_id
if isinstance(to_id, raw.types.PeerUser):
if isinstance(peer_id, raw.types.PeerUser):
if message.out:
chat_id = to_id.user_id
chat_id = peer_id.user_id
else:
chat_id = message.from_id
chat_id = utils.get_raw_peer_id(message.from_id)
else:
chat_id = utils.get_peer_id(to_id)
chat_id = utils.get_peer_id(peer_id)
messages[chat_id] = await types.Message._parse(self, message, users, chats)

View File

@@ -27,7 +27,8 @@ class PinChatMessage(Scaffold):
self,
chat_id: Union[int, str],
message_id: int,
disable_notification: bool = None
disable_notification: bool = False,
both_sides: bool = False,
) -> bool:
"""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
@@ -40,10 +41,14 @@ class PinChatMessage(Scaffold):
message_id (``int``):
Identifier of a message to pin.
disable_notification (``bool``):
disable_notification (``bool``, *optional*):
Pass True, if it is not necessary to send a notification to all chat members about the new pinned
message. Notifications are always disabled in channels.
both_sides (``bool``, *optional*):
Pass True to pin the message for both sides (you and recipient).
Applicable to private chats only. Defaults to False.
Returns:
``bool``: True on success.
@@ -60,7 +65,8 @@ class PinChatMessage(Scaffold):
raw.functions.messages.UpdatePinnedMessage(
peer=await self.resolve_peer(chat_id),
id=message_id,
silent=disable_notification or None
silent=disable_notification or None,
pm_oneside=not both_sides or None
)
)

View File

@@ -25,7 +25,8 @@ from pyrogram.scaffold import Scaffold
class UnpinChatMessage(Scaffold):
async def unpin_chat_message(
self,
chat_id: Union[int, str]
chat_id: Union[int, str],
message_id: int
) -> bool:
"""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
@@ -35,18 +36,22 @@ class UnpinChatMessage(Scaffold):
chat_id (``int`` | ``str``):
Unique identifier (int) or username (str) of the target chat.
message_id (``int``):
Identifier of a message to unpin.
Returns:
``bool``: True on success.
Example:
.. code-block:: python
app.unpin_chat_message(chat_id)
app.unpin_chat_message(chat_id, message_id)
"""
await self.send(
raw.functions.messages.UpdatePinnedMessage(
peer=await self.resolve_peer(chat_id),
id=0
id=message_id,
unpin=True
)
)

View File

@@ -34,7 +34,7 @@ class SendMediaGroup(Scaffold):
async def send_media_group(
self,
chat_id: Union[int, str],
media: List[Union["types.InputMediaPhoto", "types.InputMediaVideo"]],
media: List[Union["types.InputMediaPhoto", "types.InputMediaVideo", "types.InputMediaAudio"]],
disable_notification: bool = None,
reply_to_message_id: int = None
) -> List["types.Message"]:
@@ -152,6 +152,53 @@ class SendMediaGroup(Scaffold):
)
)
media = raw.types.InputMediaDocument(
id=raw.types.InputDocument(
id=media.document.id,
access_hash=media.document.access_hash,
file_reference=media.document.file_reference
)
)
else:
media = utils.get_input_media_from_file_id(i.media, i.file_ref, 4)
elif isinstance(i, types.InputMediaAudio):
if os.path.isfile(i.media):
media = await self.send(
raw.functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id),
media=raw.types.InputMediaUploadedDocument(
mime_type=self.guess_mime_type(i.media) or "audio/mpeg",
file=await self.save_file(i.media),
thumb=await self.save_file(i.thumb),
attributes=[
raw.types.DocumentAttributeAudio(
duration=i.duration,
performer=i.performer,
title=i.title
),
raw.types.DocumentAttributeFilename(file_name=os.path.basename(i.media))
]
)
)
)
media = raw.types.InputMediaDocument(
id=raw.types.InputDocument(
id=media.document.id,
access_hash=media.document.access_hash,
file_reference=media.document.file_reference
)
)
elif re.match("^https?://", i.media):
media = await self.send(
raw.functions.messages.UploadMedia(
peer=await self.resolve_peer(chat_id),
media=raw.types.InputMediaDocumentExternal(
url=i.media
)
)
)
media = raw.types.InputMediaDocument(
id=raw.types.InputDocument(
id=media.document.id,

View File

@@ -17,7 +17,7 @@
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
import pyrogram
from pyrogram import raw
from pyrogram import raw, utils
from pyrogram import types
from ..object import Object
@@ -64,7 +64,7 @@ class GameHighScore(Object):
@staticmethod
def _parse_action(client, service: raw.types.MessageService, users: dict):
return GameHighScore(
user=types.User._parse(client, users[service.from_id]),
user=types.User._parse(client, users[utils.get_raw_peer_id(service.from_id)]),
score=service.action.score,
client=client
)

View File

@@ -422,7 +422,7 @@ class Message(Object, Update):
if isinstance(action, raw.types.MessageActionChatAddUser):
new_chat_members = [types.User._parse(client, users[i]) for i in action.users]
elif isinstance(action, raw.types.MessageActionChatJoinedByLink):
new_chat_members = [types.User._parse(client, users[message.from_id])]
new_chat_members = [types.User._parse(client, users[utils.get_raw_peer_id(message.from_id)])]
elif isinstance(action, raw.types.MessageActionChatDeleteUser):
left_chat_member = types.User._parse(client, users[action.user_id])
elif isinstance(action, raw.types.MessageActionChatEditTitle):
@@ -444,7 +444,7 @@ class Message(Object, Update):
message_id=message.id,
date=message.date,
chat=types.Chat._parse(client, message, users, chats),
from_user=types.User._parse(client, users.get(message.from_id, None)),
from_user=types.User._parse(client, users.get(utils.get_raw_peer_id(message.from_id), None)),
service=True,
new_chat_members=new_chat_members,
left_chat_member=left_chat_member,
@@ -472,7 +472,7 @@ class Message(Object, Update):
if isinstance(action, raw.types.MessageActionGameScore):
parsed_message.game_high_score = types.GameHighScore._parse_action(client, message, users)
if message.reply_to_msg_id and replies:
if message.reply_to and replies:
try:
parsed_message.reply_to_message = await client.get_messages(
parsed_message.chat.id,
@@ -501,13 +501,17 @@ class Message(Object, Update):
forward_date = forward_header.date
if forward_header.from_id:
forward_from = types.User._parse(client, users[forward_header.from_id])
raw_peer_id = utils.get_raw_peer_id(forward_header.from_id)
peer_id = utils.get_peer_id(forward_header.from_id)
if peer_id > 0:
forward_from = types.User._parse(client, users[raw_peer_id])
else:
forward_from_chat = types.Chat._parse_channel_chat(client, chats[raw_peer_id])
forward_from_message_id = forward_header.channel_post
forward_signature = forward_header.post_author
elif forward_header.from_name:
forward_sender_name = forward_header.from_name
else:
forward_from_chat = types.Chat._parse_channel_chat(client, chats[forward_header.channel_id])
forward_from_message_id = forward_header.channel_post
forward_signature = forward_header.post_author
photo = None
location = None
@@ -608,7 +612,7 @@ class Message(Object, Update):
message_id=message.id,
date=message.date,
chat=types.Chat._parse(client, message, users, chats),
from_user=types.User._parse(client, users.get(message.from_id, None)),
from_user=types.User._parse(client, users.get(utils.get_raw_peer_id(message.from_id), None)),
text=(
Str(message.message).init(entities) or None
if media is None or web_page is not None
@@ -664,7 +668,7 @@ class Message(Object, Update):
client=client
)
if message.reply_to_msg_id and replies:
if message.reply_to and replies:
try:
parsed_message.reply_to_message = await client.get_messages(
parsed_message.chat.id,
@@ -3113,7 +3117,7 @@ class Message(Object, Update):
options=option
)
async def pin(self, disable_notification: bool = None) -> bool:
async def pin(self, disable_notification: bool = False, both_sides: bool = False) -> bool:
"""Bound method *pin* of :obj:`~pyrogram.types.Message`.
Use as a shortcut for:
@@ -3135,6 +3139,10 @@ class Message(Object, Update):
Pass True, if it is not necessary to send a notification to all chat members about the new pinned
message. Notifications are always disabled in channels.
both_sides (``bool``, *optional*):
Pass True to pin the message for both sides (you and recipient).
Applicable to private chats only. Defaults to False.
Returns:
True on success.
@@ -3144,5 +3152,34 @@ class Message(Object, Update):
return await self._client.pin_chat_message(
chat_id=self.chat.id,
message_id=self.message_id,
disable_notification=disable_notification
disable_notification=disable_notification,
both_sides=both_sides
)
async def unpin(self) -> bool:
"""Bound method *unpin* of :obj:`~pyrogram.types.Message`.
Use as a shortcut for:
.. code-block:: python
client.unpin_chat_message(
chat_id=message.chat.id,
message_id=message_id
)
Example:
.. code-block:: python
message.unpin()
Returns:
True on success.
Raises:
RPCError: In case of a Telegram RPC error.
"""
return await self._client.pin_chat_message(
chat_id=self.chat.id,
message_id=self.message_id
)

View File

@@ -224,13 +224,20 @@ class Chat(Object):
@staticmethod
def _parse(client, message: raw.types.Message or raw.types.MessageService, users: dict, chats: dict) -> "Chat":
if isinstance(message.to_id, raw.types.PeerUser):
return Chat._parse_user_chat(client, users[message.to_id.user_id if message.out else message.from_id])
if isinstance(message.peer_id, raw.types.PeerUser):
return Chat._parse_user_chat(
client,
users[
message.peer_id.user_id
if message.out
else utils.get_raw_peer_id(message.from_id)
]
)
if isinstance(message.to_id, raw.types.PeerChat):
return Chat._parse_chat_chat(client, chats[message.to_id.chat_id])
if isinstance(message.peer_id, raw.types.PeerChat):
return Chat._parse_chat_chat(client, chats[message.peer_id.chat_id])
return Chat._parse_channel_chat(client, chats[message.to_id.channel_id])
return Chat._parse_channel_chat(client, chats[message.peer_id.channel_id])
@staticmethod
def _parse_dialog(client, peer, users: dict, chats: dict):

View File

@@ -211,11 +211,10 @@ class ChatMember(Object):
client=client
)
if isinstance(member, (raw.types.ChannelParticipantCreator, raw.types.ChatParticipantCreator)):
if isinstance(member, raw.types.ChatParticipantCreator):
return ChatMember(
user=user,
status="creator",
title=getattr(member, "rank", None),
client=client
)
@@ -228,6 +227,25 @@ class ChatMember(Object):
client=client
)
if isinstance(member, raw.types.ChannelParticipantCreator):
permissions = member.admin_rights
return ChatMember(
user=user,
status="creator",
title=member.rank,
invited_by=invited_by,
can_change_info=permissions.change_info,
can_post_messages=permissions.post_messages,
can_edit_messages=permissions.edit_messages,
can_delete_messages=permissions.delete_messages,
can_restrict_members=permissions.ban_users,
can_invite_users=permissions.invite_users,
can_pin_messages=permissions.pin_messages,
can_promote_members=permissions.add_admins,
client=client
)
if isinstance(member, raw.types.ChannelParticipantAdmin):
permissions = member.admin_rights

View File

@@ -231,7 +231,22 @@ MIN_CHAT_ID = -2147483647
MAX_USER_ID = 2147483647
def get_raw_peer_id(peer: raw.base.Peer) -> Union[int, None]:
"""Get the raw peer id from a Peer object"""
if isinstance(peer, raw.types.PeerUser):
return peer.user_id
if isinstance(peer, raw.types.PeerChat):
return peer.chat_id
if isinstance(peer, raw.types.PeerChannel):
return peer.channel_id
return None
def get_peer_id(peer: raw.base.Peer) -> int:
"""Get the non-raw peer id from a Peer object"""
if isinstance(peer, raw.types.PeerUser):
return peer.user_id