2

An application was written using the WTelegramClient library. There was a task to use the proxy server settings to connect to the Internet: Address, Port, Username, Password.

Now the connection is made without a proxy. We declare variables:

using TL;

private WTelegram.Client _client;
private User _user;
private TelegramSettings telegramSettings = null;

We fill it with values:

private void saveSettingButton_Click(object sender, EventArgs e)
{
    if (telegramSettings == null) telegramSettings = new TelegramSettings();
    telegramSettings.UseProxy = checkEditUseProxy.Checked;
    telegramSettings.ProxyHost = textEditProxyHost.Text.Trim();
    telegramSettings.ProxyPort = textEditProxyPort.Text.Trim() == "" ? 0 : Convert.ToInt32(textEditProxyPort.Text.Trim());
    telegramSettings.ProxyUserName = textEditProxyUserName.Text.Trim();
    telegramSettings.ProxyPassword = textEditProxyPassword.Text.Trim();
    telegramSettings.PhoneNumber = (string)textBoxPhone.EditValue;
    telegramSettings.API_Hash = textBoxApiHash.Text.Trim();
    telegramSettings.API_ID = textBoxApiID.Text.Trim();
}

Making changes to the Сonfig:

string Config(string what)
{
    switch (what)
    {
        case "api_id": return telegramSettings.API_ID;
        case "api_hash": return telegramSettings.API_Hash;
        case "phone_number": return telegramSettings.PhoneNumber;
        case "verification_code":
        case "password":        // if user has enabled 2FA
            BeginInvoke(new Action(() => CodeNeeded(what.Replace('_', ' '))));
            _codeReady.Reset();
            _codeReady.Wait();
            return textBoxCode.Text;
        case "first_name":
        case "last_name":
            return null;        // if sign-up is required
        default: return null;
    };
}

Connecting

_client = new WTelegram.Client(Config);
_user = await _client.LoginUserIfNeeded();

How to apply proxy settings?

ShuherVit
  • 31
  • 3

1 Answers1

2

Using a proxy with WTelegramClient is described in the Examples documentation:

Set TcpHandler before login:

    client.TcpHandler = async (address, port) =>
    {
        var proxy = new Socks5ProxyClient(ProxyHost, ProxyPort, ProxyUsername, ProxyPassword);
        return proxy.CreateConnection(address, port);
    };
Wizou
  • 1,336
  • 13
  • 24