Ready-to-use .NET Standard library for convenient development of Telegram bots.
Many people know the ASP.NET Core platform and its convenience for developing web API applications.
Now you can see the same pattern in Telegram Bot API.
You can find usage examples in the TelegramBot.ConsoleTest project.
You can find Simple Cafe Market example in this controller sample.
Start by importing the library into your project
dotnet add package TelegramBot.NET
- Implement simple handler in your
Program.cs
static void Main(string[] args)
{
BotBuilder builder = new BotBuilder(args)
.UseApiKey(x => x.FromConfiguration());
var app = builder.Build();
app.MapControllers();
app.Run();
}
- Add your API key from BotFather to
appsettings.json
file, key isTelegramBotToken
:
{
"TelegramBotToken": "YOUR_API_TOKEN"
}
or use command line arguments:
./TelegramBot.Console TelegramBotToken=YOUR_API_TOKEN
- Implement controller, in this sample - for handling
/start
command:
public class CommandController(ILogger<CommandController> _logger) : BotControllerBase
{
[TextCommand("/start")]
public IActionResult HandleStartAsync()
{
_logger.LogInformation("Start command received.");
return Text("Hello!");
}
}
- Run application - and see result:
info: TelegramBot.BotApp[0]
Bot started - receiving updates.
info: TelegramBot.ConsoleTest.Controllers.HomeController[0]
Start command received.
- Dependency Injection - use
Services
property ofBotBuilder
to add services:
builder.Services.AddDbContext<AppDbContext>(x => x.UseNpgsql(connectionString));
builder.Services.AddScoped<ISomeHandler, Handler>();
- Send response to client - use
BotControllerBase
methods -Inline
,Text
,MarkDown
:
[BotCommand("/start")]
public async Task<IActionResult> HandleStartAsync()
{
string prompt = await _dbContext.Translations.GetTranslationAsync("WelcomePrompt", Language.English);
InlineKeyboardMarkup keyboard = new KeyboardBuilder()
.WithColumns(2)
.AddButton("🇺🇸 English", "/language/en")
.AddButton("🇷🇺 Русский", "/language/ru")
.AddButton("🇪🇸 Español", "/language/es")
.AddButton("🇺🇦 Українська", "/language/uk")
.Build();
return Inline(prompt, keyboard);
// or
return Text("Hello, it's me!");
// or
return Markdown("Okay, click [this](https://example.com) link");
}
- Add command handlers
- Add response types:
- Text
- Inline
- Markdown
- Image
- Video
- Delete
- Redirect
- Implement language dictionary service
- Implement router for inline query
- Inject user model in base controller
- Add user state service
See the open issues for a full list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE.md for more information.