Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

make infinite loop pyrogram

I need this part of script work infinitely(send messages), but it works only one time and then stops

f = open('text.txt')
t = 1
with app:
    while True:
        for line in f.readlines():
            try:
                app.send_message(chat_id=line[13:].rstrip(), text=txt)
                print(f"Успешно написал в чат по ссылке {line}")
                time.sleep(0.5)
            except:
                print(f"Что-то пошло не так... Возможно в чате {line} включен медленный режим")```
            

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The for loop stops when it gets to the end of the file. When the while loop repeats, there’s nothing for the for loop to read because you’re already at the end of the file.

You can seek to the beginning each time.

f = open('text.txt')
t = 1
with app:
    while True:
        f.seek(0)
        for line in f.readlines():
            try:
                app.send_message(chat_id=line[13:].rstrip(), text=txt)
                print(f"Успешно написал в чат по ссылке {line}")
                time.sleep(0.5)
            except:
                print(f"Что-то пошло не так... Возможно в чате {line} включен медленный режим")     

Or you could save the contents to a list and loop through that.

with open('text.txt') as f:
    lines = f.readlines()
t = 1
with app:
    while True:
        for line in lines:
            try:
                app.send_message(chat_id=line[13:].rstrip(), text=txt)
                print(f"Успешно написал в чат по ссылке {line}")
                time.sleep(0.5)
            except:
                print(f"Что-то пошло не так... Возможно в чате {line} включен медленный режим")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading