Telegram.Bot.Examples/Telegram.Bot.Examples.Polling at master · leonpw/Telegram.Bot.Examples · GitHub
Skip to content

Latest commit

 

History

History
 
 

README.md

Telegram.Bot Polling Example

About

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;

Prerequisites

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.Bot

Make sure that your .csproj contains these items (versions may vary):

<ItemGroup>
  <PackageReference Include="Telegram.Bot" Version="18.0.0" />
</ItemGroup>

Configuration

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.

Run Bot As Windows Service

Follow Create a Windows Service using BackgroundService article to host your bot as a Windows Service.

Run Bot Daemon On Linux

Follow .NET Core and systemd blog post to run your bot as a Linux systemd daemon.