sochix / TLSharp

Telegram client library implemented in C#
1k stars 380 forks source link

How I can add a contact by user id, phone number is hidden #946

Closed speekflayder closed 4 years ago

speekflayder commented 4 years ago

Hello, I Have a code to add a contact by name, surname and phone. But when I try to add a contact by name, surname and user id, it doesn't work. The contact has a hidden phone number. I have only id.. So, how I can add a contact by user id?

P.S. User id Has been got from TLMessage.FromId

var new_contacts = new TLInputPhoneContact()
{
      FirstName = "123",
      LastName = "456",
      ClientId = int 'User id'
};
var vectorInputPhoneContacts = new TLVector<TLInputPhoneContact>();
vectorInputPhoneContacts.Add(new_contacts);
var req = new TLRequestImportContacts
{
       Contacts = vectorInputPhoneContact
};
var importedContacts = await client.SendRequestAsync<TLImportedContacts>(req);
speekflayder commented 4 years ago

I have solved that. Firstable you have to get TLUser from your dialogs. And then just enter user.phone in contact request. It will work even if contact has a hidden phone number

TLDialogs dialogs = await client.GetUserDialogsAsync() as TLDialogs;
foreach (TLDialog dia in dialogs.Dialogs)
{
           TLPeerUser peer = (TLPeerUser)dia.Peer;
           TLUser user = dialogs.Users
                       .Where(x => x.GetType() == typeof(TLUser))
                       .Cast<TLUser>()
                       .FirstOrDefault(x => x.Id == peer.UserId);
           if (user.Id == "YOUR USER.ID") user = "YOUR USER";
}