添加了新的内联查询功能支持将 twitter 分享链接重新输出为 vxtwitter 分享链接同时去跟踪参数,将 ITelegramQuery 抽象层的要求返回从单个限定类别 queryUnit 改为 list<queryUnit<?>>

- 通过 tgapi.NamedUtils#inlineIds 规范化了 InlineQuery 的 id 定义
  - 将 raw text 的 id 重新定义为规范化行为
  - 将 my information 的 id 重新定义为规范化行为
This commit is contained in:
A.C.Sukazyo Eyre 2022-10-17 17:44:12 +08:00
parent e31dd6a0fb
commit 5bb19095c8
Signed by: Eyre_S
GPG Key ID: C17CE40291207874
8 changed files with 99 additions and 23 deletions

View File

@ -1,6 +1,6 @@
## Core ## Core
VERSION = 0.8.0.7 VERSION = 0.8.0.8
CODENAME = putian CODENAME = putian

View File

@ -4,7 +4,7 @@ package cc.sukazyo.cono.morny;
* the final field that will be updated by gradle automatically. * the final field that will be updated by gradle automatically.
*/ */
public class GradleProjectConfigures { 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 String CODENAME = "putian";
public static final long COMPILE_TIMESTAMP = 1665827278036L; public static final long COMPILE_TIMESTAMP = 1665999835121L;
} }

View File

@ -4,11 +4,12 @@ import javax.annotation.Nullable;
import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit; import cc.sukazyo.cono.morny.bot.api.InlineQueryUnit;
import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.InlineQueryResult;
public interface ITelegramQuery <T extends InlineQueryResult<T>> { import java.util.List;
public interface ITelegramQuery {
@Nullable @Nullable
InlineQueryUnit<T> query (Update event); List<InlineQueryUnit<?>> query (Update event);
} }

View File

@ -9,19 +9,20 @@ import java.util.List;
public class MornyQueries { public class MornyQueries {
private final List<ITelegramQuery<?>> queryInstances = new ArrayList<>(); private final List<ITelegramQuery> queryInstances = new ArrayList<>();
public MornyQueries () { public MornyQueries () {
queryInstances.add(new RawText()); queryInstances.add(new RawText());
queryInstances.add(new MyInformation()); queryInstances.add(new MyInformation());
queryInstances.add(new ShareToolTwitter());
} }
@Nonnull @Nonnull
public List<InlineQueryUnit<?>> query (@Nonnull Update event) { public List<InlineQueryUnit<?>> query (@Nonnull Update event) {
final List<InlineQueryUnit<?>> results = new ArrayList<>(); final List<InlineQueryUnit<?>> results = new ArrayList<>();
for (ITelegramQuery<?> instance : queryInstances) { for (ITelegramQuery instance : queryInstances) {
final InlineQueryUnit<?> r = instance.query(event); final List<InlineQueryUnit<?>> r = instance.query(event);
if (r!=null) results.add(r); if (r!=null) results.addAll(r);
} }
return results; return results;
} }

View File

@ -10,21 +10,26 @@ import com.pengrad.telegrambot.model.request.ParseMode;
import cc.sukazyo.cono.morny.util.tgapi.formatting.TelegramUserInformation; import cc.sukazyo.cono.morny.util.tgapi.formatting.TelegramUserInformation;
public class MyInformation implements ITelegramQuery<InlineQueryResultArticle> { 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 ID_PREFIX = "[morny/info/me]";
public static final String TITLE = "My Account Information"; public static final String TITLE = "My Account Information";
@Override @Override
@Nullable @Nullable
public InlineQueryUnit<InlineQueryResultArticle> query(Update event) { public List<InlineQueryUnit<?>> query(Update event) {
if (!(event.inlineQuery().query() == null || "".equals(event.inlineQuery().query()))) return null; if (!(event.inlineQuery().query() == null || "".equals(event.inlineQuery().query()))) return null;
return new InlineQueryUnit<>(new InlineQueryResultArticle( return Collections.singletonList(new InlineQueryUnit<>(new InlineQueryResultArticle(
ID_PREFIX, TITLE, inlineIds(ID_PREFIX), TITLE,
new InputTextMessageContent( new InputTextMessageContent(
TelegramUserInformation.informationOutputHTML(event.inlineQuery().from()) TelegramUserInformation.informationOutputHTML(event.inlineQuery().from())
).parseMode(ParseMode.HTML) ).parseMode(ParseMode.HTML)
)).isPersonal(true).cacheTime(10); )).isPersonal(true).cacheTime(10));
} }
} }

View File

@ -8,23 +8,24 @@ import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.InlineQueryResultArticle; import com.pengrad.telegrambot.model.request.InlineQueryResultArticle;
import com.pengrad.telegrambot.model.request.InputTextMessageContent; import com.pengrad.telegrambot.model.request.InputTextMessageContent;
import static cc.sukazyo.cono.morny.util.CommonConvert.byteArrayToHex; import java.util.Collections;
import static cc.sukazyo.cono.morny.util.CommonEncrypt.hashMd5; import java.util.List;
public class RawText implements ITelegramQuery<InlineQueryResultArticle> { 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 ID_PREFIX = "[morny/r/text]";
public static final String TITLE = "Raw Text"; public static final String TITLE = "Raw Text";
@Override @Override
@Nullable @Nullable
public InlineQueryUnit<InlineQueryResultArticle> query (Update event) { public List<InlineQueryUnit<?>> query (Update event) {
if (event.inlineQuery().query() == null || "".equals(event.inlineQuery().query())) return null; if (event.inlineQuery().query() == null || "".equals(event.inlineQuery().query())) return null;
return new InlineQueryUnit<>(new InlineQueryResultArticle( return Collections.singletonList(new InlineQueryUnit<>(new InlineQueryResultArticle(
ID_PREFIX + byteArrayToHex(hashMd5(event.inlineQuery().query())), inlineIds(ID_PREFIX, event.inlineQuery().query()), TITLE,
TITLE,
new InputTextMessageContent(event.inlineQuery().query()) new InputTextMessageContent(event.inlineQuery().query())
)); )));
} }
} }

View File

@ -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<InlineQueryUnit<?>> 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<InlineQueryUnit<?>> 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;
}
}

View File

@ -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));
}
}