WTelegramClient 0.9.1

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

WTelegramClient

Telegram client 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 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. 
.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: Restore TB nuget dependency

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.4-dev.2 54 4/20/2025
4.3.4-dev.1 193 4/6/2025
4.3.3 1,381 4/3/2025
4.3.3-dev.4 151 4/2/2025
4.3.3-dev.3 124 3/27/2025
4.3.3-dev.2 145 3/23/2025
4.3.3-dev.1 188 3/12/2025
4.3.2 2,577 3/8/2025
4.3.2-dev.2 169 3/7/2025
4.3.2-dev.1 83 3/3/2025
4.3.1 4,530 2/13/2025
4.2.9-dev.4 888 1/31/2025
4.2.9-dev.3 72 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,163 1/22/2025
4.2.7 3,878 1/2/2025
4.2.6 208 1/1/2025
4.2.6-dev.1 65 1/1/2025
4.2.5 6,841 12/4/2024
4.2.5-dev.1 58 12/4/2024
4.2.4 1,995 11/22/2024
4.2.4-dev.4 56 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,488 11/18/2024
4.2.3-dev.4 48 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 4,933 10/31/2024
4.2.2-dev.7 95 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 139 10/17/2024
4.2.2-dev.2 69 10/15/2024
4.2.2-dev.1 51 10/15/2024
4.2.1 7,881 10/7/2024
4.1.11-dev.4 58 10/7/2024
4.1.11-dev.3 94 9/30/2024
4.1.11-dev.2 113 9/22/2024
4.1.11-dev.1 75 9/21/2024
4.1.10 5,081 9/19/2024
4.1.10-dev.3 141 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,125 9/7/2024
4.1.9-dev.2 72 9/6/2024
4.1.9-dev.1 86 9/5/2024
4.1.8 5,179 8/14/2024
4.1.7 1,226 8/10/2024
4.1.7-dev.1 71 8/10/2024
4.1.6 1,303 7/31/2024
4.1.6-dev.1 58 7/29/2024
4.1.5 2,317 7/20/2024
4.1.5-dev.2 81 7/20/2024
4.1.5-dev.1 224 7/12/2024
4.1.4 3,108 7/7/2024
4.1.3 367 7/6/2024
4.1.2 1,023 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 163 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 207 5/7/2024
4.1.2-dev.1 100 5/1/2024
4.1.1 22,510 4/28/2024
4.1.1-dev.5 72 4/28/2024
4.1.1-dev.2 75 4/27/2024
4.1.1-dev.1 71 4/27/2024
4.0.1 1,997 4/24/2024
4.0.1-dev.6 95 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 84 4/13/2024
4.0.0 2,917 4/5/2024
4.0.0-dev.8 79 4/4/2024
4.0.0-dev.7 68 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 93 3/26/2024
4.0.0-dev.1 102 3/26/2024
3.7.2 2,707 3/24/2024
3.7.2-dev.3 86 3/23/2024
3.7.2-dev.2 95 3/19/2024
3.7.2-dev.1 94 3/13/2024
3.7.1 4,100 3/8/2024
3.6.7-dev.13 65 3/8/2024
3.6.7-dev.12 73 3/8/2024
3.6.7-dev.11 71 3/8/2024
3.6.7-dev.9 78 3/8/2024
3.6.7-dev.8 97 3/4/2024
3.6.7-dev.7 140 2/25/2024
3.6.7-dev.6 109 2/21/2024
3.6.7-dev.5 91 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 69 2/18/2024
3.6.6 3,259 2/1/2024
3.6.5 3,215 1/18/2024
3.6.4 644 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,133 12/31/2023
3.6.3-dev.1 91 12/31/2023
3.6.2 1,619 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,285 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,234 11/6/2023
3.5.8 1,639 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 123 10/19/2023
3.5.8-dev.1 124 10/9/2023
3.5.7 2,526 10/4/2023
3.5.6 1,515 9/22/2023
3.5.5 1,115 9/18/2023
3.5.5-dev.1 83 9/18/2023
3.5.4 3,242 9/6/2023
3.5.4-dev.2 93 9/6/2023
3.5.3 8,854 7/21/2023
3.5.2-dev.21 96 7/21/2023
3.5.2-dev.20 192 7/7/2023
3.5.2-dev.19 114 7/6/2023
3.5.2-dev.16 98 7/5/2023
3.5.2-dev.15 150 6/27/2023
3.5.2-dev.3 953 5/18/2023
3.5.2-dev.1 102 5/17/2023
3.5.1 35,639 5/17/2023
3.5.1-dev.4 104 5/17/2023
3.5.1-dev.3 496 5/9/2023
3.5.1-dev.2 165 5/5/2023
3.5.1-dev.1 119 5/2/2023
3.4.3-dev.4 108 5/1/2023
3.4.3-dev.3 121 4/29/2023
3.4.3-dev.2 206 4/25/2023
3.4.3-dev.1 131 4/25/2023
3.4.2 4,768 4/24/2023
3.4.2-dev.2 102 4/24/2023
3.4.2-dev.1 118 4/23/2023
3.4.1 1,108 4/21/2023
3.4.1-dev.5 99 4/21/2023
3.4.1-dev.4 192 4/9/2023
3.4.1-dev.2 113 4/8/2023
3.4.1-dev.1 129 4/2/2023
3.3.4-dev.1 118 4/1/2023
3.3.3 2,383 3/26/2023
3.3.3-dev.2 127 3/26/2023
3.3.3-dev.1 174 3/16/2023
3.3.2 2,039 3/9/2023
3.3.2-dev.2 108 3/9/2023
3.3.2-dev.1 113 3/9/2023
3.3.1 1,003 3/8/2023
3.3.1-dev.1 114 3/8/2023
3.2.3-dev.5 215 2/26/2023
3.2.3-dev.4 161 2/17/2023
3.2.3-dev.3 115 2/15/2023
3.2.3-dev.2 119 2/14/2023
3.2.3-dev.1 119 2/13/2023
3.2.2 9,624 2/6/2023
3.2.2-dev.7 129 2/5/2023
3.2.2-dev.6 145 2/4/2023
3.2.2-dev.5 149 1/26/2023
3.2.2-dev.4 196 1/12/2023
3.2.2-dev.3 149 1/9/2023
3.2.2-dev.2 140 1/7/2023
3.2.2-dev.1 147 1/6/2023
3.2.1 3,685 12/29/2022
3.2.1-dev.2 133 12/29/2022
3.2.1-dev.1 133 12/29/2022
3.1.6-dev.2 158 12/19/2022
3.1.6-dev.1 132 12/15/2022
3.1.5 2,451 12/7/2022
3.1.4-dev.6 119 12/7/2022
3.1.4-dev.5 135 12/5/2022
3.1.4-dev.4 137 12/2/2022
3.1.4-dev.3 147 11/26/2022
3.1.4-dev.2 125 11/26/2022
3.1.4-dev.1 126 11/26/2022
3.1.3 2,137 11/23/2022
3.1.3-dev.5 122 11/23/2022
3.1.3-dev.3 134 11/20/2022
3.1.2 1,879 11/12/2022
3.0.3 1,924 11/1/2022
3.0.2 2,133 10/26/2022
3.0.1 1,328 10/21/2022
3.0.0 3,378 10/8/2022
2.6.4 4,478 9/14/2022
2.6.3 1,856 9/2/2022
2.6.2 4,321 8/6/2022
2.5.2 4,010 7/1/2022
2.5.1 19,190 6/15/2022
2.4.1 24,739 5/20/2022
2.3.3 1,723 5/14/2022
2.3.2 2,019 5/7/2022
2.3.1 3,879 4/13/2022
2.2.1 4,882 3/28/2022
2.1.4 1,358 3/23/2022
2.1.3 1,447 3/18/2022
2.1.2 31,402 2/27/2022
2.1.1 3,041 2/13/2022
2.0.3 1,732 2/7/2022
2.0.2 1,220 2/4/2022
2.0.1 1,626 1/30/2022
2.0.0 1,654 1/24/2022
1.9.4 1,423 1/19/2022
1.9.3 1,233 1/17/2022
1.9.2 3,361 1/11/2022
1.9.1 1,245 1/3/2022
1.8.3 1,054 12/30/2021
1.8.2 1,732 12/25/2021
1.8.1 1,141 12/21/2021
1.7.6 1,343 12/12/2021
1.7.5 1,300 12/6/2021
1.7.4 1,597 12/1/2021
1.7.3 2,202 11/27/2021
1.7.2 1,607 11/20/2021
1.7.1 1,049 11/10/2021
1.6.4 1,096 11/6/2021
1.6.3 1,005 11/3/2021
1.6.2 1,005 10/31/2021
1.6.1 1,033 10/31/2021
1.5.1 1,049 10/25/2021
1.4.1 999 10/20/2021
1.3.1 984 10/19/2021
1.3.0 1,068 10/17/2021
1.0.2 1,036 10/15/2021
1.0.1 961 10/11/2021
1.0.0 1,132 9/30/2021
0.9.5 1,076 9/1/2021
0.9.4 979 8/29/2021
0.9.3 976 8/24/2021
0.9.2 971 8/19/2021
0.9.1 1,022 8/16/2021
0.8.1 1,041 8/13/2021
0.7.4 1,018 8/10/2021
0.7.3 966 8/9/2021
0.7.1 1,120 8/8/2021