Adding multiple contacts in telegram by username list in your csv file

hi everyone,
im new in python. im making a code that add multiple contacts in your telegram account by the username that listed in your csv file.

i have this code. cant seem to work. please help

from telethon.sync import TelegramClient
from telethon import TelegramClient
from import InputPeerEmpty, InputPeerChannel, InputPeerUser
from import GetDialogsRequest
from import AddChatUserRequest
from import InputPhoneContact
from import ImportContactsRequest
from telethon import functions, types
import sys
import csv
import traceback
import time
import random

name= #session
api_id = #id
api_hash = #hash
phone = #phone
client = TelegramClient(phone, api_id, api_hash)

if not client.is_user_authorized():
    client.sign_in(phone, input('40779'))

user = []   
with open(r"members.csv", 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]

    result = client(GetDialogsRequest(

    user_to_add = ['username']
    client(functions.contacts.AddContactRequest(id=user_to_add, first_name='User', last_name='', phone='', add_phone_privacy_exception=True))

Hi Jason, and welcome!

Sorry, are you asking a question or just sharing your code?

sorry im newbie, im asking a question . my code cant seem to work. i want it to work like the title. i need help

Okay. What’s your code doing, what did you expect it to do, what does it
do instead?

Are you getting an error? If you are, please copy and paste the full
traceback, starting with the line “Traceback” to the end where the error
message is shown.


This code is an api telegram if you put your friend’s telegram username in the #username and run . That user will be added to your telegram contacts

client(functions.contacts.AddContactRequest(id= **'#username'**, first_name='User', last_name='', phone='', add_phone_privacy_exception=True))

i have an csv file file like this

username, id, access_hash, name
user1,123456, 123hash, Robert
user2, 123457, 124hash, Jason

what i need is loop the code that automatically input each csv data row 0 in the function #username
it should be like this:

client(functions.contacts.AddContactRequest(id=**'user1'**, first_name='User', last_name='', phone='', add_phone_privacy_exception=True))

then the next:

client(functions.contacts.AddContactRequest(id=**'user2'**, first_name='User', last_name='', phone='', add_phone_privacy_exception=True))

and so on until csv list done

just got it working. i now have 364 contacts.

ill share the whole code later