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
|
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
|
|
|
"""
|
|
|
|
def __init__(self, client, peer, draft):
|
|
|
|
self._client = client
|
|
|
|
self._peer = peer
|
2018-01-20 13:47:17 +03:00
|
|
|
if not draft:
|
|
|
|
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
|
|
|
|
def _from_update(cls, client, update):
|
|
|
|
if not isinstance(update, UpdateDraftMessage):
|
2017-12-28 02:22:28 +03:00
|
|
|
raise TypeError(
|
2017-10-09 10:54:48 +03:00
|
|
|
'You can only create a new `Draft` from a corresponding '
|
|
|
|
'`UpdateDraftMessage` object.'
|
|
|
|
)
|
|
|
|
|
|
|
|
return cls(client=client, peer=update.peer, draft=update.draft)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def entity(self):
|
2018-03-22 20:39:42 +03:00
|
|
|
"""
|
|
|
|
The entity that belongs to this dialog (user, chat or channel).
|
|
|
|
"""
|
2017-10-09 10:54:48 +03:00
|
|
|
return self._client.get_entity(self._peer)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def input_entity(self):
|
2018-03-22 20:39:42 +03:00
|
|
|
"""
|
|
|
|
Input version of the entity.
|
|
|
|
"""
|
2017-10-09 10:54:48 +03:00
|
|
|
return self._client.get_input_entity(self._peer)
|
|
|
|
|
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-03-08 15:04:01 +03:00
|
|
|
def set_message(self, text=None, reply_to=0, parse_mode='md',
|
|
|
|
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-03-08 14:56:20 +03:00
|
|
|
raw_text, entities = self._client._parse_message_text(text, parse_mode)
|
2017-10-09 10:54:48 +03:00
|
|
|
result = self._client(SaveDraftRequest(
|
|
|
|
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
|
2018-03-22 20:39:42 +03:00
|
|
|
self.date = datetime.datetime.now()
|
2017-10-09 10:54:48 +03:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
2018-03-08 14:56:20 +03:00
|
|
|
def send(self, clear=True, parse_mode='md'):
|
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-03-08 14:37:06 +03:00
|
|
|
self._client.send_message(self._peer, self.text,
|
|
|
|
reply_to=self.reply_to_msg_id,
|
2018-03-08 15:04:01 +03:00
|
|
|
link_preview=self.link_preview,
|
2018-03-08 14:56:20 +03:00
|
|
|
parse_mode=parse_mode,
|
2018-03-08 14:37:06 +03:00
|
|
|
clear_draft=clear)
|
|
|
|
|
2017-10-09 10:54:48 +03:00
|
|
|
def delete(self):
|
|
|
|
"""
|
2018-03-22 20:39:42 +03:00
|
|
|
Deletes this draft, and returns ``True`` on success.
|
2017-10-09 10:54:48 +03:00
|
|
|
"""
|
|
|
|
return 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)
|