WTelegramClient 1.0.0
See the version list below for details.
dotnet add package WTelegramClient --version 1.0.0
NuGet\Install-Package WTelegramClient -Version 1.0.0
<PackageReference Include="WTelegramClient" Version="1.0.0" />
paket add WTelegramClient --version 1.0.0
#r "nuget: WTelegramClient, 1.0.0"
// Install WTelegramClient as a Cake Addin #addin nuget:?package=WTelegramClient&version=1.0.0 // Install WTelegramClient as a Cake Tool #tool nuget:?package=WTelegramClient&version=1.0.0
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 async Task Main(string[] _)
{
using var client = new WTelegram.Client();
await client.ConnectAsync();
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)
{
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. Those shown above are the only ones that have no default values and are required to be provided by your method.
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 chat in chats.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: ");
long id = long.Parse(Console.ReadLine());
var target = chats.First(chat => chat.ID == id);
await client.SendMessageAsync(target, "Hello, World");
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.
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, 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).
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+)
Troubleshooting guide
Here is a list of common issues and how to fix them so that your program work correctly:
Are you using the Nuget package instead of 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:
- Recommended: Use the official Nuget package or the private nuget feed of development builds
- Build your code in RELEASE mode
- Modify your config callback to reply to "server_address" with the IP address of Telegram production servers (as found on your API development tools)
After
ConnectAsync()
, are you callingLoginUserIfNeeded()
? <br/>If you don't authenticate as a user (or bot), you have access to a very limited subset of Telegram APIsDid 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.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 callclient.Dispose()
when your program ends (or exit ausing
scope).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.
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 callingclient.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
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. |
.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. |
-
.NETStandard 2.0
- IndexRange (>= 1.0.0)
- Microsoft.Bcl.HashCode (>= 1.1.1)
- System.Formats.Asn1 (>= 5.0.0)
- System.Memory (>= 4.5.4)
- System.Text.Json (>= 5.0.2)
-
net5.0
- No dependencies.
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: - Bot API 8.0 - HTTP mode support (pass HttpClient). Delayed init/connection/login. |
|
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/TgDownloader
Telegram Files Downloader
|
Version | Downloads | Last updated |
---|---|---|
4.2.4 | 408 | 11/22/2024 |
4.2.4-dev.4 | 39 | 11/22/2024 |
4.2.4-dev.3 | 51 | 11/21/2024 |
4.2.4-dev.2 | 41 | 11/20/2024 |
4.2.4-dev.1 | 50 | 11/18/2024 |
4.2.3 | 546 | 11/18/2024 |
4.2.3-dev.4 | 33 | 11/18/2024 |
4.2.3-dev.3 | 56 | 11/14/2024 |
4.2.3-dev.2 | 100 | 11/2/2024 |
4.2.3-dev.1 | 53 | 11/1/2024 |
4.2.2 | 2,790 | 10/31/2024 |
4.2.2-dev.7 | 51 | 10/29/2024 |
4.2.2-dev.5 | 128 | 10/24/2024 |
4.2.2-dev.4 | 37 | 10/24/2024 |
4.2.2-dev.3 | 103 | 10/17/2024 |
4.2.2-dev.2 | 56 | 10/15/2024 |
4.2.2-dev.1 | 39 | 10/15/2024 |
4.2.1 | 5,219 | 10/7/2024 |
4.1.11-dev.4 | 48 | 10/7/2024 |
4.1.11-dev.3 | 79 | 9/30/2024 |
4.1.11-dev.2 | 103 | 9/22/2024 |
4.1.11-dev.1 | 61 | 9/21/2024 |
4.1.10 | 3,323 | 9/19/2024 |
4.1.10-dev.3 | 125 | 9/8/2024 |
4.1.10-dev.2 | 66 | 9/7/2024 |
4.1.10-dev.1 | 49 | 9/7/2024 |
4.1.9 | 2,399 | 9/7/2024 |
4.1.9-dev.2 | 62 | 9/6/2024 |
4.1.9-dev.1 | 77 | 9/5/2024 |
4.1.8 | 4,375 | 8/14/2024 |
4.1.7 | 860 | 8/10/2024 |
4.1.7-dev.1 | 62 | 8/10/2024 |
4.1.6 | 1,089 | 7/31/2024 |
4.1.6-dev.1 | 42 | 7/29/2024 |
4.1.5 | 1,976 | 7/20/2024 |
4.1.5-dev.2 | 64 | 7/20/2024 |
4.1.5-dev.1 | 195 | 7/12/2024 |
4.1.4 | 1,621 | 7/7/2024 |
4.1.3 | 328 | 7/6/2024 |
4.1.2 | 533 | 7/2/2024 |
4.1.2-dev.8 | 66 | 7/1/2024 |
4.1.2-dev.7 | 170 | 6/15/2024 |
4.1.2-dev.6 | 58 | 6/15/2024 |
4.1.2-dev.5 | 149 | 6/4/2024 |
4.1.2-dev.4 | 87 | 5/28/2024 |
4.1.2-dev.3 | 59 | 5/27/2024 |
4.1.2-dev.2 | 193 | 5/7/2024 |
4.1.2-dev.1 | 83 | 5/1/2024 |
4.1.1 | 12,452 | 4/28/2024 |
4.1.1-dev.5 | 64 | 4/28/2024 |
4.1.1-dev.2 | 67 | 4/27/2024 |
4.1.1-dev.1 | 63 | 4/27/2024 |
4.0.1 | 1,761 | 4/24/2024 |
4.0.1-dev.6 | 86 | 4/22/2024 |
4.0.1-dev.5 | 75 | 4/18/2024 |
4.0.1-dev.4 | 62 | 4/17/2024 |
4.0.1-dev.3 | 77 | 4/16/2024 |
4.0.1-dev.2 | 74 | 4/14/2024 |
4.0.1-dev.1 | 73 | 4/13/2024 |
4.0.0 | 2,606 | 4/5/2024 |
4.0.0-dev.8 | 70 | 4/4/2024 |
4.0.0-dev.7 | 61 | 4/3/2024 |
4.0.0-dev.6 | 98 | 3/30/2024 |
4.0.0-dev.5 | 70 | 3/29/2024 |
4.0.0-dev.4 | 78 | 3/28/2024 |
4.0.0-dev.2 | 85 | 3/26/2024 |
4.0.0-dev.1 | 87 | 3/26/2024 |
3.7.2 | 1,922 | 3/24/2024 |
3.7.2-dev.3 | 76 | 3/23/2024 |
3.7.2-dev.2 | 86 | 3/19/2024 |
3.7.2-dev.1 | 85 | 3/13/2024 |
3.7.1 | 3,516 | 3/8/2024 |
3.6.7-dev.13 | 59 | 3/8/2024 |
3.6.7-dev.12 | 66 | 3/8/2024 |
3.6.7-dev.11 | 58 | 3/8/2024 |
3.6.7-dev.9 | 71 | 3/8/2024 |
3.6.7-dev.8 | 86 | 3/4/2024 |
3.6.7-dev.7 | 130 | 2/25/2024 |
3.6.7-dev.6 | 93 | 2/21/2024 |
3.6.7-dev.5 | 79 | 2/19/2024 |
3.6.7-dev.4 | 81 | 2/18/2024 |
3.6.7-dev.3 | 77 | 2/18/2024 |
3.6.7-dev.1 | 59 | 2/18/2024 |
3.6.6 | 2,744 | 2/1/2024 |
3.6.5 | 3,031 | 1/18/2024 |
3.6.4 | 594 | 1/16/2024 |
3.6.4-dev.2 | 74 | 1/16/2024 |
3.6.4-dev.1 | 69 | 1/15/2024 |
3.6.3 | 4,692 | 12/31/2023 |
3.6.3-dev.1 | 86 | 12/31/2023 |
3.6.2 | 1,447 | 12/23/2023 |
3.6.2-dev.2 | 82 | 12/23/2023 |
3.6.2-dev.1 | 110 | 12/17/2023 |
3.6.1 | 3,119 | 11/30/2023 |
3.6.1-dev.7 | 72 | 11/30/2023 |
3.6.1-dev.6 | 82 | 11/29/2023 |
3.6.1-dev.2 | 100 | 11/25/2023 |
3.6.1-dev.1 | 161 | 11/17/2023 |
3.5.10-dev.1 | 112 | 11/11/2023 |
3.5.9 | 2,830 | 11/6/2023 |
3.5.8 | 1,586 | 10/28/2023 |
3.5.8-dev.5 | 84 | 10/28/2023 |
3.5.8-dev.4 | 136 | 10/24/2023 |
3.5.8-dev.3 | 88 | 10/24/2023 |
3.5.8-dev.2 | 113 | 10/19/2023 |
3.5.8-dev.1 | 119 | 10/9/2023 |
3.5.7 | 2,500 | 10/4/2023 |
3.5.6 | 1,441 | 9/22/2023 |
3.5.5 | 1,095 | 9/18/2023 |
3.5.5-dev.1 | 71 | 9/18/2023 |
3.5.4 | 3,104 | 9/6/2023 |
3.5.4-dev.2 | 87 | 9/6/2023 |
3.5.3 | 8,096 | 7/21/2023 |
3.5.2-dev.21 | 91 | 7/21/2023 |
3.5.2-dev.20 | 180 | 7/7/2023 |
3.5.2-dev.19 | 105 | 7/6/2023 |
3.5.2-dev.16 | 91 | 7/5/2023 |
3.5.2-dev.15 | 141 | 6/27/2023 |
3.5.2-dev.3 | 904 | 5/18/2023 |
3.5.2-dev.1 | 85 | 5/17/2023 |
3.5.1 | 26,051 | 5/17/2023 |
3.5.1-dev.4 | 88 | 5/17/2023 |
3.5.1-dev.3 | 480 | 5/9/2023 |
3.5.1-dev.2 | 151 | 5/5/2023 |
3.5.1-dev.1 | 106 | 5/2/2023 |
3.4.3-dev.4 | 93 | 5/1/2023 |
3.4.3-dev.3 | 101 | 4/29/2023 |
3.4.3-dev.2 | 180 | 4/25/2023 |
3.4.3-dev.1 | 116 | 4/25/2023 |
3.4.2 | 4,709 | 4/24/2023 |
3.4.2-dev.2 | 88 | 4/24/2023 |
3.4.2-dev.1 | 103 | 4/23/2023 |
3.4.1 | 1,088 | 4/21/2023 |
3.4.1-dev.5 | 86 | 4/21/2023 |
3.4.1-dev.4 | 167 | 4/9/2023 |
3.4.1-dev.2 | 92 | 4/8/2023 |
3.4.1-dev.1 | 115 | 4/2/2023 |
3.3.4-dev.1 | 102 | 4/1/2023 |
3.3.3 | 2,319 | 3/26/2023 |
3.3.3-dev.2 | 112 | 3/26/2023 |
3.3.3-dev.1 | 158 | 3/16/2023 |
3.3.2 | 1,968 | 3/9/2023 |
3.3.2-dev.2 | 95 | 3/9/2023 |
3.3.2-dev.1 | 98 | 3/9/2023 |
3.3.1 | 941 | 3/8/2023 |
3.3.1-dev.1 | 98 | 3/8/2023 |
3.2.3-dev.5 | 202 | 2/26/2023 |
3.2.3-dev.4 | 144 | 2/17/2023 |
3.2.3-dev.3 | 101 | 2/15/2023 |
3.2.3-dev.2 | 105 | 2/14/2023 |
3.2.3-dev.1 | 98 | 2/13/2023 |
3.2.2 | 9,533 | 2/6/2023 |
3.2.2-dev.7 | 113 | 2/5/2023 |
3.2.2-dev.6 | 120 | 2/4/2023 |
3.2.2-dev.5 | 131 | 1/26/2023 |
3.2.2-dev.4 | 177 | 1/12/2023 |
3.2.2-dev.3 | 130 | 1/9/2023 |
3.2.2-dev.2 | 121 | 1/7/2023 |
3.2.2-dev.1 | 126 | 1/6/2023 |
3.2.1 | 3,625 | 12/29/2022 |
3.2.1-dev.2 | 116 | 12/29/2022 |
3.2.1-dev.1 | 111 | 12/29/2022 |
3.1.6-dev.2 | 141 | 12/19/2022 |
3.1.6-dev.1 | 111 | 12/15/2022 |
3.1.5 | 2,409 | 12/7/2022 |
3.1.4-dev.6 | 103 | 12/7/2022 |
3.1.4-dev.5 | 105 | 12/5/2022 |
3.1.4-dev.4 | 121 | 12/2/2022 |
3.1.4-dev.3 | 128 | 11/26/2022 |
3.1.4-dev.2 | 108 | 11/26/2022 |
3.1.4-dev.1 | 108 | 11/26/2022 |
3.1.3 | 2,094 | 11/23/2022 |
3.1.3-dev.5 | 105 | 11/23/2022 |
3.1.3-dev.3 | 112 | 11/20/2022 |
3.1.2 | 1,837 | 11/12/2022 |
3.0.3 | 1,894 | 11/1/2022 |
3.0.2 | 2,090 | 10/26/2022 |
3.0.1 | 1,302 | 10/21/2022 |
3.0.0 | 3,303 | 10/8/2022 |
2.6.4 | 4,375 | 9/14/2022 |
2.6.3 | 1,823 | 9/2/2022 |
2.6.2 | 4,264 | 8/6/2022 |
2.5.2 | 3,961 | 7/1/2022 |
2.5.1 | 19,138 | 6/15/2022 |
2.4.1 | 24,681 | 5/20/2022 |
2.3.3 | 1,694 | 5/14/2022 |
2.3.2 | 1,989 | 5/7/2022 |
2.3.1 | 3,835 | 4/13/2022 |
2.2.1 | 4,849 | 3/28/2022 |
2.1.4 | 1,332 | 3/23/2022 |
2.1.3 | 1,413 | 3/18/2022 |
2.1.2 | 31,311 | 2/27/2022 |
2.1.1 | 3,008 | 2/13/2022 |
2.0.3 | 1,700 | 2/7/2022 |
2.0.2 | 1,188 | 2/4/2022 |
2.0.1 | 1,589 | 1/30/2022 |
2.0.0 | 1,615 | 1/24/2022 |
1.9.4 | 1,396 | 1/19/2022 |
1.9.3 | 1,206 | 1/17/2022 |
1.9.2 | 3,324 | 1/11/2022 |
1.9.1 | 1,214 | 1/3/2022 |
1.8.3 | 1,022 | 12/30/2021 |
1.8.2 | 1,703 | 12/25/2021 |
1.8.1 | 1,104 | 12/21/2021 |
1.7.6 | 1,314 | 12/12/2021 |
1.7.5 | 1,268 | 12/6/2021 |
1.7.4 | 1,573 | 12/1/2021 |
1.7.3 | 2,173 | 11/27/2021 |
1.7.2 | 1,580 | 11/20/2021 |
1.7.1 | 1,019 | 11/10/2021 |
1.6.4 | 1,070 | 11/6/2021 |
1.6.3 | 980 | 11/3/2021 |
1.6.2 | 980 | 10/31/2021 |
1.6.1 | 1,004 | 10/31/2021 |
1.5.1 | 1,019 | 10/25/2021 |
1.4.1 | 968 | 10/20/2021 |
1.3.1 | 954 | 10/19/2021 |
1.3.0 | 1,026 | 10/17/2021 |
1.0.2 | 1,009 | 10/15/2021 |
1.0.1 | 935 | 10/11/2021 |
1.0.0 | 1,084 | 9/30/2021 |
0.9.5 | 1,049 | 9/1/2021 |
0.9.4 | 954 | 8/29/2021 |
0.9.3 | 942 | 8/24/2021 |
0.9.2 | 947 | 8/19/2021 |
0.9.1 | 996 | 8/16/2021 |
0.8.1 | 1,016 | 8/13/2021 |
0.7.4 | 987 | 8/10/2021 |
0.7.3 | 941 | 8/9/2021 |
0.7.1 | 1,060 | 8/8/2021 |