Search code examples
pythontelethon

How to send messages scraped from telegram channels using Telethon bot?


When iterated messages from a telegram channel is being sent using telethon bot, it raises telethon.errors.rpcerrorlist.MediaEmptyError error.

I am trying to scrap messages from a certain telegram channel using a USER client and then posting it using a bot client, but upto the point of scraping the messages, the code works but it raises an error when the bot tries to send the message to the user.
The bot works in such a way:

  • User sends the bot the channel link
  • the bot scrapes the channel and sends it to the user

Code for the scraping and sending part

async def scrape_channel(channel_name,chat_id):
    async for message in client2.iter_messages(entity=channel_name,limit=6, reverse=True,filter=types.InputMessagesFilterEmpty):
        if message.text or message.media and not message.sticker:
           await client.send_message(chat_id, messages=message)#chat_id here is the chat between the user and bot

If there are any inefficiencies in the code, please feel free to point out


Solution

  • The message object, itered by client2, is accessible only by client2. If the client is a participant of channel_name, you can get the message using await client.get_messages(channel_name, ids=[message.id]), or, use await client.send_message(chat_id, message.text, file=message.media, ..) (the remaining params can be found in the docs).

    Hope it helps you out!