2018-03-22 20:39:42 +03:00
|
|
|
import datetime
|
|
|
|
|
2018-03-27 12:00:46 +03:00
|
|
|
from .. import TLObject
|
2017-10-09 10:54:48 +03:00
|
|
|
from ..functions.messages import SaveDraftRequest
|
2018-01-20 13:47:17 +03:00
|
|
|
from ..types import UpdateDraftMessage, DraftMessage
|
2018-03-27 12:00:46 +03:00
|
|
|
from ...errors import RPCError
|
2018-03-08 14:56:20 +03:00
|
|
|
from ...extensions import markdown
|
2018-10-05 15:20:50 +03:00
|
|
|
from ...utils import get_peer_id, get_input_peer
|
2017-10-09 10:54:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
class Draft:
|
|
|
|
"""
|
|
|
|
Custom class that encapsulates a draft on the Telegram servers, providing
|
|
|
|
an abstraction to change the message conveniently. The library will return
|
2018-03-22 20:39:42 +03:00
|
|
|
instances of this class when calling :meth:`get_drafts()`.
|
|
|
|
|
|
|
|
Args:
|
2018-03-28 17:03:47 +03:00
|
|
|
date (`datetime`):
|
2018-03-22 20:39:42 +03:00
|
|
|
The date of the draft.
|
|
|
|
|
2018-03-28 17:03:47 +03:00
|
|
|
link_preview (`bool`):
|
2018-03-22 20:39:42 +03:00
|
|
|
Whether the link preview is enabled or not.
|
|
|
|
|
2018-03-28 17:03:47 +03:00
|
|
|
reply_to_msg_id (`int`):
|
2018-03-22 20:39:42 +03:00
|
|
|
The message ID that the draft will reply to.
|
2017-10-09 10:54:48 +03:00
|
|
|
"""
|
2018-06-25 12:03:20 +03:00
|
|
|
def __init__(self, client, peer, draft, entity):
|
2017-10-09 10:54:48 +03:00
|
|
|
self._client = client
|
|
|
|
self._peer = peer
|
2018-06-25 12:03:20 +03:00
|
|
|
self._entity = entity
|
|
|
|
self._input_entity = get_input_peer(entity) if entity else None
|
|
|
|
|
2018-06-12 20:25:13 +03:00
|
|
|
if not draft or not isinstance(draft, DraftMessage):
|
2018-01-20 13:47:17 +03:00
|
|
|
draft = DraftMessage('', None, None, None, None)
|
2017-10-09 10:54:48 +03:00
|
|
|
|
2018-03-08 14:56:20 +03:00
|
|
|
self._text = markdown.unparse(draft.message, draft.entities)
|
|
|
|
self._raw_text = draft.message
|
2017-10-09 10:54:48 +03:00
|
|
|
self.date = draft.date
|
2018-03-08 15:04:01 +03:00
|
|
|
self.link_preview = not draft.no_webpage
|
2017-10-09 10:54:48 +03:00
|
|
|
self.reply_to_msg_id = draft.reply_to_msg_id
|
|
|
|
|
|
|
|
@classmethod
|
2018-06-25 12:03:20 +03:00
|
|
|
def _from_dialog(cls, client, dialog):
|
|
|
|
return cls(client=client, peer=dialog.dialog.peer,
|
|
|
|
draft=dialog.dialog.draft, entity=dialog.entity)
|
2017-10-09 10:54:48 +03:00
|
|
|
|
2018-06-25 12:03:20 +03:00
|
|
|
@classmethod
|
|
|
|
def _from_update(cls, client, update, entities=None):
|
|
|
|
assert isinstance(update, UpdateDraftMessage)
|
|
|
|
return cls(client=client, peer=update.peer, draft=update.draft,
|
|
|
|
entity=(entities or {}).get(get_peer_id(update.peer)))
|
2017-10-09 10:54:48 +03:00
|
|
|
|
|
|
|
@property
|
2018-06-25 12:03:20 +03:00
|
|
|
def entity(self):
|
2018-03-22 20:39:42 +03:00
|
|
|
"""
|
|
|
|
The entity that belongs to this dialog (user, chat or channel).
|
|
|
|
"""
|
2018-06-25 12:03:20 +03:00
|
|
|
return self._entity
|
2017-10-09 10:54:48 +03:00
|
|
|
|
|
|
|
@property
|
2018-06-25 12:03:20 +03:00
|
|
|
def input_entity(self):
|
2018-03-22 20:39:42 +03:00
|
|
|
"""
|
|
|
|
Input version of the entity.
|
|
|
|
"""
|
2018-06-25 12:03:20 +03:00
|
|
|
if not self._input_entity:
|
|
|
|
try:
|
2019-03-26 13:39:25 +03:00
|
|
|
self._input_entity = self._client._entity_cache[self._peer]
|
|
|
|
except KeyError:
|
2018-06-25 12:03:20 +03:00
|
|
|
pass
|
|
|
|
|
|
|
|
return self._input_entity
|
|
|
|
|
|
|
|
async def get_entity(self):
|
|
|
|
"""
|
|
|
|
Returns `entity` but will make an API call if necessary.
|
|
|
|
"""
|
|
|
|
if not self.entity and await self.get_input_entity():
|
|
|
|
try:
|
|
|
|
self._entity =\
|
2018-07-14 11:43:24 +03:00
|
|
|
await self._client.get_entity(self._input_entity)
|
2018-06-25 12:03:20 +03:00
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
return self._entity
|
|
|
|
|
|
|
|
async def get_input_entity(self):
|
|
|
|
"""
|
|
|
|
Returns `input_entity` but will make an API call if necessary.
|
|
|
|
"""
|
|
|
|
# We don't actually have an API call we can make yet
|
|
|
|
# to get more info, but keep this method for consistency.
|
|
|
|
return self.input_entity
|
2017-10-09 10:54:48 +03:00
|
|
|
|
2018-03-08 14:56:20 +03:00
|
|
|
@property
|
|
|
|
def text(self):
|
2018-03-22 20:39:42 +03:00
|
|
|
"""
|
|
|
|
The markdown text contained in the draft. It will be
|
|
|
|
empty if there is no text (and hence no draft is set).
|
|
|
|
"""
|
2018-03-08 14:56:20 +03:00
|
|
|
return self._text
|
|
|
|
|
|
|
|
@property
|
|
|
|
def raw_text(self):
|
2018-03-22 20:39:42 +03:00
|
|
|
"""
|
|
|
|
The raw (text without formatting) contained in the draft.
|
|
|
|
It will be empty if there is no text (thus draft not set).
|
|
|
|
"""
|
2018-03-08 14:56:20 +03:00
|
|
|
return self._raw_text
|
|
|
|
|
2018-03-22 20:39:42 +03:00
|
|
|
@property
|
|
|
|
def is_empty(self):
|
|
|
|
"""
|
|
|
|
Convenience bool to determine if the draft is empty or not.
|
|
|
|
"""
|
|
|
|
return not self._text
|
|
|
|
|
2018-06-10 21:29:57 +03:00
|
|
|
async def set_message(
|
2018-10-08 12:33:56 +03:00
|
|
|
self, text=None, reply_to=0, parse_mode=(),
|
2018-06-10 21:29:57 +03:00
|
|
|
link_preview=None):
|
2017-10-09 10:54:48 +03:00
|
|
|
"""
|
|
|
|
Changes the draft message on the Telegram servers. The changes are
|
2018-03-08 15:04:01 +03:00
|
|
|
reflected in this object.
|
2017-10-09 10:54:48 +03:00
|
|
|
|
2018-03-08 14:56:20 +03:00
|
|
|
:param str text: New text of the draft.
|
2018-03-08 15:04:01 +03:00
|
|
|
Preserved if left as None.
|
|
|
|
|
|
|
|
:param int reply_to: Message ID to reply to.
|
|
|
|
Preserved if left as 0, erased if set to None.
|
|
|
|
|
|
|
|
:param bool link_preview: Whether to attach a web page preview.
|
|
|
|
Preserved if left as None.
|
|
|
|
|
2018-03-08 14:56:20 +03:00
|
|
|
:param str parse_mode: The parse mode to be used for the text.
|
|
|
|
:return bool: ``True`` on success.
|
2017-10-09 10:54:48 +03:00
|
|
|
"""
|
2018-03-08 15:04:01 +03:00
|
|
|
if text is None:
|
|
|
|
text = self._text
|
|
|
|
|
|
|
|
if reply_to == 0:
|
|
|
|
reply_to = self.reply_to_msg_id
|
|
|
|
|
|
|
|
if link_preview is None:
|
|
|
|
link_preview = self.link_preview
|
|
|
|
|
2018-06-10 21:29:57 +03:00
|
|
|
raw_text, entities =\
|
|
|
|
await self._client._parse_message_text(text, parse_mode)
|
|
|
|
|
|
|
|
result = await self._client(SaveDraftRequest(
|
2017-10-09 10:54:48 +03:00
|
|
|
peer=self._peer,
|
2018-03-08 14:56:20 +03:00
|
|
|
message=raw_text,
|
2018-03-08 15:04:01 +03:00
|
|
|
no_webpage=not link_preview,
|
|
|
|
reply_to_msg_id=reply_to,
|
2017-10-09 10:54:48 +03:00
|
|
|
entities=entities
|
|
|
|
))
|
|
|
|
|
|
|
|
if result:
|
2018-03-08 14:56:20 +03:00
|
|
|
self._text = text
|
|
|
|
self._raw_text = raw_text
|
2018-03-08 15:04:01 +03:00
|
|
|
self.link_preview = link_preview
|
|
|
|
self.reply_to_msg_id = reply_to
|
2019-01-24 13:16:40 +03:00
|
|
|
self.date = datetime.datetime.now(tz=datetime.timezone.utc)
|
2017-10-09 10:54:48 +03:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2018-10-08 12:33:56 +03:00
|
|
|
async def send(self, clear=True, parse_mode=()):
|
2018-03-22 20:39:42 +03:00
|
|
|
"""
|
|
|
|
Sends the contents of this draft to the dialog. This is just a
|
2018-03-23 23:40:24 +03:00
|
|
|
wrapper around ``send_message(dialog.input_entity, *args, **kwargs)``.
|
2018-03-22 20:39:42 +03:00
|
|
|
"""
|
2018-06-10 21:29:57 +03:00
|
|
|
await self._client.send_message(
|
|
|
|
self._peer, self.text, reply_to=self.reply_to_msg_id,
|
|
|
|
link_preview=self.link_preview, parse_mode=parse_mode,
|
|
|
|
clear_draft=clear
|
|
|
|
)
|
2018-03-08 14:37:06 +03:00
|
|
|
|
2018-06-10 21:29:57 +03:00
|
|
|
async def delete(self):
|
2017-10-09 10:54:48 +03:00
|
|
|
"""
|
2018-03-22 20:39:42 +03:00
|
|
|
Deletes this draft, and returns ``True`` on success.
|
2017-10-09 10:54:48 +03:00
|
|
|
"""
|
2018-06-10 21:29:57 +03:00
|
|
|
return await self.set_message(text='')
|
2018-03-27 12:00:46 +03:00
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
try:
|
|
|
|
entity = self.entity
|
|
|
|
except RPCError as e:
|
|
|
|
entity = e
|
|
|
|
|
|
|
|
return {
|
|
|
|
'_': 'Draft',
|
|
|
|
'text': self.text,
|
|
|
|
'entity': entity,
|
|
|
|
'date': self.date,
|
|
|
|
'link_preview': self.link_preview,
|
|
|
|
'reply_to_msg_id': self.reply_to_msg_id
|
|
|
|
}
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return TLObject.pretty_format(self.to_dict())
|
|
|
|
|
|
|
|
def stringify(self):
|
|
|
|
return TLObject.pretty_format(self.to_dict(), indent=0)
|