Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TelegramBots platform #523

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ If you don’t update, **permissions will default to OR behavior** in the new ve
[![Java](https://raw.githubusercontent.com/intergrav/devins-badges/v3/assets/cozy/built-with/java8_vector.svg)](https://www.java.com/)

### LiteCommands
Annotation based command framework for Velocity, Bukkit, Paper, BungeeCord, Minestom, Sponge, Fabric, JDA and future implementations.
Annotation based command framework for Velocity, Bukkit, Paper, BungeeCord, Minestom, Sponge, Fabric, JDA, TelegramBots and future implementations.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably make it a bullet list now 😮

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a bullet list below already, I guess we shouldn't make a second one


</div>

Expand Down Expand Up @@ -68,6 +68,7 @@ Annotation based command framework for Velocity, Bukkit, Paper, BungeeCord, Mine
- Sponge _(by [BlackBaroness](https://github.com/BlackBaroness))_
- Fabric _(by [huanmeng_qwq](https://github.com/huanmeng-qwq))_
- JDA
- TelegramBots _(by [BlackBaroness](https://github.com/BlackBaroness))_

## 💡 Command Example
This is an example of `/hello <name> <amount>` command:
Expand Down
3 changes: 3 additions & 0 deletions buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,7 @@ object Versions {
const val HIBERNATE_VALIDATOR = "8.0.2.Final"
const val EXPRESSLY = "5.0.0"

// TelegramBots
const val TELEGRAM_BOTS = "8.2.0"

}
16 changes: 16 additions & 0 deletions litecommands-telegrambots/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
plugins {
`litecommands-java`
`litecommands-java-17`
`litecommands-repositories`
`litecommands-publish`
}

dependencies {
api(project(":litecommands-framework"))

compileOnly("org.telegram:telegrambots-meta:${Versions.TELEGRAM_BOTS}")
}

litecommandsPublish {
artifactId = "litecommands-telegrambots"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package dev.rollczi.litecommands.telegrambots;

import dev.rollczi.litecommands.LiteCommandsBuilder;
import dev.rollczi.litecommands.LiteCommandsFactory;
import org.telegram.telegrambots.meta.api.objects.User;

public final class LiteTelegramBotsFactory {

private LiteTelegramBotsFactory() {
}

@SuppressWarnings("unchecked")
public static <B extends LiteCommandsBuilder<User, LiteTelegramBotsSettings, B>> B builder(LiteTelegramBotsSettings liteTelegramBotsSettings) {
return (B) LiteCommandsFactory.builder(User.class, new TelegramBotsPlatform(liteTelegramBotsSettings));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package dev.rollczi.litecommands.telegrambots;

import dev.rollczi.litecommands.platform.PlatformSettings;
import org.apache.commons.lang3.NotImplementedException;
import org.telegram.telegrambots.meta.api.objects.User;

import java.util.function.BiPredicate;

public class LiteTelegramBotsSettings implements PlatformSettings {

private BiPredicate<User, String> permissionChecker = (user, permission) -> {
throw new NotImplementedException("Override permissionChecker in LiteTelegramBotsSettings if you would like to use permissions");
};

public BiPredicate<User, String> getPermissionChecker() {
return permissionChecker;
}

public void setPermissionChecker(BiPredicate<User, String> permissionChecker) {
this.permissionChecker = permissionChecker;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.rollczi.litecommands.telegrambots;

import dev.rollczi.litecommands.command.CommandRoute;
import dev.rollczi.litecommands.platform.PlatformInvocationListener;
import dev.rollczi.litecommands.platform.PlatformSuggestionListener;
import org.telegram.telegrambots.meta.api.objects.User;

record TelegramBotsCommand(
CommandRoute<User> commandRoute,
PlatformInvocationListener<User> invocationHook,
PlatformSuggestionListener<User> suggestionHook
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package dev.rollczi.litecommands.telegrambots;

import dev.rollczi.litecommands.argument.parser.input.ParseableInput;
import dev.rollczi.litecommands.command.CommandRoute;
import dev.rollczi.litecommands.invocation.Invocation;
import dev.rollczi.litecommands.platform.AbstractSimplePlatform;
import dev.rollczi.litecommands.platform.PlatformInvocationListener;
import dev.rollczi.litecommands.platform.PlatformSuggestionListener;
import dev.rollczi.litecommands.shared.Preconditions;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.api.objects.message.Message;

import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

class TelegramBotsPlatform extends AbstractSimplePlatform<User, LiteTelegramBotsSettings> {

private final Map<String, TelegramBotsCommand> commands = new ConcurrentHashMap<>();

public TelegramBotsPlatform(LiteTelegramBotsSettings liteTelegramBotsSettings) {
super(liteTelegramBotsSettings, sender -> new TelegramBotsSender(sender, liteTelegramBotsSettings));
}

@Override
protected void hook(CommandRoute<User> commandRoute, PlatformInvocationListener<User> invocationHook, PlatformSuggestionListener<User> suggestionHook) {
TelegramBotsCommand command = new TelegramBotsCommand(commandRoute, invocationHook, suggestionHook);
for (String label : commandRoute.names()) {
commands.put(label, command);
}
}

@Override
protected void unhook(CommandRoute<User> commandRoute) {
for (String label : commandRoute.names()) {
commands.remove(label);
}
}

/**
* @return true if input was handled (it was a command), false otherwise (it was a message or other content)
*/
public boolean handleUpdate(Update update) {
Preconditions.notNull(update, "update");

if (!update.hasMessage()) return false;
Message message = update.getMessage();

if (!message.hasText()) return false;
String text = message.getText();
if (text.isEmpty()) return false;

if (text.charAt(0) != '/') return false;

String label;
ParseableInput<?> input;

int firstIndexOfSpace = text.indexOf(' ');
if (firstIndexOfSpace == -1) {
label = text.substring(1);
input = ParseableInput.raw(Collections.emptyList());
} else {
label = text.substring(1, firstIndexOfSpace + 1);
input = ParseableInput.raw(text.substring(firstIndexOfSpace + 1).split(" "));
}

TelegramBotsCommand command = commands.get(label);
if (command == null) return false; // todo: do something instead?

User sender = message.getFrom();
Invocation<User> invocation = new Invocation<>(sender, new TelegramBotsSender(sender, settings), command.commandRoute().getName(), label, input);
command.invocationHook().execute(invocation, input);
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.rollczi.litecommands.telegrambots;

import dev.rollczi.litecommands.identifier.Identifier;
import dev.rollczi.litecommands.platform.AbstractPlatformSender;
import org.telegram.telegrambots.meta.api.objects.User;

class TelegramBotsSender extends AbstractPlatformSender {

private final User handle;
private final LiteTelegramBotsSettings liteTelegramBotsSettings;

public TelegramBotsSender(User handle, LiteTelegramBotsSettings liteTelegramBotsSettings) {
this.handle = handle;
this.liteTelegramBotsSettings = liteTelegramBotsSettings;
}

@Override
public String getName() {
return this.handle.getUserName();
}

@Override
public String getDisplayName() {
return this.handle.getFirstName() + " " + this.handle.getLastName();
}

@Override
public Identifier getIdentifier() {
return Identifier.of(handle.getId());
}

@Override
public boolean hasPermission(String permission) {
return this.liteTelegramBotsSettings.getPermissionChecker().test(handle, permission);
}

}
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ include(":litecommands-minestom", VERSION_21)
include("litecommands-jda", VERSION_11)
include(":litecommands-sponge", VERSION_21, tests = false)
include(":litecommands-fabric", VERSION_17, tests = false)
include(":litecommands-telegrambots", VERSION_17, tests = false)

// examples
include(":examples:bukkit", tests = false)
Expand Down
Loading