skip to Main Content

I am using TLSharp library for implementing a custom Telegram client. when I run the code below:

public async Task<string> SendCodeRequest(string phoneNumber)
    {
        var completed = false;

        TL.AuthSendCodeRequest request = null;

        while (!completed)
        {
            request = new TL.AuthSendCodeRequest(phoneNumber, 5, _apiId, _apiHash, "en");
            await _sender.Send(request);
            await _sender.Receive(request);

            completed = true;
        }

        // TODO handle other types (such as SMS)
        if (request.Result is TL.AuthSentCodeType)
        {
            var result = (TL.AuthSentCodeType)request.Result;
            return result.PhoneCodeHash;
        }
        else
        {
            var result = (TL.AuthSentAppCodeType)request.Result;
            return result.PhoneCodeHash;
        }

    }

I gives me the following exception :

Your phone number registered to {dcIdx} dc. Please update settings.
See https://github.com/sochix/TLSharp#i-get-an-error-migrate_x for
details.

The mentioned github page says that TLSharp Handles these exceptions by itself. So I guess something is wrong with the library core because the code should resolve data center IPs by itself not generating an exception.

Any help would be appreciated.

2

Answers


  1. TlSharp Currently doesn’t handle this exception you have to catch the exception and get the data center number then Try to reconnect to the Data Center using the ReconnectToDc() Function.

    In the MtProtoSender.cs file you can find the following line of code that generates the exception:

    throw new InvalidOperationException($"Your phone number registered to {dcIdx} dc. Please update settings. See https://github.com/sochix/TLSharp#i-get-an-error-migrate_x for details.");
    

    Replace it with the following code so that the Exception that is generated has the required Data Center number for connecting to it.

    InvalidOperationException exception = new InvalidOperationException($"Your phone number registered to {dcIdx} dc. Please update settings. See https://github.com/sochix/TLSharp#i-get-an-error-migrate_x for details.");
    exception.Data.Add("dcId", dcIdx);
    throw exception;
    

    Change your code like this:

    while (!completed)
    {
        request = new TL.AuthSendCodeRequest(phoneNumber, 5, _apiId, _apiHash, "en");
        try
        {
            await _sender.Send(request);
            await _sender.Receive(request);
    
            completed = true;
        }
        catch (InvalidOperationException ex)
        {
            if (ex.Message.StartsWith("Your phone number registered to") && ex.Data["dcId"] != null)
            {
                await ReconnectToDc((int)ex.Data["dcId"]);
            }
            else
            {
                throw;
            }
        }
    }
    

    In the Code above the Data Center number that was attached to the Exception is used for reconnecting to the Data center.

    Login or Signup to reply.
  2. Probably your phone number is not in the format accepted by Telegram.

    Phone number must start with plus sign, use country code and phone number without gap, for example: +989333333333

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