71 lines
2.4 KiB
C#
71 lines
2.4 KiB
C#
using System.Diagnostics;
|
|
using System.Reflection;
|
|
using Microsoft.Extensions.Logging;
|
|
using Telegram.Bot;
|
|
using Telegram.Bot.Polling;
|
|
using Microsoft.Extensions.Configuration;
|
|
using TelegramBot.Config;
|
|
|
|
namespace TelegramBot;
|
|
|
|
public static class Program
|
|
{
|
|
private static ILogger Logger { get; } = LoggerFactory.Create(x => { x.AddConsole(); })
|
|
.CreateLogger("MainThread");
|
|
|
|
private static ITelegramBotClient bot { get; set; }
|
|
|
|
public static AppSettings Config { get; private set; }
|
|
|
|
public static void Main()
|
|
{
|
|
Splash(Logger);
|
|
LoadConfig();
|
|
var cts = LoadBot();
|
|
while(!cts.IsCancellationRequested)
|
|
Thread.Sleep(1000);
|
|
}
|
|
|
|
private static void Splash(ILogger logger)
|
|
{
|
|
var splash = """
|
|
____ ____ _ ____ _
|
|
/ ___|_ ____ _ _ __ | _ \ _ _ __| | ___ | __ ) ___ | |_
|
|
\___ \ \ /\ / / _` | '_ \| | | | | | |/ _` |/ _ \ | _ \ / _ \| __|
|
|
___) \ V V / (_| | |_) | |_| | |_| | (_| | __/_| |_) | (_) | |_
|
|
|____/ \_/\_/ \__,_| .__/|____/ \__,_|\__,_|\___(_)____/ \___/ \__|
|
|
|_|
|
|
""";
|
|
logger.LogWarning(splash);
|
|
logger.LogInformation(
|
|
$"Build assembly version: {FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).FileVersion}");
|
|
}
|
|
|
|
private static void LoadConfig()
|
|
{
|
|
Logger.LogInformation("Loading config...");
|
|
IConfigurationRoot configuration = new ConfigurationBuilder()
|
|
.AddJsonFile("telegramconfig.json", optional: false)
|
|
#if DEBUG
|
|
.AddJsonFile("telegramconfig.local.json", true, true)
|
|
#endif
|
|
.Build();
|
|
Config = configuration.GetSection("AppSettings").Get<AppSettings>() ?? throw new NullReferenceException();
|
|
}
|
|
|
|
private static CancellationTokenSource LoadBot()
|
|
{
|
|
Logger.LogInformation("Creating bot...");
|
|
|
|
bot = new TelegramBotClient(Config.Token ?? throw new NullReferenceException());
|
|
var cts = new CancellationTokenSource();
|
|
var receiverOptions = new ReceiverOptions
|
|
{
|
|
AllowedUpdates = { }
|
|
};
|
|
bot.StartReceiving(BotHandler.HandleUpdateAsync,
|
|
BotHandler.HandleErrorAsync, receiverOptions, cts.Token);
|
|
|
|
return cts;
|
|
}
|
|
} |