Skip to content
This repository was archived by the owner on Mar 10, 2018. It is now read-only.

Quick Start

Viktor Borisov edited this page Feb 17, 2018 · 9 revisions

The first step is getting a instance of the ITelegramClient interface:

 var client = ClientFactory.BuildClient(new FactorySettings { Id = ApiId, Hash = ApiHash, ServerAddress = _ServerAddress, ServerPort = _ServerPort, StoreProvider = new TelegramClient.Core.Sessions.FileSessionStoreProvider("session")});

Parameters ApiId, ApiHash, ServerAddress, ServerPort you can get on the official page Telegram

The next step is to set up a connection to the Telegram server

await client.ConnectService.ConnectAsync();

The third step is authentication

Ask Telegram send us a phone authentication code

var hash= await client.AuthService.SendCodeRequestAsync(NumberToAuthenticate);

We write the code from the SMS into the variable code

Sending to Telegram authenticate request

var user = await client.AuthService.MakeAuthAsync(NumberToAuthenticate, hash, code);

If user have two factor authentification, then code must be

TUser user;
try
{
    user = await client.AuthService.MakeAuthAsync(NumberToAuthenticate, hash, code);
}
catch (CloudPasswordNeededException)
{
   var password = (TPassword) await client.AuthService.GetPasswordSetting();
   var passwordStr = PasswordToAuthenticate;

   user = await client.AuthService.MakeAuthWithPasswordAsync(password, passwordStr);
}

The Telegram client saves the session in the bin folder of your application after successful authentication. The default name is session.dat. Authentication is performed once, the next time a session file is used.

Check passed authentication through method

var isAuthorized = client.AuthService.IsUserAuthorized();

To receive updates, you must subscribe to the event

client.Updates.RecieveUpdates += update =>
{
    // handle updates
};

To send message you must call the method

await client.MessagesService.SendMessageAsync(new TInputPeerUser{ UserId = userId}, messsage);

Clone this wiki locally