This example demonstrates Telegram Bot using:
- long polling (wiki).
- Worker Service template for hosting Bot application.
- cross-platform hosting;
- configuration;
- dependency injection (DI);
- logging;
Please make sure you have .NET 8 or newer installed. You can download .NET runtime from the official site.
You have to add Telegram.Bot nuget package to your project to be able to use polling:
dotnet add package Telegram.BotMake sure that your .csproj contains these items (versions may vary):
<ItemGroup>
<PackageReference Include="Telegram.Bot" Version="22.0.0" />
</ItemGroup>You should provide your Telegram Bot token with one of the available providers.
Read futher on Configuration in ASP.NET Core.
We provide placeholder for bot configuration in appsettings.json. You have to replace YOUR_BOT_TOKEN with actual Bot token:
"BotConfiguration": {
"BotToken": "YOUR_BOT_TOKEN"
}Watch Configuration in .NET 6 talk for deep dive into .NET Configuration.
Follow Create a Windows Service using BackgroundService
article to host your bot as a Windows Service.
Follow .NET Core and systemd blog post to run your
bot as a Linux systemd daemon.
