Skip to content

Commit

Permalink
Restore standard appsettings & use User Secrets
Browse files Browse the repository at this point in the history
  • Loading branch information
wiz0u committed Aug 9, 2024
1 parent 41ada65 commit a489d7c
Show file tree
Hide file tree
Showing 13 changed files with 69 additions and 37 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,13 @@ ModelManifest.xml
.idea/
*.*.iml

# Settings for different environments
appsettings.*.json
launchSettings.json

out.json
bucket-name.txt
out.yml
local.settings.json

.fake
.ionide

/Console/Properties/launchSettings.json
/InlineQueries/Properties/launchSettings.json
1 change: 1 addition & 0 deletions Console.Advanced/Console.Advanced.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>83ea0939-143a-4cb0-bd93-76083cc6c235</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 10 additions & 0 deletions Console.Advanced/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"profiles": {
"Console.Advanced": {
"commandName": "Project",
"environmentVariables": {
"DOTNET_ENVIRONMENT": "Development"
}
}
}
}
8 changes: 8 additions & 0 deletions Console.Advanced/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
12 changes: 5 additions & 7 deletions Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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}");
8 changes: 8 additions & 0 deletions FSharp.Example/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.Hosting.Lifetime": "Information"
}
}
}
4 changes: 2 additions & 2 deletions InlineQueries/InlineQueries.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Telegram.Bot" Version="21.3.0" />
<PackageReference Include="Telegram.Bot" Version="21.7.1" />
</ItemGroup>

<ItemGroup>
Expand Down
32 changes: 11 additions & 21 deletions InlineQueries/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<InlineQueryResult>();

Expand All @@ -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)

Check warning on line 80 in InlineQueries/Program.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 80 in InlineQueries/Program.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
if (uint.TryParse(chosenInlineResult.ResultId, out uint index) && index < articleNames.Length)
{
Console.WriteLine($"User {chosenInlineResult.From} has selected article: {articleNames[index]}");
}
return Task.CompletedTask;
}
3 changes: 2 additions & 1 deletion Webhook.Controllers/Webhook.Controllers.csproj
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>6521903b-26d3-496b-8a25-0bad27b56010</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions Webhook.Controllers/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
1 change: 1 addition & 0 deletions Webhook.MinimalAPIs/Webhook.MinimalAPIs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>37c33694-a3c1-43d7-b843-89936e9fbcbd</UserSecretsId>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions Webhook.MinimalAPIs/appsettings.Development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
4 changes: 2 additions & 2 deletions Webhook.MinimalAPIs/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit a489d7c

Please sign in to comment.