skip to Main Content

I Have a problem in using Telegram APIs in C#.
I want to create a contact in telegram with a specific phone number so i can send messages directly to a phone number that was not in my contacts.

How can i create a contact with specific phone number in telegram with c#?

I tried to use TLSharp to do this but i did not found any method.

4

Answers


  1. There is nothing for it in Telegram Bot API: https://core.telegram.org/bots/api

    Login or Signup to reply.
  2. TLSharp now has method ImportContactsAsync, you can use it like this:

    var list = new List<TLInputPhoneContact> 
                {
                    new TLInputPhoneContact() { Phone = phoneNumberTo, FirstName = defaultName, LastName = defaultLastname, ClientId = 0}
                };
    
                await client.ImportContactsAsync(list);
    

    Wasted some hours trying to find how to do it in the internet, but hadn’t found, hope it will help someone with the same problem.

    Login or Signup to reply.
  3. There is now WTelegramClient library, using the latest Telegram Client API protocol (connecting as a user, not bot).

    The library is very complete but also very simple to use. Follow the README on GitHub for an easy introduction.

    To create a contact by phone number and send a message to it, the whole Program.cs would be as simple as:

    using TL;
    
    using var client = new WTelegram.Client(); // or Client(Environment.GetEnvironmentVariable)
    await client.LoginUserIfNeeded();
    var contact = await client.Contacts_ImportContacts(new[] { new InputPhoneContact { phone = "+PHONENUMBER" } });
    await client.SendMessageAsync(contact.users[0], "Hello");
    
    Login or Signup to reply.
  4.     class Program
        {
            static string Config(string what)
            {
                switch (what)
                {
                    case "api_id": return "...";
                    case "api_hash": return "...";
                    case "phone_number": return "...";
                    case "verification_code": Console.Write("Code: "); return Console.ReadLine();
                    case "first_name": return "John";      // if sign-up is required
                    case "last_name": return "Doe";        // if sign-up is required
                    case "password": return "...";     // if user has enabled 2FA
                    default: return null;                  // let WTelegramClient decide the default config
                }
            }
            static async Task Main(string[] args)
            {
                Console.WriteLine("Hello This is test app!");
                using var client = new WTelegram.Client(Config); // or Client(Environment.GetEnvironmentVariable)
                await client.LoginUserIfNeeded();
                for (int i = 0; i < 100; i++)
                {
                    try
                    {
                        var contact = await client.Contacts_ImportContacts(new[] { new InputPhoneContact { phone = "+...", first_name = "Iran", last_name = "Tester" } });
                        object p = await client.SendMessageAsync(contact.users[contact.users.Keys.First()], "Hello");
                    }
                    catch (Exception)
                    {
    
                        throw;
                    }
                    finally
                    {
    
                    }
                }
    
            }
        }
    

    Right Code that works actually

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search