From 82d557df5184b5e11a9f4d393405e79711725d0a Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Fri, 7 Jan 2022 13:07:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20/nbnhhsh=20=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../morny/bot/event/OnCommandExecute.java | 4 ++ .../morny/bot/event/on_commands/Nbnhhsh.java | 52 ++++++++++++++++++ .../sukazyo/cono/morny/data/NbnhhshQuery.java | 53 +++++++++++++++++++ 5 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/data/NbnhhshQuery.java diff --git a/gradle.properties b/gradle.properties index bc97dde..e37ff62 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.4.3.1 +VERSION = 0.4.3.2 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 7481938..9cf6af4 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,6 +4,6 @@ package cc.sukazyo.cono.morny; * the final field that will be updated by gradle automatically. */ public class GradleProjectConfigures { - public static final String VERSION = "0.4.3.1"; - public static final long COMPILE_TIMESTAMP = 1641201595120L; + public static final String VERSION = "0.4.3.2"; + public static final long COMPILE_TIMESTAMP = 1641531965975L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java index 1b16641..38b4ffd 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnCommandExecute.java @@ -7,6 +7,7 @@ import cc.sukazyo.cono.morny.bot.api.EventListener; import cc.sukazyo.cono.morny.bot.event.on_commands.EventHack; import cc.sukazyo.cono.morny.bot.event.on_commands.GetUsernameAndId; import cc.sukazyo.cono.morny.bot.event.on_commands.Ip186Query; +import cc.sukazyo.cono.morny.bot.event.on_commands.Nbnhhsh; import cc.sukazyo.cono.morny.data.MornyJrrp; import cc.sukazyo.cono.morny.data.TelegramStickers; import cc.sukazyo.untitled.util.telegram.object.InputCommand; @@ -67,6 +68,9 @@ public class OnCommandExecute extends EventListener { case "/whois": Ip186Query.exec(event, command); break; + case "/nbnhhsh": + Nbnhhsh.exec(event, command); + break; default: return nonCommandExecutable(event, command); } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java new file mode 100644 index 0000000..af6602f --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/on_commands/Nbnhhsh.java @@ -0,0 +1,52 @@ +package cc.sukazyo.cono.morny.bot.event.on_commands; + +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.request.SendMessage; + +import cc.sukazyo.cono.morny.MornyCoeur; +import cc.sukazyo.cono.morny.data.NbnhhshQuery; +import cc.sukazyo.untitled.util.string.StringArrays; +import cc.sukazyo.untitled.util.telegram.object.InputCommand; + +import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; + +public class Nbnhhsh { + + public static void exec (Update event, InputCommand command) { + + try { + + String queryTarget = ""; + if (event.message().replyToMessage() != null && event.message().replyToMessage().text() != null) + queryTarget = event.message().replyToMessage().text(); + if (command.hasArgs()) + queryTarget = StringArrays.connectStringArray(command.getArgs(), " ", 0, command.getArgs().length-1); + + NbnhhshQuery.GuessResult response = NbnhhshQuery.sendGuess(queryTarget); + + StringBuilder message = new StringBuilder("## Result of nbnhhsh query :"); + + for (NbnhhshQuery.Word word : response.words) { + if (word.trans == null) continue; + message.append("\n\n[[ ").append(escapeHtml(word.name)).append(" ]]"); + for (String trans : word.trans) { + message.append("\n* ").append(escapeHtml(trans)).append(""); + } + } + + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + message.toString() + ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); + + } catch (Exception e) { + MornyCoeur.getAccount().execute(new SendMessage( + event.message().chat().id(), + "[Exception] in query:\n" + escapeHtml(e.getMessage()) + "" + ).parseMode(ParseMode.HTML).replyToMessageId(event.message().messageId())); + } + + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/data/NbnhhshQuery.java b/src/main/java/cc/sukazyo/cono/morny/data/NbnhhshQuery.java new file mode 100644 index 0000000..27c5932 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/data/NbnhhshQuery.java @@ -0,0 +1,53 @@ +package cc.sukazyo.cono.morny.data; + +import java.io.IOException; + +import com.google.gson.Gson; + +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import okhttp3.ResponseBody; + +public class NbnhhshQuery { + + public static class Word { + public String name; + public String[] trans; + } + + public static class GuessResult { + public Word[] words; + } + + public static record GuessReq (String text) { + } + + public static final String API_URL = "https://lab.magiconch.com/api/nbnhhsh/"; + public static final String API_GUESS_METHOD = "guess/"; + public static final String API_GUESS_DATA_TEMPLATE = "{ \"text\": \"%s\" }"; + + private static final OkHttpClient httpClient = new OkHttpClient(); + public static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); + + public static GuessResult sendGuess (String text) throws IOException { + final String reqJsonText = new Gson().toJson(new GuessReq(text)); + Request request = new Request.Builder() + .url(API_URL + API_GUESS_METHOD) + .post(RequestBody.create(JSON, reqJsonText)) + .build(); + try (Response response = httpClient.newCall(request).execute()) { + final ResponseBody body = response.body(); + if (body == null) throw new IOException("Null body."); + final String x = "{ \"words\": " + body.string() + " }"; + return new Gson().fromJson(x, GuessResult.class); + } + } + + public static void main(String[] args) { + System.out.println(new Gson().toJson(new GuessReq("8h28oey8 qe89 aoHO*)I'[ IK\"@+ )EOI)D\"{AIR\")Q @}"))); + } + +}