K
K
kopelev20002020-09-17 22:00:41
Python
kopelev2000, 2020-09-17 22:00:41

Telethon stops adding numbers, how to solve the problem?

there was such a problem, I wrote a Python script that checks if the number is in telegram or not (adds a number to contacts, if there is a number, it is added, then deleted, the received info is written to csv; otherwise, an error is raised, whence it follows that there is no number), but at some point, the script stops writing numbers to contacts in tg, the question is, why is this all happening and how can I fix it? Code is attached (do not judge strictly)

import telethon
from telethon import TelegramClient, sync
from telethon.tl.functions.contacts import ImportContactsRequest
from telethon.tl.types import InputPhoneContact
from telethon import functions
from time import sleep
import csv

def csv_writer(path, fieldnames, data):
    with open(path, "w", newline='') as out_file:
        writer = csv.DictWriter(out_file, delimiter=';', fieldnames=fieldnames)
        writer.writeheader()
        for row in data:
            writer.writerow(row)

if __name__ == "__main__":

    api_id = 11111
    api_hash = 'чччччччч'
    phone = "+375291111111"

    client = TelegramClient('Pars-session', api_id, api_hash)
    client.start()
    client.sign_in(phone)

    phone_numbers = open('phone-numbers1000.txt', 'r')

    x = int(1)
    data = []
    for phone_number in phone_numbers:
        # print(result.stringify())
        try:
            contact = InputPhoneContact(client_id=0, phone=phone_number, first_name=phone_number,
                                        last_name=phone_number)
            result = client(ImportContactsRequest([contact]))
            print(client.get_entity(phone_number))
            print(x, ") Номер есть в телеграм", phone_number)
            delete_result = client(functions.contacts.DeleteContactsRequest(id=[phone_number]))
            data_true = []
            data_true.append(phone_number)
            data_true.append("Да")
            data.append(data_true)
        except ValueError:
            print(x, "Номера нет в телеграм: ", phone_number)
            data_false = []
            data_false.append(phone_number)
            data_false.append("Нет")
            data.append(data_false)
        x += 1
        sleep(3)

    getContacts = telethon.tl.functions.contacts.GetContactsRequest(0)
    Contacts = client(getContacts)
    phone_numbers.close()

    my_list = []
    fieldnames = data[0]
    cell = data[1:]
    for values in cell:
        inner_dict = dict(zip(fieldnames, values))
        my_list.append(inner_dict)

    path = "dict_output.csv"
    csv_writer(path, fieldnames, my_list)`

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Matvey, 2022-04-23
@mlt_melt

I think that telegram is blocking you because of too frequent actions.
Try to make a big delay.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question