Python, Telethon send album

2.5k views Asked by At

I can't make a script for a long time. I have one telegram channel, I don't want to resend an album from this channel, but just send it to me in one message

from telethon import TelegramClient, events
from telethon import events

api_id = 
api_hash = ""

chat = ''

client = TelegramClient('', api_id, api_hash)

print('started')

@client.on(events.Album)
async def handler(event):
 #what farther
2

There are 2 answers

2
Tibebes. M On

Here is one approach to do that:

from telethon import TelegramClient, events

api_id = ...
api_hash = ' ... '

chat = -1001277xxxxxx

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

@client.on(events.Album)
async def handler(event):

    # craft a new message and send
    await client.send_message(
        chat,
        file=event.messages, # event.messages is a List - meaning we're sending an album
        message=event.original_update.message.message,  # get the caption message from the album
    )

    ## or forward it directly
    # await event.forward_to(chat)

client.start()
client.run_until_disconnected()
0
Phani Rithvij On

There is send_file which says

file (...): To send an album, you should provide a list in this parameter. If a list or similar is provided, the files in it will be sent as an album in the order in which they appear, sliced in chunks of 10 if more than 10 are given.

caption (str, optional): Optional caption for the sent media message. When sending an album, the caption may be a list of strings, which will be assigned to the files pairwise.

So extending @Tibebes answer

await client.send_file( # Note this is send_file not send_message
    chat,
    file=event.messages
    caption=list(map(lambda a: str(a.message), event.messages))
)