I
I
idpetrishenko2021-10-09 13:58:00
Python
idpetrishenko, 2021-10-09 13:58:00

I'm trying to create in python reading links line by line. So that he would receive data from them in turn. But nothing comes out. What to do?

Good afternoon!
There are 350 links to chats in Telegram. I want to automate login. I tried to specify the link addresses in the code, but it only works on a small number of links. Then I decided to try to read from the file, but for some reason there is a very long wait and then it gives an error: Attempt 1 at connecting failed: TimeoutError:

from telethon import TelegramClient
from telethon.tl.functions.channels import JoinChannelRequest
from telethon import TelegramClient, events
import asyncio
from time import sleep

# import sys

api_id = 12345678
api_hash = '123456789'

file1 = open("sample.txt", "r")

for line in lines:
    print(line.strip())

file1.close

entitys = []
counter = []

with TelegramClient('anon', api_id, api_hash) as client:
    for g in groups:
        channel = client.loop.run_until_complete(client.get_input_entity(g))
        entitys.append(channel)
        print('ожидание')


    # print(len(entitys))
    async def main():
        async with TelegramClient('anon', api_id, api_hash) as subscriber:
            await subscriber.send_message('@gle_lev', 'Hello')
            # считываем все строки
            lines = file1.readlines()

            @subscriber.on(events.NewMessage(pattern='Start'))
            async def handler(event):
                for e in entitys:
                    used_link_index = entitys.index(e)
                    used_link = groups[used_link_index]
                    counter.append(1)
                    if len(counter) <= 3:
                        await subscriber(JoinChannelRequest(channel=e))
                        await subscriber.send_message('@gle_lev', 'Done for ' + str(used_link))
                        print(used_link)
                        sleep(5)
                    elif len(counter) == 4:
                        await subscriber(JoinChannelRequest(channel=e))
                        await subscriber.send_message('@gle_lev', 'Done for ' + str(used_link))
                        print('I am sleeping for 220 seconds')
                        await subscriber.send_message('@gle_lev', 'I am sleeping for 220 seconds')
                        counter.clear()
                        print(used_link)
                        sleep(220)
                    else:
                        pass

            await subscriber.run_until_disconnected()

asyncio.run(main())

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question