TelegramBots / Telegram.Bot.Examples

Examples for the Telegram.Bot C# Library
MIT License
640 stars 294 forks source link

Telegram.Bot.Examples.WebHook #485

Closed fox-15 closed 1 year ago

fox-15 commented 1 year ago

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!; }

Fedorus commented 1 year ago

That information should be in config file

fox-15 commented 1 year ago

Sorry, but this is exactly the same as in the example.

Fedorus commented 1 year ago

https://github.com/TelegramBots/Telegram.Bot.Examples/blob/master/Telegram.Bot.Examples.WebHook/Program.cs#L55

No its not ) This class instance is populated from your config file in line 8 of the same file

fox-15 commented 1 year ago

appsettings.json Thank you!