mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2025-08-08 04:52:30 +00:00
Move events.NewMessage properties to custom.Message
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
import re
|
||||
|
||||
from .common import EventBuilder, EventCommon, name_inner_event
|
||||
from .. import utils
|
||||
from ..extensions import markdown
|
||||
from ..tl import types, functions, custom
|
||||
from ..tl import types, custom
|
||||
|
||||
|
||||
@name_inner_event
|
||||
@@ -116,23 +114,19 @@ class NewMessage(EventBuilder):
|
||||
|
||||
class Event(EventCommon):
|
||||
"""
|
||||
Represents the event of a new message.
|
||||
Represents the event of a new message. This event can be treated
|
||||
to all effects as a `telethon.tl.custom.message.Message`, so please
|
||||
**refer to its documentation** to know what you can do with this event.
|
||||
|
||||
Members:
|
||||
message (:tl:`Message`):
|
||||
This is the original :tl:`Message` object.
|
||||
This is the only difference with the received
|
||||
`telethon.tl.custom.message.Message`, and will
|
||||
return the `telethon.tl.custom.message.Message` itself,
|
||||
not the text.
|
||||
|
||||
is_private (`bool`):
|
||||
True if the message was sent as a private message.
|
||||
|
||||
is_group (`bool`):
|
||||
True if the message was sent on a group or megagroup.
|
||||
|
||||
is_channel (`bool`):
|
||||
True if the message was sent on a megagroup or channel.
|
||||
|
||||
is_reply (`str`):
|
||||
Whether the message is a reply to some other or not.
|
||||
See `telethon.tl.custom.message.Message` for the rest of
|
||||
available members and methods.
|
||||
"""
|
||||
def __init__(self, message):
|
||||
if not message.out and isinstance(message.to_id, types.PeerUser):
|
||||
@@ -146,282 +140,11 @@ class NewMessage(EventBuilder):
|
||||
msg_id=message.id, broadcast=bool(message.post))
|
||||
|
||||
self.message = message
|
||||
self._text = None
|
||||
|
||||
self._input_sender = None
|
||||
self._sender = None
|
||||
|
||||
self.is_reply = bool(message.reply_to_msg_id)
|
||||
self._reply_message = None
|
||||
|
||||
def _set_client(self, client):
|
||||
super()._set_client(client)
|
||||
self.message = custom.Message(
|
||||
client, self.message, self._entities, None)
|
||||
|
||||
def respond(self, *args, **kwargs):
|
||||
"""
|
||||
Responds to the message (not as a reply). Shorthand for
|
||||
`telethon.telegram_client.TelegramClient.send_message` with
|
||||
``entity`` already set.
|
||||
"""
|
||||
return self._client.send_message(self.input_chat, *args, **kwargs)
|
||||
|
||||
def reply(self, *args, **kwargs):
|
||||
"""
|
||||
Replies to the message (as a reply). Shorthand for
|
||||
`telethon.telegram_client.TelegramClient.send_message` with
|
||||
both ``entity`` and ``reply_to`` already set.
|
||||
"""
|
||||
kwargs['reply_to'] = self.message.id
|
||||
return self._client.send_message(self.input_chat, *args, **kwargs)
|
||||
|
||||
def forward_to(self, *args, **kwargs):
|
||||
"""
|
||||
Forwards the message. Shorthand for
|
||||
`telethon.telegram_client.TelegramClient.forward_messages` with
|
||||
both ``messages`` and ``from_peer`` already set.
|
||||
"""
|
||||
kwargs['messages'] = self.message.id
|
||||
kwargs['from_peer'] = self.input_chat
|
||||
return self._client.forward_messages(*args, **kwargs)
|
||||
|
||||
def edit(self, *args, **kwargs):
|
||||
"""
|
||||
Edits the message iff it's outgoing. Shorthand for
|
||||
`telethon.telegram_client.TelegramClient.edit_message` with
|
||||
both ``entity`` and ``message`` already set.
|
||||
|
||||
Returns ``None`` if the message was incoming, or the edited
|
||||
:tl:`Message` otherwise.
|
||||
"""
|
||||
if self.message.fwd_from:
|
||||
return None
|
||||
if not self.message.out:
|
||||
if not isinstance(self.message.to_id, types.PeerUser):
|
||||
return None
|
||||
me = self._client.get_me(input_peer=True)
|
||||
if self.message.to_id.user_id != me.user_id:
|
||||
return None
|
||||
|
||||
return self._client.edit_message(self.input_chat,
|
||||
self.message,
|
||||
*args, **kwargs)
|
||||
|
||||
def delete(self, *args, **kwargs):
|
||||
"""
|
||||
Deletes the message. You're responsible for checking whether you
|
||||
have the permission to do so, or to except the error otherwise.
|
||||
Shorthand for
|
||||
`telethon.telegram_client.TelegramClient.delete_messages` with
|
||||
``entity`` and ``message_ids`` already set.
|
||||
"""
|
||||
return self._client.delete_messages(self.input_chat,
|
||||
[self.message],
|
||||
*args, **kwargs)
|
||||
|
||||
@property
|
||||
def input_sender(self):
|
||||
"""
|
||||
This (:tl:`InputPeer`) is the input version of the user who
|
||||
sent the message. Similarly to ``input_chat``, this doesn't have
|
||||
things like username or similar, but still useful in some cases.
|
||||
|
||||
Note that this might not be available if the library can't
|
||||
find the input chat, or if the message a broadcast on a channel.
|
||||
"""
|
||||
if self._input_sender is None:
|
||||
if self.is_channel and not self.is_group:
|
||||
return None
|
||||
|
||||
try:
|
||||
self._input_sender = self._client.get_input_entity(
|
||||
self.message.from_id
|
||||
)
|
||||
except (ValueError, TypeError):
|
||||
# We can rely on self.input_chat for this
|
||||
self._sender, self._input_sender = self._get_entity(
|
||||
self.message.id,
|
||||
self.message.from_id,
|
||||
chat=self.input_chat
|
||||
)
|
||||
|
||||
return self._input_sender
|
||||
|
||||
@property
|
||||
def sender(self):
|
||||
"""
|
||||
This (:tl:`User`) may make an API call the first time to get
|
||||
the most up to date version of the sender (mostly when the event
|
||||
doesn't belong to a channel), so keep that in mind.
|
||||
|
||||
``input_sender`` needs to be available (often the case).
|
||||
"""
|
||||
if not self.input_sender:
|
||||
return None
|
||||
|
||||
if self._sender is None:
|
||||
self._sender = \
|
||||
self._entities.get(utils.get_peer_id(self._input_sender))
|
||||
|
||||
if self._sender is None:
|
||||
self._sender = self._client.get_entity(self._input_sender)
|
||||
|
||||
return self._sender
|
||||
|
||||
@property
|
||||
def sender_id(self):
|
||||
"""
|
||||
Returns the marked sender integer ID, if present.
|
||||
"""
|
||||
return self.message.from_id
|
||||
|
||||
@property
|
||||
def text(self):
|
||||
"""
|
||||
The message text, markdown-formatted.
|
||||
"""
|
||||
if self._text is None:
|
||||
if not self.message.entities:
|
||||
return self.message.message
|
||||
self._text = markdown.unparse(self.message.message,
|
||||
self.message.entities or [])
|
||||
return self._text
|
||||
|
||||
@property
|
||||
def raw_text(self):
|
||||
"""
|
||||
The raw message text, ignoring any formatting.
|
||||
"""
|
||||
return self.message.message
|
||||
|
||||
@property
|
||||
def reply_message(self):
|
||||
"""
|
||||
This optional :tl:`Message` will make an API call the first
|
||||
time to get the full :tl:`Message` object that one was replying to,
|
||||
so use with care as there is no caching besides local caching yet.
|
||||
"""
|
||||
if not self.message.reply_to_msg_id:
|
||||
return None
|
||||
|
||||
if self._reply_message is None:
|
||||
if isinstance(self.input_chat, types.InputPeerChannel):
|
||||
r = self._client(functions.channels.GetMessagesRequest(
|
||||
self.input_chat, [self.message.reply_to_msg_id]
|
||||
))
|
||||
else:
|
||||
r = self._client(functions.messages.GetMessagesRequest(
|
||||
[self.message.reply_to_msg_id]
|
||||
))
|
||||
if not isinstance(r, types.messages.MessagesNotModified):
|
||||
self._reply_message = r.messages[0]
|
||||
|
||||
return self._reply_message
|
||||
|
||||
@property
|
||||
def forward(self):
|
||||
"""
|
||||
The unmodified :tl:`MessageFwdHeader`, if present..
|
||||
"""
|
||||
return self.message.fwd_from
|
||||
|
||||
@property
|
||||
def media(self):
|
||||
"""
|
||||
The unmodified :tl:`MessageMedia`, if present.
|
||||
"""
|
||||
return self.message.media
|
||||
|
||||
@property
|
||||
def photo(self):
|
||||
"""
|
||||
If the message media is a photo,
|
||||
this returns the :tl:`Photo` object.
|
||||
"""
|
||||
if isinstance(self.message.media, types.MessageMediaPhoto):
|
||||
photo = self.message.media.photo
|
||||
if isinstance(photo, types.Photo):
|
||||
return photo
|
||||
|
||||
@property
|
||||
def document(self):
|
||||
"""
|
||||
If the message media is a document,
|
||||
this returns the :tl:`Document` object.
|
||||
"""
|
||||
if isinstance(self.message.media, types.MessageMediaDocument):
|
||||
doc = self.message.media.document
|
||||
if isinstance(doc, types.Document):
|
||||
return doc
|
||||
|
||||
def _document_by_attribute(self, kind, condition=None):
|
||||
"""
|
||||
Helper method to return the document only if it has an attribute
|
||||
that's an instance of the given kind, and passes the condition.
|
||||
"""
|
||||
doc = self.document
|
||||
if doc:
|
||||
for attr in doc.attributes:
|
||||
if isinstance(attr, kind):
|
||||
if not condition or condition(doc):
|
||||
return doc
|
||||
|
||||
@property
|
||||
def audio(self):
|
||||
"""
|
||||
If the message media is a document with an Audio attribute,
|
||||
this returns the :tl:`Document` object.
|
||||
"""
|
||||
return self._document_by_attribute(types.DocumentAttributeAudio,
|
||||
lambda attr: not attr.voice)
|
||||
|
||||
@property
|
||||
def voice(self):
|
||||
"""
|
||||
If the message media is a document with a Voice attribute,
|
||||
this returns the :tl:`Document` object.
|
||||
"""
|
||||
return self._document_by_attribute(types.DocumentAttributeAudio,
|
||||
lambda attr: attr.voice)
|
||||
|
||||
@property
|
||||
def video(self):
|
||||
"""
|
||||
If the message media is a document with a Video attribute,
|
||||
this returns the :tl:`Document` object.
|
||||
"""
|
||||
return self._document_by_attribute(types.DocumentAttributeVideo)
|
||||
|
||||
@property
|
||||
def video_note(self):
|
||||
"""
|
||||
If the message media is a document with a Video attribute,
|
||||
this returns the :tl:`Document` object.
|
||||
"""
|
||||
return self._document_by_attribute(types.DocumentAttributeVideo,
|
||||
lambda attr: attr.round_message)
|
||||
|
||||
@property
|
||||
def gif(self):
|
||||
"""
|
||||
If the message media is a document with an Animated attribute,
|
||||
this returns the :tl:`Document` object.
|
||||
"""
|
||||
return self._document_by_attribute(types.DocumentAttributeAnimated)
|
||||
|
||||
@property
|
||||
def sticker(self):
|
||||
"""
|
||||
If the message media is a document with a Sticker attribute,
|
||||
this returns the :tl:`Document` object.
|
||||
"""
|
||||
return self._document_by_attribute(types.DocumentAttributeSticker)
|
||||
|
||||
@property
|
||||
def out(self):
|
||||
"""
|
||||
Whether the message is outgoing (i.e. you sent it from
|
||||
another session) or incoming (i.e. someone else sent it).
|
||||
"""
|
||||
return self.message.out
|
||||
def __getattr__(self, item):
|
||||
return getattr(self.message, item)
|
||||
|
Reference in New Issue
Block a user