PowerBot.Lite 1.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package PowerBot.Lite --version 1.2.0
NuGet\Install-Package PowerBot.Lite -Version 1.2.0
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="1.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add PowerBot.Lite --version 1.2.0
#r "nuget: PowerBot.Lite, 1.2.0"
#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=1.2.0

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

PowerBot.Lite

Telegram bot wrapper.

Tests 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;

CoreBot botClient = new CoreBot("TOKEN")
    .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()
    {
        string messageText = $"Hi! your id is {User.TelegramId}, chatId is {ChatId}.";
        await BotClient.SendTextMessageAsync(ChatId, messageText);
    }

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

    [MessageTypeFilter(MessageType.Voice)]
    public async Task VoiceMethod()
    {
        string 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 atribute 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 make it by next template:

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");
    }
}

Depedency Injection

PowerBot.Lite supports depedency 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:

CoreBot botClient = new CoreBot(botToken)
    .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()
    {
        int randomValue = _randomService.Random(0, 100);

        string 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)
    {
        int 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 net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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