| 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)
Register required chabot services (e.g. using Telegram messenger with in memory state storage):
services.AddTelegramChabot(b =>
{
b.AddTelegramBotClient(host.Configuration.GetSection("Telegram").Bind);
b.AddTelegramLongPollingListener();
b.AddState(s => s.UseMemoryCacheStorage());
b.UseCommands(c => c.ScanCommandsFromAssembly(typeof(Program).Assembly));
});Define a command:
public class StartCommands : TelegramCommands
{
public async Task Start(
[FromMessageText]string text,
[FromChatId]long chatId)
{
await BotClient.SendTextMessageAsync(chatId, "Hello");
await Context.SetCurrentChatState(new MenuState());
}
[AllowedMessageText("/test")]
[AllowedChatState<MenuState>]
public async Task Test()
{
await BotClient.SendTextMessageAsync(ChatId, "Test action",
replyToMessageId: MessageId);
}
}Chabot is pipeline-based message handling library, so you can define your own middlewares and handlers.
Built-in handling stages:
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.
To define a command you need to create a command group (like ASP.NET controller) using ...CommandsBase as base type, e.g. TelegramCommands.
Built-in ways to configure message routing (you can implement your own rules):
AllowedMessageText- specify that command can be reached with specific command textAllowedState- specify that command can be reached with specific state (Chat state, Message state etc)AllowedUpdateType- telegram update type filter (CallbackQuery,Message,Vote, etc)Order- commands ordering (if multiple commands fit the restrictions)
Chabot selects most specific command for user's current state and message text.
Chabot supports built-in method parameters binding in following cases:
- State types - for state parameters (
FromChatMessageState,FromChatState, etc) - Message text - using
FromMessageTextAttributeattribute - Chat ID - using
FromChatIdAttributeattribute
You can add any other parameter binding rules implementing ICommandParameterValueResolverFactory and ICommandParameterValueResolver interfaces.
Chabot allows you to define any custom state type for any target (user, chat, message, etc) and use it for message routing and persist data between updates.
State is persisted between user messages and can be saved in any storage. Implemented storages:
- In memory (
Chabot.MemoryCache) - Redis (
Chabot.StackExchangeRedis) - EF Core (
Chabot.EntityFrameworkCore)
You can implement any state storage (e.g. save it to a database) implementing IStateStorage interface and registering it.
For more complete samples take a look at the Chabot.Sample.Proxy.Receiver, Chabot.Sample.Proxy.Sender projects.