WTelegramClient 0.9.2

There is a newer version of this package available.
See the version list below for details.
dotnet add package WTelegramClient --version 0.9.2
                    
NuGet\Install-Package WTelegramClient -Version 0.9.2
                    
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="0.9.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WTelegramClient" Version="0.9.2" />
                    
Directory.Packages.props
<PackageReference Include="WTelegramClient" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add WTelegramClient --version 0.9.2
                    
#r "nuget: WTelegramClient, 0.9.2"
                    
#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.
#addin nuget:?package=WTelegramClient&version=0.9.2
                    
Install WTelegramClient as a Cake Addin
#tool nuget:?package=WTelegramClient&version=0.9.2
                    
Install WTelegramClient as a Cake Tool

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 Task Main(string[] _)
{
    using var client = new WTelegram.Client();
    await client.ConnectAsync();
    var user = await client.UserAuthIfNeeded();
    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 the previous 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)
{
    if (what == "api_id") return "YOUR_API_ID";
    if (what == "api_hash") return "YOUR_API_HASH";
    if (what == "phone_number") return "+12025550156";
    if (what == "verification_code") { Console.Write("Code: "); return Console.ReadLine(); }
    if (what == "first_name") return "John";   // if sign-up is required
    if (what == "last_name") return "Doe";     // if sign-up is required
    if (what == "password") return "secret!";  // if user has enabled 2FA
    return null;
}
...
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.

The configuration items shown above are the only ones that have no default values and are required to be provided by your method.

The constructor also takes another optional delegate parameter that will be called for any other Update or other information/status/service messages that Telegram sends unsollicited, independently of your API requests.

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 chatsBase = await client.Messages_GetAllChats(null);
if (chatsBase is not Messages_Chats { chats: var chats }) throw new Exception("hu?");
Console.WriteLine("This user has joined the following:");
foreach (var chat in chats)
    switch (chat)
    {
        case Chat smallgroup when (smallgroup.flags & Chat.Flags.deactivated) == 0:
            Console.WriteLine($"{smallgroup.id}:  Small group: {smallgroup.title} with {smallgroup.participants_count} members");
            break;
        case Channel channel when (channel.flags & Channel.Flags.broadcast) != 0:
            Console.WriteLine($"{channel.id}: Channel {channel.username}: {channel.title}");
            break;
        case Channel group:
            Console.WriteLine($"{group.id}: Group {group.username}: {group.title}");
            break;
    }
Console.Write("Type a chat ID to send a message: ");
var id = int.Parse(Console.ReadLine());
var target = chats.First(chat => chat.ID == id);
await client.SendMessageAsync(target, "Hello, World");

Other things to know

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

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

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).

I've added several useful converters or implicit cast 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 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+)

Development status

The library is usable for most scenarios including (sequential or parallel) automated steps based on API requests/responses, or real-time monitoring of incoming Updates/messages. Secret chats have not been tested yet.

Developers feedback are welcome in the Telegram channel @WTelegramClient

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.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (8)

Showing the top 5 NuGet packages that depend on WTelegramClient:

Package Downloads
WTelegramBot

Telegram Bot API (local server) library providing more extended features Release Notes: - Fix NullRef on GetChat - Fix Postgres & ChatType cast issue

WTelegramClient.Extensions.Updates

Extensions over WtelegramClient For Dealing With Updates

MTProto

A MTProto client library based on wiz0u/WTelegramClient

ProfitSniper.Profitview

Package Description

WTC.Abstractions.Types

Telegram Type Abstractions For WTelegramClient

GitHub repositories (2)

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

Repository Stars
yiyungent/KnifeHub
🧰 简单易用的效率工具平台
DamianMorozov/OpenTgResearcher
Open Telegram Researcher
Version Downloads Last updated
4.3.5-dev.1 154 5/1/2025
4.3.4 1,847 4/25/2025
4.3.4-dev.2 141 4/20/2025
4.3.4-dev.1 195 4/6/2025
4.3.3 1,887 4/3/2025
4.3.3-dev.4 151 4/2/2025
4.3.3-dev.3 127 3/27/2025
4.3.3-dev.2 148 3/23/2025
4.3.3-dev.1 195 3/12/2025
4.3.2 3,190 3/8/2025
4.3.2-dev.2 170 3/7/2025
4.3.2-dev.1 83 3/3/2025
4.3.1 5,924 2/13/2025
4.2.9-dev.4 981 1/31/2025
4.2.9-dev.3 73 1/30/2025
4.2.9-dev.2 57 1/28/2025
4.2.9-dev.1 78 1/23/2025
4.2.8 3,261 1/22/2025
4.2.7 3,955 1/2/2025
4.2.6 216 1/1/2025
4.2.6-dev.1 65 1/1/2025
4.2.5 7,309 12/4/2024
4.2.5-dev.1 60 12/4/2024
4.2.4 2,028 11/22/2024
4.2.4-dev.4 57 11/22/2024
4.2.4-dev.3 66 11/21/2024
4.2.4-dev.2 60 11/20/2024
4.2.4-dev.1 72 11/18/2024
4.2.3 1,545 11/18/2024
4.2.3-dev.4 50 11/18/2024
4.2.3-dev.3 71 11/14/2024
4.2.3-dev.2 126 11/2/2024
4.2.3-dev.1 66 11/1/2024
4.2.2 5,000 10/31/2024
4.2.2-dev.7 96 10/29/2024
4.2.2-dev.5 144 10/24/2024
4.2.2-dev.4 51 10/24/2024
4.2.2-dev.3 140 10/17/2024
4.2.2-dev.2 70 10/15/2024
4.2.2-dev.1 54 10/15/2024
4.2.1 8,389 10/7/2024
4.1.11-dev.4 59 10/7/2024
4.1.11-dev.3 95 9/30/2024
4.1.11-dev.2 115 9/22/2024
4.1.11-dev.1 76 9/21/2024
4.1.10 5,263 9/19/2024
4.1.10-dev.3 142 9/8/2024
4.1.10-dev.2 77 9/7/2024
4.1.10-dev.1 61 9/7/2024
4.1.9 3,179 9/7/2024
4.1.9-dev.2 72 9/6/2024
4.1.9-dev.1 88 9/5/2024
4.1.8 5,322 8/14/2024
4.1.7 1,271 8/10/2024
4.1.7-dev.1 71 8/10/2024
4.1.6 1,326 7/31/2024
4.1.6-dev.1 58 7/29/2024
4.1.5 2,358 7/20/2024
4.1.5-dev.2 81 7/20/2024
4.1.5-dev.1 225 7/12/2024
4.1.4 3,348 7/7/2024
4.1.3 371 7/6/2024
4.1.2 1,162 7/2/2024
4.1.2-dev.8 80 7/1/2024
4.1.2-dev.7 189 6/15/2024
4.1.2-dev.6 66 6/15/2024
4.1.2-dev.5 165 6/4/2024
4.1.2-dev.4 106 5/28/2024
4.1.2-dev.3 69 5/27/2024
4.1.2-dev.2 208 5/7/2024
4.1.2-dev.1 100 5/1/2024
4.1.1 24,632 4/28/2024
4.1.1-dev.5 73 4/28/2024
4.1.1-dev.2 76 4/27/2024
4.1.1-dev.1 71 4/27/2024
4.0.1 2,002 4/24/2024
4.0.1-dev.6 96 4/22/2024
4.0.1-dev.5 83 4/18/2024
4.0.1-dev.4 71 4/17/2024
4.0.1-dev.3 85 4/16/2024
4.0.1-dev.2 84 4/14/2024
4.0.1-dev.1 87 4/13/2024
4.0.0 2,986 4/5/2024
4.0.0-dev.8 79 4/4/2024
4.0.0-dev.7 69 4/3/2024
4.0.0-dev.6 105 3/30/2024
4.0.0-dev.5 79 3/29/2024
4.0.0-dev.4 86 3/28/2024
4.0.0-dev.2 94 3/26/2024
4.0.0-dev.1 103 3/26/2024
3.7.2 2,789 3/24/2024
3.7.2-dev.3 86 3/23/2024
3.7.2-dev.2 96 3/19/2024
3.7.2-dev.1 95 3/13/2024
3.7.1 4,207 3/8/2024
3.6.7-dev.13 65 3/8/2024
3.6.7-dev.12 74 3/8/2024
3.6.7-dev.11 72 3/8/2024
3.6.7-dev.9 78 3/8/2024
3.6.7-dev.8 98 3/4/2024
3.6.7-dev.7 140 2/25/2024
3.6.7-dev.6 110 2/21/2024
3.6.7-dev.5 92 2/19/2024
3.6.7-dev.4 94 2/18/2024
3.6.7-dev.3 88 2/18/2024
3.6.7-dev.1 70 2/18/2024
3.6.6 3,367 2/1/2024
3.6.5 3,247 1/18/2024
3.6.4 650 1/16/2024
3.6.4-dev.2 83 1/16/2024
3.6.4-dev.1 74 1/15/2024
3.6.3 5,189 12/31/2023
3.6.3-dev.1 91 12/31/2023
3.6.2 1,653 12/23/2023
3.6.2-dev.2 88 12/23/2023
3.6.2-dev.1 120 12/17/2023
3.6.1 3,295 11/30/2023
3.6.1-dev.7 77 11/30/2023
3.6.1-dev.6 88 11/29/2023
3.6.1-dev.2 105 11/25/2023
3.6.1-dev.1 168 11/17/2023
3.5.10-dev.1 120 11/11/2023
3.5.9 3,311 11/6/2023
3.5.8 1,647 10/28/2023
3.5.8-dev.5 90 10/28/2023
3.5.8-dev.4 143 10/24/2023
3.5.8-dev.3 94 10/24/2023
3.5.8-dev.2 125 10/19/2023
3.5.8-dev.1 125 10/9/2023
3.5.7 2,531 10/4/2023
3.5.6 1,532 9/22/2023
3.5.5 1,115 9/18/2023
3.5.5-dev.1 83 9/18/2023
3.5.4 3,248 9/6/2023
3.5.4-dev.2 97 9/6/2023
3.5.3 9,014 7/21/2023
3.5.2-dev.21 101 7/21/2023
3.5.2-dev.20 197 7/7/2023
3.5.2-dev.19 119 7/6/2023
3.5.2-dev.16 103 7/5/2023
3.5.2-dev.15 156 6/27/2023
3.5.2-dev.3 959 5/18/2023
3.5.2-dev.1 108 5/17/2023
3.5.1 37,178 5/17/2023
3.5.1-dev.4 110 5/17/2023
3.5.1-dev.3 502 5/9/2023
3.5.1-dev.2 171 5/5/2023
3.5.1-dev.1 125 5/2/2023
3.4.3-dev.4 115 5/1/2023
3.4.3-dev.3 128 4/29/2023
3.4.3-dev.2 212 4/25/2023
3.4.3-dev.1 137 4/25/2023
3.4.2 4,788 4/24/2023
3.4.2-dev.2 109 4/24/2023
3.4.2-dev.1 124 4/23/2023
3.4.1 1,114 4/21/2023
3.4.1-dev.5 105 4/21/2023
3.4.1-dev.4 198 4/9/2023
3.4.1-dev.2 122 4/8/2023
3.4.1-dev.1 135 4/2/2023
3.3.4-dev.1 125 4/1/2023
3.3.3 2,392 3/26/2023
3.3.3-dev.2 133 3/26/2023
3.3.3-dev.1 180 3/16/2023
3.3.2 2,056 3/9/2023
3.3.2-dev.2 114 3/9/2023
3.3.2-dev.1 119 3/9/2023
3.3.1 1,009 3/8/2023
3.3.1-dev.1 120 3/8/2023
3.2.3-dev.5 221 2/26/2023
3.2.3-dev.4 168 2/17/2023
3.2.3-dev.3 121 2/15/2023
3.2.3-dev.2 125 2/14/2023
3.2.3-dev.1 125 2/13/2023
3.2.2 9,650 2/6/2023
3.2.2-dev.7 135 2/5/2023
3.2.2-dev.6 151 2/4/2023
3.2.2-dev.5 155 1/26/2023
3.2.2-dev.4 202 1/12/2023
3.2.2-dev.3 155 1/9/2023
3.2.2-dev.2 147 1/7/2023
3.2.2-dev.1 153 1/6/2023
3.2.1 3,692 12/29/2022
3.2.1-dev.2 140 12/29/2022
3.2.1-dev.1 139 12/29/2022
3.1.6-dev.2 164 12/19/2022
3.1.6-dev.1 138 12/15/2022
3.1.5 2,458 12/7/2022
3.1.4-dev.6 126 12/7/2022
3.1.4-dev.5 141 12/5/2022
3.1.4-dev.4 143 12/2/2022
3.1.4-dev.3 154 11/26/2022
3.1.4-dev.2 131 11/26/2022
3.1.4-dev.1 132 11/26/2022
3.1.3 2,144 11/23/2022
3.1.3-dev.5 128 11/23/2022
3.1.3-dev.3 140 11/20/2022
3.1.2 1,885 11/12/2022
3.0.3 1,931 11/1/2022
3.0.2 2,140 10/26/2022
3.0.1 1,334 10/21/2022
3.0.0 3,413 10/8/2022
2.6.4 4,494 9/14/2022
2.6.3 1,868 9/2/2022
2.6.2 4,332 8/6/2022
2.5.2 4,017 7/1/2022
2.5.1 19,197 6/15/2022
2.4.1 24,751 5/20/2022
2.3.3 1,732 5/14/2022
2.3.2 2,029 5/7/2022
2.3.1 3,890 4/13/2022
2.2.1 4,892 3/28/2022
2.1.4 1,368 3/23/2022
2.1.3 1,456 3/18/2022
2.1.2 31,413 2/27/2022
2.1.1 3,051 2/13/2022
2.0.3 1,743 2/7/2022
2.0.2 1,230 2/4/2022
2.0.1 1,637 1/30/2022
2.0.0 1,674 1/24/2022
1.9.4 1,436 1/19/2022
1.9.3 1,246 1/17/2022
1.9.2 3,374 1/11/2022
1.9.1 1,255 1/3/2022
1.8.3 1,064 12/30/2021
1.8.2 1,743 12/25/2021
1.8.1 1,153 12/21/2021
1.7.6 1,354 12/12/2021
1.7.5 1,312 12/6/2021
1.7.4 1,607 12/1/2021
1.7.3 2,214 11/27/2021
1.7.2 1,618 11/20/2021
1.7.1 1,059 11/10/2021
1.6.4 1,106 11/6/2021
1.6.3 1,015 11/3/2021
1.6.2 1,016 10/31/2021
1.6.1 1,045 10/31/2021
1.5.1 1,060 10/25/2021
1.4.1 1,009 10/20/2021
1.3.1 994 10/19/2021
1.3.0 1,086 10/17/2021
1.0.2 1,046 10/15/2021
1.0.1 972 10/11/2021
1.0.0 1,148 9/30/2021
0.9.5 1,086 9/1/2021
0.9.4 989 8/29/2021
0.9.3 986 8/24/2021
0.9.2 981 8/19/2021
0.9.1 1,033 8/16/2021
0.8.1 1,052 8/13/2021
0.7.4 1,028 8/10/2021
0.7.3 976 8/9/2021
0.7.1 1,133 8/8/2021