WTelegramClient 1.6.1

.NET 5.0 .NET Standard 2.0
There is a newer version of this package available.
See the version list below for details.
dotnet add package WTelegramClient --version 1.6.1
NuGet\Install-Package WTelegramClient -Version 1.6.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WTelegramClient" Version="1.6.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add WTelegramClient --version 1.6.1
#r "nuget: WTelegramClient, 1.6.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install WTelegramClient as a Cake Addin
#addin nuget:?package=WTelegramClient&version=1.6.1

// Install WTelegramClient as a Cake Tool
#tool nuget:?package=WTelegramClient&version=1.6.1

Telegram Client API library written 100% in C# and .NET Standard

How to use

⚠️ This library relies on asynchronous C# programming (async/await) so make sure you are familiar with this before proceeding.

After installing WTelegramClient through Nuget, your first Console program will be as simple as:

static async Task Main(string[] _)
    using var client = new WTelegram.Client();
    var user = await client.LoginUserIfNeeded();
    Console.WriteLine($"We are logged-in as {user.username ?? user.first_name + " " + user.last_name} (id {user.id})");

When run, this will prompt you interactively for your App api_id and api_hash (that you obtain through Telegram's API development tools page) and try to connect to Telegram servers.

Then it will attempt to sign-in as a user for which you must enter the phone_number and the verification_code that will be sent to this user (for example through SMS or another Telegram client app the user is connected to).

If the verification succeeds but the phone number is unknown to Telegram, the user might be prompted to sign-up (accepting the Terms of Service) and enter their first_name and last_name.

And that's it, you now have access to the full range of Telegram services, mainly through calls to await client.Some_TL_Method(...)

Saved session

If you run this program again, you will notice that only api_id and api_hash are requested, the other prompts are gone and you are automatically logged-on and ready to go.

This is because WTelegramClient saves (typically in the encrypted file bin\WTelegram.session) its state and the authentication keys that were negociated with Telegram so that you needn't sign-in again every time.

That file path is configurable, and under various circumstances (changing user or server address) you may want to change it or simply delete the existing session file in order to restart the authentification process.

Non-interactive configuration

Your next step will probably be to provide a configuration to the client so that the required elements (in bold above) are not prompted through the Console but answered by your program.

To do this, you need to write a method that will provide the answers, and pass it on the constructor:

static string Config(string what)
    switch (what)
        case "api_id": return "YOUR_API_ID";
        case "api_hash": return "YOUR_API_HASH";
        case "phone_number": return "+12025550156";
        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 "secret!";     // if user has enabled 2FA
        default: return null;                  // let WTelegramClient decide the default config
using var client = new WTelegram.Client(Config);

There are other configuration items that are queried to your method but returning null let WTelegramClient choose a default adequate value. Those shown above are the only ones that have no default values and <u>should be provided</u> by your method. Returning null for verification_code or password will show a prompt for console apps, or an error otherwise.

Another simpler approach is to pass Environment.GetEnvironmentVariable as the config callback and define the configuration items as environment variables. Undefined variables get the default null behavior.

Finally, if you want to redirect the library logs to your logger instead of the Console, you can install a delegate in the WTelegram.Helpers.Log static property. Its int argument is the log severity, compatible with the classic LogLevel enum

Example of API call

ℹ️ The Telegram API makes extensive usage of base and derived classes, so be ready to use the various syntaxes C# offer to check/cast base classes into the more useful derived classes (is, as, case DerivedType )

To find which derived classes are available for a given base class, the fastest is to check our TL.Schema.cs source file as they are listed in groups. Intellisense tooltips on API structures/methods will also display a web link to the adequate Telegram documentation page.

The Telegram API object classes are defined in the TL namespace, and the API functions are available as async methods of Client.

Below is an example of calling the messages.getAllChats API function and enumerating the various groups/channels the user is in, and then using client.SendMessageAsync helper function to easily send a message:

using TL;
var chats = await client.Messages_GetAllChats(null);
Console.WriteLine("This user has joined the following:");
foreach (var (id, chat) in chats.chats)
    switch (chat)
        case Chat smallgroup when (smallgroup.flags & Chat.Flags.deactivated) == 0:
            Console.WriteLine($"{id}:  Small group: {smallgroup.title} with {smallgroup.participants_count} members");
        case Channel channel when (channel.flags & Channel.Flags.broadcast) != 0:
            Console.WriteLine($"{id}: Channel {channel.username}: {channel.title}");
        case Channel group:
            Console.WriteLine($"{id}: Group {group.username}: {group.title}");
Console.Write("Type a chat ID to send a message: ");
long chatId = long.Parse(Console.ReadLine());
var target = chats.chats[chatId];
Console.WriteLine($"Sending a message in chat {chatId}: {target.Title}");
await client.SendMessageAsync(target, "Hello, World");

Terminology in Telegram Client API

In the API, Telegram uses some terms/classnames that can be confusing as they differ from the terms shown to end-users:

  • Channel : A (large) chat group (sometimes called supergroup) or a broadcast channel (the broadcast flag differenciate those)
  • Chat : A private simple chat group with few people (it may be migrated to a supergroup/channel when it doesn't fit anymore)
  • Chats : In plural, it means either Chat or Channel
  • Peer : Either a Chat, Channel or a private chat with a User
  • Dialog : The current status of a chat with a Peer (draft, last message, unread count, pinned...)
  • DC (DataCenter) : There are a few datacenters depending on where in the world the user (or an uploaded media file) is from.
  • Access Hash : For more security, Telegram requires you to provide the specific access_hash for chats, files and other resources before interacting with them (not required for a simple Chat). This is like showing a pass that proves you are entitled to access it. You obtain this hash when you first gain access to a resource and occasionnally later when some events about this resource are happening or if you query the API. You should remember this hash if you want to access that resource later.

Other things to know

The Client class also offers an Update event that is triggered when Telegram servers sends unsollicited Updates or notifications/information/status/service messages, independently of your API requests. See Examples/Program_ListenUpdates.cs

An invalid API request can result in a RpcException being raised, reflecting the error code and status text of the problem.

You can find more code examples in EXAMPLES.md and in the Examples subdirectory.

The other configuration items that you can override include: session_pathname, server_address, device_model, system_version, app_version, system_lang_code, lang_pack, lang_code, user_id

Optional API parameters have a default value of null when unset. Passing null for a required string/array is the same as empty (0-length). Required API parameters/fields can sometimes be set to 0 or null when unused (check API documentation or experiment).

I've added several useful converters, implicit cast or helper properties to various API object so that they are more easy to manipulate.

Beyond the TL async methods, the Client class offers a few other methods to simplify the sending/receiving of files, medias or messages.

This library works best with .NET 5.0+ and is also available for .NET Standard 2.0 (.NET Framework 4.6.1+ & .NET Core 2.0+)

Troubleshooting guide

Here is a list of common issues and how to fix them so that your program work correctly:

  1. Are you using the Nuget package or the library source code? <br/>It is not recommended to copy/compile the source code of the library for a normal usage. <br/>When built in DEBUG mode, the source code connects to Telegram test servers. So you can either:

  2. After ConnectAsync(), are you calling LoginUserIfNeeded()? <br/>If you don't authenticate as a user (or bot), you have access to a very limited subset of Telegram APIs

  3. Did you use await with every Client methods? <br/>This library is completely Task-based and you should learn, understand and use the asynchronous model of C# programming before proceeding further.

  4. Are you keeping a live reference to the Client instance and dispose it only at the end of your program? <br/>If you create the instance in a submethod and don't store it somewhere permanent, it might be destroyed by the garbage collector at some point. So as long as the client must be running, make sure the reference is stored in a (static) field or somewhere appropriate. <br/>Also, as the Client class inherits IDisposable, remember to call client.Dispose() when your program ends (or exit a using scope).

  5. Is your program ending immediately instead of waiting for Updates? <br/>Your program must be running/waiting continuously in order for the background Task to receive and process the Updates. So make sure your main program doesn't end immediately. For a console program, this is typical done by waiting for a key or some close event.

  6. Is every Telegram API call rejected? (typically with an exception message like AUTH_RESTART) <br/>The user authentification might have failed at some point (or the user revoked the authorization). It is therefore necessary to go through the authentification again. This can be done by deleting the WTelegram.session file, or at runtime by calling client.Reset()

Library uses and limitations

This library can be used for any Telegram scenarios including:

  • Sequential or parallel automated steps based on API requests/responses
  • Real-time monitoring of incoming Updates/Messages
  • Download/upload of files/media
  • etc...

Secret chats (end-to-end encryption, PFS) and connection to CDN DCs have not been tested yet.

Developers feedbacks are welcome in the Telegram channel @WTelegramClient

If you like this library, please consider a donation. ❤ This will help the project keep going.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on WTelegramClient:

Package Downloads

Extensions over WtelegramClient For Dealing With Updates


A MTProto client library based on wiz0u/WTelegramClient

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on WTelegramClient:

Repository Stars
🧰 简单易用的效率工具平台
Version Downloads Last updated
3.5.2-dev.3 109 5/18/2023
3.5.2-dev.1 35 5/17/2023
3.5.1 880 5/17/2023
3.5.1-dev.4 29 5/17/2023
3.5.1-dev.3 295 5/9/2023
3.5.1-dev.2 100 5/5/2023
3.5.1-dev.1 61 5/2/2023
3.4.3-dev.4 42 5/1/2023
3.4.3-dev.3 59 4/29/2023
3.4.3-dev.2 94 4/25/2023
3.4.3-dev.1 59 4/25/2023
3.4.2 2,257 4/24/2023
3.4.2-dev.2 41 4/24/2023
3.4.2-dev.1 54 4/23/2023
3.4.1 333 4/21/2023
3.4.1-dev.5 42 4/21/2023
3.4.1-dev.4 121 4/9/2023
3.4.1-dev.2 48 4/8/2023
3.4.1-dev.1 60 4/2/2023
3.3.4-dev.1 48 4/1/2023
3.3.3 1,438 3/26/2023
3.3.3-dev.2 56 3/26/2023
3.3.3-dev.1 112 3/16/2023
3.3.2 1,086 3/9/2023
3.3.2-dev.2 52 3/9/2023
3.3.2-dev.1 53 3/9/2023
3.3.1 245 3/8/2023
3.3.1-dev.1 55 3/8/2023
3.2.3-dev.5 134 2/26/2023
3.2.3-dev.4 99 2/17/2023
3.2.3-dev.3 56 2/15/2023
3.2.3-dev.2 56 2/14/2023
3.2.3-dev.1 55 2/13/2023
3.2.2 1,872 2/6/2023
3.2.2-dev.7 58 2/5/2023
3.2.2-dev.6 61 2/4/2023
3.2.2-dev.5 84 1/26/2023
3.2.2-dev.4 123 1/12/2023
3.2.2-dev.3 76 1/9/2023
3.2.2-dev.2 65 1/7/2023
3.2.2-dev.1 65 1/6/2023
3.2.1 2,231 12/29/2022
3.2.1-dev.2 64 12/29/2022
3.2.1-dev.1 66 12/29/2022
3.1.6-dev.2 98 12/19/2022
3.1.6-dev.1 73 12/15/2022
3.1.5 1,593 12/7/2022
3.1.4-dev.6 66 12/7/2022
3.1.4-dev.5 71 12/5/2022
3.1.4-dev.4 81 12/2/2022
3.1.4-dev.3 87 11/26/2022
3.1.4-dev.2 65 11/26/2022
3.1.4-dev.1 69 11/26/2022
3.1.3 1,263 11/23/2022
3.1.3-dev.5 68 11/23/2022
3.1.3-dev.3 76 11/20/2022
3.1.2 990 11/12/2022
3.0.3 1,131 11/1/2022
3.0.2 625 10/26/2022
3.0.1 641 10/21/2022
3.0.0 2,154 10/8/2022
2.6.4 3,500 9/14/2022
2.6.3 1,134 9/2/2022
2.6.2 3,398 8/6/2022
2.5.2 3,188 7/1/2022
2.5.1 7,962 6/15/2022
2.4.1 11,182 5/20/2022
2.3.3 998 5/14/2022
2.3.2 1,335 5/7/2022
2.3.1 3,062 4/13/2022
2.2.1 3,415 3/28/2022
2.1.4 669 3/23/2022
2.1.3 691 3/18/2022
2.1.2 29,727 2/27/2022
2.1.1 2,287 2/13/2022
2.0.3 995 2/7/2022
2.0.2 531 2/4/2022
2.0.1 900 1/30/2022
2.0.0 931 1/24/2022
1.9.4 728 1/19/2022
1.9.3 559 1/17/2022
1.9.2 2,502 1/11/2022
1.9.1 555 1/3/2022
1.8.3 356 12/30/2021
1.8.2 1,047 12/25/2021
1.8.1 428 12/21/2021
1.7.6 611 12/12/2021
1.7.5 611 12/6/2021
1.7.4 943 12/1/2021
1.7.3 1,513 11/27/2021
1.7.2 914 11/20/2021
1.7.1 381 11/10/2021
1.6.4 445 11/6/2021
1.6.3 342 11/3/2021
1.6.2 358 10/31/2021
1.6.1 349 10/31/2021
1.5.1 408 10/25/2021
1.4.1 376 10/20/2021
1.3.1 338 10/19/2021
1.3.0 358 10/17/2021
1.0.2 394 10/15/2021
1.0.1 331 10/11/2021
1.0.0 398 9/30/2021
0.9.5 393 9/1/2021
0.9.4 315 8/29/2021
0.9.3 326 8/24/2021
0.9.2 292 8/19/2021
0.9.1 317 8/16/2021
0.8.1 330 8/13/2021
0.7.4 322 8/10/2021
0.7.3 304 8/9/2021
0.7.1 379 8/8/2021