0

I have a problem with forwarding messages with caption_entities. Attached below is the code that I use to make the transfer itself, as well as the result of its operation. The result shows that pyrogram lost the entity and sent it without them for a reason unknown to me.

from pyrogram import Client
from pyrogram.types import Message
from pyrogram.types import ChatPrivileges, InputMediaPhoto, InputMediaVideo

client = Client()
group_id = -1
@client.on_message()
async def handler(client: Client, message: Message):
  global group_id
  if group_id != -1:
    return
  group_id = message.media_group_id
  msg_group = []; media = []
  async for msg in client.get_chat_history(chat_id=message.chat.id, limit=10):
      if msg.media_group_id == group_id:
          msg_group.append(msg)
      else:
          break
  for m in msg_group:
      if m.photo:
          media.append(InputMediaPhoto(media=m.photo.file_id, caption=m.caption if m.caption else None, caption_entities=m.caption_entities if m.caption_entities else None))
      elif m.video:
        media.append(InputMediaVideo(media=m.video.file_id, caption=m.caption if m.caption else None, caption_entities=m.caption_entities if m.caption_entities else None))

  await client.send_media_group(chat_id=message.chat.id, media=media[::-1])
  group_id = -1

client.run()

enter image description here

1
  • 1
    I deleted the information from the client for security reasons. Commented Nov 30 at 5:00

1 Answer 1

0

The issue is not with your logic — it is because Pyrogram does not automatically preserve caption entities when you manually rebuild a media group. When you extract messages from chat history using:

await client.get_chat_history(...)

Pyrogram gives you:

message.caption

message.caption_entities

BUT entities are NOT applied automatically when you create new InputMediaPhoto / InputMediaVideo. InputMediaPhoto and InputMediaVideo require you to explicitly pass the entities if you want them preserved. You are only sending:

caption=m.caption

So the entities are lost.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.