From 131fd361064afa0e33c0bd2cefe7ffb053443496 Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Wed, 17 May 2023 18:26:53 +0200 Subject: [PATCH] Fix ReactorError not correctly raised. Added Program_ReactorError example --- .github/dev.yml | 2 +- .github/workflows/telegram-api.yml | 7 +-- EXAMPLES.md | 3 +- Examples/Program_ReactorError.cs | 71 ++++++++++++++++++++++++++++++ FAQ.md | 2 +- README.md | 2 +- src/Client.cs | 4 +- 7 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 Examples/Program_ReactorError.cs diff --git a/.github/dev.yml b/.github/dev.yml index ffec59b..c719a3b 100644 --- a/.github/dev.yml +++ b/.github/dev.yml @@ -2,7 +2,7 @@ pr: none trigger: - master -name: 3.5.1-dev.$(Rev:r) +name: 3.5.2-dev.$(Rev:r) pool: vmImage: ubuntu-latest diff --git a/.github/workflows/telegram-api.yml b/.github/workflows/telegram-api.yml index 19ade4b..b9f2a14 100644 --- a/.github/workflows/telegram-api.yml +++ b/.github/workflows/telegram-api.yml @@ -9,6 +9,7 @@ permissions: jobs: action: + if: contains(github.event.issue.labels.*.name, 'telegram api') runs-on: ubuntu-latest steps: - uses: dessant/support-requests@v3.0.0 @@ -16,7 +17,7 @@ jobs: support-label: 'telegram api' issue-comment: > **Github Issues** should be used only for problems with the library itself. - - Questions about Telegram API usage should be asked on **StackOverflow** so the whole community can help and benefit. - Click here to open a question: https://stackoverflow.com/questions/ask?tags=c%23+wtelegramclient+telegram-api + + For questions about Telegram API usage, you can search the [API official documentation](https://core.telegram.org/api#getting-started) + or [click here to ask your question on **StackOverflow**](https://stackoverflow.com/questions/ask?tags=c%23+wtelegramclient+telegram-api) so the whole community can help and benefit. close-issue: true diff --git a/EXAMPLES.md b/EXAMPLES.md index 74f1df1..2ffdeae 100644 --- a/EXAMPLES.md +++ b/EXAMPLES.md @@ -469,9 +469,8 @@ private Dictionary _chats = new(); var dialogs = await client.Messages_GetAllDialogs(); dialogs.CollectUsersChats(_users, _chats); -private async Task OnUpdate(IObject arg) +private async Task OnUpdate(UpdatesBase updates) { - if (arg is not UpdatesBase updates) return; updates.CollectUsersChats(_users, _chats); ... } diff --git a/Examples/Program_ReactorError.cs b/Examples/Program_ReactorError.cs new file mode 100644 index 0000000..5a8a0a8 --- /dev/null +++ b/Examples/Program_ReactorError.cs @@ -0,0 +1,71 @@ +using System; +using System.Threading.Tasks; +using Telegram.Bot.Types; +using TL; + +namespace WTelegramClientTest +{ + static class Program_ReactorError + { + static WTelegram.Client Client; + + // go to Project Properties > Debug > Environment variables and add at least these: api_id, api_hash, phone_number + static async Task Main(string[] _) + { + Console.WriteLine("The program demonstrate how to handle ReactorError. Press any key to terminate"); + WTelegram.Helpers.Log = (l, s) => System.Diagnostics.Debug.WriteLine(s); + try + { + await CreateAndConnect(); + Console.ReadKey(); + } + finally + { + Client?.Dispose(); + } + } + + private static async Task CreateAndConnect() + { + Client = new WTelegram.Client(Environment.GetEnvironmentVariable); + Client.OnUpdate += Client_OnUpdate; + Client.OnOther += Client_OnOther; + var my = await Client.LoginUserIfNeeded(); + Console.WriteLine($"We are logged-in as " + my); + } + + private static async Task Client_OnOther(IObject arg) + { + if (arg is ReactorError err) + { + // typically: network connection was totally lost + Console.WriteLine($"Fatal reactor error: {err.Exception.Message}"); + while (true) + { + Console.WriteLine("Disposing the client and trying to reconnect in 5 seconds..."); + Client?.Dispose(); + Client = null; + await Task.Delay(5000); + try + { + await CreateAndConnect(); + break; + } + catch (Exception ex) + { + Console.WriteLine("Connection still failing: " + ex.Message); + } + } + } + else + Console.WriteLine("Other: " + arg.GetType().Name); + } + + private static Task Client_OnUpdate(UpdatesBase updates) + { + foreach (var update in updates.UpdateList) + Console.WriteLine(update.GetType().Name); + return Task.CompletedTask; + } + } +} diff --git a/FAQ.md b/FAQ.md index 4ebb202..99875b6 100644 --- a/FAQ.md +++ b/FAQ.md @@ -203,7 +203,7 @@ If Telegram servers decide to shutdown this secondary connection, it's not an is This should be transparent and pending API calls should automatically be resent upon reconnection. You can choose to increase `MaxAutoReconnects` if it happens too often because your Internet connection is unstable. -3) If you reach `MaxAutoReconnects` disconnections, then the **OnUpdate** event handler will receive a `ReactorError` object to notify you of the problem, +3) If you reach `MaxAutoReconnects` disconnections, then the **OnOther** event handler will receive a `ReactorError` object to notify you of the problem, and pending API calls throw the network IOException. In this case, the recommended action would be to dispose the client and recreate one diff --git a/README.md b/README.md index 0d91114..655e5b3 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ See [FAQ #4](https://wiz0u.github.io/WTelegramClient/FAQ#access-hash) to learn m # Other things to know -The Client class also offers an `OnUpdate` event that is triggered when Telegram servers sends Updates (like new messages or status), independently of your API requests. +The Client class also offers `OnUpdate` and `OnOther` events that are triggered when Telegram servers sends Updates (like new messages or status) or other structures, independently of your API requests. See [Examples/Program_ListenUpdates.cs](https://github.com/wiz0u/WTelegramClient/blob/master/Examples/Program_ListenUpdates.cs?ts=4#L23) An invalid API request can result in a `RpcException` being raised, reflecting the [error code and status text](https://revgram.github.io/errors.html) of the problem. diff --git a/src/Client.cs b/src/Client.cs index 83431ac..794a251 100644 --- a/src/Client.cs +++ b/src/Client.cs @@ -46,7 +46,7 @@ namespace WTelegram /// Size of chunks when uploading/downloading files. Reduce this if you don't have much memory public int FilePartSize { get; set; } = 512 * 1024; /// Is this Client instance the main or a secondary DC session - public bool IsMainDC => (_dcSession?.DataCenter?.id ?? 0) == _session.MainDC; + public bool IsMainDC => (_dcSession?.DataCenter?.id - _session.MainDC) is null or 0; /// Has this Client established connection been disconnected? public bool Disconnected => _tcpClient != null && !(_tcpClient.Client?.Connected ?? false); /// ID of the current logged-in user or 0 @@ -164,7 +164,7 @@ namespace WTelegram public static void LoadPublicKey(string pem) => Encryption.LoadPublicKey(pem); /// Builds a structure that is used to validate a 2FA password - /// Password validation configuration. You can obtain this via Account_GetPassword or through OnUpdate as part of the login process + /// Password validation configuration. You can obtain this via Account_GetPassword or through OnOther as part of the login process /// The password to validate public static Task InputCheckPassword(Account_Password accountPassword, string password) => Check2FA(accountPassword, () => Task.FromResult(password));