This example demonstrates simple Telegram Bot using long polling (wiki).
This example utilize Worker Service template for hosting Bot application. This approach gives you such benefits as:
- cross-platform hosting;
- configuration;
- dependency injection (DI);
- logging;
Please make sure you have .NET 6 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="18.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 plaecehoder for bot configuration in appsettings*.json. You have to replace {BOT_TOKEN} with actual Bot token:
"BotConfiguration": {
"BotToken": "{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.
