ArtemGet / teleroute.telegrambots

Teleroute-TelegramBots plug-in

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Maintainability

Teleroute - TelegramBots integration plug-in

Check teleroute repository

Check TelegramBots repository

Supported features:

1)Long polling

2)Sending all ancestors of BotApiMethod, e.g: SendMessage, SendPoll, SendContact...

3)Sending stickers via SendSticker

Getting started:

This library is distributed via Github Packages (actual) so you have to include this setting in your ~/.m2

<repository>
    <id>github</id>
    <name>Github ArtemGet Apache Maven Packages</name>
    <url>https://maven.pkg.github.com/ArtemGet/teleroute.telegrambots</url>
</repository>

And add dependency to your pom.xml

<dependency>
    <groupId>com.github.ArtemGet</groupId>
    <artifactId>teleroute.telegrambots</artifactId>
    <version>0.2.1</version>
</dependency>

Usage example:

Imagine our goal is to write long polling telegram bot that greet newcomers or greet already existing users by command - /greetmeplease.

Check Example source code (TODO)

Lets start by describing our bot class:

public class Bot extends TelegramLongPollingBot {
    private final String name;
    private final Route<Update, AbsSender> route;

    public Bot(String botToken, String name, Route<Update, AbsSender> route) {
        super(botToken);
        this.name = name;
        this.route = route;
    }

    @Override
    public void onUpdateReceived(Update update) {
        this.route.route(new TelegramBotsUpdWrap(update))
                .flatMap(command -> command.execute(update))
                .ifPresent(send -> send.send(this));
    }

    @Override
    public String getBotUsername() {
        return this.name;
    }
}

As you can see, in order to route update to suitable response we attached Route<Update, AbsSender> route to our bot, that means we could route all TelegramBots Update classes to commands, execute them and send response via AbsSender (for e.g TelegramLongPollingBot implements AbsSender, so you could pass just this to send method).

The possible Route configuration could be similar to:

public class Example {
    public static void main(String[] args) throws TelegramApiException {
        Users users = new PgUsers("your_db_connection");
        new TelegramBotsApi(DefaultBotSession.class)
                .registerBot(
                        new Bot(
                                "your_token",
                                "your_bot_name",
                                new DfsRoute<>(
                                        new ForkRoute<>(
                                                new FirstLoginMatch(users),
                                                new NewGreetCommand(users)
                                        ),
                                        new ForkRoute<>(
                                                new AllMatch<>(
                                                        new CmdMatch<>(),
                                                        new FullTxtMatch<>("/greetmeplease")
                                                ),
                                                new OldGreetCommand(users)
                                        )
                                )
                        )
                );
    }
}

Where match condition implements like:

public class FirstLoginMatch implements Match<Update> {
    private final Users users;

    public FirstLoginMatch(Users users) {
        this.users = users;
    }

    @Override
    public Boolean match(UpdWrap<Update> wrap) {
        return this.users.get(
                        wrap
                                .src()
                                .getMessage()
                                .getFrom()
                                .getId()
                )
                .isEmpty();
    }
}

Newcomers greet command:

public class NewGreetCommand implements Cmd<Update, AbsSender> {
private final Users users;

    public NewGreetCommand(Users users) {
        this.users = users;
    }

    @Override
    public Optional<Send<AbsSender>> execute(Update update) {
        return Optional.of(
                new SendMessageWrap<>(
                        new SendMessage(
                                update.getMessage().getFrom().getId().toString(),
                                String.format(
                                        "Greetings! %s!",
                                        this.users.add(
                                                update.getMessage().getFrom().getId(),
                                                update.getMessage().getFrom().getUserName()
                                        ).name()
                                )
                        )
                )
        );
    }
}

Existing users command:

public class OldGreetCommand implements Cmd<Update, AbsSender> {
    private final Users users;

    public OldGreetCommand(Users users) {
        this.users = users;
    }

    @Override
    public Optional<Send<AbsSender>> execute(Update update) {
        return Optional.of(
                new SendMessageWrap<>(
                        new SendMessage(
                                update.getMessage().getFrom().getId().toString(),
                                String.format(
                                        "Welcome back! %s!",
                                        this.users
                                                .get(update.getMessage().getFrom().getId())
                                                .orElse(
                                                        this.users.add(
                                                                update.getMessage().getFrom().getId(),
                                                                update.getMessage().getFrom().getUserName()
                                                        )
                                                )
                                )
                        )
                )
        );
    }
}

And business interfaces:

public interface User {
    Long id();
    String name();
}
public interface Users {
    Optional<User> get(Long id);

    User add(Long id, String name);
}

About

Teleroute-TelegramBots plug-in

License:MIT License


Languages

Language:Java 100.0%