1

So I am using Python 3 to create a script with Telegram API but have come across a pickle.

Essentially I want anything I post to one channel to automatically be posted to a list of other channels.

As there are a lot of channels I am pretty sure I have split it over multiple lines, keeping the variables aligned with the first delimiter as per PEP8 code layout advice. However, for some reason, the code only posts content to the first line of channels in the list and not the others.

Right now my code is:

from telethon import TelegramClient, events

api_id = 9969279 #API_ID
api_hash = 'd5fa4ca47bcdb6f5bda7271f30e607e9' #API_HASH
channel = [-1001612029593] #MAIN_CHANNEL_ID
to_channel = [-1001408569373,-1001450011693,-1001589562089,-1001324581893,-1001486742214,-1001262799053,-1001577547504,-1001751417089,
              -1001714682472,-1001173439993,-1001564319953,-1001458872203,-1001580609047,-1001454607304,-1001412291633,-1001307551599,
              -1001472656059,-1001577461465,-1001577461465,-1001438912928,-1001166100393,-1001226585647,-1001662052611,-1001412291633,
              -1001213093519,-1001543013350,-1001599148038,-1001250770087,-1001489822613,-1001586463068,-1001173726857,-1001425575134,
              -1001549263406,-1001566043627,-1001605175905,-1001229590696,-1001442156928] #OTHER_CHANNELS

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

@client.on(events.NewMessage(chats=channel))
async def my_event_handler(event):
    if event.message:
        for channelx in to_channel:
            await client.send_message(channelx, event.message)
        
client.start()
client.run_until_disconnected() 

I'm wondering as to whether the lines are too long - other than that I can't think of why it won't post all channels?

EDIT: When running the script I get the following notes on my CMD

Traceback (most recent call last):
  File "C:\Users\mimif\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\updates.py", line 451, in _dispatch_update
    await callback(event)
  File "C:\Users\mimif\OneDrive\Desktop\python\main.py", line 18, in my_event_handler
    await client.send_message(channelx, event.message)
  File "C:\Users\mimif\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\messages.py", line 825, in send_message
    return await self.send_file(
  File "C:\Users\mimif\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\uploads.py", line 412, in send_file
    return self._get_response_message(request, await self(request), entity)
  File "C:\Users\mimif\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\users.py", line 30, in __call__
    return await self._call(self._sender, request, ordered=ordered)
  File "C:\Users\mimif\AppData\Local\Programs\Python\Python310\lib\site-packages\telethon\client\users.py", line 84, in _call
    result = await future
telethon.errors.rpcerrorlist.ChannelPrivateError: The channel specified is private and you lack permission to access it. Another reason may be that you were banned from it (caused by SendMediaRequest)```
Mimi Beard
  • 11
  • 2
  • If you add `print(channelx)` as a line in the `for` loop, does it print all the list entries? If so, then I expect that you are being [rate limited](https://core.telegram.org/bots/faq#my-bot-is-hitting-limits-how-do-i-avoid-this) by the API. I see you aren't evaluating any responses from the API to check for success or rate-limit messages. – Dennis Williamson Sep 29 '22 at 12:54
  • Hi - really sorry just seen this! Would I put it under ```await client.send_message(channelx, event.message)``` – Mimi Beard Sep 29 '22 at 21:58
  • Before putting your line in the for loop this is the errors I get when posting (put in as edit above due to length) – Mimi Beard Sep 29 '22 at 22:18
  • Okay so I added the ```print(channelx)``` I am not getting error messages but now it isn't executing at all - nothing being sent out. But nothing showing on CMD as an error – Mimi Beard Sep 29 '22 at 23:35
  • Could this be meaningful? "The channel specified is private and you lack permission to access it. Another reason may be that you were banned from it (caused by SendMediaRequest)" – Dennis Williamson Sep 30 '22 at 03:23
  • Weirdly I am able to post in all the channels manually, annoyingly I can't see what channel it is especially - I'm starting to worry if it isn't the python part as much as how someone I asked set up the API side – Mimi Beard Sep 30 '22 at 08:58
  • Edit found that one channel and have removed from code - it's still only posting to the first 2 channels – Mimi Beard Sep 30 '22 at 09:27

0 Answers0