2017-06-16 423 views
3

如何在telethon python的联系人中保存号码?在api电报中添加新联系人python telethon

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' 

我需要什么模块添加联系人?

回答

4

您可以创建这样一个联系人:

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

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

要创建你需要传递0的CLIENT_ID一个新的联系人。

+0

请回答我的问题https://stackoverflow.com/questions/44605436/how-i-can-restore-sessions-in-telethon-telegram – netdevil

0

这里是你如何做到这一点使用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'); 
0
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' 

您可以使用

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

在列表中添加联系人后,您可以显示所有用户列表

contacts = client(GetContactsRequest(0)) 

迭代联系人并显示所有用户信息

相关问题