2017-12-24 18:18:09 +03:00
|
|
|
from . import Draft
|
2018-09-28 16:47:24 +03:00
|
|
|
from .. import TLObject, types, functions
|
2017-12-24 18:18:09 +03:00
|
|
|
from ... import utils
|
|
|
|
|
|
|
|
|
|
|
|
class Dialog:
|
|
|
|
"""
|
|
|
|
Custom class that encapsulates a dialog (an open "conversation" with
|
|
|
|
someone, a group or a channel) providing an abstraction to easily
|
|
|
|
access the input version/normal entity/message etc. The library will
|
2018-03-22 20:39:42 +03:00
|
|
|
return instances of this class when calling :meth:`.get_dialogs()`.
|
|
|
|
|
|
|
|
Args:
|
2018-03-23 23:40:24 +03:00
|
|
|
dialog (:tl:`Dialog`):
|
2018-03-22 20:39:42 +03:00
|
|
|
The original ``Dialog`` instance.
|
|
|
|
|
2018-03-28 17:03:47 +03:00
|
|
|
pinned (`bool`):
|
2018-03-22 20:39:42 +03:00
|
|
|
Whether this dialog is pinned to the top or not.
|
|
|
|
|
2019-05-10 19:10:56 +03:00
|
|
|
folder_id (`folder_id`):
|
|
|
|
The folder ID that this dialog belongs to.
|
|
|
|
|
|
|
|
archived (`bool`):
|
|
|
|
Whether this dialog is archived or not (``folder_id is None``).
|
|
|
|
|
2019-01-12 15:06:14 +03:00
|
|
|
message (`Message <telethon.tl.custom.message.Message>`):
|
2018-03-22 20:39:42 +03:00
|
|
|
The last message sent on this dialog. Note that this member
|
|
|
|
will not be updated when new messages arrive, it's only set
|
|
|
|
on creation of the instance.
|
|
|
|
|
2018-03-28 17:03:47 +03:00
|
|
|
date (`datetime`):
|
2018-03-22 20:39:42 +03:00
|
|
|
The date of the last message sent on this dialog.
|
|
|
|
|
2018-03-28 17:03:47 +03:00
|
|
|
entity (`entity`):
|
2018-03-22 20:39:42 +03:00
|
|
|
The entity that belongs to this dialog (user, chat or channel).
|
|
|
|
|
2018-03-23 23:40:24 +03:00
|
|
|
input_entity (:tl:`InputPeer`):
|
2018-03-22 20:39:42 +03:00
|
|
|
Input version of the entity.
|
|
|
|
|
2018-03-28 17:03:47 +03:00
|
|
|
id (`int`):
|
2018-03-22 20:39:42 +03:00
|
|
|
The marked ID of the entity, which is guaranteed to be unique.
|
|
|
|
|
2018-03-28 17:03:47 +03:00
|
|
|
name (`str`):
|
2018-03-22 20:39:42 +03:00
|
|
|
Display name for this dialog. For chats and channels this is
|
|
|
|
their title, and for users it's "First-Name Last-Name".
|
|
|
|
|
2018-05-30 20:20:27 +03:00
|
|
|
title (`str`):
|
|
|
|
Alias for `name`.
|
|
|
|
|
2018-03-28 17:03:47 +03:00
|
|
|
unread_count (`int`):
|
2018-03-22 20:39:42 +03:00
|
|
|
How many messages are currently unread in this dialog. Note that
|
|
|
|
this value won't update when new messages arrive.
|
|
|
|
|
2018-03-28 17:03:47 +03:00
|
|
|
unread_mentions_count (`int`):
|
2018-03-22 20:39:42 +03:00
|
|
|
How many mentions are currently unread in this dialog. Note that
|
|
|
|
this value won't update when new messages arrive.
|
|
|
|
|
2019-06-01 17:27:53 +03:00
|
|
|
draft (`Draft <telethon.tl.custom.draft.Draft>`):
|
2019-07-06 13:10:25 +03:00
|
|
|
The draft object in this dialog. It will not be `None`,
|
2018-03-22 20:39:42 +03:00
|
|
|
so you can call ``draft.set_message(...)``.
|
2018-05-30 20:20:27 +03:00
|
|
|
|
|
|
|
is_user (`bool`):
|
2019-07-06 13:10:25 +03:00
|
|
|
`True` if the `entity` is a :tl:`User`.
|
2018-05-30 20:20:27 +03:00
|
|
|
|
|
|
|
is_group (`bool`):
|
2019-07-06 13:10:25 +03:00
|
|
|
`True` if the `entity` is a :tl:`Chat`
|
2018-05-30 20:20:27 +03:00
|
|
|
or a :tl:`Channel` megagroup.
|
|
|
|
|
|
|
|
is_channel (`bool`):
|
2019-07-06 13:10:25 +03:00
|
|
|
`True` if the `entity` is a :tl:`Channel`.
|
2017-12-24 18:18:09 +03:00
|
|
|
"""
|
2019-07-23 13:44:19 +03:00
|
|
|
def __init__(self, client, dialog, entities, message):
|
2017-12-24 18:18:09 +03:00
|
|
|
# Both entities and messages being dicts {ID: item}
|
|
|
|
self._client = client
|
|
|
|
self.dialog = dialog
|
|
|
|
self.pinned = bool(dialog.pinned)
|
2019-05-10 19:10:56 +03:00
|
|
|
self.folder_id = dialog.folder_id
|
|
|
|
self.archived = dialog.folder_id is not None
|
2019-07-23 13:44:19 +03:00
|
|
|
self.message = message
|
2017-12-24 18:18:09 +03:00
|
|
|
self.date = getattr(self.message, 'date', None)
|
|
|
|
|
2017-12-28 15:31:43 +03:00
|
|
|
self.entity = entities[utils.get_peer_id(dialog.peer)]
|
2017-12-24 18:18:09 +03:00
|
|
|
self.input_entity = utils.get_input_peer(self.entity)
|
2018-03-24 20:44:13 +03:00
|
|
|
self.id = utils.get_peer_id(self.entity) # ^ May be InputPeerSelf()
|
2018-05-30 20:20:27 +03:00
|
|
|
self.name = self.title = utils.get_display_name(self.entity)
|
2017-12-24 18:18:09 +03:00
|
|
|
|
|
|
|
self.unread_count = dialog.unread_count
|
|
|
|
self.unread_mentions_count = dialog.unread_mentions_count
|
|
|
|
|
2019-08-01 21:15:32 +03:00
|
|
|
self.draft = Draft(client, self.entity, self.dialog.draft)
|
2017-12-24 18:18:09 +03:00
|
|
|
|
2018-05-30 20:20:27 +03:00
|
|
|
self.is_user = isinstance(self.entity, types.User)
|
|
|
|
self.is_group = (
|
2018-10-15 19:43:53 +03:00
|
|
|
isinstance(self.entity, (types.Chat, types.ChatForbidden)) or
|
2018-05-30 20:20:27 +03:00
|
|
|
(isinstance(self.entity, types.Channel) and self.entity.megagroup)
|
|
|
|
)
|
|
|
|
self.is_channel = isinstance(self.entity, types.Channel)
|
|
|
|
|
2018-06-10 21:29:57 +03:00
|
|
|
async def send_message(self, *args, **kwargs):
|
2017-12-24 18:18:09 +03:00
|
|
|
"""
|
|
|
|
Sends a message to this dialog. This is just a wrapper around
|
2018-03-22 20:39:42 +03:00
|
|
|
``client.send_message(dialog.input_entity, *args, **kwargs)``.
|
2017-12-24 18:18:09 +03:00
|
|
|
"""
|
2018-06-10 21:29:57 +03:00
|
|
|
return await self._client.send_message(
|
|
|
|
self.input_entity, *args, **kwargs)
|
2018-03-27 12:00:46 +03:00
|
|
|
|
2019-05-30 14:58:05 +03:00
|
|
|
async def delete(self, revoke=False):
|
2018-11-03 14:45:34 +03:00
|
|
|
"""
|
|
|
|
Deletes the dialog from your dialog list. If you own the
|
|
|
|
channel this won't destroy it, only delete it from the list.
|
2019-05-30 14:58:05 +03:00
|
|
|
|
|
|
|
Shorthand for `telethon.client.dialogs.DialogMethods.delete_dialog`
|
|
|
|
with ``entity`` already set.
|
2018-11-03 14:45:34 +03:00
|
|
|
"""
|
2020-01-07 14:13:42 +03:00
|
|
|
# Pass the entire entity so the method can determine whether
|
|
|
|
# the `Chat` is deactivated (in which case we don't kick ourselves,
|
|
|
|
# or it would raise `PEER_ID_INVALID`).
|
|
|
|
await self._client.delete_dialog(self.entity, revoke=revoke)
|
2018-09-28 16:47:24 +03:00
|
|
|
|
2019-05-10 19:10:56 +03:00
|
|
|
async def archive(self, folder=1):
|
|
|
|
"""
|
|
|
|
Archives (or un-archives) this dialog.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
folder (`int`, optional):
|
|
|
|
The folder to which the dialog should be archived to.
|
|
|
|
|
|
|
|
If you want to "un-archive" it, use ``folder=0``.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
The :tl:`Updates` object that the request produces.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# Archiving
|
|
|
|
dialog.archive()
|
|
|
|
|
|
|
|
# Un-archiving
|
|
|
|
dialog.archive(0)
|
|
|
|
"""
|
|
|
|
return await self._client(functions.folders.EditPeerFoldersRequest([
|
|
|
|
types.InputFolderPeer(self.input_entity, folder_id=folder)
|
|
|
|
]))
|
|
|
|
|
2018-03-27 12:00:46 +03:00
|
|
|
def to_dict(self):
|
|
|
|
return {
|
|
|
|
'_': 'Dialog',
|
|
|
|
'name': self.name,
|
|
|
|
'date': self.date,
|
|
|
|
'draft': self.draft,
|
|
|
|
'message': self.message,
|
|
|
|
'entity': self.entity,
|
|
|
|
}
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return TLObject.pretty_format(self.to_dict())
|
|
|
|
|
|
|
|
def stringify(self):
|
|
|
|
return TLObject.pretty_format(self.to_dict(), indent=0)
|