Closed fox-15 closed 1 year ago
That information should be in config file
Sorry, but this is exactly the same as in the example.
No its not ) This class instance is populated from your config file in line 8 of the same file
appsettings.json Thank you!
Hello! The given example does not work Please help me figure out what the problem is
using Telegram.Bot; using TelegramBotTest2; using TelegramBotTest2.Services; using TelegramBotTest2.Controllers;
var builder = WebApplication.CreateBuilder(args);
var botConfigurationSection = builder.Configuration.GetSection(BotConfiguration.Configuration); builder.Services.Configure(botConfigurationSection);
var botConfiguration = botConfigurationSection.Get();
builder.Services.AddHttpClient("telegram_bot_client")
.AddTypedClient((httpClient, sp) =>
{
BotConfiguration? botConfig = sp.GetConfiguration();
TelegramBotClientOptions options = new(botConfig.BotToken);
return new TelegramBotClient(options, httpClient);
});
builder.Services.AddScoped();
builder.Services.AddHostedService();
builder.Services .AddControllersWithViews() .AddNewtonsoftJson();
var app = builder.Build();
if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); }
app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization();
It gives an error here: -->> app.MapBotWebhookRoute(route: botConfiguration!.Route);
System.NullReferenceException: "Object reference not set to an instance of an object." botConfiguration was null.
app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.Run();
public class BotConfiguration { public static readonly string Configuration = "BotConfiguration"; public string BotToken { get; init; } = "TOKEN"; public string HostAddress { get; init; } = "https://4b24-85-175-194-168.ngrok-free.app/"; public string Route { get; init; } = "api/message/update"; public string SecretToken { get; init; } = default!; }