From b77194fef7f77c337e5dc7ecd799a62e7c5476b1 Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Mon, 31 Jan 2022 16:21:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20inline:myInformation=20?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更改使 inline:raw 功能在空 query message 时不触发 - 添加 inline:myInformation 作为 cmd:user 对自己使用的 inline 方式 - 将字符串化用户的信息的功能独立为 util method --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../morny/bot/command/GetUsernameAndId.java | 54 +--------------- .../cono/morny/bot/query/ITelegramQuery.java | 3 + .../cono/morny/bot/query/MornyQueries.java | 1 + .../cono/morny/bot/query/MyInformation.java | 26 ++++++++ .../sukazyo/cono/morny/bot/query/RawText.java | 8 ++- .../morny/util/TelegramUserInformation.java | 64 +++++++++++++++++++ 8 files changed, 106 insertions(+), 56 deletions(-) create mode 100644 src/main/java/cc/sukazyo/cono/morny/bot/query/MyInformation.java create mode 100644 src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java diff --git a/gradle.properties b/gradle.properties index d72cee5..f185b84 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.5.0.2 +VERSION = 0.5.0.3 # dependencies diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index 4e2f5c3..00ef497 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.2"; - public static final long COMPILE_TIMESTAMP = 1643612350660L; + public static final String VERSION = "0.5.0.3"; + public static final long COMPILE_TIMESTAMP = 1643617049492L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/GetUsernameAndId.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/GetUsernameAndId.java index e8bb9c8..c53e746 100644 --- a/src/main/java/cc/sukazyo/cono/morny/bot/command/GetUsernameAndId.java +++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/GetUsernameAndId.java @@ -1,7 +1,7 @@ package cc.sukazyo.cono.morny.bot.command; import cc.sukazyo.cono.morny.MornyCoeur; - +import cc.sukazyo.cono.morny.util.TelegramUserInformation; import cc.sukazyo.untitled.util.telegram.object.InputCommand; import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.User; @@ -63,59 +63,9 @@ public class GetUsernameAndId implements ITelegramCommand { final User user = response.chatMember().user(); - final StringBuilder userInformation = new StringBuilder(); - userInformation.append(String.format( - """ - userid : - - %d""", - userId - )); - if (user.username() == null) { - userInformation.append("\nusername : null"); - } else { - userInformation.append(String.format( - """ - - username : - - %s""", - escapeHtml(user.username()) - )); - } - if (user.firstName() == null) { - userInformation.append("\nfirstname : null"); - } else { - userInformation.append(String.format( - """ - - firstname : - - %s""", - escapeHtml(user.firstName()) - )); - } - if (user.lastName() == null) { - userInformation.append("\nlastname : null"); - } else { - userInformation.append(String.format( - """ - - lastname : - - %s""", - escapeHtml(user.lastName()) - )); - } - if (user.languageCode() != null) { - userInformation.append(String.format( - """ - - language-code : - - %s""", - escapeHtml(user.languageCode()) - )); - } - MornyCoeur.extra().exec(new SendMessage( event.message().chat().id(), - userInformation.toString() + TelegramUserInformation.informationOutputHTML(user) ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML)); } 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 76de728..6f1d0da 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 @@ -1,10 +1,13 @@ package cc.sukazyo.cono.morny.bot.query; +import javax.annotation.Nullable; + import com.pengrad.telegrambot.model.Update; import com.pengrad.telegrambot.model.request.InlineQueryResult; public interface ITelegramQuery > { + @Nullable 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 index 4f5de65..8bbbd30 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 @@ -13,6 +13,7 @@ public class MornyQueries { public MornyQueries () { queryInstances.add(new RawText()); + queryInstances.add(new MyInformation()); } @Nonnull 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 new file mode 100644 index 0000000..0140b6f --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/bot/query/MyInformation.java @@ -0,0 +1,26 @@ +package cc.sukazyo.cono.morny.bot.query; + +import javax.annotation.Nullable; + +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 cc.sukazyo.cono.morny.util.TelegramUserInformation; + +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 InlineQueryResultArticle 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)); + } + +} 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 4be01d0..2e3cceb 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,18 +1,24 @@ package cc.sukazyo.cono.morny.bot.query; import cc.sukazyo.cono.morny.util.EncryptUtils; + +import javax.annotation.Nullable; + 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 ID_PREFIX = "[morny/r/text]"; public static final String TITLE = "Raw Text"; @Override + @Nullable public InlineQueryResultArticle query (Update event) { + if (event.inlineQuery().query() == null || "".equals(event.inlineQuery().query())) return null; return new InlineQueryResultArticle( - "[morny/r/txt]" + EncryptUtils.encryptByMD5(event.inlineQuery().query()), + ID_PREFIX + EncryptUtils.encryptByMD5(event.inlineQuery().query()), TITLE, new InputTextMessageContent(event.inlineQuery().query()) ); diff --git a/src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java b/src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java new file mode 100644 index 0000000..011e4da --- /dev/null +++ b/src/main/java/cc/sukazyo/cono/morny/util/TelegramUserInformation.java @@ -0,0 +1,64 @@ +package cc.sukazyo.cono.morny.util; + +import com.pengrad.telegrambot.model.User; + +import static cc.sukazyo.untitled.util.telegram.formatting.MsgEscape.escapeHtml; +public class TelegramUserInformation { + + public static String informationOutputHTML (User user) { + + final StringBuilder userInformation = new StringBuilder(); + userInformation.append(String.format( + """ + userid : + - %d""", + user.id() + )); + if (user.username() == null) { + userInformation.append("\nusername : null"); + } else { + userInformation.append(String.format( + """ + + username : + - %s""", + escapeHtml(user.username()) + )); + } + if (user.firstName() == null) { + userInformation.append("\nfirstname : null"); + } else { + userInformation.append(String.format( + """ + + firstname : + - %s""", + escapeHtml(user.firstName()) + )); + } + if (user.lastName() == null) { + userInformation.append("\nlastname : null"); + } else { + userInformation.append(String.format( + """ + + lastname : + - %s""", + escapeHtml(user.lastName()) + )); + } + if (user.languageCode() != null) { + userInformation.append(String.format( + """ + + language-code : + - %s""", + escapeHtml(user.languageCode()) + )); + } + + return userInformation.toString(); + + } + +}