%s
- - %s
%s%s
- core md5_hash:
- - %s
- coding timestamp:
- - %d
- - %s [UTC]
""",
- escapeHtml(MornySystem.CODENAME.toUpperCase()),
- escapeHtml(MornySystem.VERSION_BASE),
- MornySystem.isUseDelta() ? String.format("-δ%s
", escapeHtml(Objects.requireNonNull(MornySystem.VERSION_DELTA))) : "",
- MornySystem.isGitBuild()?"\n- git "+ (MornySystem.currentCodePath()==null ?
- String.format("%s
", BuildConfig.COMMIT.substring(0, 8)) :
- String.format("%s", MornySystem.currentCodePath(), BuildConfig.COMMIT.substring(0, 8))
- ) + (MornySystem.isCleanBuild() ? "" : ".δ
") : "",
- escapeHtml(MornySystem.getJarMd5()),
- BuildConfig.CODE_TIMESTAMP,
- escapeHtml(formatDate(BuildConfig.CODE_TIMESTAMP, 0))
- )
- ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML));
+ @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { MornyInformations.echoVersion(event); }
}
private static class MornyRuntime implements ISimpleCommand {
@@ -268,71 +236,7 @@ public class MornyCommands {
@Nullable @Override public String[] getAliases () { return null; }
@Nonnull @Deprecated public String getParamRule () { return ""; }
@Nonnull @Deprecated public String getDescription () { return "获取 Bot 运行时信息(包括版本号)"; }
- @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { onCommandRuntimeExec(event); }
- }
- /**
- * @since 0.4.1.2
- */
- public static void onCommandRuntimeExec (@Nonnull Update event) {
- String hostname;
- try {
- hostname = InetAddress.getLocalHost().getHostName();
- } catch (UnknownHostException e) {
- hostname = "%s
- - %s
- - %s
- java runtime:
- - %s
- - %s
- vm memory:
- - %d
/ %d
MB
- - %d
cores
- coeur version:
- - %s
%s%s*%s
- - %s
- - %s [UTC]
- - [%d
]
- continuous:
- - %s
- - [%d
]
- - %s [UTC]
- - [%d
]""",
- // system
- escapeHtml(hostname),
- escapeHtml(String.format("%s (%s)", System.getProperty("os.name"), System.getProperty("os.arch"))),
- escapeHtml(System.getProperty("os.version")),
- // java
- escapeHtml(System.getProperty("java.vm.vendor")+"."+System.getProperty("java.vm.name")),
- escapeHtml(System.getProperty("java.vm.version")),
- // memory
- Runtime.getRuntime().totalMemory() / 1024 / 1024,
- Runtime.getRuntime().maxMemory() / 1024 / 1024,
- Runtime.getRuntime().availableProcessors(),
- // version
- escapeHtml(MornySystem.VERSION_BASE),
- MornySystem.isUseDelta() ? String.format("-δ%s
", escapeHtml(Objects.requireNonNull(MornySystem.VERSION_DELTA))) : "",
- MornySystem.isGitBuild()?String.format("-git.%s%s", MornySystem.currentCodePath()==null ? (
- String.format("%s
", BuildConfig.COMMIT.substring(0, 8))
- ) : (
- String.format("%s", MornySystem.currentCodePath(), BuildConfig.COMMIT.substring(0, 8))
- ), MornySystem.isCleanBuild()?"":".δ
"):"",
- escapeHtml(MornySystem.CODENAME.toUpperCase()),
- escapeHtml(MornySystem.getJarMd5()),
- escapeHtml(formatDate(BuildConfig.CODE_TIMESTAMP, 0)),
- BuildConfig.CODE_TIMESTAMP,
- // continuous
- escapeHtml(formatDuration(System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp)),
- System.currentTimeMillis() - MornyCoeur.coeurStartTimestamp,
- escapeHtml(formatDate(MornyCoeur.coeurStartTimestamp, 0)),
- MornyCoeur.coeurStartTimestamp
- )
- ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML));
+ @Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { MornyInformations.echoRuntime(event); }
}
private static class Jrrp implements ITelegramCommand {
diff --git a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyInformations.java b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyInformations.java
index 5558f6e..e9a6ad2 100644
--- a/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyInformations.java
+++ b/src/main/java/cc/sukazyo/cono/morny/bot/command/MornyInformations.java
@@ -1,14 +1,25 @@
package cc.sukazyo.cono.morny.bot.command;
+import cc.sukazyo.cono.morny.BuildConfig;
import cc.sukazyo.cono.morny.MornyCoeur;
+import cc.sukazyo.cono.morny.MornySystem;
import cc.sukazyo.cono.morny.data.TelegramStickers;
import cc.sukazyo.cono.morny.util.tgapi.ExtraAction;
import cc.sukazyo.cono.morny.util.tgapi.InputCommand;
import com.pengrad.telegrambot.model.Update;
+import com.pengrad.telegrambot.model.request.ParseMode;
+import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.request.SendSticker;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+import java.util.Objects;
+
+import static cc.sukazyo.cono.morny.util.CommonFormat.formatDate;
+import static cc.sukazyo.cono.morny.util.CommonFormat.formatDuration;
+import static cc.sukazyo.cono.morny.util.tgapi.formatting.MsgEscape.escapeHtml;
public class MornyInformations implements ITelegramCommand {
@@ -26,7 +37,7 @@ public class MornyInformations implements ITelegramCommand {
public void execute (@Nonnull InputCommand command, @Nonnull Update event) {
if (!command.hasArgs()) {
- MornyCommands.onCommandRuntimeExec(event);
+ echoRuntime(event);
return;
}
@@ -35,9 +46,9 @@ public class MornyInformations implements ITelegramCommand {
if (action.startsWith(SUB_STICKER)) {
echoStickers(command, event);
} else if (action.equals(SUB_RUNTIME)) {
- echoRuntime(command, event);
+ echoRuntime(event);
} else if (action.equals(SUB_VERSION) || action.equals(SUB_VERSION_2)) {
- echoVersion(command, event);
+ echoVersion(event);
} else {
echo404(event);
}
@@ -85,20 +96,134 @@ public class MornyInformations implements ITelegramCommand {
* /info 子命令 {@value #SUB_RUNTIME}
* @since 1.0.0-alpha4
*/
- public static void echoRuntime (@Nonnull InputCommand command, @Nonnull Update event) {
- if (command.getArgs().length == 1)
- MornyCommands.onCommandRuntimeExec(event);
- else echo404(event);
+ public static void echoRuntime (@Nonnull Update event) {
+ MornyCoeur.extra().exec(new SendMessage(
+ event.message().chat().id(),
+ String.format("""
+ system:
+ - %s
+ - %s
(%s
) %s
+ java runtime:
+ - %s
+ - %s
+ vm memory:
+ - %d
/ %d
MB
+ - %d
cores
+ coeur version:
+ - %s
+ - %s
+ - %s [UTC]
+ - [%d
]
+ continuous:
+ - %s
+ - [%d
]
+ - %s [UTC]
+ - [%d
]""",
+ // system
+ escapeHtml(getRuntimeHostName()==null ? "%s
+ - %s
%s%s
+ core md5_hash:
+ - %s
+ coding timestamp:
+ - %d
+ - %s [UTC]
""",
+ escapeHtml(MornySystem.CODENAME.toUpperCase()),
+ escapeHtml(MornySystem.VERSION_BASE),
+ MornySystem.isUseDelta() ? String.format("-δ%s
", escapeHtml(Objects.requireNonNull(MornySystem.VERSION_DELTA))) : "",
+ MornySystem.isGitBuild() ? "\n- git "+getVersionGitTagHtml() : "",
+ escapeHtml(MornySystem.getJarMd5()),
+ BuildConfig.CODE_TIMESTAMP,
+ escapeHtml(formatDate(BuildConfig.CODE_TIMESTAMP, 0))
+ )
+ ).replyToMessageId(event.message().messageId()).parseMode(ParseMode.HTML));
+ }
+
+ /**
+ * 取得 {@link MornySystem} 的 git commit 相关版本信息的 HTML 格式化标签.
+ * @return 格式类似于 {@code 28e8c82a.δ} 的以 HTML 方式格式化的版本号组件。
+ * 其中 {@code .δ} 对应着 {@link MornySystem#isCleanBuild};
+ * commit tag 字段如果支援 {@link MornySystem#currentCodePath} 则会以链接形式解析,否则则为 code 格式
+ * 为了对 telegram api html 格式兼容所以不支援嵌套链接与code标签。
+ * 如果 {@link MornySystem#isGitBuild} 为 {@link false},则方法会返回 {@link ""}
+ * @since 1.0.0-beta2
+ */
+ @Nonnull
+ public static String getVersionGitTagHtml () {
+ if (!MornySystem.isGitBuild()) return "";
+ final StringBuilder g = new StringBuilder();
+ final String cp = MornySystem.currentCodePath();
+ if (cp == null) g.append(String.format("%s
", BuildConfig.COMMIT.substring(0, 8)));
+ else g.append(String.format("%s", MornySystem.currentCodePath(), BuildConfig.COMMIT.substring(0, 8)));
+ if (!MornySystem.isCleanBuild()) g.append(".δ
");
+ return g.toString();
+ }
+
+ /**
+ * 取得完整 Morny 版本的 HTML 格式化标签.
+ *
+ * 相比于 {@link MornySystem#VERSION_FULL},这个版本号还包含了 {@link MornySystem#CODENAME 版本 codename}。
+ * 各个部分也被以 HTML 的格式进行了格式化以可以更好的在富文本中插入使用.
+ * @return 基于 HTML 标签进行了格式化了的类似于
+ * {@link MornySystem#VERSION_BASE 5.38.2-alpha1}{@link MornySystem#isUseDelta() -δ}{@link MornySystem#VERSION_DELTA tt}{@link MornySystem#isGitBuild() +git.}{@link #getVersionGitTagHtml() 28e8c82a.δ}*{@link MornySystem#CODENAME TOKYO}
+ * 的版本号。
+ * @since 1.0.0-beta2
+ */
+ @Nonnull
+ public static String getVersionAllFullTagHtml () {
+ final StringBuilder v = new StringBuilder();
+ v.append("").append(MornySystem.VERSION_BASE).append("
");
+ if (MornySystem.isUseDelta()) v.append("-δ").append(MornySystem.VERSION_DELTA).append("
");
+ if (MornySystem.isGitBuild()) v.append("+git.").append(getVersionGitTagHtml());
+ v.append("*").append(MornySystem.CODENAME.toUpperCase()).append("
");
+ return v.toString();
+ }
+
+ /**
+ * 获取 coeur 运行时的宿主机的主机名
+ * @return coeur 宿主机主机名,或者 {@link null} 表示获取失败
+ */
+ @Nullable
+ public static String getRuntimeHostName () {
+ try {
+ return InetAddress.getLocalHost().getHostName();
+ } catch (UnknownHostException e) {
+ return null;
+ }
}
private static void echo404 (@Nonnull Update event) {
diff --git a/src/main/java/cc/sukazyo/cono/morny/daemon/MornyReport.java b/src/main/java/cc/sukazyo/cono/morny/daemon/MornyReport.java
index c79b223..b4f7ae5 100644
--- a/src/main/java/cc/sukazyo/cono/morny/daemon/MornyReport.java
+++ b/src/main/java/cc/sukazyo/cono/morny/daemon/MornyReport.java
@@ -1,8 +1,7 @@
package cc.sukazyo.cono.morny.daemon;
-import cc.sukazyo.cono.morny.Log;
-import cc.sukazyo.cono.morny.MornyCoeur;
-import cc.sukazyo.cono.morny.MornyConfig;
+import cc.sukazyo.cono.morny.*;
+import cc.sukazyo.cono.morny.bot.command.MornyInformations;
import cc.sukazyo.cono.morny.util.tgapi.event.EventRuntimeException;
import cc.sukazyo.cono.morny.util.tgapi.formatting.TGToString;
import com.google.gson.GsonBuilder;
@@ -74,16 +73,18 @@ public class MornyReport {
* morny 登陆时的报告发送,包含已登录的账号 id 以及启动配置。
* @since 1.0.0-alpha6
*/
- public static void onMornyLogIn () {
+ static void onMornyLogIn () {
executeReport(new SendMessage(
MornyCoeur.config().reportToChat,
String.format("""
▌Morny Logged in
+ -v %s
as user @%s
as config fields:
%s
""",
+ MornyInformations.getVersionAllFullTagHtml(),
MornyCoeur.getUsername(),
sectionConfigFields(MornyCoeur.config())
)