From a489d7c89e1a4daa147c5886ba2f4cb9ea7a8640 Mon Sep 17 00:00:00 2001 From: Wizou <11647984+wiz0u@users.noreply.github.com> Date: Fri, 9 Aug 2024 16:10:10 +0200 Subject: [PATCH] Restore standard appsettings & use User Secrets --- .gitignore | 7 ++-- Console.Advanced/Console.Advanced.csproj | 1 + .../Properties/launchSettings.json | 10 ++++++ Console.Advanced/appsettings.Development.json | 8 +++++ Console/Program.cs | 12 +++---- FSharp.Example/appsettings.Development.json | 8 +++++ InlineQueries/InlineQueries.csproj | 4 +-- InlineQueries/Program.cs | 32 +++++++------------ .../Webhook.Controllers.csproj | 3 +- .../appsettings.Development.json | 8 +++++ .../Webhook.MinimalAPIs.csproj | 1 + .../appsettings.Development.json | 8 +++++ Webhook.MinimalAPIs/appsettings.json | 4 +-- 13 files changed, 69 insertions(+), 37 deletions(-) create mode 100644 Console.Advanced/Properties/launchSettings.json create mode 100644 Console.Advanced/appsettings.Development.json create mode 100644 FSharp.Example/appsettings.Development.json create mode 100644 Webhook.Controllers/appsettings.Development.json create mode 100644 Webhook.MinimalAPIs/appsettings.Development.json diff --git a/.gitignore b/.gitignore index dd2e5d6..6415fb6 100644 --- a/.gitignore +++ b/.gitignore @@ -216,10 +216,6 @@ ModelManifest.xml .idea/ *.*.iml -# Settings for different environments -appsettings.*.json -launchSettings.json - out.json bucket-name.txt out.yml @@ -227,3 +223,6 @@ local.settings.json .fake .ionide + +/Console/Properties/launchSettings.json +/InlineQueries/Properties/launchSettings.json diff --git a/Console.Advanced/Console.Advanced.csproj b/Console.Advanced/Console.Advanced.csproj index ba20042..88c81aa 100644 --- a/Console.Advanced/Console.Advanced.csproj +++ b/Console.Advanced/Console.Advanced.csproj @@ -5,6 +5,7 @@ Exe enable enable + 83ea0939-143a-4cb0-bd93-76083cc6c235 diff --git a/Console.Advanced/Properties/launchSettings.json b/Console.Advanced/Properties/launchSettings.json new file mode 100644 index 0000000..ee31f94 --- /dev/null +++ b/Console.Advanced/Properties/launchSettings.json @@ -0,0 +1,10 @@ +{ + "profiles": { + "Console.Advanced": { + "commandName": "Project", + "environmentVariables": { + "DOTNET_ENVIRONMENT": "Development" + } + } + } +} diff --git a/Console.Advanced/appsettings.Development.json b/Console.Advanced/appsettings.Development.json new file mode 100644 index 0000000..b2dcdb6 --- /dev/null +++ b/Console.Advanced/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/Console/Program.cs b/Console/Program.cs index 203eab3..62879fb 100644 --- a/Console/Program.cs +++ b/Console/Program.cs @@ -108,12 +108,12 @@ await bot.SendTextMessageAsync(msg.Chat, """ async Task OnUpdate(Update update) { - await (update switch + switch (update) { - { CallbackQuery: { } callbackQuery } => OnCallbackQuery(callbackQuery), - { PollAnswer: { } pollAnswer } => OnPollAnswer(pollAnswer), - _ => OnUnhandledUpdate(update) - }); + case { CallbackQuery: { } callbackQuery }: await OnCallbackQuery(callbackQuery); break; + case { PollAnswer: { } pollAnswer }: await OnPollAnswer(pollAnswer); break; + default: Console.WriteLine($"Received unhandled update {update.Type}"); break; + }; } async Task OnCallbackQuery(CallbackQuery callbackQuery) @@ -127,5 +127,3 @@ async Task OnPollAnswer(PollAnswer pollAnswer) if (pollAnswer.User != null) await bot.SendTextMessageAsync(pollAnswer.User.Id, $"You voted for option(s) id [{string.Join(',', pollAnswer.OptionIds)}]"); } - -async Task OnUnhandledUpdate(Update update) => Console.WriteLine($"Received unhandled update {update.Type}"); diff --git a/FSharp.Example/appsettings.Development.json b/FSharp.Example/appsettings.Development.json new file mode 100644 index 0000000..b2dcdb6 --- /dev/null +++ b/FSharp.Example/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.Hosting.Lifetime": "Information" + } + } +} diff --git a/InlineQueries/InlineQueries.csproj b/InlineQueries/InlineQueries.csproj index 7916729..c72ad0e 100644 --- a/InlineQueries/InlineQueries.csproj +++ b/InlineQueries/InlineQueries.csproj @@ -1,14 +1,14 @@ - net6.0 + net8.0 Exe enable enable - + diff --git a/InlineQueries/Program.cs b/InlineQueries/Program.cs index 4883b76..42fb433 100644 --- a/InlineQueries/Program.cs +++ b/InlineQueries/Program.cs @@ -22,33 +22,24 @@ } // start the bot -var bot = new TelegramBotClient(token); -var me = await bot.GetMeAsync(); using var cts = new CancellationTokenSource(); -bot.StartReceiving(OnUpdate, OnError, null, cts.Token); +var bot = new TelegramBotClient(token, cancellationToken: cts.Token); +var me = await bot.GetMeAsync(); +bot.OnUpdate += OnUpdate; Console.WriteLine($"Start listening for @{me.Username}"); Console.ReadLine(); +cts.Cancel(); // stop the bot -// stop the bot -cts.Cancel(); - -Task OnError(ITelegramBotClient bot, Exception ex, HandleErrorSource source, CancellationToken ct) -{ - Console.WriteLine($"A {source} occured: {ex}"); - return Task.CompletedTask; -} - -async Task OnUpdate(ITelegramBotClient bot, Update update, CancellationToken ct) +async Task OnUpdate(Update update) { try { - await (update.Type switch + switch (update.Type) { - UpdateType.InlineQuery => BotOnInlineQueryReceived(bot, update.InlineQuery!), - UpdateType.ChosenInlineResult => BotOnChosenInlineResultReceived(bot, update.ChosenInlineResult!), - _ => Task.CompletedTask - }); + case UpdateType.InlineQuery: await OnInlineQuery(bot, update.InlineQuery!); break; + case UpdateType.ChosenInlineResult: await OnChosenInlineResult(bot, update.ChosenInlineResult!); break; + }; } #pragma warning disable CA1031 catch (Exception ex) @@ -59,7 +50,7 @@ async Task OnUpdate(ITelegramBotClient bot, Update update, CancellationToken ct) } // for this method to be called, you need to enable "Inline mode" on in BotFather -async Task BotOnInlineQueryReceived(ITelegramBotClient botClient, InlineQuery inlineQuery) +async Task OnInlineQuery(ITelegramBotClient botClient, InlineQuery inlineQuery) { var results = new List(); @@ -86,11 +77,10 @@ async Task BotOnInlineQueryReceived(ITelegramBotClient botClient, InlineQuery in } // for this method to be called, you need to enable "Inline feedback" in BotFather (100% if you want to know all your users choices) -Task BotOnChosenInlineResultReceived(ITelegramBotClient botClient, ChosenInlineResult chosenInlineResult) +async Task OnChosenInlineResult(ITelegramBotClient botClient, ChosenInlineResult chosenInlineResult) { if (uint.TryParse(chosenInlineResult.ResultId, out uint index) && index < articleNames.Length) { Console.WriteLine($"User {chosenInlineResult.From} has selected article: {articleNames[index]}"); } - return Task.CompletedTask; } diff --git a/Webhook.Controllers/Webhook.Controllers.csproj b/Webhook.Controllers/Webhook.Controllers.csproj index d90162f..70d05b0 100644 --- a/Webhook.Controllers/Webhook.Controllers.csproj +++ b/Webhook.Controllers/Webhook.Controllers.csproj @@ -1,9 +1,10 @@ - + net8.0 enable enable + 6521903b-26d3-496b-8a25-0bad27b56010 diff --git a/Webhook.Controllers/appsettings.Development.json b/Webhook.Controllers/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Webhook.Controllers/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Webhook.MinimalAPIs/Webhook.MinimalAPIs.csproj b/Webhook.MinimalAPIs/Webhook.MinimalAPIs.csproj index 2ed4655..3972f4a 100644 --- a/Webhook.MinimalAPIs/Webhook.MinimalAPIs.csproj +++ b/Webhook.MinimalAPIs/Webhook.MinimalAPIs.csproj @@ -4,6 +4,7 @@ net8.0 enable enable + 37c33694-a3c1-43d7-b843-89936e9fbcbd diff --git a/Webhook.MinimalAPIs/appsettings.Development.json b/Webhook.MinimalAPIs/appsettings.Development.json new file mode 100644 index 0000000..0c208ae --- /dev/null +++ b/Webhook.MinimalAPIs/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/Webhook.MinimalAPIs/appsettings.json b/Webhook.MinimalAPIs/appsettings.json index 169e823..724c016 100644 --- a/Webhook.MinimalAPIs/appsettings.json +++ b/Webhook.MinimalAPIs/appsettings.json @@ -5,7 +5,7 @@ "Microsoft.AspNetCore": "Warning" } }, + "AllowedHosts": "*", "BotToken": "YOUR_BOT_TOKEN", - "BotWebhookUrl": "https://your.public.host/bot", - "AllowedHosts": "*" + "BotWebhookUrl": "https://your.public.host/bot" }