Add new contact in API telegram Python telethon
Asked Answered
Z

4

8

How do I save a number in my contacts in telethon Python?

from telethon import TelegramClient
from telethon.tl.functions.contacts import GetContactsRequest
from telethon.tl.types import InputPeerUser
client = TelegramClient('arta0', api_id, api_hash)
client.connect()
#number=+19133704541
#name='ali karimi'

What module do I need to add contact?

Zina answered 16/6, 2017 at 20:21 Comment(0)
F
8

You can create a contact like this:

contact = InputPhoneContact(client_id = 0, phone = "+12345678", first_name="ABC", last_name="abc")

result = client.invoke(ImportContactsRequest([contact], replace=True))

To create a new contact you need to pass 0 for the client_id.

Faustinafaustine answered 17/6, 2017 at 9:3 Comment(1)
please answer my question #44605936Zina
J
6

You can create a new contact like this:

from telethon.sync import TelegramClient
from telethon import TelegramClient
from telethon.tl.functions.messages import AddChatUserRequest
from telethon.tl.types import InputPhoneContact
from telethon.tl.functions.contacts import ImportContactsRequest
from telethon import functions, types

# Create Client Object
api_id = xxxxxxx
api_hash = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
phone = '+xxxxxxxxxxxx'

# Login
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: '))

# add user to contact
phoneNum= "+98xxxxxxxxxx"
contact = InputPhoneContact(client_id=0, phone=phoneNum, first_name="", last_name="")
result = client(ImportContactsRequest([contact]))

To create a new contact you need to pass 0 for the client_id.

Jetliner answered 1/8, 2020 at 22:5 Comment(0)
C
2
contact = InputPhoneContact(client_id=0, phone='+918962141530', first_name='<First Name its required field>', last_name='<Last Name its optional field>')

client.invoke(ImportContactsRequest[contact],replace=True ))
*** TypeError: __init__() got an unexpected keyword argument 'replace'

You can use

result = client.invoke(ImportContactsRequest([contact]))

After add contact in list you can show all user list

contacts = client(GetContactsRequest(0))

Iterate contacts and show all users info

Caught answered 27/11, 2017 at 15:9 Comment(1)
Hey do you happen to know how to"iterate over contacts ? I get TypeError: 'Contacts' object is not iterable when I try to iterate over it using loops.Knightly
S
1

Here's how you'd do it using daniil.it/MadelineProto:

try {
     $MadelineProto = \danog\MadelineProto\Serialization::unserialize('session.madeline'); // Unserialize a stored session, if you haven't saved it yet, login first, see below
} catch (\danog\MadelineProto\Exception $e) { // If 
    $MadelineProto = new \danog\MadelineProto\API();
    // Login as a user
    $sentCode = $MadelineProto->phone_login($number);
    echo 'Enter the code you received: ';
    $code = '';
    for ($x = 0; $x < $sentCode['type']['length']; $x++) {
        $code .= fgetc(STDIN);
    }
    $MadelineProto->complete_phone_login($code);
}
$inputContacts = [];
$inputContacts[0] = ['_' => 'inputPhoneContact', 'client_id' => 0, 'phone' => '+172737', 'first_name' => 'first', 'last_name' => 'last', ];
$inputContacts[1] = ['_' => 'inputPhoneContact', 'client_id' => 0, 'phone' => '+172737', 'first_name' => 'first', 'last_name' => 'last', ];
// You can add maximum 4000000000 contacts

 $contacts_ImportedContacts = $MadelineProto->contacts->importContacts(['contacts' => $InputContacts, 'replace' => false, ]);

 $MadelineProto->serialize('session.madeline');
Shipman answered 17/6, 2017 at 9:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.