Skip to content
/ MCB Public

A base for writing a telegram bot with a standard implementation or with the ability to interact with a large number of users in multi-threaded mode.

License

Notifications You must be signed in to change notification settings

VilShiro/MCB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MCB - Multi Client Bot

Version 1.5 this is a transitional version, in version 2.0 the features of 1.0 (creation through function overrides) will be cut in favor of an emphasis on the annotation management system, and also the possibilities of multi-botUser multi-threaded work will be implemented

The operation of the multi-botUser bot has not been tested and is at the development stage, bugs and critical errors are possible

Publishing to Maven Central with version 2.0 release

Please give my repository a ⭐, it will help me know that someone is using it

Installation

Version 1.5.x early access

Maven

<dependency>
    <groupId>io.github.vilshiro</groupId>
    <artifactId>mcb</artifactId>
    <version>1.5.4</version>
</dependency>

Gradle

implementation group: 'io.github.vilshiro', name: 'mcb', version: '1.5.4'

Maven Central Repository

Get started(Only for Bot class)

Old Variant

Create a class that is a descendant of the class Bot and override the necessary functions, the constructor accepts the bot token

Use the getBot() command to get a bot instance

MyBot.java

public class MyBot extends Bot {
    public MyBot() {
        super("your token");
    }

    @Override
    protected void updateParse(Update update) {
        // your logic
    }

    @Override
    protected void messageParse(Message message) {
        // parsing the messages
    }

    @Override
    protected void callbackQueryParse(CallbackQuery query) {
        // callback data...
    }
}

Present variant

Bot superclass and @BotConfiguration annotation

First, you must create a class that is a descendant of the Bot class and implement one of the constructors (recommended with a configuration class)

MyBot.java

public class MyBot extends Bot{
    public MyBot(){
        super(MyConfig.class);
    }
}

Next, in the specified MyConfig class, add the BotConfiguration annotation and specify the token

MyConfig.java

@BotConfiguration(botToken = "your token")
public class MyConfig {
    
}

Additional BotConfiguration parameters:

parameter type functionality
threadSeparation boolean each command will run in its own thread
startCommand String start command, usually /start
doubleDispatch boolean when disabled - updates come only to annotated functions, when enabled - to annotated functions and overridden ones
staticBuild boolean does not create an instance of the configuration class within itself, the value is ignored if the @BotConfiguration annotation is on a class that is a descendant of the Bot class, only static functions will be executed

@Feedback annotation

Create any function, add the annotation @Feedback, add any set of parameters available for this function In the annotation, specify a string indicating the type of updates that it can process

@BotConfiguration(botToken = "your token")
public class MyConfig {

    @Feedback("update")
    public void onUpdate() {
        // parse your update here
    }

    @Feedback("message")
    private static void message(Bot bot) {
        // also parse messages here
    }

    @Feedback("start")
    private void runBot(Message message, Bot bot) {
        // code here
    }

}

Parameter sets for update processing functions Constants.java

type parameter set(classes)
update com.pengrad.telegrambot.model.Update, org.fbs.mcb.form.Bot
start com.pengrad.telegrambot.model.Update, com.pengrad.telegrambot.model.Message, org.fbs.mcb.form.Bot
message com.pengrad.telegrambot.model.Update, com.pengrad.telegrambot.model.Message, org.fbs.mcb.form.Bot
entities com.pengrad.telegrambot.model.Update, com.pengrad.telegrambot.model.Message, com.pengrad.telegrambot.model.MessageEntity[], org.fbs.mcb.form.Bot
callback_query com.pengrad.telegrambot.model.Update, com.pengrad.telegrambot.model.CallbackQuery, org.fbs.mcb.form.Bot
inline_query com.pengrad.telegrambot.model.Update, com.pengrad.telegrambot.model.InlineQuery, org.fbs.mcb.form.Bot
command com.pengrad.telegrambot.model.Update, com.pengrad.telegrambot.model.Message, com.pengrad.telegrambot.model.MessageEntity[], org.fbs.mcb.form.Bot

@Command annotation

Create any function, add the annotation @Command, inside specify a line pointing to the command that is bound to this function

@BotConfiguration(botToken = "your token")
public class MyConfig {

    @Command("/help")
    private void help(Bot bot) {
        // realize algorithm here
    }

    @Command("/exit")
    public static void save(Message message){
        // saving logic...
    }

}

Command functions have the same parameter sets as @Feedback("entities"), but without MessageEntities array

Launch(for both implementations)

public class Main{
    public static void main(String[] args) {
        new MyBot();
    }
}

Priorities

  • If the bot token is specified via the constructor, but a configuration containing the token is also added, the bot will have the token specified via the constructor

Todo

  • Add a buffer of processed methods for faster response to updates
  • Implementation of multi-botUser functionality
  • Possibility of implementing third-party Telegram Bot API
  • Increase the autonomy of each element

Thanks

About

A base for writing a telegram bot with a standard implementation or with the ability to interact with a large number of users in multi-threaded mode.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages