Add some ways to convert message

This commit is contained in:
Lonami Exo 2023-09-01 19:50:40 +02:00
parent d2fc9b6cfc
commit 14ed4ea764
3 changed files with 76 additions and 1 deletions

View File

@ -12,6 +12,7 @@ from ...tl.core.request import Request
from ..types.chat import ChatLike
from ..types.chat.user import User
from ..types.login_token import LoginToken
from ..types.message import Message
from ..types.password_token import PasswordToken
from .account import edit_2fa, end_takeout, takeout
from .auth import (
@ -40,6 +41,7 @@ from .downloads import download_media, download_profile_photo, iter_download
from .messages import (
delete_messages,
edit_message,
find_updates_message,
forward_messages,
iter_messages,
pin_message,
@ -205,6 +207,14 @@ class Client:
async def unpin_message(self) -> None:
await unpin_message(self)
def _find_updates_message(
self,
result: abcs.Updates,
random_id: int,
chat: Optional[abcs.InputPeer],
) -> Message:
return find_updates_message(self, result, random_id, chat)
async def set_receive_updates(self) -> None:
await set_receive_updates(self)

View File

@ -1,6 +1,9 @@
from __future__ import annotations
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Dict, List, Optional, Union
from ...tl import abcs, types
from ..types.message import Message
if TYPE_CHECKING:
from .client import Client
@ -44,3 +47,49 @@ async def pin_message(self: Client) -> None:
async def unpin_message(self: Client) -> None:
self
raise NotImplementedError
def find_updates_message(
self: Client,
result: abcs.Updates,
random_id: int,
chat: Optional[abcs.InputPeer],
) -> Message:
if isinstance(result, types.UpdateShort):
updates = [result.update]
entities: Dict[int, object] = {}
elif isinstance(result, (types.Updates, types.UpdatesCombined)):
updates = result.updates
entities = {}
raise NotImplementedError()
else:
return Message._from_raw(
types.MessageEmpty(id=0, peer_id=self._input_as_peer(chat))
)
random_to_id = {}
id_to_message = {}
for update in updates:
if isinstance(update, types.UpdateMessageId):
random_to_id[update.random_id] = update.id
elif isinstance(
update,
(
types.UpdateNewChannelMessage,
types.UpdateNewMessage,
types.UpdateEditMessage,
types.UpdateEditChannelMessage,
types.UpdateNewScheduledMessage,
),
):
assert isinstance(
update.message,
(types.Message, types.MessageService, types.MessageEmpty),
)
id_to_message[update.message.id] = update.message
elif isinstance(update, types.UpdateMessagePoll):
raise NotImplementedError()
return Message._from_raw(id_to_message[random_to_id[random_id]])

View File

@ -0,0 +1,16 @@
from typing import Self
from telethon._impl.tl import abcs
from .meta import NoPublicConstructor
class Message(metaclass=NoPublicConstructor):
__slots__ = ("_message",)
def __init__(self, message: abcs.Message) -> None:
self._message = message
@classmethod
def _from_raw(cls, message: abcs.Message) -> Self:
return cls._create(message)