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.
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.
// Install WTelegramClient as a Cake Addin
#addin nuget:?package=WTelegramClient&version=0.9.1

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

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

Showing the top 5 NuGet packages that depend on WTelegramClient:

Package Downloads
WTelegramClient.Extensions.Updates

Extensions over WtelegramClient For Dealing With Updates

WTelegramBot

Telegram Bot API (local server) library providing more extended features Release Notes: - Bot API 7.6 (https://core.telegram.org/bots/api#july-1-2024): Paid media, Star transactions, more bot business methods, caption-above, effects... - Full compatibility with Telegram.Bot v21.4 ! Migration of code is even simplified as: - We are sharing the same types (Telegram.Bot is now a dependency of the library) - No more MessageEntity specifics - WTelegramBotClient class is compatible with ITelegramBotClient

MTProto

A MTProto client library based on wiz0u/WTelegramClient

WTC.Abstractions.Types

Telegram Type Abstractions For WTelegramClient

WTC.Abstractions.Account

WTelegramClient Abstractions for easier (Multiple) Account handling.

GitHub repositories (2)

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

Repository Stars
yiyungent/KnifeHub
🧰 简单易用的效率工具平台
DamianMorozov/TgDownloader
Telegram Downloader
Version Downloads Last updated
4.1.5-dev.1 29 7/12/2024
4.1.4 527 7/7/2024
4.1.3 224 7/6/2024
4.1.2 367 7/2/2024
4.1.2-dev.8 53 7/1/2024
4.1.2-dev.7 157 6/15/2024
4.1.2-dev.6 48 6/15/2024
4.1.2-dev.5 131 6/4/2024
4.1.2-dev.4 73 5/28/2024
4.1.2-dev.3 47 5/27/2024
4.1.2-dev.2 154 5/7/2024
4.1.2-dev.1 62 5/1/2024
4.1.1 4,562 4/28/2024
4.1.1-dev.5 50 4/28/2024
4.1.1-dev.2 53 4/27/2024
4.1.1-dev.1 49 4/27/2024
4.0.1 1,012 4/24/2024
4.0.1-dev.6 64 4/22/2024
4.0.1-dev.5 52 4/18/2024
4.0.1-dev.4 42 4/17/2024
4.0.1-dev.3 55 4/16/2024
4.0.1-dev.2 54 4/14/2024
4.0.1-dev.1 59 4/13/2024
4.0.0 2,048 4/5/2024
4.0.0-dev.8 56 4/4/2024
4.0.0-dev.7 48 4/3/2024
4.0.0-dev.6 84 3/30/2024
4.0.0-dev.5 56 3/29/2024
4.0.0-dev.4 63 3/28/2024
4.0.0-dev.2 72 3/26/2024
4.0.0-dev.1 58 3/26/2024
3.7.2 1,324 3/24/2024
3.7.2-dev.3 65 3/23/2024
3.7.2-dev.2 75 3/19/2024
3.7.2-dev.1 72 3/13/2024
3.7.1 3,030 3/8/2024
3.6.7-dev.13 49 3/8/2024
3.6.7-dev.12 50 3/8/2024
3.6.7-dev.11 46 3/8/2024
3.6.7-dev.9 61 3/8/2024
3.6.7-dev.8 65 3/4/2024
3.6.7-dev.7 109 2/25/2024
3.6.7-dev.6 80 2/21/2024
3.6.7-dev.5 61 2/19/2024
3.6.7-dev.4 68 2/18/2024
3.6.7-dev.3 59 2/18/2024
3.6.7-dev.1 49 2/18/2024
3.6.6 2,226 2/1/2024
3.6.5 2,563 1/18/2024
3.6.4 525 1/16/2024
3.6.4-dev.2 60 1/16/2024
3.6.4-dev.1 55 1/15/2024
3.6.3 3,422 12/31/2023
3.6.3-dev.1 73 12/31/2023
3.6.2 1,127 12/23/2023
3.6.2-dev.2 67 12/23/2023
3.6.2-dev.1 92 12/17/2023
3.6.1 2,406 11/30/2023
3.6.1-dev.7 58 11/30/2023
3.6.1-dev.6 63 11/29/2023
3.6.1-dev.2 85 11/25/2023
3.6.1-dev.1 149 11/17/2023
3.5.10-dev.1 96 11/11/2023
3.5.9 2,572 11/6/2023
3.5.8 1,441 10/28/2023
3.5.8-dev.5 68 10/28/2023
3.5.8-dev.4 122 10/24/2023
3.5.8-dev.3 70 10/24/2023
3.5.8-dev.2 101 10/19/2023
3.5.8-dev.1 104 10/9/2023
3.5.7 2,396 10/4/2023
3.5.6 1,350 9/22/2023
3.5.5 1,037 9/18/2023
3.5.5-dev.1 59 9/18/2023
3.5.4 2,743 9/6/2023
3.5.4-dev.2 70 9/6/2023
3.5.3 7,033 7/21/2023
3.5.2-dev.21 77 7/21/2023
3.5.2-dev.20 167 7/7/2023
3.5.2-dev.19 92 7/6/2023
3.5.2-dev.16 78 7/5/2023
3.5.2-dev.15 127 6/27/2023
3.5.2-dev.3 890 5/18/2023
3.5.2-dev.1 74 5/17/2023
3.5.1 20,280 5/17/2023
3.5.1-dev.4 77 5/17/2023
3.5.1-dev.3 467 5/9/2023
3.5.1-dev.2 140 5/5/2023
3.5.1-dev.1 95 5/2/2023
3.4.3-dev.4 82 5/1/2023
3.4.3-dev.3 90 4/29/2023
3.4.3-dev.2 163 4/25/2023
3.4.3-dev.1 105 4/25/2023
3.4.2 4,579 4/24/2023
3.4.2-dev.2 77 4/24/2023
3.4.2-dev.1 92 4/23/2023
3.4.1 1,056 4/21/2023
3.4.1-dev.5 75 4/21/2023
3.4.1-dev.4 156 4/9/2023
3.4.1-dev.2 80 4/8/2023
3.4.1-dev.1 104 4/2/2023
3.3.4-dev.1 91 4/1/2023
3.3.3 2,252 3/26/2023
3.3.3-dev.2 101 3/26/2023
3.3.3-dev.1 148 3/16/2023
3.3.2 1,913 3/9/2023
3.3.2-dev.2 85 3/9/2023
3.3.2-dev.1 88 3/9/2023
3.3.1 910 3/8/2023
3.3.1-dev.1 88 3/8/2023
3.2.3-dev.5 191 2/26/2023
3.2.3-dev.4 134 2/17/2023
3.2.3-dev.3 90 2/15/2023
3.2.3-dev.2 87 2/14/2023
3.2.3-dev.1 89 2/13/2023
3.2.2 7,779 2/6/2023
3.2.2-dev.7 102 2/5/2023
3.2.2-dev.6 109 2/4/2023
3.2.2-dev.5 121 1/26/2023
3.2.2-dev.4 167 1/12/2023
3.2.2-dev.3 119 1/9/2023
3.2.2-dev.2 110 1/7/2023
3.2.2-dev.1 109 1/6/2023
3.2.1 3,570 12/29/2022
3.2.1-dev.2 105 12/29/2022
3.2.1-dev.1 100 12/29/2022
3.1.6-dev.2 130 12/19/2022
3.1.6-dev.1 100 12/15/2022
3.1.5 2,372 12/7/2022
3.1.4-dev.6 92 12/7/2022
3.1.4-dev.5 94 12/5/2022
3.1.4-dev.4 110 12/2/2022
3.1.4-dev.3 115 11/26/2022
3.1.4-dev.2 95 11/26/2022
3.1.4-dev.1 97 11/26/2022
3.1.3 2,030 11/23/2022
3.1.3-dev.5 93 11/23/2022
3.1.3-dev.3 100 11/20/2022
3.1.2 1,727 11/12/2022
3.0.3 1,861 11/1/2022
3.0.2 1,852 10/26/2022
3.0.1 1,275 10/21/2022
3.0.0 3,240 10/8/2022
2.6.4 4,295 9/14/2022
2.6.3 1,794 9/2/2022
2.6.2 4,180 8/6/2022
2.5.2 3,912 7/1/2022
2.5.1 18,377 6/15/2022
2.4.1 22,347 5/20/2022
2.3.3 1,664 5/14/2022
2.3.2 1,966 5/7/2022
2.3.1 3,791 4/13/2022
2.2.1 4,743 3/28/2022
2.1.4 1,297 3/23/2022
2.1.3 1,367 3/18/2022
2.1.2 31,236 2/27/2022
2.1.1 2,973 2/13/2022
2.0.3 1,664 2/7/2022
2.0.2 1,152 2/4/2022
2.0.1 1,554 1/30/2022
2.0.0 1,582 1/24/2022
1.9.4 1,361 1/19/2022
1.9.3 1,173 1/17/2022
1.9.2 3,223 1/11/2022
1.9.1 1,181 1/3/2022
1.8.3 989 12/30/2021
1.8.2 1,668 12/25/2021
1.8.1 1,080 12/21/2021
1.7.6 1,280 12/12/2021
1.7.5 1,226 12/6/2021
1.7.4 1,542 12/1/2021
1.7.3 2,146 11/27/2021
1.7.2 1,524 11/20/2021
1.7.1 986 11/10/2021
1.6.4 1,043 11/6/2021
1.6.3 952 11/3/2021
1.6.2 953 10/31/2021
1.6.1 973 10/31/2021
1.5.1 994 10/25/2021
1.4.1 932 10/20/2021
1.3.1 924 10/19/2021
1.3.0 993 10/17/2021
1.0.2 979 10/15/2021
1.0.1 903 10/11/2021
1.0.0 1,047 9/30/2021
0.9.5 1,021 9/1/2021
0.9.4 924 8/29/2021
0.9.3 907 8/24/2021
0.9.2 915 8/19/2021
0.9.1 963 8/16/2021
0.8.1 962 8/13/2021
0.7.4 958 8/10/2021
0.7.3 913 8/9/2021
0.7.1 1,012 8/8/2021