Fix ReactorError not correctly raised. Added Program_ReactorError example

This commit is contained in:
Wizou 2023-05-17 18:26:53 +02:00
parent e8b0bb9245
commit 131fd36106
7 changed files with 81 additions and 10 deletions

2
.github/dev.yml vendored
View file

@ -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

View file

@ -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
@ -17,6 +18,6 @@ jobs:
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

View file

@ -469,9 +469,8 @@ private Dictionary<long, ChatBase> _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);
...
}

View file

@ -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;
}
}
}

2
FAQ.md
View file

@ -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

View file

@ -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.

View file

@ -46,7 +46,7 @@ namespace WTelegram
/// <summary>Size of chunks when uploading/downloading files. Reduce this if you don't have much memory</summary>
public int FilePartSize { get; set; } = 512 * 1024;
/// <summary>Is this Client instance the main or a secondary DC session</summary>
public bool IsMainDC => (_dcSession?.DataCenter?.id ?? 0) == _session.MainDC;
public bool IsMainDC => (_dcSession?.DataCenter?.id - _session.MainDC) is null or 0;
/// <summary>Has this Client established connection been disconnected?</summary>
public bool Disconnected => _tcpClient != null && !(_tcpClient.Client?.Connected ?? false);
/// <summary>ID of the current logged-in user or 0</summary>
@ -164,7 +164,7 @@ namespace WTelegram
public static void LoadPublicKey(string pem) => Encryption.LoadPublicKey(pem);
/// <summary>Builds a structure that is used to validate a 2FA password</summary>
/// <param name="accountPassword">Password validation configuration. You can obtain this via <c>Account_GetPassword</c> or through OnUpdate as part of the login process</param>
/// <param name="accountPassword">Password validation configuration. You can obtain this via <c>Account_GetPassword</c> or through OnOther as part of the login process</param>
/// <param name="password">The password to validate</param>
public static Task<InputCheckPasswordSRP> InputCheckPassword(Account_Password accountPassword, string password)
=> Check2FA(accountPassword, () => Task.FromResult(password));