2023-05-17 18:26:53 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using TL;
|
|
|
|
|
|
|
|
|
|
|
|
namespace WTelegramClientTest
|
|
|
|
|
|
{
|
|
|
|
|
|
static class Program_ReactorError
|
|
|
|
|
|
{
|
|
|
|
|
|
static WTelegram.Client Client;
|
|
|
|
|
|
|
2024-04-16 15:19:12 +02:00
|
|
|
|
// go to Project Properties > Debug > Launch Profiles > Environment variables and add at least these: api_id, api_hash, phone_number
|
2023-05-17 18:26:53 +02:00
|
|
|
|
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
|
|
|
|
|
|
{
|
2024-09-07 01:59:27 +02:00
|
|
|
|
if (Client != null) await Client.DisposeAsync();
|
2023-05-17 18:26:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static async Task CreateAndConnect()
|
|
|
|
|
|
{
|
|
|
|
|
|
Client = new WTelegram.Client(Environment.GetEnvironmentVariable);
|
2024-03-29 16:42:58 +01:00
|
|
|
|
Client.OnUpdates += Client_OnUpdates;
|
2023-05-17 18:26:53 +02:00
|
|
|
|
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...");
|
2024-09-07 01:59:27 +02:00
|
|
|
|
if (Client != null) await Client.DisposeAsync();
|
2023-05-17 18:26:53 +02:00
|
|
|
|
Client = null;
|
|
|
|
|
|
await Task.Delay(5000);
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
await CreateAndConnect();
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2024-06-15 17:35:25 +02:00
|
|
|
|
catch (Exception ex) when (ex is not ObjectDisposedException)
|
2023-05-17 18:26:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
Console.WriteLine("Connection still failing: " + ex.Message);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
Console.WriteLine("Other: " + arg.GetType().Name);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-29 16:42:58 +01:00
|
|
|
|
private static Task Client_OnUpdates(UpdatesBase updates)
|
2023-05-17 18:26:53 +02:00
|
|
|
|
{
|
|
|
|
|
|
foreach (var update in updates.UpdateList)
|
|
|
|
|
|
Console.WriteLine(update.GetType().Name);
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|