I have such a task, I have ready-made sessions to which I connect, and sometimes there is a non-existent session and when you enter api_id, api_hash, the phone instead of authorization asks me to enter a confirmation code, so I have a question - when it asks for a confirmation code how to throw exceptions to exit this session and try to enter new ones?
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using TL;
namespace AccountConnections
{
internal class Program
{
//static double version = 1.0;
static List<Account> Accounts = new List<Account>();
static Account Active_account = null;
static string path = "json/";
static void Main(string[] args)
{
Console.WriteLine(" --------------CONNECTION ACCOUNTS--------------");
ImportData(path);
ConnectionAccounts();
Console.ReadKey();
}
static string Config(string what)
{
switch (what)
{
case "api_id": return Active_account.app_id.ToString();
case "api_hash": return Active_account.app_hash;
case "phone_number": return Active_account.session_file;
case "verification_code": return "error"; //
default: return null;// let WTelegramClient decide the default config
}
}
static async void ImportData(string path) {
var AllFiles = new DirectoryInfo(path).GetFiles();
foreach (var file in AllFiles)
{
string data = File.ReadAllText(file.ToString());
Account account = JsonSerializer.Deserialize<Account>(data)!;
Accounts.Add(account);
}
}
static async void ConnectionAccounts()
{
Active_account = Accounts[0];
try{
//throw exceptions and exit functions if the code asks
using var client = new WTelegram.Client(Config);
var user = await client.LoginUserIfNeeded();
Console.WriteLine($"We are logged-in as {user.username ?? user.first_name + " " + user.last_name} (id {user.id})");
}
catch (RpcException) { Console.WriteLine("Error"); }
}
}
class Account
{
public string session_file { get; set; }
public int app_id { get; set; }
public string app_hash { get; set; }
}
class ConnactionExcaption : Exception
{
public ConnactionExcaption(){}
}
}
Thanks for the help!