-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify Webhook.Controllers, harmonize stuff
- Loading branch information
Showing
26 changed files
with
127 additions
and
234 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,6 @@ | |
} | ||
}, | ||
"BotConfiguration": { | ||
"BotToken": "{BOT_TOKEN}" | ||
"BotToken": "YOUR_BOT_TOKEN", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
Serverless/AwsLambda.Webhook/lambda-bot/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"profiles": { | ||
"Mock Lambda Test Tool": { | ||
"commandName": "Executable", | ||
"commandLineArgs": "--port 5050", | ||
"workingDirectory": ".\\bin\\$(Configuration)\\netcoreapp3.1", | ||
"executablePath": "C:\\Users\\%USERNAME%\\.dotnet\\tools\\dotnet-lambda-test-tool-3.1.exe" | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
Serverless/AzureFunctions.IsolatedProcess.Webhook/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"profiles": { | ||
"AzureFunctions_IsolatedProcess_Webhook": { | ||
"commandName": "Project", | ||
"commandLineArgs": "--port 7071", | ||
"launchBrowser": false | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
public class BotConfiguration | ||
{ | ||
public string BotToken { get; init; } = default!; | ||
public Uri BotWebhookUrl { get; init; } = default!; | ||
public string SecretToken { get; init; } = default!; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
52 changes: 0 additions & 52 deletions
52
Webhook.Controllers/Filters/ValidateTelegramBotRequestAttribute.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,25 @@ | ||
using Telegram.Bot; | ||
using Telegram.Bot.Controllers; | ||
using Telegram.Bot.Services; | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
// Setup Bot configuration | ||
var botConfigurationSection = builder.Configuration.GetSection(BotConfiguration.Configuration); | ||
builder.Services.Configure<BotConfiguration>(botConfigurationSection); | ||
|
||
var botConfiguration = botConfigurationSection.Get<BotConfiguration>(); | ||
|
||
// Register named HttpClient to get benefits of IHttpClientFactory | ||
// and consume it with ITelegramBotClient typed client. | ||
// More read: | ||
// https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests#typed-clients | ||
// https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests | ||
builder.Services.AddHttpClient("telegram_bot_client") | ||
.AddTypedClient<ITelegramBotClient>((httpClient, sp) => | ||
{ | ||
BotConfiguration? botConfig = sp.GetConfiguration<BotConfiguration>(); | ||
TelegramBotClientOptions options = new(botConfig.BotToken); | ||
return new TelegramBotClient(options, httpClient); | ||
}); | ||
|
||
// Dummy business-logic service | ||
builder.Services.AddScoped<UpdateHandler>(); | ||
|
||
// There are several strategies for completing asynchronous tasks during startup. | ||
// Some of them could be found in this article https://andrewlock.net/running-async-tasks-on-app-startup-in-asp-net-core-part-1/ | ||
// We are going to use IHostedService to add and later remove Webhook | ||
builder.Services.AddHostedService<ConfigureWebhook>(); | ||
// Setup bot configuration | ||
var botConfigSection = builder.Configuration.GetSection("BotConfiguration"); | ||
builder.Services.Configure<BotConfiguration>(botConfigSection); | ||
builder.Services.AddHttpClient("tgwebhook").RemoveAllLoggers().AddTypedClient( | ||
httpClient => new TelegramBotClient(botConfigSection.Get<BotConfiguration>()!.BotToken, httpClient)); | ||
builder.Services.AddScoped<Webhook.Controllers.Services.UpdateHandler>(); | ||
builder.Services.ConfigureTelegramBotMvc(); | ||
|
||
builder.Services.AddControllers(); | ||
|
||
// The Telegram.Bot library heavily depends on System.Text.Json library with special Json | ||
// settings to deserialize incoming webhook updates and send serialized responses back. | ||
builder.Services.ConfigureTelegramBotMvc(); | ||
|
||
var app = builder.Build(); | ||
// Construct webhook route from the Route configuration parameter | ||
// It is expected that BotController has single method accepting Update | ||
app.MapBotWebhookRoute<BotController>(route: botConfiguration.Route); | ||
|
||
// Configure the HTTP request pipeline. | ||
|
||
app.UseHttpsRedirection(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.MapControllers(); | ||
app.Run(); | ||
|
||
#pragma warning disable CA1050 // Declare types in namespaces | ||
#pragma warning disable RCS1110 // Declare type inside namespace. | ||
public class BotConfiguration | ||
#pragma warning restore RCS1110 // Declare type inside namespace. | ||
#pragma warning restore CA1050 // Declare types in namespaces | ||
{ | ||
public static readonly string Configuration = "BotConfiguration"; | ||
|
||
public string BotToken { get; init; } = default!; | ||
public string HostAddress { get; init; } = default!; | ||
public string Route { get; init; } = default!; | ||
public string SecretToken { get; init; } = default!; | ||
} | ||
app.Run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:5131", | ||
"sslPort": 44315 | ||
} | ||
}, | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "bot/setWebhook", | ||
"applicationUrl": "http://localhost:5227", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"dotnetRunMessages": true, | ||
"launchBrowser": true, | ||
"launchUrl": "bot/setWebhook", | ||
"applicationUrl": "https://localhost:7057;http://localhost:5227", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"launchUrl": "bot/setWebhook", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.