From 5bb19095c885b06a661435a1012aef5532e0e7f2 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Mon, 17 Oct 2022 17:44:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E6=96=B0=E7=9A=84?= =?UTF-8?q?=E5=86=85=E8=81=94=E6=9F=A5=E8=AF=A2=E5=8A=9F=E8=83=BD=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=B0=86=20twitter=20=E5=88=86=E4=BA=AB=E9=93=BE?= =?UTF-8?q?=E6=8E=A5=E9=87=8D=E6=96=B0=E8=BE=93=E5=87=BA=E4=B8=BA=20vxtwit?= =?UTF-8?q?ter=20=E5=88=86=E4=BA=AB=E9=93=BE=E6=8E=A5=E5=90=8C=E6=97=B6?= =?UTF-8?q?=E5=8E=BB=E8=B7=9F=E8=B8=AA=E5=8F=82=E6=95=B0=EF=BC=8C=E5=B0=86?= =?UTF-8?q?=20ITelegramQuery=20=E6=8A=BD=E8=B1=A1=E5=B1=82=E7=9A=84?= =?UTF-8?q?=E8=A6=81=E6=B1=82=E8=BF=94=E5=9B=9E=E4=BB=8E=E5=8D=95=E4=B8=AA?= =?UTF-8?q?=E9=99=90=E5=AE=9A=E7=B1=BB=E5=88=AB=20queryUnit=20=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=20list>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 通过 tgapi.NamedUtils#inlineIds 规范化了 InlineQuery 的 id 定义 - 将 raw text 的 id 重新定义为规范化行为 - 将 my information 的 id 重新定义为规范化行为 --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cono/morny/bot/query/ITelegramQuery.java | 7 +-- .../cono/morny/bot/query/MornyQueries.java | 9 ++-- .../cono/morny/bot/query/MyInformation.java | 15 ++++-- .../sukazyo/cono/morny/bot/query/RawText.java | 17 ++++--- .../morny/bot/query/ShareToolTwitter.java | 50 +++++++++++++++++++ .../util/tgapi/formatting/NamedUtils.java | 18 +++++++ 8 files changed, 99 insertions(+), 23 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/query/ShareToolTwitter.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/NamedUtils.java diff --git a/gradle.properties b/gradle.properties index 3c89bfe..5576ec9 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.8.0.7 +VERSION = 0.8.0.8 CODENAME = putian diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index d6ac208..2c188e6 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -4,7 +4,7 @@ package cc.sukazyo.cono.morny; * the final field that will be updated by gradle automatically. */ public class GradleProjectConfigures { - public static final String VERSION = "0.8.0.7"; + public static final String VERSION = "0.8.0.8"; public static final String CODENAME = "putian"; - public static final long COMPILE_TIMESTAMP = 1665827278036L; + public static final long COMPILE_TIMESTAMP = 1665999835121L; } 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 b0fa978..c79ab37 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 @@ -4,11 +4,12 @@ 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 > { +import java.util.List; + +public interface ITelegramQuery { @Nullable - InlineQueryUnit query (Update event); + List> 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 a10f00d..a561303 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 @@ -9,19 +9,20 @@ import java.util.List; public class MornyQueries { - private final List> queryInstances = new ArrayList<>(); + private final List queryInstances = new ArrayList<>(); public MornyQueries () { queryInstances.add(new RawText()); queryInstances.add(new MyInformation()); + queryInstances.add(new ShareToolTwitter()); } @Nonnull public List> query (@Nonnull Update event) { final List> results = new ArrayList<>(); - for (ITelegramQuery instance : queryInstances) { - final InlineQueryUnit r = instance.query(event); - if (r!=null) results.add(r); + for (ITelegramQuery instance : queryInstances) { + final List> r = instance.query(event); + if (r!=null) results.addAll(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 53f666a..7921174 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 @@ -10,21 +10,26 @@ import com.pengrad.telegrambot.model.request.ParseMode; import cc.sukazyo.cono.morny.util.tgapi.formatting.TelegramUserInformation; -public class MyInformation implements ITelegramQuery { +import java.util.Collections; +import java.util.List; + +import static cc.sukazyo.cono.morny.util.tgapi.formatting.NamedUtils.inlineIds; + +public class MyInformation implements ITelegramQuery { public static final String ID_PREFIX = "[morny/info/me]"; public static final String TITLE = "My Account Information"; @Override @Nullable - public InlineQueryUnit query(Update event) { + public List> query(Update event) { if (!(event.inlineQuery().query() == null || "".equals(event.inlineQuery().query()))) return null; - return new InlineQueryUnit<>(new InlineQueryResultArticle( - ID_PREFIX, TITLE, + return Collections.singletonList(new InlineQueryUnit<>(new InlineQueryResultArticle( + inlineIds(ID_PREFIX), TITLE, new InputTextMessageContent( TelegramUserInformation.informationOutputHTML(event.inlineQuery().from()) ).parseMode(ParseMode.HTML) - )).isPersonal(true).cacheTime(10); + )).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 655d03a..a773f6b 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 @@ -8,23 +8,24 @@ import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.InlineQueryResultArticle; import com.pengrad.telegrambot.model.request.InputTextMessageContent; -import static cc.sukazyo.cono.morny.util.CommonConvert.byteArrayToHex; -import static cc.sukazyo.cono.morny.util.CommonEncrypt.hashMd5; +import java.util.Collections; +import java.util.List; -public class RawText implements ITelegramQuery { +import static cc.sukazyo.cono.morny.util.tgapi.formatting.NamedUtils.inlineIds; + +public class RawText implements ITelegramQuery { public static final String ID_PREFIX = "[morny/r/text]"; public static final String TITLE = "Raw Text"; @Override @Nullable - public InlineQueryUnit query (Update event) { + public List> query (Update event) { if (event.inlineQuery().query() == null || "".equals(event.inlineQuery().query())) return null; - return new InlineQueryUnit<>(new InlineQueryResultArticle( - ID_PREFIX + byteArrayToHex(hashMd5(event.inlineQuery().query())), - TITLE, + return Collections.singletonList(new InlineQueryUnit<>(new InlineQueryResultArticle( + inlineIds(ID_PREFIX, event.inlineQuery().query()), TITLE, new InputTextMessageContent(event.inlineQuery().query()) - )); + ))); } } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/query/ShareToolTwitter.java b/src/main/java/cc/sukazyo/cono/morny/bot/query/ShareToolTwitter.java new file mode 100644 index 0000000..9c50d6f --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/ShareToolTwitter.java @@ -0,0 +1,50 @@ +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.InlineQueryResultArticle; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.ArrayList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import static cc.sukazyo.cono.morny.util.tgapi.formatting.NamedUtils.inlineIds; + +public class ShareToolTwitter implements ITelegramQuery { + + public static final String TITLE_VX = "[tweet] Share as VxTwitter"; + public static final String TITLE_VX_COMBINED = "[tweet] Share as VxTwitter(combination)"; + public static final String ID_PREFIX_VX = "[morny/share/twitter/vxtwi]"; + public static final String ID_PREFIX_VX_COMBINED = "[morny/share/twitter/vxtwi_combine]"; + + public static final Pattern REGEX_TWEET_LINK = Pattern.compile( + "^(?:https?://)?((?:(?:c\\.)?vx|fx|www\\.)?twitter\\.com)/((\\w+)/status/(\\d+)(?:/photo/(\\d+))?)/?(\\?[\\w&=-]+)?$"); + + @Nullable + @Override + public List> query (@Nonnull Update event) { + if (event.inlineQuery().query() == null) return null; + final Matcher regex = REGEX_TWEET_LINK.matcher(event.inlineQuery().query()); + if (regex.matches()) { + + List> result = new ArrayList<>(); + + result.add(new InlineQueryUnit<>(new InlineQueryResultArticle( + inlineIds(ID_PREFIX_VX+event.inlineQuery().query()), TITLE_VX, + String.format("https://vxtwitter.com/%s", regex.group(2)) + ))); + result.add(new InlineQueryUnit<>(new InlineQueryResultArticle( + inlineIds(ID_PREFIX_VX_COMBINED+event.inlineQuery().query()), TITLE_VX_COMBINED, + String.format("https://c.vxtwitter.com/%s", regex.group(2)) + ))); + + return result; + + } + return null; + } + +} diff --git a/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/NamedUtils.java b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/NamedUtils.java new file mode 100644 index 0000000..b046f30 --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/tgapi/formatting/NamedUtils.java @@ -0,0 +1,18 @@ +package cc.sukazyo.cono.morny.util.tgapi.formatting; + +import cc.sukazyo.cono.morny.util.CommonConvert; +import cc.sukazyo.cono.morny.util.CommonEncrypt; + +import javax.annotation.Nonnull; + +public class NamedUtils { + + public static String inlineIds (@Nonnull String tag) { + return inlineIds(tag, ""); + } + + public static String inlineIds (@Nonnull String tag, @Nonnull String taggedData) { + return CommonConvert.byteArrayToHex(CommonEncrypt.hashMd5(tag+taggedData)); + } + +}