建立 InlineQuery 抽象层与管理器

This commit is contained in:
A.C.Sukazyo Eyre 2022-01-31 15:00:13 +08:00
parent 6dbe214476
commit a229ce9add
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
8 changed files with 80 additions and 16 deletions

View File

@ -1,6 +1,6 @@
## Core
VERSION = 0.5.0.1
VERSION = 0.5.0.2
# dependencies

View File

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

View File

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

View File

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

View File

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

View File

@ -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 extends InlineQueryResult<T>> {
T query (Update event);
}

View File

@ -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<ITelegramQuery<?>> queryInstances = new ArrayList<>();
public MornyQueries () {
queryInstances.add(new RawText());
}
@Nonnull
public List<InlineQueryResult<?>> query (@Nonnull Update event) {
final List<InlineQueryResult<?>> results = new ArrayList<>();
for (ITelegramQuery<?> instance : queryInstances) {
final InlineQueryResult<?> r = instance.query(event);
if (r!=null) results.add(r);
}
return results;
}
}

View File

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