diff --git a/gradle.properties b/gradle.properties index 5c06ab4..3c3e879 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.6.2.0 +VERSION = 0.6.3.0 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 05db217..da23614 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.6.2.0"; - public static final long COMPILE_TIMESTAMP = 1647533461453L; + public static final String VERSION = "0.6.3.0"; + public static final long COMPILE_TIMESTAMP = 1651635619528L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/api/InlineQueryUnit.java b/src/main/java/cc/sukazyo/cono/morny/bot/api/InlineQueryUnit.java new file mode 100644 index 0000000..43f0376 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/api/InlineQueryUnit.java @@ -0,0 +1,36 @@ +package cc.sukazyo.cono.morny.bot.api; + +import com.pengrad.telegrambot.model.request.InlineQueryResult; + +public class InlineQueryUnit> { + + public static final int DEFAULT_INLINE_CACHE_TIME = 300; + public static final boolean DEFAULT_INLINE_PERSONAL_RESP = false; + + private int cacheTime = DEFAULT_INLINE_CACHE_TIME; + private boolean isPersonal = DEFAULT_INLINE_PERSONAL_RESP; + public final T result; + + public InlineQueryUnit (T result) { + this.result = result; + } + + public int cacheTime () { + return cacheTime; + } + + public InlineQueryUnit cacheTime (int cacheTime) { + this.cacheTime = cacheTime; + return this; + } + + public boolean isPersonal () { + return isPersonal; + } + + public InlineQueryUnit isPersonal (boolean isPersonal) { + this.isPersonal = isPersonal; + return this; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQueries.java b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQueries.java index 4e113c8..ed06f0b 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQueries.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/event/OnInlineQueries.java @@ -2,6 +2,7 @@ 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.bot.api.InlineQueryUnit; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.InlineQueryResult; import com.pengrad.telegrambot.request.AnswerInlineQuery; @@ -23,11 +24,22 @@ public class OnInlineQueries extends EventListener { @Override public boolean onInlineQuery (@Nonnull Update update) { - List> results = MornyCoeur.queryManager().query(update); + List> results = MornyCoeur.queryManager().query(update); + + int cacheTime = Integer.MAX_VALUE; + boolean isPersonal = InlineQueryUnit.DEFAULT_INLINE_PERSONAL_RESP; + InlineQueryResult[] inlineQueryResults = new InlineQueryResult[results.size()]; + for (int i = 0; i < results.size(); i++) { + inlineQueryResults[i] = results.get(i).result; + if (cacheTime > results.get(i).cacheTime()) cacheTime = results.get(i).cacheTime(); + if (results.get(i).isPersonal()) isPersonal = true; + } if (results.size() == 0) return false; - MornyCoeur.extra().exec(new AnswerInlineQuery(update.inlineQuery().id(), results.toArray(InlineQueryResult[]::new))); + MornyCoeur.extra().exec(new AnswerInlineQuery( + update.inlineQuery().id(), inlineQueryResults + ).cacheTime(cacheTime).isPersonal(isPersonal)); 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 index 6f1d0da..b0fa978 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/query/ITelegramQuery.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/ITelegramQuery.java @@ -2,12 +2,13 @@ package cc.sukazyo.cono.morny.bot.query; import javax.annotation.Nullable; +import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.InlineQueryResult; public interface ITelegramQuery > { @Nullable - T query (Update event); + InlineQueryUnit 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 index 8bbbd30..a10f00d 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/query/MornyQueries.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/MornyQueries.java @@ -1,7 +1,7 @@ package cc.sukazyo.cono.morny.bot.query; +import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit; import com.pengrad.telegrambot.model.Update; -import com.pengrad.telegrambot.model.request.InlineQueryResult; import javax.annotation.Nonnull; import java.util.ArrayList; @@ -17,10 +17,10 @@ public class MornyQueries { } @Nonnull - public List> query (@Nonnull Update event) { - final List> results = new ArrayList<>(); + public List> query (@Nonnull Update event) { + final List> results = new ArrayList<>(); for (ITelegramQuery instance : queryInstances) { - final InlineQueryResult r = instance.query(event); + final InlineQueryUnit r = instance.query(event); if (r!=null) results.add(r); } return results; diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/query/MyInformation.java b/src/main/java/cc/sukazyo/cono/morny/bot/query/MyInformation.java index 0140b6f..7d5dcb1 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/query/MyInformation.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/MyInformation.java @@ -2,6 +2,7 @@ package cc.sukazyo.cono.morny.bot.query; import javax.annotation.Nullable; +import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.InlineQueryResultArticle; import com.pengrad.telegrambot.model.request.InputTextMessageContent; @@ -16,11 +17,14 @@ public class MyInformation implements ITelegramQuery { @Override @Nullable - public InlineQueryResultArticle query(Update event) { + public InlineQueryUnit query(Update event) { if (!(event.inlineQuery().query() == null || "".equals(event.inlineQuery().query()))) return null; - return new InlineQueryResultArticle(ID_PREFIX, TITLE, new InputTextMessageContent( - TelegramUserInformation.informationOutputHTML(event.inlineQuery().from()) - ).parseMode(ParseMode.HTML)); + return new InlineQueryUnit<>(new InlineQueryResultArticle( + ID_PREFIX, TITLE, + new InputTextMessageContent( + TelegramUserInformation.informationOutputHTML(event.inlineQuery().from()) + ).parseMode(ParseMode.HTML) + )).isPersonal(true).cacheTime(10); } } 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 index 2e3cceb..91333ed 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/RawText.java @@ -1,5 +1,6 @@ package cc.sukazyo.cono.morny.bot.query; +import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit; import cc.sukazyo.cono.morny.util.EncryptUtils; import javax.annotation.Nullable; @@ -15,13 +16,13 @@ public class RawText implements ITelegramQuery { @Override @Nullable - public InlineQueryResultArticle query (Update event) { + public InlineQueryUnit query (Update event) { if (event.inlineQuery().query() == null || "".equals(event.inlineQuery().query())) return null; - return new InlineQueryResultArticle( + return new InlineQueryUnit<>(new InlineQueryResultArticle( ID_PREFIX + EncryptUtils.encryptByMD5(event.inlineQuery().query()), TITLE, new InputTextMessageContent(event.inlineQuery().query()) - ); + )); } }