Fix bot
All checks were successful
Local Deploy with Docker / build-and-deploy (push) Successful in 17s
All checks were successful
Local Deploy with Docker / build-and-deploy (push) Successful in 17s
This commit is contained in:
parent
ac708b5e69
commit
6918e3c853
37
Program.cs
37
Program.cs
@ -9,6 +9,7 @@ using Dapper;
|
|||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using Npgsql;
|
using Npgsql;
|
||||||
using UserOfTheDayBot.Data;
|
using UserOfTheDayBot.Data;
|
||||||
using UserOfTheDayBot.Services;
|
using UserOfTheDayBot.Services;
|
||||||
@ -23,6 +24,11 @@ public class Program
|
|||||||
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
|
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
|
||||||
config.AddEnvironmentVariables(); // Добавление переменных среды
|
config.AddEnvironmentVariables(); // Добавление переменных среды
|
||||||
})
|
})
|
||||||
|
.ConfigureLogging(logging =>
|
||||||
|
{
|
||||||
|
logging.ClearProviders();
|
||||||
|
logging.AddConsole(); // Вывод логов в консоль
|
||||||
|
})
|
||||||
.ConfigureServices((context, services) =>
|
.ConfigureServices((context, services) =>
|
||||||
{
|
{
|
||||||
services.AddSingleton<IUserRepository, UserRepository>();
|
services.AddSingleton<IUserRepository, UserRepository>();
|
||||||
@ -38,5 +44,36 @@ public class Program
|
|||||||
});
|
});
|
||||||
})
|
})
|
||||||
.Build();
|
.Build();
|
||||||
|
|
||||||
|
var logger = host.Services.GetRequiredService<ILogger<Program>>();
|
||||||
|
logger.LogInformation("Bot is starting...");
|
||||||
|
|
||||||
|
var botService = host.Services.GetRequiredService<BotService>();
|
||||||
|
var botClient = host.Services.GetRequiredService<ITelegramBotClient>();
|
||||||
|
|
||||||
|
using var cts = new CancellationTokenSource();
|
||||||
|
|
||||||
|
Console.CancelKeyPress += (_, _) => cts.Cancel();
|
||||||
|
|
||||||
|
botClient.StartReceiving(
|
||||||
|
updateHandler: async (bot, update, token) =>
|
||||||
|
{
|
||||||
|
// Получение вашего BotService из DI
|
||||||
|
var botService = host.Services.GetRequiredService<BotService>();
|
||||||
|
await botService.HandleUpdateAsync(update, token);
|
||||||
|
},
|
||||||
|
errorHandler: async (bot, exception, token) =>
|
||||||
|
{
|
||||||
|
logger.LogError($"Telegram Bot Error: {exception.Message}");
|
||||||
|
await Task.CompletedTask;
|
||||||
|
},
|
||||||
|
receiverOptions: new Telegram.Bot.Polling.ReceiverOptions
|
||||||
|
{
|
||||||
|
AllowedUpdates = Array.Empty<UpdateType>() // Получать все обновления
|
||||||
|
},
|
||||||
|
cancellationToken: cts.Token
|
||||||
|
);
|
||||||
|
logger.LogInformation("Bot is running. Press Ctrl+C to stop.");
|
||||||
|
await Task.Delay(Timeout.Infinite, cts.Token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user