PowerBot.Lite
1.2.0
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
<PackageReference Include="PowerBot.Lite" Version="1.2.0" />
<PackageVersion Include="PowerBot.Lite" Version="1.2.0" />
<PackageReference Include="PowerBot.Lite" />
paket add PowerBot.Lite --version 1.2.0
#r "nuget: PowerBot.Lite, 1.2.0"
#:package PowerBot.Lite@1.2.0
#addin nuget:?package=PowerBot.Lite&version=1.2.0
#tool nuget:?package=PowerBot.Lite&version=1.2.0
PowerBot.Lite
Telegram bot wrapper.
- This project is in early stage!
How to use
Add package to your project
Main code in Program.cs
using PowerBot.Lite;
CoreBot botClient = new CoreBot("TOKEN")
.Build();
botClient.StartReveiving();
// Wait for eternity
await Task.Delay(Int32.MaxValue);
- Create class that inherits
BaseHandlerclass 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 | Versions 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net7.0
- Autofac (>= 6.5.0)
- Telegram.Bot.Extensions.Polling (>= 1.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.