Skip to content

Ready-to-use library for convenient development of Telegram bots.

License

Notifications You must be signed in to change notification settings

bvdcode/TelegramBot.NET

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GitHub Nuget Static Badge GitHub Actions Workflow Status NuGet version (TelegramBot.NET) CodeFactor GitHub repo size

TelegramBot.NET

Ready-to-use .NET Standard library for convenient development of Telegram bots.

Purposes

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.

Getting Started

Start by importing the library into your project

dotnet add package TelegramBot.NET

  1. 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();
}
  1. Add your API key from BotFather to appsettings.json file, key is TelegramBotToken:
{
  "TelegramBotToken": "YOUR_API_TOKEN"
}

or use command line arguments:

./TelegramBot.Console TelegramBotToken=YOUR_API_TOKEN
  1. 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!");
    }
}
  1. Run application - and see result:
info: TelegramBot.BotApp[0]
      Bot started - receiving updates.
info: TelegramBot.ConsoleTest.Controllers.HomeController[0]
      Start command received.

back to top

Features

  • Dependency Injection - use Services property of BotBuilder 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");
}

back to top

Roadmap

  • 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).

back to top

Contributing

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

back to top

License

Distributed under the MIT License. See LICENSE.md for more information.

Contact

E-Mail

About

Ready-to-use library for convenient development of Telegram bots.

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Languages