2019-05-10 19:10:56 +03:00
|
|
|
import asyncio
|
2019-09-08 11:56:35 +03:00
|
|
|
import inspect
|
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
|
|
|
|
2020-01-07 14:13:42 +03:00
|
|
|
from .. import helpers, utils, hints, errors
|
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-07-23 13:44:19 +03:00
|
|
|
def _dialog_message_key(peer, message_id):
|
|
|
|
"""
|
|
|
|
Get the key to get messages from a dialog.
|
|
|
|
|
|
|
|
We cannot just use the message ID because channels share message IDs,
|
|
|
|
and the peer ID is required to distinguish between them. But it is not
|
|
|
|
necessary in small group chats and private chats.
|
|
|
|
"""
|
|
|
|
return (peer.channel_id if isinstance(peer, types.PeerChannel) else None), message_id
|
|
|
|
|
|
|
|
|
2019-02-27 12:37:40 +03:00
|
|
|
class _DialogsIter(RequestIter):
|
|
|
|
async def _init(
|
2019-06-11 10:55:13 +03:00
|
|
|
self, offset_date, offset_id, offset_peer, ignore_pinned, 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,
|
2019-06-11 10:55:13 +03:00
|
|
|
exclude_pinned=ignore_pinned,
|
2019-05-10 16:45:24 +03:00
|
|
|
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
|
2020-11-13 12:59:25 +03:00
|
|
|
for x in itertools.chain(r.users, r.chats)
|
|
|
|
if not isinstance(x, (types.UserEmpty, types.ChatEmpty))}
|
2019-02-27 12:37:40 +03:00
|
|
|
|
2022-09-27 12:31:41 +03:00
|
|
|
self.client._mb_entity_cache.extend(r.users, r.chats)
|
|
|
|
|
2019-02-27 12:37:40 +03:00
|
|
|
messages = {}
|
|
|
|
for m in r.messages:
|
2019-03-13 11:11:58 +03:00
|
|
|
m._finish_init(self.client, entities, None)
|
2020-10-01 13:22:55 +03:00
|
|
|
messages[_dialog_message_key(m.peer_id, m.id)] = m
|
2019-02-27 12:37:40 +03:00
|
|
|
|
|
|
|
for d in r.dialogs:
|
|
|
|
# We check the offset date here because Telegram may ignore it
|
2019-07-23 13:44:19 +03:00
|
|
|
message = messages.get(_dialog_message_key(d.peer, d.top_message))
|
2019-02-27 12:37:40 +03:00
|
|
|
if self.offset_date:
|
2019-07-23 13:44:19 +03:00
|
|
|
date = getattr(message, 'date', None)
|
2019-02-27 12:37:40 +03:00
|
|
|
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)
|
2020-11-13 12:59:25 +03:00
|
|
|
if peer_id not in entities:
|
|
|
|
# > In which case can a UserEmpty appear in the list of banned members?
|
|
|
|
# > In a very rare cases. This is possible but isn't an expected behavior.
|
|
|
|
# Real world example: https://t.me/TelethonChat/271471
|
|
|
|
continue
|
|
|
|
|
2019-07-23 13:44:19 +03:00
|
|
|
cd = custom.Dialog(self.client, d, entities, message)
|
2019-02-27 12:37:40 +03:00
|
|
|
if cd.dialog.pts:
|
2022-09-27 12:31:41 +03:00
|
|
|
self.client._message_box.try_set_channel_state(
|
|
|
|
utils.get_peer_id(d.peer, add_mark=False), cd.dialog.pts)
|
2019-02-27 12:37:40 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
2022-11-09 18:12:17 +03:00
|
|
|
if not self.buffer or len(r.dialogs) < self.request.limit\
|
2019-02-27 12:37:40 +03:00
|
|
|
or not isinstance(r, types.messages.DialogsSlice):
|
2022-11-09 18:12:17 +03:00
|
|
|
# Buffer being empty means all returned dialogs were skipped (due to offsets).
|
2019-02-27 12:37:40 +03:00
|
|
|
# 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-06-11 11:04:36 +03:00
|
|
|
# We can't use `messages[-1]` as the offset ID / date.
|
|
|
|
# Why? Because pinned dialogs will mess with the order
|
|
|
|
# in this list. Instead, we find the last dialog which
|
|
|
|
# has a message, and use it as an offset.
|
2019-07-23 13:44:19 +03:00
|
|
|
last_message = next(filter(None, (
|
|
|
|
messages.get(_dialog_message_key(d.peer, d.top_message))
|
2019-06-11 11:04:36 +03:00
|
|
|
for d in reversed(r.dialogs)
|
2019-07-23 13:44:19 +03:00
|
|
|
)), None)
|
2019-06-11 11:04:36 +03:00
|
|
|
|
2019-02-27 12:37:40 +03:00
|
|
|
self.request.exclude_pinned = True
|
2019-06-11 11:04:36 +03:00
|
|
|
self.request.offset_id = last_message.id if last_message else 0
|
|
|
|
self.request.offset_date = last_message.date if last_message else None
|
2020-11-13 12:59:25 +03:00
|
|
|
self.request.offset_peer = self.buffer[-1].input_entity
|
2019-02-27 12:37:40 +03:00
|
|
|
|
|
|
|
|
|
|
|
class _DraftsIter(RequestIter):
|
2019-08-01 21:15:32 +03:00
|
|
|
async def _init(self, entities, **kwargs):
|
|
|
|
if not entities:
|
|
|
|
r = await self.client(functions.messages.GetAllDraftsRequest())
|
|
|
|
items = r.updates
|
|
|
|
else:
|
|
|
|
peers = []
|
|
|
|
for entity in entities:
|
|
|
|
peers.append(types.InputDialogPeer(
|
|
|
|
await self.client.get_input_entity(entity)))
|
|
|
|
|
|
|
|
r = await self.client(functions.messages.GetPeerDialogsRequest(peers))
|
|
|
|
items = r.dialogs
|
2019-06-28 21:34:30 +03:00
|
|
|
|
|
|
|
# TODO Maybe there should be a helper method for this?
|
|
|
|
entities = {utils.get_peer_id(x): x
|
|
|
|
for x in itertools.chain(r.users, r.chats)}
|
|
|
|
|
2019-08-01 21:15:32 +03:00
|
|
|
self.buffer.extend(
|
|
|
|
custom.Draft(self.client, entities[utils.get_peer_id(d.peer)], d.draft)
|
|
|
|
for d in items
|
|
|
|
)
|
2019-02-27 12:37:40 +03:00
|
|
|
|
|
|
|
async def _load_next_chunk(self):
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
2019-06-24 18:48:46 +03:00
|
|
|
class DialogMethods:
|
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-06-11 10:55:13 +03:00
|
|
|
ignore_pinned: bool = False,
|
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
|
|
|
|
2019-06-28 21:44:06 +03:00
|
|
|
The order is the same as the one seen in official applications
|
|
|
|
(first pinned, them from those with the most recent message to
|
|
|
|
those with the oldest message).
|
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Arguments
|
2018-06-09 23:09:02 +03:00
|
|
|
limit (`int` | `None`):
|
|
|
|
How many dialogs to be retrieved as maximum. Can be set to
|
2019-07-06 13:10:25 +03:00
|
|
|
`None` to retrieve all dialogs. Note that this may take
|
2018-06-09 23:09:02 +03:00
|
|
|
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.
|
|
|
|
|
2019-06-11 10:55:13 +03:00
|
|
|
ignore_pinned (`bool`, optional):
|
|
|
|
Whether pinned dialogs should be ignored or not.
|
2019-07-06 13:10:25 +03:00
|
|
|
When set to `True`, these won't be yielded at all.
|
2019-06-11 10:55:13 +03:00
|
|
|
|
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
|
2019-07-06 13:10:25 +03:00
|
|
|
dialogs are returned, but setting this to `True` will ignore
|
2019-06-11 10:55:13 +03:00
|
|
|
(i.e. skip) them in the same way official applications do.
|
2018-06-20 13:03:42 +03:00
|
|
|
|
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,
|
2019-07-06 13:10:25 +03:00
|
|
|
`False` implies ``folder=0`` and `True` implies ``folder=1``.
|
2019-05-20 12:38:26 +03:00
|
|
|
Yields
|
2019-06-01 17:27:53 +03:00
|
|
|
Instances of `Dialog <telethon.tl.custom.dialog.Dialog>`.
|
2019-05-09 13:24:37 +03:00
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Example
|
2019-05-09 13:24:37 +03:00
|
|
|
.. code-block:: python
|
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
# Print all dialog IDs and the title, nicely formatted
|
2019-08-14 00:33:39 +03:00
|
|
|
async for dialog in client.iter_dialogs():
|
2019-05-20 12:38:26 +03:00
|
|
|
print('{:>14}: {}'.format(dialog.id, dialog.title))
|
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-06-11 10:55:13 +03:00
|
|
|
ignore_pinned=ignore_pinned,
|
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.
|
2019-05-20 12:38:26 +03:00
|
|
|
|
|
|
|
Example
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# Get all open conversation, print the title of the first
|
2019-08-14 00:33:39 +03:00
|
|
|
dialogs = await client.get_dialogs()
|
2019-05-20 12:38:26 +03:00
|
|
|
first = dialogs[0]
|
|
|
|
print(first.title)
|
|
|
|
|
|
|
|
# Use the dialog somewhere else
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_message(first, 'hi')
|
2019-05-20 12:38:26 +03:00
|
|
|
|
|
|
|
# Getting only non-archived dialogs (both equivalent)
|
2019-08-14 00:33:39 +03:00
|
|
|
non_archived = await client.get_dialogs(folder=0)
|
|
|
|
non_archived = await client.get_dialogs(archived=False)
|
2019-05-20 12:38:26 +03:00
|
|
|
|
|
|
|
# Getting only archived dialogs (both equivalent)
|
2019-08-14 00:33:39 +03:00
|
|
|
archived = await client.get_dialogs(folder=1)
|
2020-02-24 15:15:56 +03:00
|
|
|
archived = await client.get_dialogs(archived=True)
|
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-09-08 11:56:35 +03:00
|
|
|
get_dialogs.__signature__ = inspect.signature(iter_dialogs)
|
|
|
|
|
2019-08-01 21:15:32 +03:00
|
|
|
def iter_drafts(
|
|
|
|
self: 'TelegramClient',
|
|
|
|
entity: 'hints.EntitiesLike' = None
|
|
|
|
) -> _DraftsIter:
|
2018-06-09 23:09:02 +03:00
|
|
|
"""
|
2019-08-01 21:15:32 +03:00
|
|
|
Iterator over draft messages.
|
2018-06-09 23:09:02 +03:00
|
|
|
|
2019-06-28 21:44:06 +03:00
|
|
|
The order is unspecified.
|
|
|
|
|
2019-08-01 21:15:32 +03:00
|
|
|
Arguments
|
|
|
|
entity (`hints.EntitiesLike`, optional):
|
|
|
|
The entity or entities for which to fetch the draft messages.
|
|
|
|
If left unspecified, all draft messages will be returned.
|
|
|
|
|
2019-06-01 17:27:53 +03:00
|
|
|
Yields
|
|
|
|
Instances of `Draft <telethon.tl.custom.draft.Draft>`.
|
2019-05-20 12:38:26 +03:00
|
|
|
|
|
|
|
Example
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# Clear all drafts
|
2019-08-14 00:33:39 +03:00
|
|
|
async for draft in client.get_drafts():
|
|
|
|
await draft.delete()
|
2019-08-01 21:15:32 +03:00
|
|
|
|
|
|
|
# Getting the drafts with 'bot1' and 'bot2'
|
2019-08-14 00:33:39 +03:00
|
|
|
async for draft in client.iter_drafts(['bot1', 'bot2']):
|
2019-08-01 21:15:32 +03:00
|
|
|
print(draft.text)
|
2018-06-09 23:09:02 +03:00
|
|
|
"""
|
2019-08-01 21:15:32 +03:00
|
|
|
if entity and not utils.is_list_like(entity):
|
|
|
|
entity = (entity,)
|
|
|
|
|
2019-02-27 12:37:40 +03:00
|
|
|
# TODO Passing a limit here makes no sense
|
2019-08-01 21:15:32 +03:00
|
|
|
return _DraftsIter(self, None, entities=entity)
|
2018-06-09 23:09:02 +03:00
|
|
|
|
2019-08-01 21:15:32 +03:00
|
|
|
async def get_drafts(
|
|
|
|
self: 'TelegramClient',
|
|
|
|
entity: 'hints.EntitiesLike' = None
|
|
|
|
) -> '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.
|
2019-05-20 12:38:26 +03:00
|
|
|
|
|
|
|
Example
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# Get drafts, print the text of the first
|
2019-08-14 00:33:39 +03:00
|
|
|
drafts = await client.get_drafts()
|
2019-05-20 12:38:26 +03:00
|
|
|
print(drafts[0].text)
|
2019-08-01 21:15:32 +03:00
|
|
|
|
|
|
|
# Get the draft in your chat
|
2019-08-14 00:33:39 +03:00
|
|
|
draft = await client.get_drafts('me')
|
2019-08-01 21:15:32 +03:00
|
|
|
print(drafts.text)
|
2018-06-09 23:09:02 +03:00
|
|
|
"""
|
2019-08-01 21:15:32 +03:00
|
|
|
items = await self.iter_drafts(entity).collect()
|
|
|
|
if not entity or utils.is_list_like(entity):
|
|
|
|
return items
|
|
|
|
else:
|
|
|
|
return items[0]
|
2018-06-09 23:09:02 +03:00
|
|
|
|
2019-05-22 12:19:00 +03:00
|
|
|
async def edit_folder(
|
2019-05-10 19:10:56 +03:00
|
|
|
self: 'TelegramClient',
|
|
|
|
entity: 'hints.EntitiesLike' = None,
|
2019-05-22 12:19:00 +03:00
|
|
|
folder: typing.Union[int, typing.Sequence[int]] = None,
|
2019-05-10 19:10:56 +03:00
|
|
|
*,
|
|
|
|
unpack=None
|
|
|
|
) -> types.Updates:
|
|
|
|
"""
|
2019-05-22 12:19:00 +03:00
|
|
|
Edits the folder used by one or more dialogs to archive them.
|
2019-05-10 19:10:56 +03:00
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Arguments
|
2019-05-10 19:10:56 +03:00
|
|
|
entity (entities):
|
|
|
|
The entity or list of entities to move to the desired
|
|
|
|
archive folder.
|
|
|
|
|
2019-05-22 12:19:00 +03:00
|
|
|
folder (`int`):
|
2019-05-10 19:10:56 +03:00
|
|
|
The folder to which the dialog should be archived to.
|
|
|
|
|
2019-05-22 12:19:00 +03:00
|
|
|
If you want to "archive" a dialog, use ``folder=1``.
|
|
|
|
|
2019-05-10 19:10:56 +03:00
|
|
|
If you want to "un-archive" it, use ``folder=0``.
|
|
|
|
|
|
|
|
You may also pass a list with the same length as
|
|
|
|
`entities` if you want to control where each entity
|
|
|
|
will go.
|
|
|
|
|
|
|
|
unpack (`int`, optional):
|
|
|
|
If you want to unpack an archived folder, set this
|
|
|
|
parameter to the folder number that you want to
|
|
|
|
delete.
|
|
|
|
|
|
|
|
When you unpack a folder, all the dialogs inside are
|
|
|
|
moved to the folder number 0.
|
|
|
|
|
|
|
|
You can only use this parameter if the other two
|
|
|
|
are not set.
|
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Returns
|
2019-05-10 19:10:56 +03:00
|
|
|
The :tl:`Updates` object that the request produces.
|
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Example
|
2019-05-10 19:10:56 +03:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# Archiving the first 5 dialogs
|
2019-08-14 00:33:39 +03:00
|
|
|
dialogs = await client.get_dialogs(5)
|
|
|
|
await client.edit_folder(dialogs, 1)
|
2019-05-10 19:10:56 +03:00
|
|
|
|
|
|
|
# Un-archiving the third dialog (archiving to folder 0)
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.edit_folder(dialog[2], 0)
|
2019-05-22 12:19:00 +03:00
|
|
|
|
|
|
|
# Moving the first dialog to folder 0 and the second to 1
|
2019-08-14 00:33:39 +03:00
|
|
|
dialogs = await client.get_dialogs(2)
|
|
|
|
await client.edit_folder(dialogs, [0, 1])
|
2019-05-10 19:10:56 +03:00
|
|
|
|
|
|
|
# Un-archiving all dialogs
|
2021-05-09 16:33:28 +03:00
|
|
|
await client.edit_folder(unpack=1)
|
2019-05-10 19:10:56 +03:00
|
|
|
"""
|
|
|
|
if (entity is None) == (unpack is None):
|
|
|
|
raise ValueError('You can only set either entities or unpack, not both')
|
|
|
|
|
|
|
|
if unpack is not None:
|
|
|
|
return await self(functions.folders.DeleteFolderRequest(
|
|
|
|
folder_id=unpack
|
|
|
|
))
|
|
|
|
|
|
|
|
if not utils.is_list_like(entity):
|
|
|
|
entities = [await self.get_input_entity(entity)]
|
|
|
|
else:
|
|
|
|
entities = await asyncio.gather(
|
2020-07-25 19:39:35 +03:00
|
|
|
*(self.get_input_entity(x) for x in entity))
|
2019-05-10 19:10:56 +03:00
|
|
|
|
2019-05-22 12:19:00 +03:00
|
|
|
if folder is None:
|
|
|
|
raise ValueError('You must specify a folder')
|
|
|
|
elif not utils.is_list_like(folder):
|
2019-05-10 19:10:56 +03:00
|
|
|
folder = [folder] * len(entities)
|
|
|
|
elif len(entities) != len(folder):
|
|
|
|
raise ValueError('Number of folders does not match number of entities')
|
|
|
|
|
|
|
|
return await self(functions.folders.EditPeerFoldersRequest([
|
|
|
|
types.InputFolderPeer(x, folder_id=y)
|
|
|
|
for x, y in zip(entities, folder)
|
|
|
|
]))
|
|
|
|
|
2019-05-30 14:58:05 +03:00
|
|
|
async def delete_dialog(
|
|
|
|
self: 'TelegramClient',
|
|
|
|
entity: 'hints.EntityLike',
|
|
|
|
*,
|
|
|
|
revoke: bool = False
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Deletes a dialog (leaves a chat or channel).
|
|
|
|
|
2019-09-10 22:38:07 +03:00
|
|
|
This method can be used as a user and as a bot. However,
|
|
|
|
bots will only be able to use it to leave groups and channels
|
|
|
|
(trying to delete a private conversation will do nothing).
|
|
|
|
|
2019-05-30 14:58:05 +03:00
|
|
|
See also `Dialog.delete() <telethon.tl.custom.dialog.Dialog.delete>`.
|
|
|
|
|
|
|
|
Arguments
|
|
|
|
entity (entities):
|
|
|
|
The entity of the dialog to delete. If it's a chat or
|
|
|
|
channel, you will leave it. Note that the chat itself
|
|
|
|
is not deleted, only the dialog, because you left it.
|
|
|
|
|
|
|
|
revoke (`bool`, optional):
|
|
|
|
On private chats, you may revoke the messages from
|
2019-07-06 13:10:25 +03:00
|
|
|
the other peer too. By default, it's `False`. Set
|
|
|
|
it to `True` to delete the history for both.
|
2019-05-30 14:58:05 +03:00
|
|
|
|
2019-09-10 22:38:07 +03:00
|
|
|
This makes no difference for bot accounts, who can
|
|
|
|
only leave groups and channels.
|
|
|
|
|
2019-05-30 14:58:05 +03:00
|
|
|
Returns
|
|
|
|
The :tl:`Updates` object that the request produces,
|
|
|
|
or nothing for private conversations.
|
|
|
|
|
|
|
|
Example
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# Deleting the first dialog
|
2019-08-14 00:33:39 +03:00
|
|
|
dialogs = await client.get_dialogs(5)
|
|
|
|
await client.delete_dialog(dialogs[0])
|
2019-05-30 14:58:05 +03:00
|
|
|
|
|
|
|
# Leaving a channel by username
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.delete_dialog('username')
|
2019-05-30 14:58:05 +03:00
|
|
|
"""
|
2020-01-07 14:13:42 +03:00
|
|
|
# If we have enough information (`Dialog.delete` gives it to us),
|
|
|
|
# then we know we don't have to kick ourselves in deactivated chats.
|
|
|
|
if isinstance(entity, types.Chat):
|
|
|
|
deactivated = entity.deactivated
|
|
|
|
else:
|
|
|
|
deactivated = False
|
|
|
|
|
2019-05-30 14:58:05 +03:00
|
|
|
entity = await self.get_input_entity(entity)
|
2019-12-23 15:52:07 +03:00
|
|
|
ty = helpers._entity_type(entity)
|
|
|
|
if ty == helpers._EntityType.CHANNEL:
|
2019-05-30 14:58:05 +03:00
|
|
|
return await self(functions.channels.LeaveChannelRequest(entity))
|
|
|
|
|
2020-01-07 14:13:42 +03:00
|
|
|
if ty == helpers._EntityType.CHAT and not deactivated:
|
|
|
|
try:
|
|
|
|
result = await self(functions.messages.DeleteChatUserRequest(
|
2021-03-14 13:05:47 +03:00
|
|
|
entity.chat_id, types.InputUserSelf(), revoke_history=revoke
|
2021-01-28 21:48:01 +03:00
|
|
|
))
|
2020-01-07 14:13:42 +03:00
|
|
|
except errors.PeerIdInvalidError:
|
|
|
|
# Happens if we didn't have the deactivated information
|
|
|
|
result = None
|
2019-05-30 14:58:05 +03:00
|
|
|
else:
|
|
|
|
result = None
|
|
|
|
|
2019-09-10 22:38:07 +03:00
|
|
|
if not await self.is_bot():
|
|
|
|
await self(functions.messages.DeleteHistoryRequest(entity, 0, revoke=revoke))
|
|
|
|
|
2019-05-30 14:58:05 +03:00
|
|
|
return result
|
|
|
|
|
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.
|
|
|
|
|
2021-09-11 12:02:10 +03:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
This Conversation API has certain shortcomings, such as lacking
|
|
|
|
persistence, poor interaction with other event handlers, and
|
|
|
|
overcomplicated usage for anything beyond the simplest case.
|
|
|
|
|
|
|
|
If you plan to interact with a bot without handlers, this works
|
|
|
|
fine, but when running a bot yourself, you may instead prefer
|
|
|
|
to follow the advice from https://stackoverflow.com/a/62246569/.
|
|
|
|
|
2019-05-06 12:38:26 +03:00
|
|
|
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
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Arguments
|
2018-08-03 18:51:56 +03:00
|
|
|
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.
|
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Returns
|
2018-08-03 18:51:56 +03:00
|
|
|
A `Conversation <telethon.tl.custom.conversation.Conversation>`.
|
2019-05-09 13:24:37 +03:00
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Example
|
2019-05-09 13:24:37 +03:00
|
|
|
.. 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")
|
|
|
|
|
2020-02-20 13:19:39 +03:00
|
|
|
# <usr> Human
|
2019-05-09 13:24:37 +03:00
|
|
|
name = conv.get_response().raw_text
|
|
|
|
|
2020-02-20 13:19:39 +03:00
|
|
|
# <you> Thanks Human!
|
2019-05-09 13:24:37 +03:00
|
|
|
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
|