在api telegram python telethon

时间:2017-06-16 20:21:13

标签: python api telegram telethon

如何在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'

我需要在哪个模块中添加联系人?

4 个答案:

答案 0 :(得分:7)

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

# Removing this option results in an evaluation nested too deeply error
options( expressions = 5e5 )

library(sparklyr)
library(dplyr)
sc <- spark_connect(master = "local", spark_home = spark_home_dir())

n = 500

l = as.list(1:n)

for (i in 1:n) {
  l[[i]] <- copy_to(
    dest = sc, 
    iris, 
    name = paste0("iris", i), 
    overwrite = TRUE, 
    cache = TRUE)
}

dt = l %>% purrr::reduce(dplyr::union_all)
dt

要创建新联系人,您需要为client_id传递0。

答案 1 :(得分:1)

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))

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

答案 2 :(得分: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');

答案 3 :(得分:0)

您可以这样创建新联系人:

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]))

要创建新联系人,您需要为client_id传递0。