-2

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!

Igor Biyar
  • 19
  • 4
  • There is not enough code to go on (1) you really infer a widely known knowledge of Telegram (which is not widely known) - post more code, what did you expect and why and do post a good question – riffnl Feb 28 '22 at 00:30

1 Answers1

0

Your question isn't very clear, but I think you mean you have obtained "ready-made" sessions by buying them from Internet, and those don't work well.

This practice is in violation of Telegram API TOS:
2.1 You must obtain your own api_id for your application.

WTelegramClient has no support for session files obtained from outside libraries/vendors

We also don't provide specific support for developers intending to automate of a large number of user accounts as it is very likely for spamming/scamming purpose, which goes against Telegram TOS

Wizou
  • 1,336
  • 13
  • 24