Flexible, session-based and documentated console telegram bot template (boilerplate) that works on Telegram.Bot and Entity Framework Core.
Inspired by grammy and telegraf
#StandWithUkraine
- session storage
- complex listener & command management with validators
- unit tests
- built-in command argument parser
- all popular databases supported (Sqlite, MySQL, PostgreSQL, SqlServer)
- python scripts for better user experience
Overrided Run() method should return the string that will be automatically sent to telegram user as a response.
public class StartCommand : Command {
public StartCommand(Bot bot): base(bot) {
Names = new string[]{"/start", "/starting", "!start"};
}
public override string Run(Context context, CancellationToken cancellationToken)
{
return "Welcome! Press /help to see my functions.";
}
}
If the command takes some arguments, built-in argument parser will help validate and parse these arguments:
public class EchoCommand : Command {
public EchoCommand(Bot bot): base(bot) {
Names = new string[]{"/echo", "!echo"};
}
public override string Run(Context context, CancellationToken cancellationToken)
{
var message = context.Update.Message;
if (ArgumentParser.Validate(message.Text)) {
var args = ArgumentParser.Parse(message.Text);
return args.ArgumentsText;
}
return "No arguments provided.";
}
}
Example with all messages listener and session storage:
public class MessageListener : Listener
{
public MessageListener(Bot bot):base(bot) {}
public override bool Validate(Context context, CancellationToken cancellationToken)
{
if (context.Update.Type != UpdateType.Message)
return false;
return true;
}
public override async Task Handler(Context context, CancellationToken cancellationToken)
{
var session = await GetSession(context.Update.Message);
session.Messages++;
await SaveChanges();
}
}
To use this boilerplate you need to install it via Installation Guide
You can perform some complex configurations using built-in scripts.
Common
About commands
- Command arguments parser
- Use DI Framework for listener & command management
- Implement types for
Callback
andKeyboard
Menus - Generic
Context
with facade methods for basic responses - Add i18n (bot internationalization) features
- Add webhook support
- Docker Containerization
MIT - Made by tsziming