A
A
Andrey2020-12-29 00:46:31
Python
Andrey, 2020-12-29 00:46:31

How to add a user to contacts using telethon?

I'm trying to add a contact using the telethon library.

from telethon.tl.functions.contacts import GetContactsRequest
from telethon.tl.types import InputPeerUser
from telethon.tl.types import InputPhoneContact
from telethon import TelegramClient, events, sync

api_id = xxxx
api_hash = 'xxxx'

phone_number='xxxx'

client = TelegramClient('session_name', api_id, api_hash)
client.connect()

contact = InputPhoneContact(client_id=0, phone=phone_number, first_name="xxx", last_name="xxx")
result = client.invoke(ImportContactsRequest([contact]))

contact_info = client.get_entity(phone_number)
print(contact_info.username)


In response I get an error

File "tg.py", line 17, in <module>
    result = client.invoke(ImportContactsRequest([contact]))
AttributeError: 'TelegramClient' object has no attribute 'invoke'


Judging by the answers in the telethon github, this attribute has been removed. How then can you add to contacts now? Any solutions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-12-29
Shults @noroots

https://tl.telethon.dev/methods/contacts/import_co...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question