Chabot 1.0.5
See the version list below for details.
dotnet add package Chabot --version 1.0.5
NuGet\Install-Package Chabot -Version 1.0.5
<PackageReference Include="Chabot" Version="1.0.5" />
<PackageVersion Include="Chabot" Version="1.0.5" />
<PackageReference Include="Chabot" />
paket add Chabot --version 1.0.5
#r "nuget: Chabot, 1.0.5"
#:package Chabot@1.0.5
#addin nuget:?package=Chabot&version=1.0.5
#tool nuget:?package=Chabot&version=1.0.5
chabot
| Integrations | |
|---|---|
| Build | |
| Coverage | |
| Quality |
Chabot - library that helps you to build stateful chat bots easily in ASP.NET MVC-style.
Messenger-idempotent. Currently supported messengers:
- Telegram (using https://github.com/TelegramBots/Telegram.Bot)
Quick start
Register required chabot services (e.g. using Telegram messenger with in memory state storage):
services.AddTelegramChabot((c, _) => c.Token = <BOT_TOKEN>, c =>
{
// Specify to listen telegran using long polling updates
c.UseTelegramPollingUpdates();
// Use user state
c.UseState(s => s
.UseSystemTextJsonSerializer()
// Use in memory state storage
.UseInMemoryStateStorage((_, user) => user.Id));
// Use commands
c.UseCommands();
});
Define a commands:
public class HelloWorldCommandGroup : TelegramCommandGroup
{
// Command is allowed with any message
[Command(AllowedWithAnyCommandText = true)]
// Command is allowed in any state
[AllowedInAnyState]
public async Task HelloCommand(
// Automatically binded message text
[FromMessageText]string messageText)
{
await BotClient.SendTextMessageAsync(ChatId, $"Hello, {messageText}");
}
}
Message handling
Chabot is pipeline-based message handling library, where you can define your own middlewares and handlers.
Built-in handling stages:
UseState()- fetches and sets the user's stateUseCommands()- adds message routing to defined commands
You can add your custom middlewares (metrics, logging, whitelists, etc) to message handling pipeline using UseMiddleware... methods and IMiddleware interface.
Middlewares are executing in order of addition to the pipeline.
Commands and routing
To define a command you need to create a command group (like ASP.NET controller) using ...CoomandGroupType as base type, e.g. TelegramCommandGroup and a mark method with CommandAttribute.
Routing
Available ways to configure message routing:
CommandAttribute.AllowedWithAnyCommandText = true- specify that command can be reached with any command textAllowedInAnyStateAttribute- specify that command can be reached in any user current stateAllowedStateAttribute(Type)- specify that command can be reached only in specified user state types
Chabot selects most specific command for user's current state and message text.
Commmand method parameters
Chabot supports built-in method parameters binding in following cases:
- User state types - for
IStateparameters (if user is in the specified state) - Message text - using
FromMessageTextAttributeattribute - User ID - using
FromUserIdAttributeattribute
You can add any other parameter binding rules implementing ICommandParameterValueResolverFactory and ICommandParameterValueResolver interfaces.
User state
Chabot allows you to define any custom state type and use it for message routing and persist data between user message.
To define a state type you just need to implement an empty IState interface:
public class SomeState : IState {}
State is persisted between user messages and can be saved in any storage. Built-in storages:
- In memory storage - call the
UseInMemoryStateStorage()method to use in memory state storage
You can implement any state storage (e.g. save it to a database) implementing IStateStorage interface and registering it.
Sample
Sample, where user enters some value, then confirms it in the next message and the server uses a previously entered value.
Commands:
public class TestCommandGroup : TelegramCommandGroup
{
[Command(AllowedWithAnyCommandText = true)]
[AllowedInAnyState]
public async Task DefaultCommand()
{
await BotClient.SendTextMessageAsync(ChatId,
"Enter a value", replyToMessageId: MessageId);
await SetState(new WaitingForValueState());
}
[Command(AllowedWithAnyCommandText = true)]
[AllowedState(typeof(WaitingForValueState))]
public async Task FooCommand(
[FromMessageText]string messageText)
{
await BotClient.SendTextMessageAsync(ChatId,
$"Type /confirm to confirm {messageText}");
await SetState(new ValueEnteredState
{
Value = messageText
});
}
[Command("confirm")]
[AllowedState(typeof(ValueEnteredState))]
public async Task ConfirmEnteredValue(ValueEnteredState valueEnteredState)
{
var enteredValue = valueEnteredState.Value;
// some logic with previously entered value
await BotClient.SendTextMessageAsync(ChatId,
$"{enteredValue} confirmed");
await SetState(DefaultState.Instance);
}
[Command(AllowedWithAnyCommandText = true)]
[AllowedState(typeof(ValueEnteredState))]
public async Task EnteredValueNotConfirmed()
{
await BotClient.SendTextMessageAsync(ChatId,
"Invalid confirmation, type /confirm to confirm");
}
}
Used state types:
public class WaitingForValueState : IState
{
}
public class ValueEnteredState : IState
{
public string Value { get; set; } = default!;
}
For more complete samples take a look at the Chabot.Sample.Telegram, Chabot.Sample.Proxy.Listener, Chabot.Sample.Proxy.Worker projects.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. 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. |
-
net6.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 6.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 6.0.2)
- Microsoft.Extensions.Options (>= 6.0.0)
NuGet packages (7)
Showing the top 5 NuGet packages that depend on Chabot:
| Package | Downloads |
|---|---|
|
Chabot.Telegram
Package Description |
|
|
Chabot.RabbitMq
Package Description |
|
|
Chabot.EntityFrameworkCore
Package Description |
|
|
Chabot.SystemTextJson
Package Description |
|
|
Chabot.NewtonsoftJson
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 2.1.4 | 286 | 2/22/2025 |
| 2.1.3 | 157 | 2/22/2025 |
| 2.1.2 | 168 | 2/22/2025 |
| 2.1.1 | 177 | 2/22/2025 |
| 2.1.0 | 159 | 2/22/2025 |
| 2.0.0 | 626 | 2/20/2025 |
| 1.2.2 | 313 | 12/11/2023 |
| 1.2.1 | 208 | 12/11/2023 |
| 1.1.0 | 1,095 | 11/6/2022 |
| 1.0.5 | 488 | 11/5/2022 |
| 1.0.4 | 984 | 10/31/2022 |
| 1.0.3 | 549 | 10/9/2022 |
| 1.0.2 | 873 | 10/9/2022 |
| 1.0.1 | 745 | 10/9/2022 |
| 1.0.0 | 521 | 10/9/2022 |