forward_messages()
невозможно изменить список entities
(список объектов форматирования текста) на уровне самого API Telegram.send_file()
, в этот метод можно передать список файлов, подпись и те самые entities
.from telethon import client, types
import typing
class MessagesGroup:
def __init__(self):
self._grouped_id: int | None = None
self._filelist: list[types.MessageMediaPhoto] = []
self._caption: str | None = None
self._entities: list[types.TypeMessageEntity] = []
self._supported_types = (types.MessageMediaPhoto, types.MessageMediaDocument)
def __len__(self):
return len(self._filelist)
def __iter__(self):
yield from self._filelist
@property
def grouped_id(self) -> int:
return self._grouped_id
async def add_message(self, message: types.Message) -> None:
if self.__len__() < 10:
if isinstance(message, types.Message):
if message.media and isinstance(message.media, self._supported_types):
if message.media not in self._filelist:
self._filelist.append(message.media)
if message.message and not self._caption:
self._caption = message.message
self._entities = message.entities
else:
print('The message file is already in the file list')
else:
raise AttributeError('The message must contain a photo, video, or animation')
else:
raise TypeError('The message must be an instance of types.Message')
else:
raise ValueError('The file list is full')
async def add_messages(self, iterable: typing.Iterable[types.Message], skip_error=False):
for message in iterable:
try:
await self.add_message(message)
except Exception as e:
if skip_error:
continue
raise
async def send(self, to_entity, from_client: client.TelegramClient, reply_to=None):
print(self._caption) # это уже делал для проверки
print(self._entities) # и это тоже
await from_client.send_file(
entity=to_entity,
file=self._filelist,
reply_to=reply_to,
caption=self._caption,
formatting_entities=self._entities
)
self._entities
содержит объекты форматирования в таком виде:[<telethon.tl.types.MessageEntityBold object at 0x0000019CDA361A10>]
async def _send_album(self: 'TelegramClient', entity, files, caption='',
formatting_entities=None,
progress_callback=None, reply_to=None,
parse_mode=(), silent=None, schedule=None,
supports_streaming=None, clear_draft=None,
force_document=False, background=None, ttl=None):
formatted_entities
result = []
while file:
result += await self._send_album(
entity, file[:10], caption=captions[:10], formatting_entities=formatting_entities,
progress_callback=used_callback, reply_to=reply_to,
parse_mode=parse_mode, silent=silent, schedule=schedule,
supports_streaming=supports_streaming, clear_draft=clear_draft,
force_document=force_document, background=background,
)
file = file[10:]
captions = captions[10:]
sent_count += 10
return result
formatted_entities
к первому сообщению, если оно содержит текст:if captions and captions[0]:
captions[0] = (captions[0][0], formatting_entities)