2018-06-09 23:09:02 +03:00
|
|
|
import itertools
|
2019-05-03 22:37:27 +03:00
|
|
|
import typing
|
2018-06-09 23:09:02 +03:00
|
|
|
|
|
|
|
from .users import UserMethods
|
2019-05-03 22:37:27 +03:00
|
|
|
from .. import utils, hints
|
2019-02-27 12:37:40 +03:00
|
|
|
from ..requestiter import RequestIter
|
2018-06-10 22:50:28 +03:00
|
|
|
from ..tl import types, functions, custom
|
2018-06-09 23:09:02 +03:00
|
|
|
|
2019-02-27 15:07:25 +03:00
|
|
|
_MAX_CHUNK_SIZE = 100
|
|
|
|
|
2019-05-03 22:37:27 +03:00
|
|
|
if typing.TYPE_CHECKING:
|
|
|
|
from .telegramclient import TelegramClient
|
|
|
|
|
2018-06-09 23:09:02 +03:00
|
|
|
|
2019-02-27 12:37:40 +03:00
|
|
|
class _DialogsIter(RequestIter):
|
|
|
|
async def _init(
|
2019-05-10 16:45:24 +03:00
|
|
|
self, offset_date, offset_id, offset_peer, ignore_migrated, folder
|
2019-02-27 12:37:40 +03:00
|
|
|
):
|
|
|
|
self.request = functions.messages.GetDialogsRequest(
|
|
|
|
offset_date=offset_date,
|
|
|
|
offset_id=offset_id,
|
|
|
|
offset_peer=offset_peer,
|
|
|
|
limit=1,
|
2019-05-10 16:45:24 +03:00
|
|
|
hash=0,
|
|
|
|
folder_id=folder
|
2019-02-27 12:37:40 +03:00
|
|
|
)
|
|
|
|
|
2019-02-27 15:01:04 +03:00
|
|
|
if self.limit <= 0:
|
2019-02-27 12:37:40 +03:00
|
|
|
# Special case, get a single dialog and determine count
|
|
|
|
dialogs = await self.client(self.request)
|
|
|
|
self.total = getattr(dialogs, 'count', len(dialogs.dialogs))
|
|
|
|
raise StopAsyncIteration
|
|
|
|
|
|
|
|
self.seen = set()
|
|
|
|
self.offset_date = offset_date
|
|
|
|
self.ignore_migrated = ignore_migrated
|
|
|
|
|
|
|
|
async def _load_next_chunk(self):
|
2019-02-27 15:07:25 +03:00
|
|
|
self.request.limit = min(self.left, _MAX_CHUNK_SIZE)
|
2019-02-27 12:37:40 +03:00
|
|
|
r = await self.client(self.request)
|
|
|
|
|
|
|
|
self.total = getattr(r, 'count', len(r.dialogs))
|
|
|
|
|
|
|
|
entities = {utils.get_peer_id(x): x
|
|
|
|
for x in itertools.chain(r.users, r.chats)}
|
|
|
|
|
|
|
|
messages = {}
|
|
|
|
for m in r.messages:
|
2019-03-13 11:11:58 +03:00
|
|
|
m._finish_init(self.client, entities, None)
|
2019-02-27 12:37:40 +03:00
|
|
|
messages[m.id] = m
|
|
|
|
|
|
|
|
for d in r.dialogs:
|
|
|
|
# We check the offset date here because Telegram may ignore it
|
|
|
|
if self.offset_date:
|
|
|
|
date = getattr(messages.get(
|
|
|
|
d.top_message, None), 'date', None)
|
|
|
|
|
|
|
|
if not date or date.timestamp() > self.offset_date.timestamp():
|
|
|
|
continue
|
|
|
|
|
|
|
|
peer_id = utils.get_peer_id(d.peer)
|
|
|
|
if peer_id not in self.seen:
|
|
|
|
self.seen.add(peer_id)
|
2019-03-13 11:11:58 +03:00
|
|
|
cd = custom.Dialog(self.client, d, entities, messages)
|
2019-02-27 12:37:40 +03:00
|
|
|
if cd.dialog.pts:
|
|
|
|
self.client._channel_pts[cd.id] = cd.dialog.pts
|
|
|
|
|
|
|
|
if not self.ignore_migrated or getattr(
|
|
|
|
cd.entity, 'migrated_to', None) is None:
|
2019-02-27 13:24:47 +03:00
|
|
|
self.buffer.append(cd)
|
2019-02-27 12:37:40 +03:00
|
|
|
|
|
|
|
if len(r.dialogs) < self.request.limit\
|
|
|
|
or not isinstance(r, types.messages.DialogsSlice):
|
|
|
|
# Less than we requested means we reached the end, or
|
|
|
|
# we didn't get a DialogsSlice which means we got all.
|
2019-02-27 13:24:47 +03:00
|
|
|
return True
|
2019-02-27 12:37:40 +03:00
|
|
|
|
2019-04-16 14:00:18 +03:00
|
|
|
# Don't set `request.offset_id` to the last message ID.
|
|
|
|
# Why? It seems to cause plenty of dialogs to be skipped.
|
|
|
|
#
|
|
|
|
# By leaving it to 0 after the first iteration, even if
|
|
|
|
# the user originally passed another ID, we ensure that
|
|
|
|
# it will work correctly.
|
|
|
|
self.request.offset_id = 0
|
2019-02-27 12:37:40 +03:00
|
|
|
self.request.exclude_pinned = True
|
2019-02-27 13:24:47 +03:00
|
|
|
self.request.offset_date = r.messages[-1].date
|
|
|
|
self.request.offset_peer =\
|
|
|
|
entities[utils.get_peer_id(r.dialogs[-1].peer)]
|
2019-02-27 12:37:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
class _DraftsIter(RequestIter):
|
|
|
|
async def _init(self, **kwargs):
|
|
|
|
r = await self.client(functions.messages.GetAllDraftsRequest())
|
2019-02-27 13:24:47 +03:00
|
|
|
self.buffer.extend(custom.Draft._from_update(self.client, u)
|
|
|
|
for u in r.updates)
|
2019-02-27 12:37:40 +03:00
|
|
|
|
|
|
|
async def _load_next_chunk(self):
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2018-06-09 23:09:02 +03:00
|
|
|
class DialogMethods(UserMethods):
|
2018-06-10 23:00:55 +03:00
|
|
|
|
2018-06-09 23:09:02 +03:00
|
|
|
# region Public methods
|
|
|
|
|
2019-02-27 12:37:40 +03:00
|
|
|
def iter_dialogs(
|
2019-05-03 22:37:27 +03:00
|
|
|
self: 'TelegramClient',
|
|
|
|
limit: float = None,
|
|
|
|
*,
|
2019-05-08 18:16:09 +03:00
|
|
|
offset_date: 'hints.DateLike' = None,
|
2019-05-03 22:37:27 +03:00
|
|
|
offset_id: int = 0,
|
2019-05-08 18:16:09 +03:00
|
|
|
offset_peer: 'hints.EntityLike' = types.InputPeerEmpty(),
|
2019-05-10 16:45:24 +03:00
|
|
|
ignore_migrated: bool = False,
|
|
|
|
folder: int = None,
|
|
|
|
archived: bool = None
|
2019-05-03 22:37:27 +03:00
|
|
|
) -> _DialogsIter:
|
2018-06-09 23:09:02 +03:00
|
|
|
"""
|
2019-05-06 12:38:26 +03:00
|
|
|
Iterator over the dialogs (open conversations/subscribed channels).
|
2018-06-09 23:09:02 +03:00
|
|
|
|
|
|
|
Args:
|
|
|
|
limit (`int` | `None`):
|
|
|
|
How many dialogs to be retrieved as maximum. Can be set to
|
|
|
|
``None`` to retrieve all dialogs. Note that this may take
|
|
|
|
whole minutes if you have hundreds of dialogs, as Telegram
|
|
|
|
will tell the library to slow down through a
|
|
|
|
``FloodWaitError``.
|
|
|
|
|
|
|
|
offset_date (`datetime`, optional):
|
|
|
|
The offset date to be used.
|
|
|
|
|
|
|
|
offset_id (`int`, optional):
|
|
|
|
The message ID to be used as an offset.
|
|
|
|
|
|
|
|
offset_peer (:tl:`InputPeer`, optional):
|
|
|
|
The peer to be used as an offset.
|
|
|
|
|
2018-06-20 13:03:42 +03:00
|
|
|
ignore_migrated (`bool`, optional):
|
|
|
|
Whether :tl:`Chat` that have ``migrated_to`` a :tl:`Channel`
|
|
|
|
should be included or not. By default all the chats in your
|
|
|
|
dialogs are returned, but setting this to ``True`` will hide
|
|
|
|
them in the same way official applications do.
|
|
|
|
|
2019-05-10 16:45:24 +03:00
|
|
|
folder (`int`, optional):
|
|
|
|
The folder from which the dialogs should be retrieved.
|
|
|
|
|
|
|
|
If left unspecified, all dialogs (including those from
|
|
|
|
folders) will be returned.
|
|
|
|
|
|
|
|
If set to ``0``, all dialogs that don't belong to any
|
|
|
|
folder will be returned.
|
|
|
|
|
|
|
|
If set to a folder number like ``1``, only those from
|
|
|
|
said folder will be returned.
|
|
|
|
|
|
|
|
By default Telegram assigns the folder ID ``1`` to
|
|
|
|
archived chats, so you should use that if you need
|
|
|
|
to fetch the archived dialogs.
|
|
|
|
|
|
|
|
archived (`bool`, optional):
|
|
|
|
Alias for `folder`. If unspecified, all will be returned,
|
|
|
|
``False`` implies ``folder=0`` and ``True`` implies ``folder=1``.
|
2018-06-09 23:09:02 +03:00
|
|
|
Yields:
|
|
|
|
Instances of `telethon.tl.custom.dialog.Dialog`.
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# Get all open conversation, print the title of the first
|
|
|
|
dialogs = client.get_dialogs()
|
|
|
|
first = dialogs[0]
|
|
|
|
print(first.title)
|
|
|
|
|
|
|
|
# Use the dialog somewhere else
|
|
|
|
client.send_message(first, 'hi')
|
|
|
|
|
|
|
|
# Get drafts
|
|
|
|
drafts = client.get_drafts()
|
2019-05-10 16:45:24 +03:00
|
|
|
|
|
|
|
# Getting only non-archived dialogs (both equivalent)
|
|
|
|
non_archived = client.get_dialogs(folder=0)
|
|
|
|
non_archived = client.get_dialogs(archived=False)
|
|
|
|
|
|
|
|
# Getting only archived dialogs (both equivalent)
|
|
|
|
archived = client.get_dialogs(folder=1)
|
|
|
|
non_archived = client.get_dialogs(archived=True)
|
2018-06-09 23:09:02 +03:00
|
|
|
"""
|
2019-05-10 16:45:24 +03:00
|
|
|
if archived is not None:
|
|
|
|
folder = 1 if archived else 0
|
|
|
|
|
2019-02-27 12:37:40 +03:00
|
|
|
return _DialogsIter(
|
|
|
|
self,
|
|
|
|
limit,
|
2018-06-09 23:09:02 +03:00
|
|
|
offset_date=offset_date,
|
|
|
|
offset_id=offset_id,
|
|
|
|
offset_peer=offset_peer,
|
2019-05-10 16:45:24 +03:00
|
|
|
ignore_migrated=ignore_migrated,
|
|
|
|
folder=folder
|
2018-06-09 23:09:02 +03:00
|
|
|
)
|
|
|
|
|
2019-05-08 18:16:09 +03:00
|
|
|
async def get_dialogs(self: 'TelegramClient', *args, **kwargs) -> 'hints.TotalList':
|
2018-06-09 23:09:02 +03:00
|
|
|
"""
|
2019-05-09 13:24:37 +03:00
|
|
|
Same as `iter_dialogs()`, but returns a
|
2018-08-03 00:00:10 +03:00
|
|
|
`TotalList <telethon.helpers.TotalList>` instead.
|
2018-06-09 23:09:02 +03:00
|
|
|
"""
|
2019-02-27 12:37:40 +03:00
|
|
|
return await self.iter_dialogs(*args, **kwargs).collect()
|
|
|
|
|
2019-05-03 22:37:27 +03:00
|
|
|
def iter_drafts(self: 'TelegramClient') -> _DraftsIter:
|
2018-06-09 23:09:02 +03:00
|
|
|
"""
|
|
|
|
Iterator over all open draft messages.
|
|
|
|
|
|
|
|
Instances of `telethon.tl.custom.draft.Draft` are yielded.
|
|
|
|
You can call `telethon.tl.custom.draft.Draft.set_message`
|
|
|
|
to change the message or `telethon.tl.custom.draft.Draft.delete`
|
|
|
|
among other things.
|
|
|
|
"""
|
2019-02-27 12:37:40 +03:00
|
|
|
# TODO Passing a limit here makes no sense
|
|
|
|
return _DraftsIter(self, None)
|
2018-06-09 23:09:02 +03:00
|
|
|
|
2019-05-08 18:16:09 +03:00
|
|
|
async def get_drafts(self: 'TelegramClient') -> 'hints.TotalList':
|
2018-06-09 23:09:02 +03:00
|
|
|
"""
|
2019-05-09 13:24:37 +03:00
|
|
|
Same as `iter_drafts()`, but returns a list instead.
|
2018-06-09 23:09:02 +03:00
|
|
|
"""
|
2019-02-27 12:37:40 +03:00
|
|
|
return await self.iter_drafts().collect()
|
2018-06-09 23:09:02 +03:00
|
|
|
|
2018-08-03 18:51:56 +03:00
|
|
|
def conversation(
|
2019-05-03 22:37:27 +03:00
|
|
|
self: 'TelegramClient',
|
2019-05-08 18:16:09 +03:00
|
|
|
entity: 'hints.EntityLike',
|
2019-05-03 22:37:27 +03:00
|
|
|
*,
|
|
|
|
timeout: float = 60,
|
|
|
|
total_timeout: float = None,
|
|
|
|
max_messages: int = 100,
|
|
|
|
exclusive: bool = True,
|
|
|
|
replies_are_responses: bool = True) -> custom.Conversation:
|
2018-08-03 18:51:56 +03:00
|
|
|
"""
|
2018-09-04 12:27:10 +03:00
|
|
|
Creates a `Conversation <telethon.tl.custom.conversation.Conversation>`
|
2019-05-06 12:38:26 +03:00
|
|
|
with the given entity.
|
|
|
|
|
|
|
|
This is not the same as just sending a message to create a "dialog"
|
|
|
|
with them, but rather a way to easily send messages and await for
|
2018-09-04 12:27:10 +03:00
|
|
|
responses or other reactions. Refer to its documentation for more.
|
2018-08-03 18:51:56 +03:00
|
|
|
|
|
|
|
Args:
|
|
|
|
entity (`entity`):
|
|
|
|
The entity with which a new conversation should be opened.
|
|
|
|
|
|
|
|
timeout (`int` | `float`, optional):
|
2019-03-18 19:30:45 +03:00
|
|
|
The default timeout (in seconds) *per action* to be used. You
|
|
|
|
may also override this timeout on a per-method basis. By
|
|
|
|
default each action can take up to 60 seconds (the value of
|
|
|
|
this timeout).
|
2018-08-03 18:51:56 +03:00
|
|
|
|
|
|
|
total_timeout (`int` | `float`, optional):
|
2019-03-18 19:30:45 +03:00
|
|
|
The total timeout (in seconds) to use for the whole
|
|
|
|
conversation. This takes priority over per-action
|
|
|
|
timeouts. After these many seconds pass, subsequent
|
|
|
|
actions will result in ``asyncio.TimeoutError``.
|
2018-08-03 18:51:56 +03:00
|
|
|
|
|
|
|
max_messages (`int`, optional):
|
|
|
|
The maximum amount of messages this conversation will
|
|
|
|
remember. After these many messages arrive in the
|
|
|
|
specified chat, subsequent actions will result in
|
|
|
|
``ValueError``.
|
|
|
|
|
2018-10-12 23:17:07 +03:00
|
|
|
exclusive (`bool`, optional):
|
|
|
|
By default, conversations are exclusive within a single
|
|
|
|
chat. That means that while a conversation is open in a
|
|
|
|
chat, you can't open another one in the same chat, unless
|
|
|
|
you disable this flag.
|
|
|
|
|
|
|
|
If you try opening an exclusive conversation for
|
|
|
|
a chat where it's already open, it will raise
|
|
|
|
``AlreadyInConversationError``.
|
|
|
|
|
2018-08-03 18:51:56 +03:00
|
|
|
replies_are_responses (`bool`, optional):
|
|
|
|
Whether replies should be treated as responses or not.
|
|
|
|
|
|
|
|
If the setting is enabled, calls to `conv.get_response
|
|
|
|
<telethon.tl.custom.conversation.Conversation.get_response>`
|
|
|
|
and a subsequent call to `conv.get_reply
|
|
|
|
<telethon.tl.custom.conversation.Conversation.get_reply>`
|
|
|
|
will return different messages, otherwise they may return
|
|
|
|
the same message.
|
|
|
|
|
|
|
|
Consider the following scenario with one outgoing message,
|
|
|
|
1, and two incoming messages, the second one replying::
|
|
|
|
|
|
|
|
Hello! <1
|
|
|
|
2> (reply to 1) Hi!
|
|
|
|
3> (reply to 1) How are you?
|
|
|
|
|
|
|
|
And the following code:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
async with client.conversation(chat) as conv:
|
|
|
|
msg1 = await conv.send_message('Hello!')
|
|
|
|
msg2 = await conv.get_response()
|
|
|
|
msg3 = await conv.get_reply()
|
|
|
|
|
|
|
|
With the setting enabled, ``msg2`` will be ``'Hi!'`` and
|
|
|
|
``msg3`` be ``'How are you?'`` since replies are also
|
|
|
|
responses, and a response was already returned.
|
|
|
|
|
|
|
|
With the setting disabled, both ``msg2`` and ``msg3`` will
|
|
|
|
be ``'Hi!'`` since one is a response and also a reply.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A `Conversation <telethon.tl.custom.conversation.Conversation>`.
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# <you> denotes outgoing messages you sent
|
|
|
|
# <usr> denotes incoming response messages
|
|
|
|
with bot.conversation(chat) as conv:
|
|
|
|
# <you> Hi!
|
|
|
|
conv.send_message('Hi!')
|
|
|
|
|
|
|
|
# <usr> Hello!
|
|
|
|
hello = conv.get_response()
|
|
|
|
|
|
|
|
# <you> Please tell me your name
|
|
|
|
conv.send_message('Please tell me your name')
|
|
|
|
|
|
|
|
# <usr> ?
|
|
|
|
name = conv.get_response().raw_text
|
|
|
|
|
|
|
|
while not any(x.isalpha() for x in name):
|
|
|
|
# <you> Your name didn't have any letters! Try again
|
|
|
|
conv.send_message("Your name didn't have any letters! Try again")
|
|
|
|
|
|
|
|
# <usr> Lonami
|
|
|
|
name = conv.get_response().raw_text
|
|
|
|
|
|
|
|
# <you> Thanks Lonami!
|
|
|
|
conv.send_message('Thanks {}!'.format(name))
|
2018-08-03 18:51:56 +03:00
|
|
|
"""
|
|
|
|
return custom.Conversation(
|
|
|
|
self,
|
|
|
|
entity,
|
|
|
|
timeout=timeout,
|
|
|
|
total_timeout=total_timeout,
|
|
|
|
max_messages=max_messages,
|
2018-10-12 23:17:07 +03:00
|
|
|
exclusive=exclusive,
|
2018-08-03 18:51:56 +03:00
|
|
|
replies_are_responses=replies_are_responses
|
|
|
|
|
|
|
|
)
|
|
|
|
|
2018-06-09 23:09:02 +03:00
|
|
|
# endregion
|