-
-
Notifications
You must be signed in to change notification settings - Fork 25
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
BlackBaroness
wants to merge
3
commits into
Rollczi:master
Choose a base branch
from
BlackBaroness:feat/telegrambots
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
17 changes: 17 additions & 0 deletions
17
...mands-telegrambots/src/dev/rollczi/litecommands/telegrambots/LiteTelegramBotsFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...ands-telegrambots/src/dev/rollczi/litecommands/telegrambots/LiteTelegramBotsSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
litecommands-telegrambots/src/dev/rollczi/litecommands/telegrambots/TelegramBotsCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
76 changes: 76 additions & 0 deletions
76
...commands-telegrambots/src/dev/rollczi/litecommands/telegrambots/TelegramBotsPlatform.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
litecommands-telegrambots/src/dev/rollczi/litecommands/telegrambots/TelegramBotsSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 😮
There was a problem hiding this comment.
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