Skip to content

tsziming/TelegramBotBoilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Telegram Bot Boilerplate

Boilerplate preview

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

⭐️ Features

  • 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

👀 Overview

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();
        }
    }

🚩 Getting started

To use this boilerplate you need to install it via Installation Guide

🔧 Scripts

You can perform some complex configurations using built-in scripts.

📕 Guides

Common

About commands

📝 Roadmap

  • Command arguments parser
  • Use DI Framework for listener & command management
  • Implement types for Callback and Keyboard Menus
  • Generic Context with facade methods for basic responses
  • Add i18n (bot internationalization) features
  • Add webhook support
  • Docker Containerization

License

MIT - Made by tsziming