WTelegramClient 0.7.1
See the version list below for details.
dotnet add package WTelegramClient --version 0.7.1
NuGet\Install-Package WTelegramClient -Version 0.7.1
<PackageReference Include="WTelegramClient" Version="0.7.1" />
paket add WTelegramClient --version 0.7.1
#r "nuget: WTelegramClient, 0.7.1"
// Install WTelegramClient as a Cake Addin #addin nuget:?package=WTelegramClient&version=0.7.1 // Install WTelegramClient as a Cake Tool #tool nuget:?package=WTelegramClient&version=0.7.1
WTelegramClient
Telegram client library written 100% in C# and .NET Core
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.CallAsync(...)
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.
For that you need to write a method that will provide the answer, 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
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 and 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.
The Telegram API object classes are defined in the TL
namespace, and the request classes (API functions) usable with client.CallAsync
are under the TL.Fn
static class.
Below is an example of calling the messages.getAllChats API function and enumerating the various groups/channels the user is in:
using TL;
...
var chatsBase = await client.CallAsync(new Fn.Messages_GetAllChats { });
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;
}
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.
Beyond CallAsync, the Client class offers a few other methods to simplify the sending of files, medias or messages.
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
Development status
The library is already well usable for many scenarios involving automated steps based on API requests/responses.
Here are the main expected developments:
- Encrypt session file
- Support SignUp of unregistered users
- Improve code Generator (import of TL-schema JSONs)
- Improve Nuget deployment experience (debug symbols? XML documentation?)
- Convert API functions classes to real methods and serialize structures without using Reflection
- Separate task/thread for reading/handling update messages independently from CallAsync
- Support MTProto 2.0
- Support users with 2FA enabled
- Support secret chats end-to-end encryption & PFS
- Support all service messages
Product | Versions 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. |
-
net5.0
- Crc32.NET (>= 1.2.0)
NuGet packages (6)
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: Fill msg.BusinessConnectionId on sent/edited business messages |
|
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/TgDownloader
Telegram Downloader
|
Version | Downloads | Last updated |
---|---|---|
4.1.11-dev.3 | 55 | 9/30/2024 |
4.1.11-dev.2 | 81 | 9/22/2024 |
4.1.11-dev.1 | 45 | 9/21/2024 |
4.1.10 | 1,403 | 9/19/2024 |
4.1.10-dev.3 | 91 | 9/8/2024 |
4.1.10-dev.2 | 53 | 9/7/2024 |
4.1.10-dev.1 | 35 | 9/7/2024 |
4.1.9 | 1,483 | 9/7/2024 |
4.1.9-dev.2 | 51 | 9/6/2024 |
4.1.9-dev.1 | 64 | 9/5/2024 |
4.1.8 | 3,732 | 8/14/2024 |
4.1.7 | 689 | 8/10/2024 |
4.1.7-dev.1 | 54 | 8/10/2024 |
4.1.6 | 1,016 | 7/31/2024 |
4.1.6-dev.1 | 33 | 7/29/2024 |
4.1.5 | 1,655 | 7/20/2024 |
4.1.5-dev.2 | 52 | 7/20/2024 |
4.1.5-dev.1 | 97 | 7/12/2024 |
4.1.4 | 1,278 | 7/7/2024 |
4.1.3 | 316 | 7/6/2024 |
4.1.2 | 488 | 7/2/2024 |
4.1.2-dev.8 | 58 | 7/1/2024 |
4.1.2-dev.7 | 162 | 6/15/2024 |
4.1.2-dev.6 | 50 | 6/15/2024 |
4.1.2-dev.5 | 138 | 6/4/2024 |
4.1.2-dev.4 | 79 | 5/28/2024 |
4.1.2-dev.3 | 52 | 5/27/2024 |
4.1.2-dev.2 | 174 | 5/7/2024 |
4.1.2-dev.1 | 71 | 5/1/2024 |
4.1.1 | 9,598 | 4/28/2024 |
4.1.1-dev.5 | 54 | 4/28/2024 |
4.1.1-dev.2 | 56 | 4/27/2024 |
4.1.1-dev.1 | 52 | 4/27/2024 |
4.0.1 | 1,582 | 4/24/2024 |
4.0.1-dev.6 | 75 | 4/22/2024 |
4.0.1-dev.5 | 64 | 4/18/2024 |
4.0.1-dev.4 | 51 | 4/17/2024 |
4.0.1-dev.3 | 59 | 4/16/2024 |
4.0.1-dev.2 | 64 | 4/14/2024 |
4.0.1-dev.1 | 65 | 4/13/2024 |
4.0.0 | 2,400 | 4/5/2024 |
4.0.0-dev.8 | 61 | 4/4/2024 |
4.0.0-dev.7 | 53 | 4/3/2024 |
4.0.0-dev.6 | 91 | 3/30/2024 |
4.0.0-dev.5 | 62 | 3/29/2024 |
4.0.0-dev.4 | 71 | 3/28/2024 |
4.0.0-dev.2 | 78 | 3/26/2024 |
4.0.0-dev.1 | 71 | 3/26/2024 |
3.7.2 | 1,585 | 3/24/2024 |
3.7.2-dev.3 | 69 | 3/23/2024 |
3.7.2-dev.2 | 80 | 3/19/2024 |
3.7.2-dev.1 | 77 | 3/13/2024 |
3.7.1 | 3,390 | 3/8/2024 |
3.6.7-dev.13 | 51 | 3/8/2024 |
3.6.7-dev.12 | 58 | 3/8/2024 |
3.6.7-dev.11 | 48 | 3/8/2024 |
3.6.7-dev.9 | 64 | 3/8/2024 |
3.6.7-dev.8 | 75 | 3/4/2024 |
3.6.7-dev.7 | 121 | 2/25/2024 |
3.6.7-dev.6 | 85 | 2/21/2024 |
3.6.7-dev.5 | 70 | 2/19/2024 |
3.6.7-dev.4 | 74 | 2/18/2024 |
3.6.7-dev.3 | 65 | 2/18/2024 |
3.6.7-dev.1 | 55 | 2/18/2024 |
3.6.6 | 2,532 | 2/1/2024 |
3.6.5 | 2,927 | 1/18/2024 |
3.6.4 | 553 | 1/16/2024 |
3.6.4-dev.2 | 68 | 1/16/2024 |
3.6.4-dev.1 | 61 | 1/15/2024 |
3.6.3 | 4,458 | 12/31/2023 |
3.6.3-dev.1 | 80 | 12/31/2023 |
3.6.2 | 1,286 | 12/23/2023 |
3.6.2-dev.2 | 75 | 12/23/2023 |
3.6.2-dev.1 | 103 | 12/17/2023 |
3.6.1 | 2,814 | 11/30/2023 |
3.6.1-dev.7 | 63 | 11/30/2023 |
3.6.1-dev.6 | 73 | 11/29/2023 |
3.6.1-dev.2 | 92 | 11/25/2023 |
3.6.1-dev.1 | 153 | 11/17/2023 |
3.5.10-dev.1 | 104 | 11/11/2023 |
3.5.9 | 2,741 | 11/6/2023 |
3.5.8 | 1,535 | 10/28/2023 |
3.5.8-dev.5 | 75 | 10/28/2023 |
3.5.8-dev.4 | 132 | 10/24/2023 |
3.5.8-dev.3 | 79 | 10/24/2023 |
3.5.8-dev.2 | 109 | 10/19/2023 |
3.5.8-dev.1 | 113 | 10/9/2023 |
3.5.7 | 2,474 | 10/4/2023 |
3.5.6 | 1,414 | 9/22/2023 |
3.5.5 | 1,077 | 9/18/2023 |
3.5.5-dev.1 | 67 | 9/18/2023 |
3.5.4 | 2,931 | 9/6/2023 |
3.5.4-dev.2 | 83 | 9/6/2023 |
3.5.3 | 7,691 | 7/21/2023 |
3.5.2-dev.21 | 86 | 7/21/2023 |
3.5.2-dev.20 | 175 | 7/7/2023 |
3.5.2-dev.19 | 100 | 7/6/2023 |
3.5.2-dev.16 | 86 | 7/5/2023 |
3.5.2-dev.15 | 134 | 6/27/2023 |
3.5.2-dev.3 | 897 | 5/18/2023 |
3.5.2-dev.1 | 80 | 5/17/2023 |
3.5.1 | 22,802 | 5/17/2023 |
3.5.1-dev.4 | 83 | 5/17/2023 |
3.5.1-dev.3 | 475 | 5/9/2023 |
3.5.1-dev.2 | 146 | 5/5/2023 |
3.5.1-dev.1 | 101 | 5/2/2023 |
3.4.3-dev.4 | 88 | 5/1/2023 |
3.4.3-dev.3 | 96 | 4/29/2023 |
3.4.3-dev.2 | 173 | 4/25/2023 |
3.4.3-dev.1 | 111 | 4/25/2023 |
3.4.2 | 4,667 | 4/24/2023 |
3.4.2-dev.2 | 83 | 4/24/2023 |
3.4.2-dev.1 | 98 | 4/23/2023 |
3.4.1 | 1,077 | 4/21/2023 |
3.4.1-dev.5 | 81 | 4/21/2023 |
3.4.1-dev.4 | 162 | 4/9/2023 |
3.4.1-dev.2 | 86 | 4/8/2023 |
3.4.1-dev.1 | 110 | 4/2/2023 |
3.3.4-dev.1 | 97 | 4/1/2023 |
3.3.3 | 2,278 | 3/26/2023 |
3.3.3-dev.2 | 107 | 3/26/2023 |
3.3.3-dev.1 | 153 | 3/16/2023 |
3.3.2 | 1,952 | 3/9/2023 |
3.3.2-dev.2 | 90 | 3/9/2023 |
3.3.2-dev.1 | 93 | 3/9/2023 |
3.3.1 | 933 | 3/8/2023 |
3.3.1-dev.1 | 93 | 3/8/2023 |
3.2.3-dev.5 | 196 | 2/26/2023 |
3.2.3-dev.4 | 138 | 2/17/2023 |
3.2.3-dev.3 | 96 | 2/15/2023 |
3.2.3-dev.2 | 95 | 2/14/2023 |
3.2.3-dev.1 | 93 | 2/13/2023 |
3.2.2 | 9,519 | 2/6/2023 |
3.2.2-dev.7 | 108 | 2/5/2023 |
3.2.2-dev.6 | 115 | 2/4/2023 |
3.2.2-dev.5 | 126 | 1/26/2023 |
3.2.2-dev.4 | 171 | 1/12/2023 |
3.2.2-dev.3 | 125 | 1/9/2023 |
3.2.2-dev.2 | 116 | 1/7/2023 |
3.2.2-dev.1 | 120 | 1/6/2023 |
3.2.1 | 3,610 | 12/29/2022 |
3.2.1-dev.2 | 111 | 12/29/2022 |
3.2.1-dev.1 | 106 | 12/29/2022 |
3.1.6-dev.2 | 136 | 12/19/2022 |
3.1.6-dev.1 | 106 | 12/15/2022 |
3.1.5 | 2,403 | 12/7/2022 |
3.1.4-dev.6 | 98 | 12/7/2022 |
3.1.4-dev.5 | 100 | 12/5/2022 |
3.1.4-dev.4 | 114 | 12/2/2022 |
3.1.4-dev.3 | 121 | 11/26/2022 |
3.1.4-dev.2 | 99 | 11/26/2022 |
3.1.4-dev.1 | 103 | 11/26/2022 |
3.1.3 | 2,066 | 11/23/2022 |
3.1.3-dev.5 | 100 | 11/23/2022 |
3.1.3-dev.3 | 107 | 11/20/2022 |
3.1.2 | 1,825 | 11/12/2022 |
3.0.3 | 1,885 | 11/1/2022 |
3.0.2 | 2,062 | 10/26/2022 |
3.0.1 | 1,293 | 10/21/2022 |
3.0.0 | 3,278 | 10/8/2022 |
2.6.4 | 4,331 | 9/14/2022 |
2.6.3 | 1,818 | 9/2/2022 |
2.6.2 | 4,246 | 8/6/2022 |
2.5.2 | 3,951 | 7/1/2022 |
2.5.1 | 19,113 | 6/15/2022 |
2.4.1 | 24,327 | 5/20/2022 |
2.3.3 | 1,686 | 5/14/2022 |
2.3.2 | 1,984 | 5/7/2022 |
2.3.1 | 3,820 | 4/13/2022 |
2.2.1 | 4,841 | 3/28/2022 |
2.1.4 | 1,325 | 3/23/2022 |
2.1.3 | 1,408 | 3/18/2022 |
2.1.2 | 31,276 | 2/27/2022 |
2.1.1 | 3,001 | 2/13/2022 |
2.0.3 | 1,689 | 2/7/2022 |
2.0.2 | 1,178 | 2/4/2022 |
2.0.1 | 1,584 | 1/30/2022 |
2.0.0 | 1,607 | 1/24/2022 |
1.9.4 | 1,389 | 1/19/2022 |
1.9.3 | 1,201 | 1/17/2022 |
1.9.2 | 3,311 | 1/11/2022 |
1.9.1 | 1,209 | 1/3/2022 |
1.8.3 | 1,015 | 12/30/2021 |
1.8.2 | 1,696 | 12/25/2021 |
1.8.1 | 1,099 | 12/21/2021 |
1.7.6 | 1,305 | 12/12/2021 |
1.7.5 | 1,263 | 12/6/2021 |
1.7.4 | 1,566 | 12/1/2021 |
1.7.3 | 2,166 | 11/27/2021 |
1.7.2 | 1,573 | 11/20/2021 |
1.7.1 | 1,013 | 11/10/2021 |
1.6.4 | 1,063 | 11/6/2021 |
1.6.3 | 973 | 11/3/2021 |
1.6.2 | 973 | 10/31/2021 |
1.6.1 | 999 | 10/31/2021 |
1.5.1 | 1,014 | 10/25/2021 |
1.4.1 | 961 | 10/20/2021 |
1.3.1 | 949 | 10/19/2021 |
1.3.0 | 1,014 | 10/17/2021 |
1.0.2 | 1,004 | 10/15/2021 |
1.0.1 | 928 | 10/11/2021 |
1.0.0 | 1,077 | 9/30/2021 |
0.9.5 | 1,044 | 9/1/2021 |
0.9.4 | 949 | 8/29/2021 |
0.9.3 | 933 | 8/24/2021 |
0.9.2 | 942 | 8/19/2021 |
0.9.1 | 991 | 8/16/2021 |
0.8.1 | 986 | 8/13/2021 |
0.7.4 | 980 | 8/10/2021 |
0.7.3 | 934 | 8/9/2021 |
0.7.1 | 1,045 | 8/8/2021 |