PowerBot.Lite 2.1.3

dotnet add package PowerBot.Lite --version 2.1.3
NuGet\Install-Package PowerBot.Lite -Version 2.1.3
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="PowerBot.Lite" Version="2.1.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PowerBot.Lite --version 2.1.3
#r "nuget: PowerBot.Lite, 2.1.3"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install PowerBot.Lite as a Cake Addin
#addin nuget:?package=PowerBot.Lite&version=2.1.3

// Install PowerBot.Lite as a Cake Tool
#tool nuget:?package=PowerBot.Lite&version=2.1.3

PowerBot.Lite

Telegram bot wrapper.

Tests

Tests

Build and test Tests Tests Tests

Tests Tests Tests License

- This project is in early stage!

How to use

  1. Add package to your project

  2. Main code in Program.cs

using PowerBot.Lite;

var botClient = new CoreBot("TOKEN")
    .RegisterHandler<SampleHandler>()
    .Build();

botClient.StartReveiving();

// Wait for eternity
await Task.Delay(Int32.MaxValue);
  1. Create class that inherits BaseHandler class and define bot methods:
class SampleHandler : BaseHandler
{
    [MessageReaction(ChatAction.Typing)]
    [MessageHandler("^/start$")]
    public async Task Start()
    {
        var messageText = $"Hi! your id is {User.TelegramId}, chatId is {ChatId}.";
        await BotClient.SendTextMessageAsync(ChatId, messageText);
    }

    [MessageReaction(ChatAction.Typing)]
    [MessageHandler("^/test$")]
    public async Task TestMethod()
    {
        var messageText = $"Test passed successfully!";
        await BotClient.SendTextMessageAsync(ChatId, messageText);
    }

    [MessageTypeFilter(MessageType.Voice)]
    public async Task VoiceMethod()
    {
        var messageText = $"Voice message!";
        await BotClient.SendTextMessageAsync(ChatId, messageText);
    }
}

Powerbot.Lite maps defined message handlers to incoming telegram.Update and run them when attribute filters are match. PowerBot.Lite will find and run only the first one matched method from each defined class that inherits BaseHandler type.

Methods attributes

For method matching PowerBot.Lite uses next attributes:

*Filter attributes ordered by priority

[MessageReaction(ChatAction.Typing)]

Makes bot, send defined ChatAction before runs matched method.

Message attribute filters:

[MessageHandler(string pattern)]

Filter for text messages, string pattern - is regex matching pattern.

[CallbackQueryHandler(string dataPattern)]

Filter for CallbackQuery events, string dataPattern - is regex matching pattern for CallbackQuery.Data. By default its equals null and matches any CallbackQuery event.

[MessageTypeFilter(MessageType messageType)]

Filter for any messages by defined type (Text, Photo, Audio, Video, Voice, ...)

[UpdateTypeFilter(UpdateType updateType)]

Filter for any updates by defined type (Message, InlineQuery, ChosenInlineResult, CallbackQuery, EditedMessage, ...)

Middleware

Also You can define your own middlewares by creating class that inherits BaseMiddleware and register it in CoreBot builder it by next template:

var botClient = new CoreBot("TOKEN")
    .RegisterMiddleware<FirstMiddleware>()
    .RegisterMiddleware<SecondMiddleware>()
    ...
public class FirstMiddleware : BaseMiddleware
{
    public override async Task Invoke(ITelegramBotClient bot, Update update, Func<Task> func)
    {
        Console.WriteLine("FirstMiddleware before _nextMiddleware log");

        // Do next middleware or process telegram.Update from defined handler if there no other defined middlewares.
        await _nextMiddleware.Invoke(bot, update, func);

        Console.WriteLine("FirstMiddleware after _nextMiddleware log");
    }
}
public class SecondMiddleware : BaseMiddleware
{
    public override async Task Invoke(ITelegramBotClient bot, Update update, Func<Task> func)
    {
        Console.WriteLine("SecondMiddleware before _nextMiddleware log");

        // Do next middleware or process telegram.Update from defined handler if there no other defined middlewares.
        await _nextMiddleware.Invoke(bot, update, func);

        Console.WriteLine("SecondMiddleware after _nextMiddleware log");
    }
}

Console output:

FirstMiddleware before _nextMiddleware log
SecondMiddleware before _nextMiddleware log
SecondMiddleware after _nextMiddleware log
FirstMiddleware after _nextMiddleware log

Dependency Injection

PowerBot.Lite supports dependency injection, based on Autofac container provider.

To use it, firstly you need to define your DI services as class with interface:

public interface IRandomService
{
    public int Random(int min, int max);
}

public class RandomService : IRandomService
{
    private Random _random;

    public RandomService()
    {
        _random = new Random();
    }

    public int Random(int min, int max)
    {
        return _random.Next(min, max);
    }
}

And manually map them in PowerBot.Lite by using RegisterContainers(...) method:

var botClient = new CoreBot(botToken)
    .RegisterMiddleware<BotMiddleware>()
    .RegisterHandler<BotHandler>()
    .RegisterContainers(x => {
        x.RegisterType<RandomService>()
            .As<IRandomService>()
            .SingleInstance();
    })
    .Build();

After that you can inject IRandomService to your handlers or middlewares:

public class BotHandler : BaseHandler
{
    private readonly IRandomService _randomService;

    public BotHandler(IRandomService randomService)
    {
        _randomService = randomService;
    }

    [MessageReaction(ChatAction.Typing)]
    [MessageHandler("/start")]
    public Task Start()
    {
        var randomValue = _randomService.Random(0, 100);
        var messageText = $"Hi! Random integer is: {randomValue}";

        return BotClient.SendTextMessageAsync(ChatId, messageText);
    }
}

public class BotMiddleware : BaseMiddleware
{
    private readonly IRandomService _randomService;

    public BotMiddleware(IRandomService randomService)
    {
        _randomService = randomService;
    }

    public override async Task Invoke(ITelegramBotClient bot, Update update, Func<Task> func)
    {
        var randomValue = _randomService.Random(0, 100);
        Console.WriteLine($"Random number is {randomValue}");

        await _nextMiddleware.Invoke(bot, update, func);
    }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.3 220 12/16/2023
2.1.2 119 12/16/2023
2.1.1 148 5/20/2023
2.1.0 127 5/20/2023
2.0.0 163 5/18/2023
1.2.0 250 2/10/2023