/
aa.gerasimenko
/
tg-proxy-rate
Обзор
Документация
Войти
/
aa.gerasimenko
/
tg-proxy-rate
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Program.cs
139 строк
4 KB
Anton Gerasimenko
parallel requests
28 апр 2026, 17:56
28 апр 2026, 17:56
0ae88a1
Код
Авторство
О чём код?
using System.Collections.Concurrent; using Telegram.Bot; using Telegram.Bot.Types; using tg_proxy_rate.Services; // Create bot configuration var botConfig = new TelegramBotConfig { // Get bot token from environment variable or use a default value BotToken = Environment.GetEnvironmentVariable("TELEGRAM_BOT_TOKEN") ?? "YOUR_BOT_TOKEN_HERE", // Replace with your actual bot token }; // Read proxies from file var proxies = await ReadProxiesFromFile("proxies.txt"); if (!proxies.Any()) { Console.WriteLine("No proxies found in proxies.txt file"); return; } Console.WriteLine($"Testing {proxies.Count} proxies..."); // Dictionary to store successful bot services var successfulBots = new ConcurrentBag<(TelegramBotService Service, string Proxy)>(); // Create tasks for testing each proxy var testTasks = proxies.Select(async proxy => { try { Console.WriteLine($"Testing proxy: {proxy}"); // Create bot configuration with current proxy var config = new TelegramBotConfig { BotToken = botConfig.BotToken, Proxy = proxy }; // Create bot service var botService = new TelegramBotService(config); // Test connection by getting bot info with timeout using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(10)); var me = await botService.GetMeAsync(cts.Token); // If successful, add to successful bots successfulBots.Add((botService, proxy)); Console.WriteLine($"✓ Successfully connected using proxy {proxy}. Bot username: @{me.Username}"); } catch (OperationCanceledException) { // Timeout occurred Console.WriteLine($"✗ Timeout while testing proxy {proxy}"); } catch (Exception ex) { Console.WriteLine($"✗ Failed to connect using proxy {proxy}: {ex.Message}"); } }).ToList(); // Wait for all tests to complete await Task.WhenAll(testTasks); // Check if any proxy was successful if (successfulBots.IsEmpty) { Console.WriteLine("All proxies failed. Unable to connect to Telegram."); return; } // Take the first successful bot service var (botService, workingProxy) = successfulBots.First(); Console.WriteLine($"Using proxy: {workingProxy} for bot operations"); // Define update handler async Task HandleUpdate(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken) { // Handle different types of updates if (update.Message != null) { var message = update.Message; var chatId = message.Chat.Id; // Echo back the received message await botService.SendMessageAsync(chatId, $"You said: {message.Text}", cancellationToken: cancellationToken); } } Console.WriteLine("Bot is running..."); // Start receiving updates using var mainCts = new CancellationTokenSource(); Console.CancelKeyPress += (_, e) => { e.Cancel = true; mainCts.Cancel(); Console.WriteLine("Stopping bot..."); }; try { botService.StartReceiving(HandleUpdate, mainCts.Token); await Task.Delay(-1, mainCts.Token); // Keep the bot running } catch (OperationCanceledException) { Console.WriteLine("Bot stopped."); } /// <summary> /// Reads proxy list from file /// </summary> /// <param name="filePath">Path to proxies file</param> /// <returns>List of proxy strings</returns> static async Task<List<string>> ReadProxiesFromFile(string filePath) { var proxies = new List<string>(); if (!File.Exists(filePath)) { Console.WriteLine($"Proxies file not found: {filePath}"); return proxies; } using var reader = new StreamReader(filePath); string? line; while ((line = await reader.ReadLineAsync()) != null) { line = line.Trim(); if (!string.IsNullOrEmpty(line)) { proxies.Add(line); } } return proxies; }