Skip to content

FinWave-App/Telegram-Abstractions-Tools

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Telegram Abstractions Tools

Telegram Abstractions Tools (TAT) is a Java library designed to simplify the creation of Telegram bots by providing an abstraction layer. Instead of dealing directly with requests and responses, developers work with handlers that manage the bot's context. These handlers include chat handlers, user handlers, and global handlers which make bot development more intuitive and organized.

Powered by Java Telegram Bot API

In The Context Of The FinWave Project:

This library is used in the FinWave Telegram bot, as well as in some other projects. While not all Telegram API features are implemented, only those that are necessary are included.

Features

  • AbstractChatHandler: For managing chat interactions.
  • BaseMenu: Simplifies button handling in messages by allowing lambda functions.
  • CommandAbstractChatHandler: Handles text commands from chat messages.
  • ScenedAbstractChatHandler: Supports multiple scenes within a chat.
  • Event Handling: A robust event handling system to manage various types of events such as messages, callback queries, polls, and more.

Install

Gradle

repositories {
    mavenCentral()
    maven {
        url "https://nexus.finwave.app/repository/maven-public"
    }
}

dependencies {
    implementation 'app.finwave.tat:telegram-abstractions-tools:2.0.3'
}

Some Examples

Setting up the BotCore

import app.finwave.tat.BotCore;
import handlers.app.finwave.tat.AbstractChatHandler;
import handlers.app.finwave.tat.AbstractGlobalHandler;
import handlers.app.finwave.tat.AbstractUserHandler;

public class MyBot {
    public static void main(String[] args) {
        String token = "YOUR_BOT_TOKEN";
        
        botCore = new BotCore(token);
        botCore.setHandlers(new GlobalHandler(botCore), // One for the whole bot
                chatId -> new MyChatHandler(botCore, chatId), // For each user
                userId -> new UserHandler(botCore, userId)); // For each chat
    }
}

Creating a Chat Handler

import handlers.app.finwave.tat.AbstractChatHandler;
import app.finwave.tat.BotCore;
import chat.event.app.finwave.tat.NewMessageEvent;

public class MyChatHandler extends AbstractChatHandler {
    public MyChatHandler(BotCore core, long chatId) {
        super(core, chatId);
    }

    @Override
    public void start() {
        eventHandler.registerListener(NewMessageEvent.class, this::onNewMessage);
    }

    private void onNewMessage(NewMessageEvent event) {
        System.out.println("New message received: " + event.data.text());
    }
}

Using Command Handler

import command.handlers.app.finwave.tat.CommandAbstractChatHandler;
import app.finwave.tat.BotCore;
import chat.event.app.finwave.tat.NewMessageEvent;
import command.handlers.app.finwave.tat.AbstractCommand;

public class MyCommandHandler extends CommandAbstractChatHandler {
    public MyCommandHandler(BotCore core, long chatId) {
        super(core, chatId);
        registerCommand(AbstractCommand.command("start", this::startCommand));
    }

    private void startCommand(String[] args, NewMessageEvent event) {
        sendMessage(MessageBuilder.text("Welcome!")).thenAccept(response -> {
            System.out.println("Sent welcome message");
        });
    }
}

Creating a Menu

import menu.app.finwave.tat.BaseMenu;
import scene.app.finwave.tat.BaseScene;
import app.finwave.tat.BotCore;
import com.pengrad.telegrambot.model.request.InlineKeyboardButton;
import utils.app.finwave.tat.MessageBuilder;

public class MyMenu extends BaseMenu {
    public MyMenu(BaseScene<?> scene) {
        super(scene);
        setMessage(MessageBuilder.text("Choose an option:"));
        addButton(new InlineKeyboardButton("Option 1"), this::onOption1);
        addButton(new InlineKeyboardButton("Option 2"), this::onOption2);
    }

    private void onOption1(CallbackQueryEvent event) {
        // Handle option 1 action
    }

    private void onOption2(CallbackQueryEvent event) {
        // Handle option 2 action
    }
}

Defining a Scene

import scene.app.finwave.tat.BaseScene;
import handlers.app.finwave.tat.AbstractChatHandler;

public class MyScene extends BaseScene<String> {
    private MyMenu menu;

    public MyScene(AbstractChatHandler abstractChatHandler) {
        super(abstractChatHandler);
        menu = new MyMenu(this);
    }

    @Override
    public void start() {
        super.start();
        menu.apply();
    }

    @Override
    public void start(String arg) {
        super.start(arg);
        menu.apply();
    }
}

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.

About

Java library for Telegram bots development

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages