Chabot 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Chabot --version 1.1.0
                    
NuGet\Install-Package Chabot -Version 1.1.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="Chabot" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Chabot" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Chabot" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Chabot --version 1.1.0
                    
#r "nuget: Chabot, 1.1.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.
#:package Chabot@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Chabot&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Chabot&version=1.1.0
                    
Install as a Cake Tool

chabot

Integrations
Build chabot-ci
Coverage Codecov branch
Quality CodeFactor

Chabot - library that helps you to build stateful chat bots easily in ASP.NET MVC-style.

Messenger-idempotent. Currently supported messengers:

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 state
  • UseCommands() - 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 text
  • AllowedInAnyStateAttribute - specify that command can be reached in any user current state
  • AllowedStateAttribute(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 IState parameters (if user is in the specified state)
  • Message text - using FromMessageTextAttribute attribute
  • User ID - using FromUserIdAttribute attribute

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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