简体   繁体   中英

Python, Telethon send album

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

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()

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))
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM