0

When my bot try to resend a group of photos, it splits it into separate messages. If it is a group of photos (in Telegram), I need to resend it wholly, as a group within one message. How to fix it?

from telethon import TelegramClient, events
import asyncio

Id_bot = 
Id_Group2 = 
Id_Group3 = 
Id_Group4 = 
Id_Group5 = 
api_id = ''
api_hash = ''

client = TelegramClient('none', api_id, api_hash)

@client.on(events.NewMessage)
async def handler(event):
    chat = await event.get_chat()
    chat_id = event.chat_id
    print(chat_id)

    # Forward only if the message is not sent in Id_Group1 to avoid an infinite loop
    if chat_id not in [Id_Group2, Id_Group3, Id_Group4, Id_Group5]:
        if event.media:
            if isinstance(event.media, list):
                # Multiple media files in a single message
                media = []
                for file in event.media:
                    media.append(await client.upload_file(file))
                await client.send_file(Id_bot, media[0], caption=event.message.message, file=media[1:])
            else:
                # Single media file
                caption = event.message.message if event.message.message else None
                await client.send_file(Id_bot, event.media, caption=caption)
        elif event.message.message:
            await client.send_message(Id_bot, event.message.message)

client.start()
client.run_until_disconnected()

1 Answers1

0

You've misunderstood how the api works.. Telegram galleries are all single messages, they're gathered by the Telegram apps to display them as a collage using the message.grouped_id which signifies if a single message is part of a group. so .media will never be a list, your conditional will never be executed.

There is a dedicated handler to deal with Albums, this handler is the same as NewMessage, but it does the caching and gathering all messages into a single list.

Also, telethon already offers a way to clone a message, you don't need to handle text/media (send_message/send_file) seperately. they're dealt with by only passing them the entire Message object, the method will extract the caption or media and any other info and clones the message.

so your code can be written as:

Id_bot = 
Id_Group2 = 
Id_Group3 = 
Id_Group4 = 
Id_Group5 = 

api_id = ''
api_hash = ''

chats_to_listen_to = [Id_Group1]

client = TelegramClient('none', api_id, api_hash)

@client.on(events.NewMessage(chats=chats_to_listen_to))
async def handler(event):
    if event.grouped_id:
        return    # ignore messages that are gallery here

    await client.send_message(Id_bot, event.message)

@client.on(events.Album(chats=chats_to_listen_to))
async def handler(event):
    await client.send_file(Id_bot, event.messages)

client.start()
client.run_until_disconnected()

a little note about your other misunderstanding:

Forward only if the message is not sent in Id_Group1 to avoid an infinite loop

If i understood what you're implying right, This is wrong. sending something will deliver you a response by the server to your session, so, you it won't dispatch something you sent yourself through NewMessage events and cause a loop, only your other active sessions will receive it as events.

MustA
  • 932
  • 5
  • 8