chore: init commit from GitHub
This commit is contained in:
71
TelegramBot/Program.cs
Normal file
71
TelegramBot/Program.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user