def add_contact_from_mobile(client, first_name, mobile, last_name=''): with client.telegram_client as client: result = client( functions.contacts.ImportContactsRequest([ types.InputPhoneContact(client_id=0, phone=mobile, first_name=first_name, last_name=last_name) ])) return result
def save_to_db(users, client): db_worker = SQLither(config.database_name) contacts = [] for user in users: contacts.append(types.InputPhoneContact( client_id= random.randint(-10000000,10000000), phone=user.get_phone(), first_name=user.get_first_name(), last_name=user.get_last_name() )) result = client(functions.contacts.ImportContactsRequest( contacts=contacts )) user_dic = dict() for user in users: user_dic[user.get_phone()[1:]] = user.get_initials() print("======================================") print("In telegram") print("======================================") phones = set() for user in result.users: print("{} - {}".format(user.phone, user_dic[user.phone])) phones.add(user.phone) db_worker.insert_real_user(user.id, user_dic[user.phone], "+{}".format(user.phone)) print("======================================") print("Not in telegram") print("======================================") for user in users: if not user.get_phone()[1:] in phones: print("{} - {} {}".format(user.get_phone()[1:], user.get_first_name(), user.get_last_name())) db_worker.insert_non_exist_user(user.get_initials(), user.get_phone()) # if i == 40: # time.sleep(300) # i = 0 # if is_real(result): # db_worker.insert_real_user(get_id(result), user.get_initials(), user.get_phone()) # else: # db_worker.insert_non_exist_user(user.get_initials(), user.get_phone()) # i += 1 db_worker.close()
async def insert_user_from_csv(file_path, rows_to_skip, col_name="phone"): client = tc.client session_name = tc.session_name with open(file_path) as csv_file: csv_reader = csv.DictReader(csv_file) for i in range(int(rows_to_skip)): next(csv_reader) inserted_user = 0 count = rows_to_skip print("Starting pushing data from csv to mongo-") for row in csv_reader: if row[col_name]: count += 1 while (True): try: result = await client( functions.contacts.ImportContactsRequest(contacts=[ types.InputPhoneContact( client_id=random.randrange(-2**63, 2**63), phone='+91' + row[col_name][-10:], first_name='', last_name='') ])) break except errors.FloodWaitError as e: print('FloodWaitError for session:', session_name, ' time:', e.seconds) client, session_name = await tc.get_next_client( client, session_name, e.seconds) try: if (result.users): user = result.users[0] if (hasattr(user.status, 'was_online') and user.status.was_online.date() > (date.today() - timedelta(days=60))): print("found user at:", count, "user_id:", row['user_id']) user.email = row['email'] if (user.first_name is None): user.first_name = row['firstname'] if telegram.insert_user_to_db(telegram_obj, user): inserted_user += 1 except Exception as e: print("Exception after fetching user", e) print(inserted_user, "- unique users are inserted from csv file")
client.start(phone) # Join channels channels_joined = all([ join_channel(client, from_channel), join_channel(client, to_channel) ]) if not channels_joined: print_output(client, status="Cannot join channels") continue me = client.get_me() main_client( contacts.ImportContactsRequest([ types.InputPhoneContact(0, phone, me.first_name or '.', me.last_name or '.') ])) if from_channel_entity.broadcast: main_client.edit_admin(from_channel, phone, is_admin=True) if to_channel_entity.broadcast: main_client.edit_admin(to_channel, phone, is_admin=True) while user_rows: row = user_rows.pop(0) user_id = int(row[0])