2019-09-08 11:56:35 +03:00
|
|
|
import inspect
|
2018-06-09 23:05:06 +03:00
|
|
|
import itertools
|
2019-05-03 22:37:27 +03:00
|
|
|
import typing
|
2020-10-01 13:22:55 +03:00
|
|
|
import warnings
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2019-12-23 15:52:07 +03:00
|
|
|
from .. import helpers, utils, errors, hints
|
2019-02-26 22:26:40 +03:00
|
|
|
from ..requestiter import RequestIter
|
2019-05-03 22:37:27 +03:00
|
|
|
from ..tl import types, functions
|
2019-02-26 22:26:40 +03:00
|
|
|
|
2019-02-27 14:57:54 +03:00
|
|
|
_MAX_CHUNK_SIZE = 100
|
|
|
|
|
2019-05-03 22:37:27 +03:00
|
|
|
if typing.TYPE_CHECKING:
|
|
|
|
from .telegramclient import TelegramClient
|
|
|
|
|
2019-02-26 22:26:40 +03:00
|
|
|
|
2019-02-27 11:31:15 +03:00
|
|
|
class _MessagesIter(RequestIter):
|
|
|
|
"""
|
|
|
|
Common factor for all requests that need to iterate over messages.
|
|
|
|
"""
|
|
|
|
async def _init(
|
2019-02-27 14:57:54 +03:00
|
|
|
self, entity, offset_id, min_id, max_id,
|
2021-08-29 12:53:06 +03:00
|
|
|
from_user, offset_date, add_offset, filter, search, reply_to,
|
|
|
|
scheduled
|
2019-02-27 11:31:15 +03:00
|
|
|
):
|
2019-07-06 13:10:25 +03:00
|
|
|
# Note that entity being `None` will perform a global search.
|
2019-02-27 11:31:15 +03:00
|
|
|
if entity:
|
|
|
|
self.entity = await self.client.get_input_entity(entity)
|
|
|
|
else:
|
|
|
|
self.entity = None
|
|
|
|
if self.reverse:
|
|
|
|
raise ValueError('Cannot reverse global search')
|
2019-02-26 22:26:40 +03:00
|
|
|
|
|
|
|
# Telegram doesn't like min_id/max_id. If these IDs are low enough
|
|
|
|
# (starting from last_id - 100), the request will return nothing.
|
|
|
|
#
|
|
|
|
# We can emulate their behaviour locally by setting offset = max_id
|
|
|
|
# and simply stopping once we hit a message with ID <= min_id.
|
|
|
|
if self.reverse:
|
|
|
|
offset_id = max(offset_id, min_id)
|
|
|
|
if offset_id and max_id:
|
|
|
|
if max_id - offset_id <= 1:
|
|
|
|
raise StopAsyncIteration
|
|
|
|
|
|
|
|
if not max_id:
|
|
|
|
max_id = float('inf')
|
|
|
|
else:
|
|
|
|
offset_id = max(offset_id, max_id)
|
|
|
|
if offset_id and min_id:
|
|
|
|
if offset_id - min_id <= 1:
|
|
|
|
raise StopAsyncIteration
|
|
|
|
|
|
|
|
if self.reverse:
|
|
|
|
if offset_id:
|
|
|
|
offset_id += 1
|
2019-05-22 13:20:02 +03:00
|
|
|
elif not offset_date:
|
|
|
|
# offset_id has priority over offset_date, so don't
|
|
|
|
# set offset_id to 1 if we want to offset by date.
|
2019-02-26 22:26:40 +03:00
|
|
|
offset_id = 1
|
|
|
|
|
|
|
|
if from_user:
|
|
|
|
from_user = await self.client.get_input_entity(from_user)
|
2020-10-09 22:14:31 +03:00
|
|
|
self.from_id = await self.client.get_peer_id(from_user)
|
2019-02-27 11:31:15 +03:00
|
|
|
else:
|
|
|
|
self.from_id = None
|
2019-02-26 22:26:40 +03:00
|
|
|
|
2020-10-01 13:06:51 +03:00
|
|
|
# `messages.searchGlobal` only works with text `search` or `filter` queries.
|
|
|
|
# If we want to perform global a search with `from_user` we have to perform
|
|
|
|
# a normal `messages.search`, *but* we can make the entity be `inputPeerEmpty`.
|
|
|
|
if not self.entity and from_user:
|
2019-12-05 18:19:46 +03:00
|
|
|
self.entity = types.InputPeerEmpty()
|
|
|
|
|
2020-10-01 13:06:51 +03:00
|
|
|
if filter is None:
|
|
|
|
filter = types.InputMessagesFilterEmpty()
|
2021-02-09 00:56:27 +03:00
|
|
|
else:
|
|
|
|
filter = filter() if isinstance(filter, type) else filter
|
2020-10-01 13:06:51 +03:00
|
|
|
|
2019-02-27 11:31:15 +03:00
|
|
|
if not self.entity:
|
|
|
|
self.request = functions.messages.SearchGlobalRequest(
|
|
|
|
q=search or '',
|
2020-10-01 13:06:51 +03:00
|
|
|
filter=filter,
|
2020-10-01 13:22:55 +03:00
|
|
|
min_date=None,
|
|
|
|
max_date=offset_date,
|
2021-01-26 23:06:23 +03:00
|
|
|
offset_rate=0,
|
2019-02-27 11:31:15 +03:00
|
|
|
offset_peer=types.InputPeerEmpty(),
|
|
|
|
offset_id=offset_id,
|
|
|
|
limit=1
|
|
|
|
)
|
2021-08-29 12:53:06 +03:00
|
|
|
elif scheduled:
|
|
|
|
self.request = functions.messages.GetScheduledHistoryRequest(
|
|
|
|
peer=entity,
|
|
|
|
hash=0
|
|
|
|
)
|
2020-10-16 11:39:02 +03:00
|
|
|
elif reply_to is not None:
|
|
|
|
self.request = functions.messages.GetRepliesRequest(
|
|
|
|
peer=self.entity,
|
|
|
|
msg_id=reply_to,
|
|
|
|
offset_id=offset_id,
|
|
|
|
offset_date=offset_date,
|
|
|
|
add_offset=add_offset,
|
|
|
|
limit=1,
|
|
|
|
max_id=0,
|
|
|
|
min_id=0,
|
|
|
|
hash=0
|
|
|
|
)
|
2021-02-09 00:56:27 +03:00
|
|
|
elif search is not None or not isinstance(filter, types.InputMessagesFilterEmpty) or from_user:
|
2019-02-27 11:31:15 +03:00
|
|
|
# Telegram completely ignores `from_id` in private chats
|
2019-12-23 15:52:07 +03:00
|
|
|
ty = helpers._entity_type(self.entity)
|
|
|
|
if ty == helpers._EntityType.USER:
|
2019-02-27 11:31:15 +03:00
|
|
|
# Don't bother sending `from_user` (it's ignored anyway),
|
|
|
|
# but keep `from_id` defined above to check it locally.
|
|
|
|
from_user = None
|
|
|
|
else:
|
|
|
|
# Do send `from_user` to do the filtering server-side,
|
|
|
|
# and set `from_id` to None to avoid checking it locally.
|
|
|
|
self.from_id = None
|
|
|
|
|
|
|
|
self.request = functions.messages.SearchRequest(
|
|
|
|
peer=self.entity,
|
|
|
|
q=search or '',
|
2021-02-09 00:56:27 +03:00
|
|
|
filter=filter,
|
2019-02-27 11:31:15 +03:00
|
|
|
min_date=None,
|
|
|
|
max_date=offset_date,
|
|
|
|
offset_id=offset_id,
|
|
|
|
add_offset=add_offset,
|
|
|
|
limit=0, # Search actually returns 0 items if we ask it to
|
|
|
|
max_id=0,
|
|
|
|
min_id=0,
|
|
|
|
hash=0,
|
|
|
|
from_id=from_user
|
|
|
|
)
|
2019-03-18 19:36:06 +03:00
|
|
|
|
|
|
|
# Workaround issue #1124 until a better solution is found.
|
|
|
|
# Telegram seemingly ignores `max_date` if `filter` (and
|
|
|
|
# nothing else) is specified, so we have to rely on doing
|
|
|
|
# a first request to offset from the ID instead.
|
|
|
|
#
|
|
|
|
# Even better, using `filter` and `from_id` seems to always
|
|
|
|
# trigger `RPC_CALL_FAIL` which is "internal issues"...
|
2020-10-25 12:33:36 +03:00
|
|
|
if not isinstance(filter, types.InputMessagesFilterEmpty) \
|
|
|
|
and offset_date and not search and not offset_id:
|
2019-03-18 19:36:06 +03:00
|
|
|
async for m in self.client.iter_messages(
|
|
|
|
self.entity, 1, offset_date=offset_date):
|
|
|
|
self.request.offset_id = m.id + 1
|
2019-02-27 11:31:15 +03:00
|
|
|
else:
|
|
|
|
self.request = functions.messages.GetHistoryRequest(
|
|
|
|
peer=self.entity,
|
|
|
|
limit=1,
|
|
|
|
offset_date=offset_date,
|
|
|
|
offset_id=offset_id,
|
|
|
|
min_id=0,
|
|
|
|
max_id=0,
|
|
|
|
add_offset=add_offset,
|
|
|
|
hash=0
|
|
|
|
)
|
2019-02-26 22:26:40 +03:00
|
|
|
|
2019-02-27 15:01:04 +03:00
|
|
|
if self.limit <= 0:
|
2019-02-26 22:26:40 +03:00
|
|
|
# No messages, but we still need to know the total message count
|
|
|
|
result = await self.client(self.request)
|
|
|
|
if isinstance(result, types.messages.MessagesNotModified):
|
|
|
|
self.total = result.count
|
|
|
|
else:
|
|
|
|
self.total = getattr(result, 'count', len(result.messages))
|
|
|
|
raise StopAsyncIteration
|
|
|
|
|
2019-02-27 11:32:33 +03:00
|
|
|
if self.wait_time is None:
|
|
|
|
self.wait_time = 1 if self.limit > 3000 else 0
|
|
|
|
|
2019-02-26 22:26:40 +03:00
|
|
|
# When going in reverse we need an offset of `-limit`, but we
|
|
|
|
# also want to respect what the user passed, so add them together.
|
|
|
|
if self.reverse:
|
2019-02-27 14:57:54 +03:00
|
|
|
self.request.add_offset -= _MAX_CHUNK_SIZE
|
2019-02-26 22:26:40 +03:00
|
|
|
|
|
|
|
self.add_offset = add_offset
|
|
|
|
self.max_id = max_id
|
|
|
|
self.min_id = min_id
|
|
|
|
self.last_id = 0 if self.reverse else float('inf')
|
|
|
|
|
|
|
|
async def _load_next_chunk(self):
|
2019-02-27 14:57:54 +03:00
|
|
|
self.request.limit = min(self.left, _MAX_CHUNK_SIZE)
|
|
|
|
if self.reverse and self.request.limit != _MAX_CHUNK_SIZE:
|
2019-02-26 22:26:40 +03:00
|
|
|
# Remember that we need -limit when going in reverse
|
|
|
|
self.request.add_offset = self.add_offset - self.request.limit
|
|
|
|
|
|
|
|
r = await self.client(self.request)
|
|
|
|
self.total = getattr(r, 'count', len(r.messages))
|
|
|
|
|
|
|
|
entities = {utils.get_peer_id(x): x
|
|
|
|
for x in itertools.chain(r.users, r.chats)}
|
|
|
|
|
|
|
|
messages = reversed(r.messages) if self.reverse else r.messages
|
|
|
|
for message in messages:
|
|
|
|
if (isinstance(message, types.MessageEmpty)
|
2020-10-09 22:14:31 +03:00
|
|
|
or self.from_id and message.sender_id != self.from_id):
|
2019-02-26 22:26:40 +03:00
|
|
|
continue
|
|
|
|
|
2019-02-27 11:31:15 +03:00
|
|
|
if not self._message_in_range(message):
|
2019-02-27 13:24:47 +03:00
|
|
|
return True
|
2019-02-26 22:26:40 +03:00
|
|
|
|
|
|
|
# There has been reports that on bad connections this method
|
|
|
|
# was returning duplicated IDs sometimes. Using ``last_id``
|
|
|
|
# is an attempt to avoid these duplicates, since the message
|
|
|
|
# IDs are returned in descending order (or asc if reverse).
|
|
|
|
self.last_id = message.id
|
|
|
|
message._finish_init(self.client, entities, self.entity)
|
2019-02-27 13:24:47 +03:00
|
|
|
self.buffer.append(message)
|
2019-02-26 22:26:40 +03:00
|
|
|
|
2022-10-18 17:00:18 +03:00
|
|
|
# Not a slice (using offset would return the same, with e.g. SearchGlobal).
|
|
|
|
if isinstance(r, types.messages.Messages):
|
|
|
|
return True
|
|
|
|
|
2022-10-13 14:40:25 +03:00
|
|
|
# Some channels are "buggy" and may return less messages than
|
|
|
|
# requested (apparently, the messages excluded are, for example,
|
|
|
|
# "not displayable due to local laws").
|
|
|
|
#
|
|
|
|
# This means it's not safe to rely on `len(r.messages) < req.limit` as
|
|
|
|
# the stop condition. Unfortunately more requests must be made.
|
|
|
|
#
|
|
|
|
# However we can still check if the highest ID is equal to or lower
|
|
|
|
# than the limit, in which case there won't be any more messages
|
|
|
|
# because the lowest message ID is 1.
|
|
|
|
#
|
|
|
|
# We also assume the API will always return, at least, one message if
|
|
|
|
# there is more to fetch.
|
|
|
|
if not r.messages or r.messages[0].id <= self.request.limit:
|
2019-02-27 13:24:47 +03:00
|
|
|
return True
|
2019-02-26 22:26:40 +03:00
|
|
|
|
2019-02-27 11:37:12 +03:00
|
|
|
# Get the last message that's not empty (in some rare cases
|
2019-02-26 22:26:40 +03:00
|
|
|
# it can happen that the last message is :tl:`MessageEmpty`)
|
2019-02-27 13:24:47 +03:00
|
|
|
if self.buffer:
|
2020-10-01 13:06:51 +03:00
|
|
|
self._update_offset(self.buffer[-1], r)
|
2019-02-27 11:31:15 +03:00
|
|
|
else:
|
2019-02-26 22:26:40 +03:00
|
|
|
# There are some cases where all the messages we get start
|
|
|
|
# being empty. This can happen on migrated mega-groups if
|
|
|
|
# the history was cleared, and we're using search. Telegram
|
|
|
|
# acts incredibly weird sometimes. Messages are returned but
|
|
|
|
# only "empty", not their contents. If this is the case we
|
|
|
|
# should just give up since there won't be any new Message.
|
2019-02-27 13:24:47 +03:00
|
|
|
return True
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2019-02-27 11:31:15 +03:00
|
|
|
def _message_in_range(self, message):
|
|
|
|
"""
|
|
|
|
Determine whether the given message is in the range or
|
|
|
|
it should be ignored (and avoid loading more chunks).
|
|
|
|
"""
|
|
|
|
# No entity means message IDs between chats may vary
|
|
|
|
if self.entity:
|
|
|
|
if self.reverse:
|
|
|
|
if message.id <= self.last_id or message.id >= self.max_id:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if message.id >= self.last_id or message.id <= self.min_id:
|
|
|
|
return False
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2019-02-27 11:31:15 +03:00
|
|
|
return True
|
2019-02-26 23:04:46 +03:00
|
|
|
|
2020-10-01 13:06:51 +03:00
|
|
|
def _update_offset(self, last_message, response):
|
2019-02-27 11:31:15 +03:00
|
|
|
"""
|
|
|
|
After making the request, update its offset with the last message.
|
|
|
|
"""
|
|
|
|
self.request.offset_id = last_message.id
|
2019-02-26 23:04:46 +03:00
|
|
|
if self.reverse:
|
2019-02-27 11:31:15 +03:00
|
|
|
# We want to skip the one we already have
|
|
|
|
self.request.offset_id += 1
|
2019-02-26 23:04:46 +03:00
|
|
|
|
2019-02-27 11:31:15 +03:00
|
|
|
if isinstance(self.request, functions.messages.SearchRequest):
|
2019-02-27 11:48:47 +03:00
|
|
|
# Unlike getHistory and searchGlobal that use *offset* date,
|
|
|
|
# this is *max* date. This means that doing a search in reverse
|
|
|
|
# will break it. Since it's not really needed once we're going
|
|
|
|
# (only for the first request), it's safe to just clear it off.
|
|
|
|
self.request.max_date = None
|
2019-02-26 23:04:46 +03:00
|
|
|
else:
|
2020-10-16 11:39:02 +03:00
|
|
|
# getHistory, searchGlobal and getReplies call it offset_date
|
2019-02-27 11:31:15 +03:00
|
|
|
self.request.offset_date = last_message.date
|
2019-02-26 23:04:46 +03:00
|
|
|
|
2019-02-27 11:31:15 +03:00
|
|
|
if isinstance(self.request, functions.messages.SearchGlobalRequest):
|
2020-10-01 14:18:54 +03:00
|
|
|
if last_message.input_chat:
|
|
|
|
self.request.offset_peer = last_message.input_chat
|
|
|
|
else:
|
|
|
|
self.request.offset_peer = types.InputPeerEmpty()
|
|
|
|
|
2021-01-26 23:06:23 +03:00
|
|
|
self.request.offset_rate = getattr(response, 'next_rate', 0)
|
2019-02-26 23:04:46 +03:00
|
|
|
|
|
|
|
|
2019-02-27 11:31:15 +03:00
|
|
|
class _IDsIter(RequestIter):
|
2019-02-27 12:04:12 +03:00
|
|
|
async def _init(self, entity, ids):
|
2019-08-26 13:16:16 +03:00
|
|
|
self.total = len(ids)
|
|
|
|
self._ids = list(reversed(ids)) if self.reverse else ids
|
|
|
|
self._offset = 0
|
|
|
|
self._entity = (await self.client.get_input_entity(entity)) if entity else None
|
2019-12-30 12:56:20 +03:00
|
|
|
self._ty = helpers._entity_type(self._entity) if self._entity else None
|
2019-02-26 23:04:46 +03:00
|
|
|
|
2019-08-26 13:16:16 +03:00
|
|
|
# 30s flood wait every 300 messages (3 requests of 100 each, 30 of 10, etc.)
|
|
|
|
if self.wait_time is None:
|
|
|
|
self.wait_time = 10 if self.limit > 300 else 0
|
2019-02-27 12:04:12 +03:00
|
|
|
|
2019-08-26 13:16:16 +03:00
|
|
|
async def _load_next_chunk(self):
|
|
|
|
ids = self._ids[self._offset:self._offset + _MAX_CHUNK_SIZE]
|
|
|
|
if not ids:
|
|
|
|
raise StopAsyncIteration
|
|
|
|
|
|
|
|
self._offset += _MAX_CHUNK_SIZE
|
2019-02-27 12:04:12 +03:00
|
|
|
|
|
|
|
from_id = None # By default, no need to validate from_id
|
2019-12-23 15:52:07 +03:00
|
|
|
if self._ty == helpers._EntityType.CHANNEL:
|
2019-02-27 12:04:12 +03:00
|
|
|
try:
|
|
|
|
r = await self.client(
|
2019-08-26 13:16:16 +03:00
|
|
|
functions.channels.GetMessagesRequest(self._entity, ids))
|
2019-02-27 12:04:12 +03:00
|
|
|
except errors.MessageIdsEmptyError:
|
|
|
|
# All IDs were invalid, use a dummy result
|
|
|
|
r = types.messages.MessagesNotModified(len(ids))
|
|
|
|
else:
|
|
|
|
r = await self.client(functions.messages.GetMessagesRequest(ids))
|
2019-08-26 13:16:16 +03:00
|
|
|
if self._entity:
|
2020-10-23 20:02:43 +03:00
|
|
|
from_id = await self.client._get_peer(self._entity)
|
2019-02-27 12:04:12 +03:00
|
|
|
|
|
|
|
if isinstance(r, types.messages.MessagesNotModified):
|
2019-02-27 13:24:47 +03:00
|
|
|
self.buffer.extend(None for _ in ids)
|
2019-02-27 12:04:12 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
entities = {utils.get_peer_id(x): x
|
|
|
|
for x in itertools.chain(r.users, r.chats)}
|
|
|
|
|
|
|
|
# Telegram seems to return the messages in the order in which
|
|
|
|
# we asked them for, so we don't need to check it ourselves,
|
|
|
|
# unless some messages were invalid in which case Telegram
|
|
|
|
# may decide to not send them at all.
|
|
|
|
#
|
|
|
|
# The passed message IDs may not belong to the desired entity
|
|
|
|
# since the user can enter arbitrary numbers which can belong to
|
|
|
|
# arbitrary chats. Validate these unless ``from_id is None``.
|
|
|
|
for message in r.messages:
|
|
|
|
if isinstance(message, types.MessageEmpty) or (
|
2020-10-01 13:22:55 +03:00
|
|
|
from_id and message.peer_id != from_id):
|
2019-02-27 13:24:47 +03:00
|
|
|
self.buffer.append(None)
|
2019-02-27 12:04:12 +03:00
|
|
|
else:
|
2019-08-26 13:16:16 +03:00
|
|
|
message._finish_init(self.client, entities, self._entity)
|
2019-02-27 13:24:47 +03:00
|
|
|
self.buffer.append(message)
|
2019-02-26 23:04:46 +03:00
|
|
|
|
|
|
|
|
2019-06-24 18:48:46 +03:00
|
|
|
class MessageMethods:
|
2018-06-09 23:05:06 +03:00
|
|
|
|
|
|
|
# region Public methods
|
|
|
|
|
|
|
|
# region Message retrieval
|
|
|
|
|
2019-02-27 11:49:14 +03:00
|
|
|
def iter_messages(
|
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
|
|
|
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,
|
|
|
|
max_id: int = 0,
|
|
|
|
min_id: int = 0,
|
|
|
|
add_offset: int = 0,
|
|
|
|
search: str = None,
|
2019-05-08 18:16:09 +03:00
|
|
|
filter: 'typing.Union[types.TypeMessagesFilter, typing.Type[types.TypeMessagesFilter]]' = None,
|
|
|
|
from_user: 'hints.EntityLike' = None,
|
2019-05-03 22:37:27 +03:00
|
|
|
wait_time: float = None,
|
2019-05-08 18:16:09 +03:00
|
|
|
ids: 'typing.Union[int, typing.Sequence[int]]' = None,
|
2020-10-16 11:39:02 +03:00
|
|
|
reverse: bool = False,
|
2021-08-29 12:53:06 +03:00
|
|
|
reply_to: int = None,
|
|
|
|
scheduled: bool = False
|
2019-05-08 18:16:09 +03:00
|
|
|
) -> 'typing.Union[_MessagesIter, _IDsIter]':
|
2018-06-09 23:05:06 +03:00
|
|
|
"""
|
2019-05-06 12:38:26 +03:00
|
|
|
Iterator over the messages for the given chat.
|
|
|
|
|
2019-06-28 21:44:06 +03:00
|
|
|
The default order is from newest to oldest, but this
|
|
|
|
behaviour can be changed with the `reverse` parameter.
|
|
|
|
|
2018-06-09 23:05:06 +03:00
|
|
|
If either `search`, `filter` or `from_user` are provided,
|
|
|
|
:tl:`messages.Search` will be used instead of :tl:`messages.getHistory`.
|
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Telegram's flood wait limit for :tl:`GetHistoryRequest` seems to
|
|
|
|
be around 30 seconds per 10 requests, therefore a sleep of 1
|
|
|
|
second is the default for this limit (or above).
|
|
|
|
|
|
|
|
Arguments
|
2018-06-09 23:05:06 +03:00
|
|
|
entity (`entity`):
|
|
|
|
The entity from whom to retrieve the message history.
|
|
|
|
|
2019-07-06 13:10:25 +03:00
|
|
|
It may be `None` to perform a global search, or
|
2018-07-30 00:16:01 +03:00
|
|
|
to get messages by their ID from no particular chat.
|
|
|
|
Note that some of the offsets will not work if this
|
|
|
|
is the case.
|
|
|
|
|
|
|
|
Note that if you want to perform a global search,
|
2019-12-05 18:19:46 +03:00
|
|
|
you **must** set a non-empty `search` string, a `filter`.
|
|
|
|
or `from_user`.
|
2018-07-30 00:16:01 +03:00
|
|
|
|
2018-06-09 23:05:06 +03:00
|
|
|
limit (`int` | `None`, optional):
|
|
|
|
Number of messages to be retrieved. Due to limitations with
|
|
|
|
the API retrieving more than 3000 messages will take longer
|
|
|
|
than half a minute (or even more based on previous calls).
|
2018-10-06 21:20:11 +03:00
|
|
|
|
2019-07-06 13:10:25 +03:00
|
|
|
The limit may also be `None`, which would eventually return
|
2018-06-09 23:05:06 +03:00
|
|
|
the whole history.
|
|
|
|
|
|
|
|
offset_date (`datetime`):
|
|
|
|
Offset date (messages *previous* to this date will be
|
|
|
|
retrieved). Exclusive.
|
|
|
|
|
|
|
|
offset_id (`int`):
|
|
|
|
Offset message ID (only messages *previous* to the given
|
|
|
|
ID will be retrieved). Exclusive.
|
|
|
|
|
|
|
|
max_id (`int`):
|
|
|
|
All the messages with a higher (newer) ID or equal to this will
|
|
|
|
be excluded.
|
|
|
|
|
|
|
|
min_id (`int`):
|
|
|
|
All the messages with a lower (older) ID or equal to this will
|
|
|
|
be excluded.
|
|
|
|
|
|
|
|
add_offset (`int`):
|
|
|
|
Additional message offset (all of the specified offsets +
|
|
|
|
this offset = older messages).
|
|
|
|
|
|
|
|
search (`str`):
|
|
|
|
The string to be used as a search query.
|
|
|
|
|
|
|
|
filter (:tl:`MessagesFilter` | `type`):
|
|
|
|
The filter to use when returning messages. For instance,
|
|
|
|
:tl:`InputMessagesFilterPhotos` would yield only messages
|
|
|
|
containing photos.
|
|
|
|
|
|
|
|
from_user (`entity`):
|
2020-10-31 13:41:37 +03:00
|
|
|
Only messages from this entity will be returned.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
|
|
|
wait_time (`int`):
|
2019-03-18 19:30:45 +03:00
|
|
|
Wait time (in seconds) between different
|
|
|
|
:tl:`GetHistoryRequest`. Use this parameter to avoid hitting
|
2019-07-06 13:10:25 +03:00
|
|
|
the ``FloodWaitError`` as needed. If left to `None`, it will
|
2019-03-18 19:30:45 +03:00
|
|
|
default to 1 second only if the limit is higher than 3000.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2019-08-26 13:16:16 +03:00
|
|
|
If the ``ids`` parameter is used, this time will default
|
|
|
|
to 10 seconds only if the amount of IDs is higher than 300.
|
|
|
|
|
2018-06-09 23:05:06 +03:00
|
|
|
ids (`int`, `list`):
|
|
|
|
A single integer ID (or several IDs) for the message that
|
|
|
|
should be returned. This parameter takes precedence over
|
|
|
|
the rest (which will be ignored if this is set). This can
|
|
|
|
for instance be used to get the message with ID 123 from
|
2019-07-06 13:10:25 +03:00
|
|
|
a channel. Note that if the message doesn't exist, `None`
|
2018-06-09 23:05:06 +03:00
|
|
|
will appear in its place, so that zipping the list of IDs
|
|
|
|
with the messages can match one-to-one.
|
|
|
|
|
2018-11-29 16:19:56 +03:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
At the time of writing, Telegram will **not** return
|
|
|
|
:tl:`MessageEmpty` for :tl:`InputMessageReplyTo` IDs that
|
|
|
|
failed (i.e. the message is not replying to any, or is
|
|
|
|
replying to a deleted message). This means that it is
|
|
|
|
**not** possible to match messages one-by-one, so be
|
|
|
|
careful if you use non-integers in this parameter.
|
|
|
|
|
2018-06-28 22:15:29 +03:00
|
|
|
reverse (`bool`, optional):
|
2019-07-06 13:10:25 +03:00
|
|
|
If set to `True`, the messages will be returned in reverse
|
2018-06-28 22:15:29 +03:00
|
|
|
order (from oldest to newest, instead of the default newest
|
|
|
|
to oldest). This also means that the meaning of `offset_id`
|
|
|
|
and `offset_date` parameters is reversed, although they will
|
|
|
|
still be exclusive. `min_id` becomes equivalent to `offset_id`
|
|
|
|
instead of being `max_id` as well since messages are returned
|
|
|
|
in ascending order.
|
|
|
|
|
2019-07-06 13:10:25 +03:00
|
|
|
You cannot use this if both `entity` and `ids` are `None`.
|
2018-07-30 00:16:01 +03:00
|
|
|
|
2020-10-16 11:39:02 +03:00
|
|
|
reply_to (`int`, optional):
|
|
|
|
If set to a message ID, the messages that reply to this ID
|
|
|
|
will be returned. This feature is also known as comments in
|
|
|
|
posts of broadcast channels, or viewing threads in groups.
|
|
|
|
|
|
|
|
This feature can only be used in broadcast channels and their
|
|
|
|
linked megagroups. Using it in a chat or private conversation
|
|
|
|
will result in ``telethon.errors.PeerIdInvalidError`` to occur.
|
|
|
|
|
|
|
|
When using this parameter, the ``filter`` and ``search``
|
|
|
|
parameters have no effect, since Telegram's API doesn't
|
|
|
|
support searching messages in replies.
|
|
|
|
|
|
|
|
.. note::
|
|
|
|
|
|
|
|
This feature is used to get replies to a message in the
|
|
|
|
*discussion* group. If the same broadcast channel sends
|
|
|
|
a message and replies to it itself, that reply will not
|
|
|
|
be included in the results.
|
|
|
|
|
2021-08-29 12:53:06 +03:00
|
|
|
scheduled (`bool`, optional):
|
|
|
|
If set to `True`, messages which are scheduled will be returned.
|
|
|
|
All other parameter will be ignored for this, except `entity`.
|
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Yields
|
2019-06-01 17:27:53 +03:00
|
|
|
Instances of `Message <telethon.tl.custom.message.Message>`.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Example
|
2019-05-09 13:24:37 +03:00
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# From most-recent to oldest
|
2019-08-14 00:33:39 +03:00
|
|
|
async for message in client.iter_messages(chat):
|
2019-05-09 13:24:37 +03:00
|
|
|
print(message.id, message.text)
|
|
|
|
|
|
|
|
# From oldest to most-recent
|
2019-08-14 00:33:39 +03:00
|
|
|
async for message in client.iter_messages(chat, reverse=True):
|
2019-05-09 13:24:37 +03:00
|
|
|
print(message.id, message.text)
|
|
|
|
|
|
|
|
# Filter by sender
|
2019-08-14 00:33:39 +03:00
|
|
|
async for message in client.iter_messages(chat, from_user='me'):
|
2019-05-09 13:24:37 +03:00
|
|
|
print(message.text)
|
|
|
|
|
|
|
|
# Server-side search with fuzzy text
|
2019-08-14 00:33:39 +03:00
|
|
|
async for message in client.iter_messages(chat, search='hello'):
|
2019-05-09 13:24:37 +03:00
|
|
|
print(message.id)
|
|
|
|
|
|
|
|
# Filter by message type:
|
|
|
|
from telethon.tl.types import InputMessagesFilterPhotos
|
2019-08-14 00:33:39 +03:00
|
|
|
async for message in client.iter_messages(chat, filter=InputMessagesFilterPhotos):
|
2019-05-09 13:24:37 +03:00
|
|
|
print(message.photo)
|
2020-10-16 11:39:02 +03:00
|
|
|
|
|
|
|
# Getting comments from a post in a channel:
|
|
|
|
async for message in client.iter_messages(channel, reply_to=123):
|
|
|
|
print(message.chat.title, message.text)
|
2018-06-09 23:05:06 +03:00
|
|
|
"""
|
2019-02-27 11:31:15 +03:00
|
|
|
if ids is not None:
|
2019-08-26 13:16:16 +03:00
|
|
|
if not utils.is_list_like(ids):
|
|
|
|
ids = [ids]
|
|
|
|
|
|
|
|
return _IDsIter(
|
|
|
|
client=self,
|
|
|
|
reverse=reverse,
|
|
|
|
wait_time=wait_time,
|
|
|
|
limit=len(ids),
|
|
|
|
entity=entity,
|
|
|
|
ids=ids
|
|
|
|
)
|
2018-07-30 00:16:01 +03:00
|
|
|
|
2019-02-27 11:31:15 +03:00
|
|
|
return _MessagesIter(
|
|
|
|
client=self,
|
|
|
|
reverse=reverse,
|
|
|
|
wait_time=wait_time,
|
|
|
|
limit=limit,
|
|
|
|
entity=entity,
|
|
|
|
offset_id=offset_id,
|
|
|
|
min_id=min_id,
|
|
|
|
max_id=max_id,
|
|
|
|
from_user=from_user,
|
|
|
|
offset_date=offset_date,
|
|
|
|
add_offset=add_offset,
|
|
|
|
filter=filter,
|
2020-10-16 11:39:02 +03:00
|
|
|
search=search,
|
2021-08-29 12:53:06 +03:00
|
|
|
reply_to=reply_to,
|
|
|
|
scheduled=scheduled
|
2019-02-27 11:31:15 +03:00
|
|
|
)
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2019-05-08 18:16:09 +03:00
|
|
|
async def get_messages(self: 'TelegramClient', *args, **kwargs) -> 'hints.TotalList':
|
2018-06-09 23:05:06 +03:00
|
|
|
"""
|
2019-05-09 13:24:37 +03:00
|
|
|
Same as `iter_messages()`, but returns a
|
2018-08-03 00:00:10 +03:00
|
|
|
`TotalList <telethon.helpers.TotalList>` instead.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
|
|
|
If the `limit` is not set, it will be 1 by default unless both
|
|
|
|
`min_id` **and** `max_id` are set (as *named* arguments), in
|
|
|
|
which case the entire range will be returned.
|
|
|
|
|
|
|
|
This is so because any integer limit would be rather arbitrary and
|
|
|
|
it's common to only want to fetch one message, but if a range is
|
|
|
|
specified it makes sense that it should return the entirety of it.
|
|
|
|
|
|
|
|
If `ids` is present in the *named* arguments and is not a list,
|
2019-01-12 15:06:14 +03:00
|
|
|
a single `Message <telethon.tl.custom.message.Message>` will be
|
|
|
|
returned for convenience instead of a list.
|
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
|
|
|
|
|
|
|
|
# Get 0 photos and print the total to show how many photos there are
|
|
|
|
from telethon.tl.types import InputMessagesFilterPhotos
|
2019-08-14 00:33:39 +03:00
|
|
|
photos = await client.get_messages(chat, 0, filter=InputMessagesFilterPhotos)
|
2019-05-09 13:24:37 +03:00
|
|
|
print(photos.total)
|
|
|
|
|
|
|
|
# Get all the photos
|
2019-08-14 00:33:39 +03:00
|
|
|
photos = await client.get_messages(chat, None, filter=InputMessagesFilterPhotos)
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
# Get messages by ID:
|
2019-12-05 13:14:02 +03:00
|
|
|
message_1337 = await client.get_messages(chat, ids=1337)
|
2018-06-09 23:05:06 +03:00
|
|
|
"""
|
|
|
|
if len(args) == 1 and 'limit' not in kwargs:
|
|
|
|
if 'min_id' in kwargs and 'max_id' in kwargs:
|
|
|
|
kwargs['limit'] = None
|
|
|
|
else:
|
|
|
|
kwargs['limit'] = 1
|
|
|
|
|
2019-02-27 12:15:32 +03:00
|
|
|
it = self.iter_messages(*args, **kwargs)
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2019-02-27 12:15:32 +03:00
|
|
|
ids = kwargs.get('ids')
|
|
|
|
if ids and not utils.is_list_like(ids):
|
|
|
|
async for message in it:
|
|
|
|
return message
|
|
|
|
else:
|
|
|
|
# Iterator exhausted = empty, to handle InputMessageReplyTo
|
|
|
|
return None
|
|
|
|
|
|
|
|
return await it.collect()
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2019-09-08 11:56:35 +03:00
|
|
|
get_messages.__signature__ = inspect.signature(iter_messages)
|
|
|
|
|
2018-06-09 23:05:06 +03:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Message sending/editing/deleting
|
|
|
|
|
2021-01-28 23:05:00 +03:00
|
|
|
async def _get_comment_data(
|
|
|
|
self: 'TelegramClient',
|
|
|
|
entity: 'hints.EntityLike',
|
|
|
|
message: 'typing.Union[int, types.Message]'
|
|
|
|
):
|
|
|
|
r = await self(functions.messages.GetDiscussionMessageRequest(
|
|
|
|
peer=entity,
|
|
|
|
msg_id=utils.get_message_id(message)
|
|
|
|
))
|
|
|
|
m = r.messages[0]
|
|
|
|
chat = next(c for c in r.chats if c.id == m.peer_id.channel_id)
|
|
|
|
return utils.get_input_peer(chat), m.id
|
|
|
|
|
2018-06-09 23:05:06 +03:00
|
|
|
async def send_message(
|
2019-05-03 22:37:27 +03:00
|
|
|
self: 'TelegramClient',
|
2019-05-08 18:16:09 +03:00
|
|
|
entity: 'hints.EntityLike',
|
|
|
|
message: 'hints.MessageLike' = '',
|
2019-05-03 22:37:27 +03:00
|
|
|
*,
|
2019-05-08 18:16:09 +03:00
|
|
|
reply_to: 'typing.Union[int, types.Message]' = None,
|
2021-05-31 16:36:40 +03:00
|
|
|
attributes: 'typing.Sequence[types.TypeDocumentAttribute]' = None,
|
2019-05-03 22:37:27 +03:00
|
|
|
parse_mode: typing.Optional[str] = (),
|
2020-10-02 23:06:48 +03:00
|
|
|
formatting_entities: typing.Optional[typing.List[types.TypeMessageEntity]] = None,
|
2019-05-03 22:37:27 +03:00
|
|
|
link_preview: bool = True,
|
2019-07-17 13:37:16 +03:00
|
|
|
file: 'typing.Union[hints.FileLike, typing.Sequence[hints.FileLike]]' = None,
|
2021-06-15 23:57:32 +03:00
|
|
|
thumb: 'hints.FileLike' = None,
|
2019-05-03 22:37:27 +03:00
|
|
|
force_document: bool = False,
|
|
|
|
clear_draft: bool = False,
|
2022-09-20 19:03:16 +03:00
|
|
|
buttons: typing.Optional['hints.MarkupLike'] = None,
|
2019-09-06 14:45:31 +03:00
|
|
|
silent: bool = None,
|
2021-08-05 11:54:07 +03:00
|
|
|
background: bool = None,
|
2021-05-31 16:36:40 +03:00
|
|
|
supports_streaming: bool = False,
|
2021-01-28 23:05:00 +03:00
|
|
|
schedule: 'hints.DateLike' = None,
|
|
|
|
comment_to: 'typing.Union[int, types.Message]' = None
|
2019-09-06 14:45:31 +03:00
|
|
|
) -> 'types.Message':
|
2018-06-09 23:05:06 +03:00
|
|
|
"""
|
2019-05-06 12:38:26 +03:00
|
|
|
Sends a message to the specified user, chat or channel.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
|
|
|
The default parse mode is the same as the official applications
|
|
|
|
(a custom flavour of markdown). ``**bold**, `code` or __italic__``
|
|
|
|
are available. In addition you can send ``[links](https://example.com)``
|
|
|
|
and ``[mentions](@username)`` (or using IDs like in the Bot API:
|
|
|
|
``[mention](tg://user?id=123456789)``) and ``pre`` blocks with three
|
|
|
|
backticks.
|
|
|
|
|
|
|
|
Sending a ``/start`` command with a parameter (like ``?start=data``)
|
|
|
|
is also done through this method. Simply send ``'/start data'`` to
|
|
|
|
the bot.
|
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
See also `Message.respond() <telethon.tl.custom.message.Message.respond>`
|
|
|
|
and `Message.reply() <telethon.tl.custom.message.Message.reply>`.
|
2019-05-09 13:24:37 +03:00
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Arguments
|
2018-06-09 23:05:06 +03:00
|
|
|
entity (`entity`):
|
|
|
|
To who will it be sent.
|
|
|
|
|
2019-01-12 15:06:14 +03:00
|
|
|
message (`str` | `Message <telethon.tl.custom.message.Message>`):
|
2018-06-09 23:05:06 +03:00
|
|
|
The message to be sent, or another message object to resend.
|
|
|
|
|
|
|
|
The maximum length for a message is 35,000 bytes or 4,096
|
|
|
|
characters. Longer messages will not be sliced automatically,
|
|
|
|
and you should slice them manually if the text to send is
|
|
|
|
longer than said length.
|
|
|
|
|
2019-01-12 15:06:14 +03:00
|
|
|
reply_to (`int` | `Message <telethon.tl.custom.message.Message>`, optional):
|
2018-06-09 23:05:06 +03:00
|
|
|
Whether to reply to a message or not. If an integer is provided,
|
|
|
|
it should be the ID of the message that it should reply to.
|
|
|
|
|
2021-05-31 16:36:40 +03:00
|
|
|
attributes (`list`, optional):
|
|
|
|
Optional attributes that override the inferred ones, like
|
|
|
|
:tl:`DocumentAttributeFilename` and so on.
|
|
|
|
|
2018-06-09 23:05:06 +03:00
|
|
|
parse_mode (`object`, optional):
|
2019-01-12 15:06:14 +03:00
|
|
|
See the `TelegramClient.parse_mode
|
|
|
|
<telethon.client.messageparse.MessageParseMethods.parse_mode>`
|
|
|
|
property for allowed values. Markdown parsing will be used by
|
|
|
|
default.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2020-10-02 23:06:48 +03:00
|
|
|
formatting_entities (`list`, optional):
|
|
|
|
A list of message formatting entities. When provided, the ``parse_mode`` is ignored.
|
|
|
|
|
2018-06-09 23:05:06 +03:00
|
|
|
link_preview (`bool`, optional):
|
|
|
|
Should the link preview be shown?
|
|
|
|
|
|
|
|
file (`file`, optional):
|
|
|
|
Sends a message with a file attached (e.g. a photo,
|
|
|
|
video, audio or document). The ``message`` may be empty.
|
|
|
|
|
2021-06-15 23:57:32 +03:00
|
|
|
thumb (`str` | `bytes` | `file`, optional):
|
|
|
|
Optional JPEG thumbnail (for documents). **Telegram will
|
|
|
|
ignore this parameter** unless you pass a ``.jpg`` file!
|
|
|
|
The file must also be small in dimensions and in disk size.
|
|
|
|
Successful thumbnails were files below 20kB and 320x320px.
|
|
|
|
Width/height and dimensions/size ratios may be important.
|
|
|
|
For Telegram to accept a thumbnail, you must provide the
|
|
|
|
dimensions of the underlying media through ``attributes=``
|
|
|
|
with :tl:`DocumentAttributesVideo` or by installing the
|
|
|
|
optional ``hachoir`` dependency.
|
|
|
|
|
2018-06-09 23:05:06 +03:00
|
|
|
force_document (`bool`, optional):
|
|
|
|
Whether to send the given file as a document or not.
|
|
|
|
|
|
|
|
clear_draft (`bool`, optional):
|
|
|
|
Whether the existing draft should be cleared or not.
|
|
|
|
|
2018-10-06 21:20:11 +03:00
|
|
|
buttons (`list`, `custom.Button <telethon.tl.custom.button.Button>`, :tl:`KeyboardButton`):
|
2018-07-10 13:42:57 +03:00
|
|
|
The matrix (list of lists), row list or button to be shown
|
|
|
|
after sending the message. This parameter will only work if
|
|
|
|
you have signed in as a bot. You can also pass your own
|
|
|
|
:tl:`ReplyMarkup` here.
|
|
|
|
|
2018-08-07 13:46:05 +03:00
|
|
|
All the following limits apply together:
|
|
|
|
|
|
|
|
* There can be 100 buttons at most (any more are ignored).
|
|
|
|
* There can be 8 buttons per row at most (more are ignored).
|
|
|
|
* The maximum callback data per button is 64 bytes.
|
|
|
|
* The maximum data that can be embedded in total is just
|
|
|
|
over 4KB, shared between inline callback data and text.
|
|
|
|
|
2018-07-11 11:16:21 +03:00
|
|
|
silent (`bool`, optional):
|
|
|
|
Whether the message should notify people in a broadcast
|
2019-07-06 13:10:25 +03:00
|
|
|
channel or not. Defaults to `False`, which means it will
|
|
|
|
notify them. Set it to `True` to alter this behaviour.
|
2018-07-11 11:16:21 +03:00
|
|
|
|
2021-08-05 11:54:07 +03:00
|
|
|
background (`bool`, optional):
|
|
|
|
Whether the message should be send in background.
|
|
|
|
|
2021-05-31 16:36:40 +03:00
|
|
|
supports_streaming (`bool`, optional):
|
|
|
|
Whether the sent video supports streaming or not. Note that
|
|
|
|
Telegram only recognizes as streamable some formats like MP4,
|
|
|
|
and others like AVI or MKV will not work. You should convert
|
|
|
|
these to MP4 before sending if you want them to be streamable.
|
|
|
|
Unsupported formats will result in ``VideoContentTypeError``.
|
|
|
|
|
2019-09-06 14:45:31 +03:00
|
|
|
schedule (`hints.DateLike`, optional):
|
|
|
|
If set, the message won't send immediately, and instead
|
|
|
|
it will be scheduled to be automatically sent at a later
|
|
|
|
time.
|
|
|
|
|
2021-01-28 23:05:00 +03:00
|
|
|
comment_to (`int` | `Message <telethon.tl.custom.message.Message>`, optional):
|
|
|
|
Similar to ``reply_to``, but replies in the linked group of a
|
|
|
|
broadcast channel instead (effectively leaving a "comment to"
|
|
|
|
the specified message).
|
|
|
|
|
|
|
|
This parameter takes precedence over ``reply_to``. If there is
|
|
|
|
no linked chat, `telethon.errors.sgIdInvalidError` is raised.
|
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Returns
|
2018-07-10 13:42:57 +03:00
|
|
|
The sent `custom.Message <telethon.tl.custom.message.Message>`.
|
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
|
|
|
# Markdown is the default
|
2020-02-20 13:19:39 +03:00
|
|
|
await client.send_message('me', 'Hello **world**!')
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
# Default to another parse mode
|
|
|
|
client.parse_mode = 'html'
|
|
|
|
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_message('me', 'Some <b>bold</b> and <i>italic</i> text')
|
|
|
|
await client.send_message('me', 'An <a href="https://example.com">URL</a>')
|
2019-05-09 15:13:43 +03:00
|
|
|
# code and pre tags also work, but those break the documentation :)
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_message('me', '<a href="tg://user?id=me">Mentions</a>')
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
# Explicit parse mode
|
|
|
|
# No parse mode by default
|
|
|
|
client.parse_mode = None
|
|
|
|
|
|
|
|
# ...but here I want markdown
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_message('me', 'Hello, **world**!', parse_mode='md')
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
# ...and here I need HTML
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_message('me', 'Hello, <i>world</i>!', parse_mode='html')
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
# If you logged in as a bot account, you can send buttons
|
|
|
|
from telethon import events, Button
|
|
|
|
|
|
|
|
@client.on(events.CallbackQuery)
|
|
|
|
async def callback(event):
|
|
|
|
await event.edit('Thank you for clicking {}!'.format(event.data))
|
|
|
|
|
|
|
|
# Single inline button
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_message(chat, 'A single button, with "clk1" as data',
|
|
|
|
buttons=Button.inline('Click me', b'clk1'))
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
# Matrix of inline buttons
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_message(chat, 'Pick one from this grid', buttons=[
|
2019-05-09 13:24:37 +03:00
|
|
|
[Button.inline('Left'), Button.inline('Right')],
|
2020-02-20 13:19:39 +03:00
|
|
|
[Button.url('Check this site!', 'https://example.com')]
|
2019-05-09 13:24:37 +03:00
|
|
|
])
|
|
|
|
|
|
|
|
# Reply keyboard
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_message(chat, 'Welcome', buttons=[
|
2019-05-09 13:24:37 +03:00
|
|
|
Button.text('Thanks!', resize=True, single_use=True),
|
|
|
|
Button.request_phone('Send phone'),
|
|
|
|
Button.request_location('Send location')
|
|
|
|
])
|
|
|
|
|
|
|
|
# Forcing replies or clearing buttons.
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_message(chat, 'Reply to me', buttons=Button.force_reply())
|
|
|
|
await client.send_message(chat, 'Bye Keyboard!', buttons=Button.clear())
|
2019-09-06 14:45:31 +03:00
|
|
|
|
|
|
|
# Scheduling a message to be sent after 5 minutes
|
|
|
|
from datetime import timedelta
|
|
|
|
await client.send_message(chat, 'Hi, future!', schedule=timedelta(minutes=5))
|
2018-06-09 23:05:06 +03:00
|
|
|
"""
|
|
|
|
if file is not None:
|
|
|
|
return await self.send_file(
|
|
|
|
entity, file, caption=message, reply_to=reply_to,
|
2021-05-31 16:36:40 +03:00
|
|
|
attributes=attributes, parse_mode=parse_mode,
|
2021-06-15 23:57:32 +03:00
|
|
|
force_document=force_document, thumb=thumb,
|
2020-01-22 16:16:27 +03:00
|
|
|
buttons=buttons, clear_draft=clear_draft, silent=silent,
|
2021-05-31 16:36:40 +03:00
|
|
|
schedule=schedule, supports_streaming=supports_streaming,
|
|
|
|
formatting_entities=formatting_entities,
|
2021-08-05 11:54:07 +03:00
|
|
|
comment_to=comment_to, background=background
|
2018-06-09 23:05:06 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
entity = await self.get_input_entity(entity)
|
2021-01-28 23:05:00 +03:00
|
|
|
if comment_to is not None:
|
|
|
|
entity, reply_to = await self._get_comment_data(entity, comment_to)
|
|
|
|
|
2018-06-09 23:05:06 +03:00
|
|
|
if isinstance(message, types.Message):
|
2018-07-10 13:42:57 +03:00
|
|
|
if buttons is None:
|
|
|
|
markup = message.reply_markup
|
|
|
|
else:
|
2018-07-21 14:54:36 +03:00
|
|
|
markup = self.build_reply_markup(buttons)
|
2018-07-10 13:42:57 +03:00
|
|
|
|
2018-07-11 11:16:21 +03:00
|
|
|
if silent is None:
|
|
|
|
silent = message.silent
|
|
|
|
|
2018-08-19 12:49:36 +03:00
|
|
|
if (message.media and not isinstance(
|
|
|
|
message.media, types.MessageMediaWebPage)):
|
|
|
|
return await self.send_file(
|
|
|
|
entity,
|
|
|
|
message.media,
|
|
|
|
caption=message.message,
|
|
|
|
silent=silent,
|
2021-08-05 11:54:07 +03:00
|
|
|
background=background,
|
2018-08-19 12:49:36 +03:00
|
|
|
reply_to=reply_to,
|
|
|
|
buttons=markup,
|
2020-10-02 23:06:48 +03:00
|
|
|
formatting_entities=message.entities,
|
2019-09-06 14:45:31 +03:00
|
|
|
schedule=schedule
|
2018-08-19 12:49:36 +03:00
|
|
|
)
|
|
|
|
|
2018-06-09 23:05:06 +03:00
|
|
|
request = functions.messages.SendMessageRequest(
|
|
|
|
peer=entity,
|
|
|
|
message=message.message or '',
|
2018-07-11 11:16:21 +03:00
|
|
|
silent=silent,
|
2021-08-05 11:54:07 +03:00
|
|
|
background=background,
|
2018-08-19 12:49:36 +03:00
|
|
|
reply_to_msg_id=utils.get_message_id(reply_to),
|
2018-07-10 13:42:57 +03:00
|
|
|
reply_markup=markup,
|
2018-06-09 23:05:06 +03:00
|
|
|
entities=message.entities,
|
|
|
|
clear_draft=clear_draft,
|
|
|
|
no_webpage=not isinstance(
|
2019-09-06 14:45:31 +03:00
|
|
|
message.media, types.MessageMediaWebPage),
|
|
|
|
schedule_date=schedule
|
2018-06-09 23:05:06 +03:00
|
|
|
)
|
|
|
|
message = message.message
|
|
|
|
else:
|
2020-10-02 23:06:48 +03:00
|
|
|
if formatting_entities is None:
|
|
|
|
message, formatting_entities = await self._parse_message_text(message, parse_mode)
|
2019-07-17 13:06:23 +03:00
|
|
|
if not message:
|
|
|
|
raise ValueError(
|
|
|
|
'The message cannot be empty unless a file is provided'
|
|
|
|
)
|
|
|
|
|
2018-06-09 23:05:06 +03:00
|
|
|
request = functions.messages.SendMessageRequest(
|
|
|
|
peer=entity,
|
|
|
|
message=message,
|
2020-10-02 23:06:48 +03:00
|
|
|
entities=formatting_entities,
|
2018-06-09 23:05:06 +03:00
|
|
|
no_webpage=not link_preview,
|
|
|
|
reply_to_msg_id=utils.get_message_id(reply_to),
|
2018-07-10 13:42:57 +03:00
|
|
|
clear_draft=clear_draft,
|
2018-07-11 11:16:21 +03:00
|
|
|
silent=silent,
|
2021-08-05 11:54:07 +03:00
|
|
|
background=background,
|
2019-09-06 14:45:31 +03:00
|
|
|
reply_markup=self.build_reply_markup(buttons),
|
|
|
|
schedule_date=schedule
|
2018-06-09 23:05:06 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
result = await self(request)
|
|
|
|
if isinstance(result, types.UpdateShortSentMessage):
|
2018-07-22 20:20:55 +03:00
|
|
|
message = types.Message(
|
2018-06-09 23:05:06 +03:00
|
|
|
id=result.id,
|
2020-10-23 22:27:39 +03:00
|
|
|
peer_id=await self._get_peer(entity),
|
2018-06-09 23:05:06 +03:00
|
|
|
message=message,
|
|
|
|
date=result.date,
|
|
|
|
out=result.out,
|
|
|
|
media=result.media,
|
2019-01-12 14:53:01 +03:00
|
|
|
entities=result.entities,
|
2021-02-23 21:42:09 +03:00
|
|
|
reply_markup=request.reply_markup,
|
2022-09-20 17:05:48 +03:00
|
|
|
ttl_period=result.ttl_period,
|
|
|
|
reply_to=types.MessageReplyHeader(request.reply_to_msg_id)
|
2018-07-22 20:20:55 +03:00
|
|
|
)
|
|
|
|
message._finish_init(self, {}, entity)
|
|
|
|
return message
|
2018-06-09 23:05:06 +03:00
|
|
|
|
|
|
|
return self._get_response_message(request, result, entity)
|
|
|
|
|
2019-05-03 22:37:27 +03:00
|
|
|
async def forward_messages(
|
|
|
|
self: 'TelegramClient',
|
2019-05-08 18:16:09 +03:00
|
|
|
entity: 'hints.EntityLike',
|
|
|
|
messages: 'typing.Union[hints.MessageIDLike, typing.Sequence[hints.MessageIDLike]]',
|
|
|
|
from_peer: 'hints.EntityLike' = None,
|
2019-05-03 22:37:27 +03:00
|
|
|
*,
|
2021-08-05 11:54:07 +03:00
|
|
|
background: bool = None,
|
|
|
|
with_my_score: bool = None,
|
2019-05-03 22:37:27 +03:00
|
|
|
silent: bool = None,
|
2019-09-06 14:45:31 +03:00
|
|
|
as_album: bool = None,
|
|
|
|
schedule: 'hints.DateLike' = None
|
|
|
|
) -> 'typing.Sequence[types.Message]':
|
2018-06-09 23:05:06 +03:00
|
|
|
"""
|
2019-05-06 12:38:26 +03:00
|
|
|
Forwards the given messages to the specified entity.
|
|
|
|
|
|
|
|
If you want to "forward" a message without the forward header
|
|
|
|
(the "forwarded from" text), you should use `send_message` with
|
|
|
|
the original message instead. This will send a copy of it.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
See also `Message.forward_to() <telethon.tl.custom.message.Message.forward_to>`.
|
|
|
|
|
|
|
|
Arguments
|
2018-06-09 23:05:06 +03:00
|
|
|
entity (`entity`):
|
|
|
|
To which entity the message(s) will be forwarded.
|
|
|
|
|
2019-01-12 15:06:14 +03:00
|
|
|
messages (`list` | `int` | `Message <telethon.tl.custom.message.Message>`):
|
2018-06-09 23:05:06 +03:00
|
|
|
The message(s) to forward, or their integer IDs.
|
|
|
|
|
|
|
|
from_peer (`entity`):
|
|
|
|
If the given messages are integer IDs and not instances
|
|
|
|
of the ``Message`` class, this *must* be specified in
|
2019-03-10 15:29:34 +03:00
|
|
|
order for the forward to work. This parameter indicates
|
|
|
|
the entity from which the messages should be forwarded.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2018-07-11 11:16:21 +03:00
|
|
|
silent (`bool`, optional):
|
2019-08-10 10:06:05 +03:00
|
|
|
Whether the message should notify people with sound or not.
|
|
|
|
Defaults to `False` (send with a notification sound unless
|
|
|
|
the person has the chat muted). Set it to `True` to alter
|
|
|
|
this behaviour.
|
2018-07-11 11:16:21 +03:00
|
|
|
|
2021-08-05 11:54:07 +03:00
|
|
|
background (`bool`, optional):
|
|
|
|
Whether the message should be forwarded in background.
|
|
|
|
|
|
|
|
with_my_score (`bool`, optional):
|
|
|
|
Whether forwarded should contain your game score.
|
|
|
|
|
2019-04-13 12:02:54 +03:00
|
|
|
as_album (`bool`, optional):
|
2020-10-01 13:22:55 +03:00
|
|
|
This flag no longer has any effect.
|
2019-04-11 13:47:14 +03:00
|
|
|
|
2019-09-06 14:45:31 +03:00
|
|
|
schedule (`hints.DateLike`, optional):
|
|
|
|
If set, the message(s) won't forward immediately, and
|
|
|
|
instead they will be scheduled to be automatically sent
|
|
|
|
at a later time.
|
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Returns
|
2019-06-01 17:27:53 +03:00
|
|
|
The list of forwarded `Message <telethon.tl.custom.message.Message>`,
|
2018-06-09 23:05:06 +03:00
|
|
|
or a single one if a list wasn't provided as input.
|
2018-12-20 22:33:25 +03:00
|
|
|
|
|
|
|
Note that if all messages are invalid (i.e. deleted) the call
|
|
|
|
will fail with ``MessageIdInvalidError``. If only some are
|
2019-07-06 13:10:25 +03:00
|
|
|
invalid, the list will have `None` instead of those messages.
|
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
|
|
|
|
|
|
|
|
# a single one
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.forward_messages(chat, message)
|
2019-05-09 13:24:37 +03:00
|
|
|
# or
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.forward_messages(chat, message_id, from_chat)
|
2019-05-09 13:24:37 +03:00
|
|
|
# or
|
2019-08-14 00:33:39 +03:00
|
|
|
await message.forward_to(chat)
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
# multiple
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.forward_messages(chat, messages)
|
2019-05-09 13:24:37 +03:00
|
|
|
# or
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.forward_messages(chat, message_ids, from_chat)
|
2019-05-09 13:24:37 +03:00
|
|
|
|
|
|
|
# Forwarding as a copy
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_message(chat, message)
|
2018-06-09 23:05:06 +03:00
|
|
|
"""
|
2020-10-01 13:22:55 +03:00
|
|
|
if as_album is not None:
|
|
|
|
warnings.warn('the as_album argument is deprecated and no longer has any effect')
|
|
|
|
|
2018-06-09 23:05:06 +03:00
|
|
|
single = not utils.is_list_like(messages)
|
|
|
|
if single:
|
|
|
|
messages = (messages,)
|
|
|
|
|
2019-04-02 11:46:37 +03:00
|
|
|
entity = await self.get_input_entity(entity)
|
|
|
|
|
|
|
|
if from_peer:
|
|
|
|
from_peer = await self.get_input_entity(from_peer)
|
|
|
|
from_peer_id = await self.get_peer_id(from_peer)
|
|
|
|
else:
|
|
|
|
from_peer_id = None
|
|
|
|
|
2020-10-01 13:22:55 +03:00
|
|
|
def get_key(m):
|
2019-04-02 11:46:37 +03:00
|
|
|
if isinstance(m, int):
|
|
|
|
if from_peer_id is not None:
|
2020-10-01 13:22:55 +03:00
|
|
|
return from_peer_id
|
2019-04-02 11:46:37 +03:00
|
|
|
|
|
|
|
raise ValueError('from_peer must be given if integer IDs are used')
|
|
|
|
elif isinstance(m, types.Message):
|
2020-10-01 13:22:55 +03:00
|
|
|
return m.chat_id
|
2019-04-02 11:46:37 +03:00
|
|
|
else:
|
|
|
|
raise TypeError('Cannot forward messages of type {}'.format(type(m)))
|
|
|
|
|
|
|
|
sent = []
|
2020-10-01 13:22:55 +03:00
|
|
|
for _chat_id, chunk in itertools.groupby(messages, key=get_key):
|
2019-04-13 12:02:54 +03:00
|
|
|
chunk = list(chunk)
|
|
|
|
if isinstance(chunk[0], int):
|
2019-04-02 11:46:37 +03:00
|
|
|
chat = from_peer
|
|
|
|
else:
|
2019-04-13 12:02:54 +03:00
|
|
|
chat = await chunk[0].get_input_chat()
|
|
|
|
chunk = [m.id for m in chunk]
|
2019-04-02 11:46:37 +03:00
|
|
|
|
|
|
|
req = functions.messages.ForwardMessagesRequest(
|
|
|
|
from_peer=chat,
|
2019-04-13 12:02:54 +03:00
|
|
|
id=chunk,
|
2019-04-02 11:46:37 +03:00
|
|
|
to_peer=entity,
|
2019-04-11 13:47:14 +03:00
|
|
|
silent=silent,
|
2021-08-05 11:54:07 +03:00
|
|
|
background=background,
|
|
|
|
with_my_score=with_my_score,
|
2019-09-06 14:45:31 +03:00
|
|
|
schedule_date=schedule
|
2019-04-02 11:46:37 +03:00
|
|
|
)
|
|
|
|
result = await self(req)
|
|
|
|
sent.extend(self._get_response_message(req, result, entity))
|
|
|
|
|
2019-04-02 11:44:42 +03:00
|
|
|
return sent[0] if single else sent
|
2018-06-09 23:05:06 +03:00
|
|
|
|
|
|
|
async def edit_message(
|
2019-05-03 22:37:27 +03:00
|
|
|
self: 'TelegramClient',
|
2019-05-08 18:16:09 +03:00
|
|
|
entity: 'typing.Union[hints.EntityLike, types.Message]',
|
2022-08-30 13:32:21 +03:00
|
|
|
message: 'hints.MessageLike' = None,
|
2019-05-03 22:37:27 +03:00
|
|
|
text: str = None,
|
|
|
|
*,
|
|
|
|
parse_mode: str = (),
|
2021-05-31 16:36:40 +03:00
|
|
|
attributes: 'typing.Sequence[types.TypeDocumentAttribute]' = None,
|
2020-10-02 23:06:48 +03:00
|
|
|
formatting_entities: typing.Optional[typing.List[types.TypeMessageEntity]] = None,
|
2019-05-03 22:37:27 +03:00
|
|
|
link_preview: bool = True,
|
2019-05-08 18:16:09 +03:00
|
|
|
file: 'hints.FileLike' = None,
|
2021-06-15 23:57:32 +03:00
|
|
|
thumb: 'hints.FileLike' = None,
|
2019-11-23 13:00:51 +03:00
|
|
|
force_document: bool = False,
|
2022-09-20 19:03:16 +03:00
|
|
|
buttons: typing.Optional['hints.MarkupLike'] = None,
|
2021-05-31 16:36:40 +03:00
|
|
|
supports_streaming: bool = False,
|
2019-09-06 14:45:31 +03:00
|
|
|
schedule: 'hints.DateLike' = None
|
|
|
|
) -> 'types.Message':
|
2018-06-09 23:05:06 +03:00
|
|
|
"""
|
2019-05-06 12:38:26 +03:00
|
|
|
Edits the given message to change its text or media.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
See also `Message.edit() <telethon.tl.custom.message.Message.edit>`.
|
|
|
|
|
|
|
|
Arguments
|
2019-01-12 15:06:14 +03:00
|
|
|
entity (`entity` | `Message <telethon.tl.custom.message.Message>`):
|
2018-06-09 23:05:06 +03:00
|
|
|
From which chat to edit the message. This can also be
|
|
|
|
the message to be edited, and the entity will be inferred
|
|
|
|
from it, so the next parameter will be assumed to be the
|
|
|
|
message text.
|
|
|
|
|
2019-03-23 21:25:45 +03:00
|
|
|
You may also pass a :tl:`InputBotInlineMessageID`,
|
|
|
|
which is the only way to edit messages that were sent
|
|
|
|
after the user selects an inline query result.
|
|
|
|
|
2019-01-12 15:06:14 +03:00
|
|
|
message (`int` | `Message <telethon.tl.custom.message.Message>` | `str`):
|
|
|
|
The ID of the message (or `Message
|
|
|
|
<telethon.tl.custom.message.Message>` itself) to be edited.
|
|
|
|
If the `entity` was a `Message
|
|
|
|
<telethon.tl.custom.message.Message>`, then this message
|
|
|
|
will be treated as the new text.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
|
|
|
text (`str`, optional):
|
|
|
|
The new text of the message. Does nothing if the `entity`
|
2019-01-12 15:06:14 +03:00
|
|
|
was a `Message <telethon.tl.custom.message.Message>`.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
|
|
|
parse_mode (`object`, optional):
|
2019-01-12 15:06:14 +03:00
|
|
|
See the `TelegramClient.parse_mode
|
|
|
|
<telethon.client.messageparse.MessageParseMethods.parse_mode>`
|
|
|
|
property for allowed values. Markdown parsing will be used by
|
|
|
|
default.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2021-05-31 16:36:40 +03:00
|
|
|
attributes (`list`, optional):
|
|
|
|
Optional attributes that override the inferred ones, like
|
|
|
|
:tl:`DocumentAttributeFilename` and so on.
|
|
|
|
|
2020-10-02 23:06:48 +03:00
|
|
|
formatting_entities (`list`, optional):
|
|
|
|
A list of message formatting entities. When provided, the ``parse_mode`` is ignored.
|
|
|
|
|
2018-06-09 23:05:06 +03:00
|
|
|
link_preview (`bool`, optional):
|
|
|
|
Should the link preview be shown?
|
|
|
|
|
|
|
|
file (`str` | `bytes` | `file` | `media`, optional):
|
|
|
|
The file object that should replace the existing media
|
|
|
|
in the message.
|
|
|
|
|
2021-06-15 23:57:32 +03:00
|
|
|
thumb (`str` | `bytes` | `file`, optional):
|
|
|
|
Optional JPEG thumbnail (for documents). **Telegram will
|
|
|
|
ignore this parameter** unless you pass a ``.jpg`` file!
|
|
|
|
The file must also be small in dimensions and in disk size.
|
|
|
|
Successful thumbnails were files below 20kB and 320x320px.
|
|
|
|
Width/height and dimensions/size ratios may be important.
|
|
|
|
For Telegram to accept a thumbnail, you must provide the
|
|
|
|
dimensions of the underlying media through ``attributes=``
|
|
|
|
with :tl:`DocumentAttributesVideo` or by installing the
|
|
|
|
optional ``hachoir`` dependency.
|
|
|
|
|
2019-11-23 13:00:51 +03:00
|
|
|
force_document (`bool`, optional):
|
|
|
|
Whether to send the given file as a document or not.
|
|
|
|
|
2018-10-06 21:20:11 +03:00
|
|
|
buttons (`list`, `custom.Button <telethon.tl.custom.button.Button>`, :tl:`KeyboardButton`):
|
2018-07-10 17:13:58 +03:00
|
|
|
The matrix (list of lists), row list or button to be shown
|
|
|
|
after sending the message. This parameter will only work if
|
|
|
|
you have signed in as a bot. You can also pass your own
|
|
|
|
:tl:`ReplyMarkup` here.
|
|
|
|
|
2021-05-31 16:36:40 +03:00
|
|
|
supports_streaming (`bool`, optional):
|
|
|
|
Whether the sent video supports streaming or not. Note that
|
|
|
|
Telegram only recognizes as streamable some formats like MP4,
|
|
|
|
and others like AVI or MKV will not work. You should convert
|
|
|
|
these to MP4 before sending if you want them to be streamable.
|
|
|
|
Unsupported formats will result in ``VideoContentTypeError``.
|
|
|
|
|
2019-09-06 14:45:31 +03:00
|
|
|
schedule (`hints.DateLike`, optional):
|
|
|
|
If set, the message won't be edited immediately, and instead
|
|
|
|
it will be scheduled to be automatically edited at a later
|
|
|
|
time.
|
|
|
|
|
|
|
|
Note that this parameter will have no effect if you are
|
|
|
|
trying to edit a message that was sent via inline bots.
|
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Returns
|
2019-06-01 17:27:53 +03:00
|
|
|
The edited `Message <telethon.tl.custom.message.Message>`,
|
|
|
|
unless `entity` was a :tl:`InputBotInlineMessageID` in which
|
2019-05-20 12:38:26 +03:00
|
|
|
case this method returns a boolean.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Raises
|
2018-06-09 23:05:06 +03:00
|
|
|
``MessageAuthorRequiredError`` if you're not the author of the
|
|
|
|
message but tried editing it anyway.
|
|
|
|
|
|
|
|
``MessageNotModifiedError`` if the contents of the message were
|
|
|
|
not modified at all.
|
|
|
|
|
2019-07-20 12:10:01 +03:00
|
|
|
``MessageIdInvalidError`` if the ID of the message is invalid
|
|
|
|
(the ID itself may be correct, but the message with that ID
|
|
|
|
cannot be edited). For example, when trying to edit messages
|
|
|
|
with a reply markup (or clear markup) this error will be raised.
|
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Example
|
2019-05-09 13:24:37 +03:00
|
|
|
.. code-block:: python
|
|
|
|
|
2019-08-14 00:33:39 +03:00
|
|
|
message = await client.send_message(chat, 'hello')
|
2019-05-20 12:38:26 +03:00
|
|
|
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.edit_message(chat, message, 'hello!')
|
2019-05-09 13:24:37 +03:00
|
|
|
# or
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.edit_message(chat, message.id, 'hello!!')
|
2019-05-09 13:24:37 +03:00
|
|
|
# or
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.edit_message(message, 'hello!!!')
|
2018-06-09 23:05:06 +03:00
|
|
|
"""
|
2019-03-23 21:25:45 +03:00
|
|
|
if isinstance(entity, types.InputBotInlineMessageID):
|
2020-12-08 12:06:19 +03:00
|
|
|
text = text or message
|
2019-03-23 21:25:45 +03:00
|
|
|
message = entity
|
|
|
|
elif isinstance(entity, types.Message):
|
2018-06-09 23:05:06 +03:00
|
|
|
text = message # Shift the parameters to the right
|
|
|
|
message = entity
|
2020-10-01 13:22:55 +03:00
|
|
|
entity = entity.peer_id
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2020-10-02 23:06:48 +03:00
|
|
|
if formatting_entities is None:
|
|
|
|
text, formatting_entities = await self._parse_message_text(text, parse_mode)
|
2019-11-23 13:00:51 +03:00
|
|
|
file_handle, media, image = await self._file_to_media(file,
|
2021-05-31 16:36:40 +03:00
|
|
|
supports_streaming=supports_streaming,
|
2021-06-15 23:57:32 +03:00
|
|
|
thumb=thumb,
|
2021-05-31 16:36:40 +03:00
|
|
|
attributes=attributes,
|
2019-11-23 13:00:51 +03:00
|
|
|
force_document=force_document)
|
2019-03-23 21:25:45 +03:00
|
|
|
|
|
|
|
if isinstance(entity, types.InputBotInlineMessageID):
|
2020-04-03 19:37:46 +03:00
|
|
|
request = functions.messages.EditInlineBotMessageRequest(
|
2019-03-23 21:25:45 +03:00
|
|
|
id=entity,
|
|
|
|
message=text,
|
|
|
|
no_webpage=not link_preview,
|
2020-10-02 23:06:48 +03:00
|
|
|
entities=formatting_entities,
|
2019-03-23 21:25:45 +03:00
|
|
|
media=media,
|
|
|
|
reply_markup=self.build_reply_markup(buttons)
|
2020-04-03 19:37:46 +03:00
|
|
|
)
|
|
|
|
# Invoke `messages.editInlineBotMessage` from the right datacenter.
|
|
|
|
# Otherwise, Telegram will error with `MESSAGE_ID_INVALID` and do nothing.
|
|
|
|
exported = self.session.dc_id != entity.dc_id
|
|
|
|
if exported:
|
|
|
|
try:
|
|
|
|
sender = await self._borrow_exported_sender(entity.dc_id)
|
2020-04-28 21:49:57 +03:00
|
|
|
return await self._call(sender, request)
|
2020-04-03 19:37:46 +03:00
|
|
|
finally:
|
|
|
|
await self._return_exported_sender(sender)
|
|
|
|
else:
|
|
|
|
return await self(request)
|
2019-03-23 21:25:45 +03:00
|
|
|
|
|
|
|
entity = await self.get_input_entity(entity)
|
2018-06-09 23:05:06 +03:00
|
|
|
request = functions.messages.EditMessageRequest(
|
|
|
|
peer=entity,
|
|
|
|
id=utils.get_message_id(message),
|
|
|
|
message=text,
|
|
|
|
no_webpage=not link_preview,
|
2020-10-02 23:06:48 +03:00
|
|
|
entities=formatting_entities,
|
2018-07-10 17:13:58 +03:00
|
|
|
media=media,
|
2019-09-06 14:45:31 +03:00
|
|
|
reply_markup=self.build_reply_markup(buttons),
|
|
|
|
schedule_date=schedule
|
2018-06-09 23:05:06 +03:00
|
|
|
)
|
2018-06-14 18:09:20 +03:00
|
|
|
msg = self._get_response_message(request, await self(request), entity)
|
2018-06-09 23:05:06 +03:00
|
|
|
return msg
|
|
|
|
|
2019-05-03 22:37:27 +03:00
|
|
|
async def delete_messages(
|
|
|
|
self: 'TelegramClient',
|
2019-05-08 18:16:09 +03:00
|
|
|
entity: 'hints.EntityLike',
|
|
|
|
message_ids: 'typing.Union[hints.MessageIDLike, typing.Sequence[hints.MessageIDLike]]',
|
2019-05-03 22:37:27 +03:00
|
|
|
*,
|
2019-05-08 18:16:09 +03:00
|
|
|
revoke: bool = True) -> 'typing.Sequence[types.messages.AffectedMessages]':
|
2018-06-09 23:05:06 +03:00
|
|
|
"""
|
2019-05-06 12:38:26 +03:00
|
|
|
Deletes the given messages, optionally "for everyone".
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
See also `Message.delete() <telethon.tl.custom.message.Message.delete>`.
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
This method does **not** validate that the message IDs belong
|
|
|
|
to the chat that you passed! It's possible for the method to
|
|
|
|
delete messages from different private chats and small group
|
|
|
|
chats at once, so make sure to pass the right IDs.
|
|
|
|
|
|
|
|
Arguments
|
2018-06-09 23:05:06 +03:00
|
|
|
entity (`entity`):
|
|
|
|
From who the message will be deleted. This can actually
|
2019-07-06 13:10:25 +03:00
|
|
|
be `None` for normal chats, but **must** be present
|
2018-06-09 23:05:06 +03:00
|
|
|
for channels and megagroups.
|
|
|
|
|
2019-01-12 15:06:14 +03:00
|
|
|
message_ids (`list` | `int` | `Message <telethon.tl.custom.message.Message>`):
|
2018-06-09 23:05:06 +03:00
|
|
|
The IDs (or ID) or messages to be deleted.
|
|
|
|
|
|
|
|
revoke (`bool`, optional):
|
|
|
|
Whether the message should be deleted for everyone or not.
|
|
|
|
By default it has the opposite behaviour of official clients,
|
|
|
|
and it will delete the message for everyone.
|
2019-03-25 10:45:25 +03:00
|
|
|
|
|
|
|
`Since 24 March 2019
|
|
|
|
<https://telegram.org/blog/unsend-privacy-emoji>`_, you can
|
|
|
|
also revoke messages of any age (i.e. messages sent long in
|
|
|
|
the past) the *other* person sent in private conversations
|
|
|
|
(and of course your messages too).
|
|
|
|
|
|
|
|
Disabling this has no effect on channels or megagroups,
|
|
|
|
since it will unconditionally delete the message for everyone.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Returns
|
2018-06-09 23:05:06 +03:00
|
|
|
A list of :tl:`AffectedMessages`, each item being the result
|
|
|
|
for the delete calls of the messages in chunks of 100 each.
|
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-08-14 00:33:39 +03:00
|
|
|
await client.delete_messages(chat, messages)
|
2018-06-09 23:05:06 +03:00
|
|
|
"""
|
|
|
|
if not utils.is_list_like(message_ids):
|
|
|
|
message_ids = (message_ids,)
|
|
|
|
|
|
|
|
message_ids = (
|
|
|
|
m.id if isinstance(m, (
|
|
|
|
types.Message, types.MessageService, types.MessageEmpty))
|
|
|
|
else int(m) for m in message_ids
|
|
|
|
)
|
|
|
|
|
2020-04-23 21:40:23 +03:00
|
|
|
if entity:
|
|
|
|
entity = await self.get_input_entity(entity)
|
|
|
|
ty = helpers._entity_type(entity)
|
|
|
|
else:
|
|
|
|
# no entity (None), set a value that's not a channel for private delete
|
|
|
|
ty = helpers._EntityType.USER
|
|
|
|
|
|
|
|
if ty == helpers._EntityType.CHANNEL:
|
2018-06-09 23:05:06 +03:00
|
|
|
return await self([functions.channels.DeleteMessagesRequest(
|
|
|
|
entity, list(c)) for c in utils.chunks(message_ids)])
|
|
|
|
else:
|
|
|
|
return await self([functions.messages.DeleteMessagesRequest(
|
|
|
|
list(c), revoke) for c in utils.chunks(message_ids)])
|
|
|
|
|
|
|
|
# endregion
|
|
|
|
|
|
|
|
# region Miscellaneous
|
|
|
|
|
2018-06-27 14:05:19 +03:00
|
|
|
async def send_read_acknowledge(
|
2019-05-03 22:37:27 +03:00
|
|
|
self: 'TelegramClient',
|
2019-05-08 18:16:09 +03:00
|
|
|
entity: 'hints.EntityLike',
|
|
|
|
message: 'typing.Union[hints.MessageIDLike, typing.Sequence[hints.MessageIDLike]]' = None,
|
2019-05-03 22:37:27 +03:00
|
|
|
*,
|
|
|
|
max_id: int = None,
|
2022-04-01 15:54:28 +03:00
|
|
|
clear_mentions: bool = False,
|
|
|
|
clear_reactions: bool = False) -> bool:
|
2018-06-09 23:05:06 +03:00
|
|
|
"""
|
2019-05-06 12:38:26 +03:00
|
|
|
Marks messages as read and optionally clears mentions.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
|
|
|
This effectively marks a message as read (or more than one) in the
|
|
|
|
given conversation.
|
|
|
|
|
2018-11-24 22:30:10 +03:00
|
|
|
If neither message nor maximum ID are provided, all messages will be
|
|
|
|
marked as read by assuming that ``max_id = 0``.
|
|
|
|
|
2020-04-13 12:31:39 +03:00
|
|
|
If a message or maximum ID is provided, all the messages up to and
|
|
|
|
including such ID will be marked as read (for all messages whose ID
|
|
|
|
≤ max_id).
|
|
|
|
|
2019-09-24 17:19:35 +03:00
|
|
|
See also `Message.mark_read() <telethon.tl.custom.message.Message.mark_read>`.
|
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Arguments
|
2018-06-09 23:05:06 +03:00
|
|
|
entity (`entity`):
|
|
|
|
The chat where these messages are located.
|
|
|
|
|
2019-01-12 15:06:14 +03:00
|
|
|
message (`list` | `Message <telethon.tl.custom.message.Message>`):
|
2018-06-09 23:05:06 +03:00
|
|
|
Either a list of messages or a single message.
|
|
|
|
|
|
|
|
max_id (`int`):
|
2020-04-13 12:31:39 +03:00
|
|
|
Until which message should the read acknowledge be sent for.
|
|
|
|
This has priority over the ``message`` parameter.
|
2018-06-09 23:05:06 +03:00
|
|
|
|
|
|
|
clear_mentions (`bool`):
|
|
|
|
Whether the mention badge should be cleared (so that
|
|
|
|
there are no more mentions) or not for the given entity.
|
|
|
|
|
|
|
|
If no message is provided, this will be the only action
|
|
|
|
taken.
|
2019-05-09 13:24:37 +03:00
|
|
|
|
2022-04-01 15:54:28 +03:00
|
|
|
clear_reactions (`bool`):
|
|
|
|
Whether the reactions badge should be cleared (so that
|
|
|
|
there are no more reaction notifications) or not for the given entity.
|
|
|
|
|
|
|
|
If no message is provided, this will be the only action
|
|
|
|
taken.
|
|
|
|
|
2019-05-20 12:38:26 +03:00
|
|
|
Example
|
2019-05-09 13:24:37 +03:00
|
|
|
.. code-block:: python
|
|
|
|
|
2019-08-11 11:23:45 +03:00
|
|
|
# using a Message object
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_read_acknowledge(chat, message)
|
2019-08-11 11:23:45 +03:00
|
|
|
# ...or using the int ID of a Message
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_read_acknowledge(chat, message_id)
|
2019-08-11 11:23:45 +03:00
|
|
|
# ...or passing a list of messages to mark as read
|
2019-08-14 00:33:39 +03:00
|
|
|
await client.send_read_acknowledge(chat, messages)
|
2018-06-09 23:05:06 +03:00
|
|
|
"""
|
|
|
|
if max_id is None:
|
2018-11-24 22:30:10 +03:00
|
|
|
if not message:
|
|
|
|
max_id = 0
|
|
|
|
else:
|
2018-06-09 23:05:06 +03:00
|
|
|
if utils.is_list_like(message):
|
|
|
|
max_id = max(msg.id for msg in message)
|
|
|
|
else:
|
|
|
|
max_id = message.id
|
|
|
|
|
|
|
|
entity = await self.get_input_entity(entity)
|
|
|
|
if clear_mentions:
|
|
|
|
await self(functions.messages.ReadMentionsRequest(entity))
|
2022-04-01 15:54:28 +03:00
|
|
|
if max_id is None and not clear_reactions:
|
|
|
|
return True
|
|
|
|
if clear_reactions:
|
|
|
|
await self(functions.messages.ReadReactionsRequest(entity))
|
2018-06-09 23:05:06 +03:00
|
|
|
if max_id is None:
|
|
|
|
return True
|
|
|
|
|
|
|
|
if max_id is not None:
|
2019-12-23 15:52:07 +03:00
|
|
|
if helpers._entity_type(entity) == helpers._EntityType.CHANNEL:
|
2018-06-09 23:05:06 +03:00
|
|
|
return await self(functions.channels.ReadHistoryRequest(
|
2019-05-03 22:37:27 +03:00
|
|
|
utils.get_input_channel(entity), max_id=max_id))
|
2018-06-09 23:05:06 +03:00
|
|
|
else:
|
|
|
|
return await self(functions.messages.ReadHistoryRequest(
|
|
|
|
entity, max_id=max_id))
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2019-05-30 18:15:50 +03:00
|
|
|
async def pin_message(
|
|
|
|
self: 'TelegramClient',
|
|
|
|
entity: 'hints.EntityLike',
|
|
|
|
message: 'typing.Optional[hints.MessageIDLike]',
|
|
|
|
*,
|
2021-07-09 20:50:47 +03:00
|
|
|
notify: bool = False,
|
|
|
|
pm_oneside: bool = False
|
2019-05-30 18:15:50 +03:00
|
|
|
):
|
|
|
|
"""
|
2020-10-30 22:06:31 +03:00
|
|
|
Pins a message in a chat.
|
2019-05-30 18:15:50 +03:00
|
|
|
|
|
|
|
The default behaviour is to *not* notify members, unlike the
|
|
|
|
official applications.
|
|
|
|
|
|
|
|
See also `Message.pin() <telethon.tl.custom.message.Message.pin>`.
|
|
|
|
|
|
|
|
Arguments
|
|
|
|
entity (`entity`):
|
|
|
|
The chat where the message should be pinned.
|
|
|
|
|
|
|
|
message (`int` | `Message <telethon.tl.custom.message.Message>`):
|
|
|
|
The message or the message ID to pin. If it's
|
2020-10-31 13:31:09 +03:00
|
|
|
`None`, all messages will be unpinned instead.
|
2019-05-30 18:15:50 +03:00
|
|
|
|
|
|
|
notify (`bool`, optional):
|
|
|
|
Whether the pin should notify people or not.
|
2021-08-22 14:46:26 +03:00
|
|
|
|
2021-07-09 20:50:47 +03:00
|
|
|
pm_oneside (`bool`, optional):
|
|
|
|
Whether the message should be pinned for everyone or not.
|
|
|
|
By default it has the opposite behaviour of official clients,
|
|
|
|
and it will pin the message for both sides, in private chats.
|
2019-05-30 18:15:50 +03:00
|
|
|
|
|
|
|
Example
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# Send and pin a message to annoy everyone
|
2019-08-14 00:33:39 +03:00
|
|
|
message = await client.send_message(chat, 'Pinotifying is fun!')
|
|
|
|
await client.pin_message(chat, message, notify=True)
|
2019-05-30 18:15:50 +03:00
|
|
|
"""
|
2021-07-09 20:50:47 +03:00
|
|
|
return await self._pin(entity, message, unpin=False, notify=notify, pm_oneside=pm_oneside)
|
2020-10-31 13:31:09 +03:00
|
|
|
|
|
|
|
async def unpin_message(
|
|
|
|
self: 'TelegramClient',
|
|
|
|
entity: 'hints.EntityLike',
|
|
|
|
message: 'typing.Optional[hints.MessageIDLike]' = None,
|
|
|
|
*,
|
|
|
|
notify: bool = False
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
Unpins a message in a chat.
|
|
|
|
|
|
|
|
If no message ID is specified, all pinned messages will be unpinned.
|
|
|
|
|
|
|
|
See also `Message.unpin() <telethon.tl.custom.message.Message.unpin>`.
|
|
|
|
|
|
|
|
Arguments
|
|
|
|
entity (`entity`):
|
|
|
|
The chat where the message should be pinned.
|
|
|
|
|
|
|
|
message (`int` | `Message <telethon.tl.custom.message.Message>`):
|
|
|
|
The message or the message ID to unpin. If it's
|
|
|
|
`None`, all messages will be unpinned instead.
|
|
|
|
|
|
|
|
Example
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
# Unpin all messages from a chat
|
|
|
|
await client.unpin_message(chat)
|
|
|
|
"""
|
|
|
|
return await self._pin(entity, message, unpin=True, notify=notify)
|
|
|
|
|
2021-07-09 20:50:47 +03:00
|
|
|
async def _pin(self, entity, message, *, unpin, notify=False, pm_oneside=False):
|
2019-11-04 11:46:17 +03:00
|
|
|
message = utils.get_message_id(message) or 0
|
2019-05-30 18:15:50 +03:00
|
|
|
entity = await self.get_input_entity(entity)
|
2020-10-31 13:31:09 +03:00
|
|
|
if message <= 0: # old behaviour accepted negative IDs to unpin
|
|
|
|
await self(functions.messages.UnpinAllMessagesRequest(entity))
|
|
|
|
return
|
|
|
|
|
2020-04-29 11:29:14 +03:00
|
|
|
request = functions.messages.UpdatePinnedMessageRequest(
|
2019-05-30 18:15:50 +03:00
|
|
|
peer=entity,
|
|
|
|
id=message,
|
2020-10-31 13:31:09 +03:00
|
|
|
silent=not notify,
|
|
|
|
unpin=unpin,
|
2021-07-09 20:50:47 +03:00
|
|
|
pm_oneside=pm_oneside
|
2020-04-29 11:29:14 +03:00
|
|
|
)
|
|
|
|
result = await self(request)
|
|
|
|
|
2021-08-22 14:46:26 +03:00
|
|
|
# Unpinning does not produce a service message.
|
|
|
|
# Pinning a message that was already pinned also produces no service message.
|
|
|
|
# Pinning a message in your own chat does not produce a service message,
|
|
|
|
# but pinning on a private conversation with someone else does.
|
|
|
|
if unpin or not result.updates:
|
2020-04-29 11:29:14 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
# Pinning a message that doesn't exist would RPC-error earlier
|
|
|
|
return self._get_response_message(request, result, entity)
|
2019-05-30 18:15:50 +03:00
|
|
|
|
2018-06-09 23:05:06 +03:00
|
|
|
# endregion
|
|
|
|
|
|
|
|
# endregion
|