1

Hi there I have an idea for this script as usaull telethon libary when the system internet was dissconnect (not isp) it will try for 5 time(s) , I want to increase the number of trying to reconnect for exmple for many times or a infinity loop for trying to reconnect .this is my code if the internet was dissconnet telethon trying to reconnect for 5 times I want increase that as infinity loop

from config import configuration
from telethon.sync import TelegramClient, events


client = TelegramClient('anon', configuration["api_id"], configuration["api_hash"])

print('please wait this action take a less than minute'.title())

with client:
    for dialog in client.iter_dialogs():
        if dialog.is_channel:
            if dialog.name == configuration["send_channel"]:
                channel_id = dialog.id
                break

print('start'.title())


@client.on(events.NewMessage(chats=configuration["monitoring_channel"]))
async def my_event_handler(event):
    print(event.raw_text)
    await client.send_message(entity=channel_id, message=event.message)


client.start()
client.run_until_disconnected()

enter image description here

MmDYi
  • 41
  • 8
  • What is the code you are running? what have u Tried so far? and so on there are so many open Questions bc you did not give us any information about the program you are running just the error that is meaningless with out any code – gerda die gandalfziege Jan 25 '22 at 15:11
  • @gerda die gandalfziege sorry you can check it now – MmDYi Jan 25 '22 at 15:16

1 Answers1

3

This code retries to connect again if the connection failed.

from config import configuration
from telethon.sync import TelegramClient, events


client = TelegramClient('anon', configuration["api_id"], configuration["api_hash"])

print('please wait this action take a less than minute'.title())

with client:

    for dialog in client.iter_dialogs():

        if dialog.is_channel and dialog.name == configuration["send_channel"]:

            channel_id = dialog.id

            break

print('start'.title())


@client.on(events.NewMessage(chats=configuration["monitoring_channel"]))
async def my_event_handler(event):
    print(event.raw_text)
    await client.send_message(entity=channel_id, message=event.message)


def test():
    
    try:

        client.start()
        client.run_until_disconnected()

    except ConnectionError: #catches the ConnectionError and starts the connections process again

        print('ConnectionError')
        test()