All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.butkoprojects.bots.util.TelegramBot Maven / Gradle / Ivy

package io.github.butkoprojects.bots.util;

import io.github.butkoprojects.bots.handler.BotRequestHandler;
import io.github.butkoprojects.bots.handler.processor.BotMethodProcessor;
import io.github.butkoprojects.bots.preprocess.controller.BotApiMethodController;
import org.springframework.beans.factory.annotation.Autowired;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.objects.Update;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public abstract class TelegramBot extends TelegramLongPollingBot {

    public Map tmpStorage = new HashMap<>();

    @Autowired
    private List processors;

    @Autowired
    private BotRequestHandler handler;

    private final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

    @Override
    public void onUpdateReceived( Update update ) {
        handler.determineController( update ).process( update )
                .subscribe( method -> {
                    if ( method == null ) {
                        updateReceived( update );
                    } else {
                        executeWithoutCheckedException( method );
                    }
                } );
    }

    private void executeWithoutCheckedException( final Object method ) {
        processors.stream()
                .filter( processor -> processor.instanceOf( method ) )
                .findFirst()
                .ifPresent( processor -> processor.processMethod( method, this ) );
    }

    public void updateReceived( Update update ) {
        System.out.println( "Override updateReceived method for your telegram bot" );
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy