I used to be able to send atleast 4 messages through telegram api before encountering PeerFloodError but now it occurs after every dm. Can it be fixed

31 views Asked by At

When I started testing out the code and used the api for the first few times I was able to send messages without any problems but at that time to I was encountering the PeerFloodError after 4 tries so I tried switching to a different api after it gives me the error. At first the test were running properly it used to send 4 messages with time interval each and switch to a different api after getting the PeerFloodError but after a while of testing it started giving the PeerFloodError after each message. Here is the code I use to send out messages:

def send_custom_messages(api_id, api_hash, phone, input_file, message, last_sent_index=0):
    try:
        client = TelegramClient(phone, api_id, api_hash)

        client.connect()
        if not client.is_user_authorized():
            client.send_code_request(phone)
            client.sign_in(phone, input('[+] Enter the code: '))

        users = []
        with open(input_file, encoding='UTF-8') as f:
            rows = csv.reader(f, delimiter=",", lineterminator="\n")
            next(rows, None)
            for row in rows:
                user = {}
                user['username'] = row[0]
                user['id'] = int(row[1])
                user['access_hash'] = int(row[2])
                user['name'] = row[3]
                users.append(user)

        # Start sending messages from the last sent index
        for user in users[last_sent_index:]:
            try:
                if user['username']:
                    receiver = client.get_input_entity(user['username'])
                else:
                    receiver = InputPeerUser(user['id'], user['access_hash'])

                print(f"[+] Using API {api_id}")
                print(f"[+] Sending Message to: {user['name']}")
                client.send_message(receiver, message.format(user['name']))
                wait_time = random.randint(90, 150)  # Random wait time
                for i in range(wait_time,0):
                    print(f"[+] Waiting {i} seconds")
                    time.sleep(1)
            except PeerFloodError:
                print("Getting Flood Error from Telegram. Switching to another API...")
                client.disconnect()
                return True, users.index(user)  # Indicate flood error and return index of last sent message
            except Exception as e:
                print(f"Error: {e}. Trying to continue...")
                continue

        client.disconnect()
        print("Done. Message sent to all users.")
        return False, len(users)  # Indicate success and return total number of users
    except Exception as e:
        print(f"Error: {e}")
        return False, 0

I tried waiting 120s but still the error persists. I tired waiting 24hrs to run the program again but still encountered the same errors.

0

There are 0 answers