diff --git a/EXAMPLES.md b/EXAMPLES.md index 286de14..ba16c86 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -23,8 +23,8 @@ More examples can also be found in answers to [StackOverflow questions](https:// ### Send a message to someone by @username ```csharp -var resolved = await client.Contacts_ResolveUsername("username"); // without the @ -await client.SendMessageAsync(resolved, "Hello!"); +var resolved = await client.Contacts_ResolveUsername("MyEch0_Bot"); // username without the @ +await client.SendMessageAsync(resolved, "/start"); ``` *Note: This also works if the @username points to a channel/group, but you must already have joined that channel before posting there. If the username is invalid/unused, the API call raises an exception.* diff --git a/README.md b/README.md index 7530047..731684a 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,15 @@ [![NuGet version](https://img.shields.io/nuget/v/WTelegramClient)](https://www.nuget.org/packages/WTelegramClient/) [![Build Status](https://img.shields.io/azure-devops/build/wiz0u/WTelegramClient/7)](https://dev.azure.com/wiz0u/WTelegramClient/_build?definitionId=7) -[![API Layer](https://img.shields.io/badge/API_Layer-139-blueviolet)](https://corefork.telegram.org/methods) +[![API Layer](https://img.shields.io/badge/API_Layer-140-blueviolet)](https://corefork.telegram.org/methods) [![dev nuget](https://img.shields.io/badge/dynamic/json?color=ffc040&label=dev%20nuget&query=%24.versions%5B0%5D&url=https%3A%2F%2Fpkgs.dev.azure.com%2Fwiz0u%2F81bd92b7-0bb9-4701-b426-09090b27e037%2F_packaging%2F46ce0497-7803-4bd4-8c6c-030583e7c371%2Fnuget%2Fv3%2Fflat2%2Fwtelegramclient%2Findex.json)](https://dev.azure.com/wiz0u/WTelegramClient/_packaging?_a=package&feed=WTelegramClient&package=WTelegramClient&protocolType=NuGet) [![Support Chat](https://img.shields.io/badge/Chat_with_us-on_Telegram-0088cc)](https://t.me/WTelegramClient) [![Donate](https://img.shields.io/badge/Help_this_project:-Donate-ff4444)](http://wizou.fr/donate.html) ## _Telegram Client API library written 100% in C# and .NET Standard_ -This ReadMe is a **quick but important tutorial** to learn the fundamentals about this library. Please read it all. +This ReadMe is a **quick but important tutorial** to learn the fundamentals about this library. Please read it all. +This library allows you to connect to Telegram and control a user programmatically (or a bot, but [Telegram.Bot](https://github.com/TelegramBots/Telegram.Bot) is much easier for that). +All the Telegram Client APIs are supported so you can do everything the user could do with a full Telegram GUI client. >⚠️ This library relies on asynchronous C# programming (`async/await`) so make sure you are familiar with this advanced topic before proceeding. >If you are a beginner in C#, starting a project based on this library might not be a great idea. diff --git a/src/Client.cs b/src/Client.cs index 35d05d0..ba174f4 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -1110,11 +1110,21 @@ namespace WTelegram switch (result) { case T resultT: return resultT; - case RpcError rpcError: - var x = rpcError.ParseX(); - if (rpcError.error_code == 303 && rpcError.error_message.EndsWith("_MIGRATE_X")) + case RpcError { error_code: var code, error_message: var message }: + int x = -1; + for (int index = message.Length - 1; index > 0 && (index = message.LastIndexOf('_', index - 1)) >= 0;) + if (message[index + 1] is >= '0' and <= '9') + { + int end = ++index; + do end++; while (end < message.Length && message[end] is >= '0' and <= '9'); + x = int.Parse(message[index..end]); + message = $"{message[0..index]}X{message[end..]}"; + break; + } + + if (code == 303 && message.EndsWith("_MIGRATE_X")) { - if (rpcError.error_message != "FILE_MIGRATE_X") + if (message != "FILE_MIGRATE_X") { // this is a hack to migrate _dcSession in-place (staying in same Client): Session.DCSession dcSession; @@ -1129,7 +1139,7 @@ namespace WTelegram goto retry; } } - else if (rpcError.error_code == 420 && rpcError.error_message.EndsWith("_WAIT_X")) + else if (code == 420 && message.EndsWith("_WAIT_X")) { if (x <= FloodRetryThreshold) { @@ -1137,17 +1147,17 @@ namespace WTelegram goto retry; } } - else if (rpcError.error_code == -503 && !got503) + else if (code == -503 && !got503) { got503 = true; goto retry; } - else if (rpcError.error_code == 500 && rpcError.error_message == "AUTH_RESTART") + else if (code == 500 && message == "AUTH_RESTART") { _session.UserId = 0; // force a full login authorization flow, next time lock (_session) _session.Save(); } - throw new RpcException(rpcError.error_code, rpcError.error_message, x); + throw new RpcException(code, message, x); case ReactorError: goto retry; default: diff --git a/src/TL.MTProto.cs b/src/TL.MTProto.cs index 45ef72d..e05fe98 100644 --- a/src/TL.MTProto.cs +++ b/src/TL.MTProto.cs @@ -198,22 +198,6 @@ namespace TL { public int error_code; public string error_message; - - public int ParseX() // ⚠ Method replace number in error_message with a X - { - for (int index = error_message.Length - 1; index > 0 && (index = error_message.LastIndexOf('_', index - 1)) >= 0;) - { - if (error_message[index + 1] is >= '0' and <= '9') - { - int end = ++index; - do end++; while (end < error_message.Length && error_message[end] is >= '0' and <= '9'); - var x = int.Parse(error_message[index..end]); - error_message = $"{error_message[0..index]}X{error_message[end..]}"; - return x; - } - } - return -1; - } } public abstract class RpcDropAnswer : IObject { }