Skip to content

Commit

Permalink
Add more commands
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON committed Mar 22, 2021
1 parent e2fa3bd commit 2b83e6a
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/main/java/com/example/bot/TestBotHandler.java
@@ -1,13 +1,13 @@
package com.example.bot;

import com.annimon.tgbotsmodule.BotHandler;
import com.annimon.tgbotsmodule.api.methods.Methods;
import com.annimon.tgbotsmodule.commands.CommandRegistry;
import com.annimon.tgbotsmodule.commands.SimpleCommand;
import com.annimon.tgbotsmodule.commands.authority.For;
import com.annimon.tgbotsmodule.commands.authority.SimpleAuthority;
import com.annimon.tgbotsmodule.services.YamlConfigLoaderService;
import java.util.Locale;
import com.example.bot.commands.English2Kana;
import com.example.bot.commands.YouTubeThumbnail;
import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.objects.Update;
Expand All @@ -24,9 +24,17 @@ public TestBotHandler() {
final var authority = new SimpleAuthority(this, botConfig.getAdminId());
commands = new CommandRegistry<>(this, authority);
commands.register(new SimpleCommand("/start", ctx -> {
ctx.reply("Hi, " + ctx.user().getFirstName())
ctx.reply("Hi, " + ctx.user().getFirstName() + "\n" +
"This bot is an example for tgbots-module library.\n" +
"https://github.com/aNNiMON/tgbots-module/\n\n" +
"Available commands:\n" +
" - /kana word — convert English word to Katakana\n" +
"\nAlso, you can send me a link to YouTube video and I'll send you a video thumbnail as a photo.")
.disableWebPagePreview()
.callAsync(ctx.sender);
}));
commands.register(new YouTubeThumbnail());
commands.register(new English2Kana());
}

@Override
Expand All @@ -35,11 +43,12 @@ protected BotApiMethod<?> onUpdate(@NotNull Update update) {
return null;
}

final var msg = update.getMessage();
if (msg != null && msg.hasText()) {
Methods.sendMessage(msg.getChatId(), msg.getText().toUpperCase(Locale.ROOT))
.callAsync(this);
}
// Process other messages
//final var msg = update.getMessage();
//if (msg != null && msg.hasText()) {
// Methods.sendMessage(msg.getChatId(), msg.getText().toUpperCase(Locale.ROOT))
// .callAsync(this);
//}
return null;
}

Expand Down
81 changes: 81 additions & 0 deletions src/main/java/com/example/bot/commands/English2Kana.java
@@ -0,0 +1,81 @@
package com.example.bot.commands;

import com.annimon.tgbotsmodule.commands.RegexCommand;
import com.annimon.tgbotsmodule.commands.authority.For;
import com.annimon.tgbotsmodule.commands.context.RegexMessageContext;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.EnumSet;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jetbrains.annotations.NotNull;

public class English2Kana implements RegexCommand {

private final OkHttpClient client = new OkHttpClient();
private final ObjectMapper mapper = new ObjectMapper();

@Override
public Pattern pattern() {
return Pattern.compile("/kana ([a-zA-Z']{1,30})");
}

@SuppressWarnings("unchecked")
@Override
public EnumSet<For> authority() {
return For.all();
}

@Override
public void accept(@NotNull RegexMessageContext ctx) {
final var word = URLEncoder.encode(ctx.group(1), StandardCharsets.UTF_8);
final var request = new Request.Builder()
.header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:86.0) Gecko/20100101 Firefox/86.0")
.url("https://www.sljfaq.org/cgi/e2k.cgi?o=json&lang=en&word=" + word)
.build();
client.newCall(request)
.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
ctx.replyToMessage("Unable to precess this query").callAsync(ctx.sender);
}

@Override
public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
try (final var body = response.body()) {
if (body == null) return;
var kanaResp = mapper.readValue(body.string(), Kana.class);

var text = kanaResp.words.stream()
.map(word -> word.j_pron_spell)
.collect(Collectors.joining("・"));
if (kanaResp.romaji2kana != null) {
text += "\nThis looks like it might be romanized Japanese:\n" + kanaResp.romaji2kana;
}
ctx.replyToMessage(text).callAsync(ctx.sender);
}
}
});
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Kana {
public List<Word> words;
public String romaji2kana;
}

@JsonIgnoreProperties(ignoreUnknown = true)
public static class Word {
public String type;
public String j_pron_spell;
}
}
32 changes: 32 additions & 0 deletions src/main/java/com/example/bot/commands/YouTubeThumbnail.java
@@ -0,0 +1,32 @@
package com.example.bot.commands;

import com.annimon.tgbotsmodule.commands.RegexCommand;
import com.annimon.tgbotsmodule.commands.authority.For;
import com.annimon.tgbotsmodule.commands.context.RegexMessageContext;
import java.util.EnumSet;
import java.util.regex.Pattern;
import org.jetbrains.annotations.NotNull;
import org.telegram.telegrambots.meta.api.objects.InputFile;

public class YouTubeThumbnail implements RegexCommand {

@Override
public Pattern pattern() {
return Pattern.compile("https?://(?:www\\.)?youtu(?:\\.be/|be.com/watch\\?v=)([^#&?\\s]+)");
}

@SuppressWarnings("unchecked")
@Override
public EnumSet<For> authority() {
return For.all();
}

@Override
public void accept(@NotNull RegexMessageContext ctx) {
var url = "https://img.youtube.com/vi/" + ctx.group(1) + "/hqdefault.jpg";
ctx.replyWithPhoto()
.setFile(new InputFile(url))
.setCaption(url)
.callAsync(ctx.sender);
}
}

0 comments on commit 2b83e6a

Please sign in to comment.