2017-06-16 49 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)) 

迭代聯繫人並顯示所有用戶信息

相關問題