From a229ce9add0cd318dc8b6cef5e703ddaee808021 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Mon, 31 Jan 2022 15:00:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BB=BA=E7=AB=8B=20InlineQuery=20=E6=8A=BD?= =?UTF-8?q?=E8=B1=A1=E5=B1=82=E4=B8=8E=E7=AE=A1=E7=90=86=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +-- .../cc/sukazyo/cono/morny/MornyCoeur.java | 7 +++++ .../cono/morny/bot/event/EventListeners.java | 2 +- ...nInlineQuery.java => OnInlineQueries.java} | 22 +++++++-------- .../cono/morny/bot/query/ITelegramQuery.java | 10 +++++++ .../cono/morny/bot/query/MornyQueries.java | 28 +++++++++++++++++++ .../sukazyo/cono/morny/bot/query/RawText.java | 21 ++++++++++++++ 8 files changed, 80 insertions(+), 16 deletions(-) rename src/main/java/cc/sukazyo/cono/morny/bot/event/{OnInlineQuery.java => OnInlineQueries.java} (50%) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/query/ITelegramQuery.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/query/MornyQueries.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java diff --git a/gradle.properties b/gradle.properties index c5f6335..d72cee5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.5.0.1 +VERSION = 0.5.0.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 0ce77cb..4e2f5c3 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.5.0.1"; - public static final long COMPILE_TIMESTAMP = 1643447337882L; + public static final String VERSION = "0.5.0.2"; + public static final long COMPILE_TIMESTAMP = 1643612350660L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index 50b0ef2..321b14c 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -3,6 +3,7 @@ package cc.sukazyo.cono.morny; import cc.sukazyo.cono.morny.bot.api.OnUpdate; import cc.sukazyo.cono.morny.bot.command.MornyCommands; import cc.sukazyo.cono.morny.bot.event.EventListeners; +import cc.sukazyo.cono.morny.bot.query.MornyQueries; import cc.sukazyo.cono.morny.data.tracker.TrackerDataManager; import cc.sukazyo.untitled.telegram.api.extra.ExtraAction; @@ -28,6 +29,7 @@ public class MornyCoeur { private final MornyTrusted trusted; /** 当前 Morny 的 telegram 命令管理器 */ private final MornyCommands commandManager = new MornyCommands(); + private final MornyQueries queryManager = new MornyQueries(); /** morny 的 bot 账户 */ private final TelegramBot account; @@ -255,6 +257,11 @@ public class MornyCoeur { return INSTANCE.commandManager; } + @Nonnull + public static MornyQueries queryManager () { + return INSTANCE.queryManager; + } + @Nonnull public static ExtraAction extra () { return INSTANCE.extraActionInstance; diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java index 6d1d64d..3920a57 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/EventListeners.java @@ -8,7 +8,7 @@ public class EventListeners { public static final OnActivityRecord ACTIVITY_RECORDER = new OnActivityRecord(); public static final OnUserSlashAction USER_SLASH_ACTION = new OnUserSlashAction(); public static final OnUpdateTimestampOffsetLock UPDATE_TIMESTAMP_OFFSET_LOCK = new OnUpdateTimestampOffsetLock(); - public static final OnInlineQuery INLINE_QUERY = new OnInlineQuery(); + public static final OnInlineQueries INLINE_QUERY = new OnInlineQueries(); public static final OnCallMe CALL_ME = new OnCallMe(); public static final OnEventHackHandle EVENT_HACK_HANDLE = new OnEventHackHandle(); diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQueries.java similarity index 50% rename from src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java rename to src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQueries.java index 5a64be5..4e113c8 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQuery.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQueries.java @@ -2,14 +2,12 @@ package cc.sukazyo.cono.morny.bot.event; import cc.sukazyo.cono.morny.MornyCoeur; import cc.sukazyo.cono.morny.bot.api.EventListener; -import cc.sukazyo.cono.morny.util.EncryptUtils; import com.pengrad.telegrambot.model.Update; -import com.pengrad.telegrambot.model.request.InlineQueryResultArticle; -import com.pengrad.telegrambot.model.request.InputTextMessageContent; -import com.pengrad.telegrambot.model.request.ParseMode; +import com.pengrad.telegrambot.model.request.InlineQueryResult; import com.pengrad.telegrambot.request.AnswerInlineQuery; import javax.annotation.Nonnull; +import java.util.List; /** * telegram inlineQuery 功能的处理类, @@ -17,21 +15,21 @@ import javax.annotation.Nonnull; * * @since 0.4.1.3 */ -public class OnInlineQuery extends EventListener { +public class OnInlineQueries extends EventListener { /** * @since 0.4.1.3 */ @Override public boolean onInlineQuery (@Nonnull Update update) { - MornyCoeur.extra().exec(new AnswerInlineQuery(update.inlineQuery().id(), new InlineQueryResultArticle[]{ - new InlineQueryResultArticle( - EncryptUtils.encryptByMD5(update.inlineQuery().query()), - "Raw Input", - new InputTextMessageContent(update.inlineQuery().query()).parseMode(ParseMode.MarkdownV2) - ) - })); + + List> results = MornyCoeur.queryManager().query(update); + + if (results.size() == 0) return false; + + MornyCoeur.extra().exec(new AnswerInlineQuery(update.inlineQuery().id(), results.toArray(InlineQueryResult[]::new))); return true; + } } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/query/ITelegramQuery.java b/src/main/java/cc/sukazyo/cono/morny/bot/query/ITelegramQuery.java new file mode 100644 index 0000000..76de728 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/ITelegramQuery.java @@ -0,0 +1,10 @@ +package cc.sukazyo.cono.morny.bot.query; + +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.InlineQueryResult; + +public interface ITelegramQuery > { + + T query (Update event); + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/query/MornyQueries.java b/src/main/java/cc/sukazyo/cono/morny/bot/query/MornyQueries.java new file mode 100644 index 0000000..4f5de65 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/MornyQueries.java @@ -0,0 +1,28 @@ +package cc.sukazyo.cono.morny.bot.query; + +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.InlineQueryResult; + +import javax.annotation.Nonnull; +import java.util.ArrayList; +import java.util.List; + +public class MornyQueries { + + private final List> queryInstances = new ArrayList<>(); + + public MornyQueries () { + queryInstances.add(new RawText()); + } + + @Nonnull + public List> query (@Nonnull Update event) { + final List> results = new ArrayList<>(); + for (ITelegramQuery instance : queryInstances) { + final InlineQueryResult r = instance.query(event); + if (r!=null) results.add(r); + } + return results; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java b/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java new file mode 100644 index 0000000..4be01d0 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java @@ -0,0 +1,21 @@ +package cc.sukazyo.cono.morny.bot.query; + +import cc.sukazyo.cono.morny.util.EncryptUtils; +import com.pengrad.telegrambot.model.Update; +import com.pengrad.telegrambot.model.request.InlineQueryResultArticle; +import com.pengrad.telegrambot.model.request.InputTextMessageContent; + +public class RawText implements ITelegramQuery { + + public static final String TITLE = "Raw Text"; + + @Override + public InlineQueryResultArticle query (Update event) { + return new InlineQueryResultArticle( + "[morny/r/txt]" + EncryptUtils.encryptByMD5(event.inlineQuery().query()), + TITLE, + new InputTextMessageContent(event.inlineQuery().query()) + ); + } + +}