[[[release 0.7.1.3*fuzhou]]]

## 🧯Bug Fix

- 优化对命令格式的检查,使其更加符合 telegram 标准
- 修复 EventHack 中对频道消息对象的错误处理导致事件报错

## 📇功能

- 吃药提醒功能提醒对象改为频道,取消了对提醒的pin,提醒对象改为文本类型
- 吃药提醒添加对最近一个提醒的监听,并提供为其附加时间戳(UTC+8)的功能
- 添加 /quit /stop 别名可用于 /exit

## 🔩技术修改/typo

- 启动线程在呼叫 MornyCoeur 启动时现在会把线程名改为 `morny-init`
- 添加了一条 log 用于记录事件监听器已打开
- 将退出时的数据清理线程名称改为小写
This commit is contained in:
A.C.Sukazyo Eyre 2022-06-10 18:57:42 +08:00
commit 72aea15209
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
14 changed files with 95 additions and 22 deletions

View File

@ -12,6 +12,8 @@
# ~~给所有喜欢morny的大家的~~ Morny Coeur 源代码
~~"你们又有意见又不发issue这样子我很为难的啊"~~
![social preview card](morny-github-social-preview-card@0.75x.png)
一个 telegram 上的服侍 A.C.Sukazyo Eyre 和它的花宫成员的 bot 的内核源

View File

@ -90,7 +90,7 @@ publishing {
repositories{
maven {
name 'builds'
url = "file://" + new File("./build/publishing").getAbsolutePath()
url publishLocalArchiveRepoUrl
}
maven {
name '-ws-'

View File

@ -1,6 +1,6 @@
## Core
VERSION = 0.7.0.16
VERSION = 0.7.1.3
CODENAME = fuzhou

View File

@ -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.7.0.16";
public static final String VERSION = "0.7.1.3";
public static final String CODENAME = "fuzhou";
public static final long COMPILE_TIMESTAMP = 1654072970797L;
public static final long COMPILE_TIMESTAMP = 1654858585641L;
}

View File

@ -152,6 +152,7 @@ public class MornyCoeur {
isRemoveCommandListWhenExit
);
MornyDaemons.start();
logger.info("start telegram events listening");
EventListeners.registerAllListeners();
INSTANCE.account.setUpdatesListener(OnUpdate::onNormalUpdate);
if (isAutomaticResetCommandList) {
@ -187,7 +188,7 @@ public class MornyCoeur {
* 为程序在虚拟机上添加退出钩子
*/
private void configureSafeExit () {
Runtime.getRuntime().addShutdownHook(new Thread(this::exitCleanup, "Exit-Cleaning"));
Runtime.getRuntime().addShutdownHook(new Thread(this::exitCleanup, "exit-cleaning"));
}
/**

View File

@ -21,6 +21,8 @@ public class ServerMain {
public static final String PROP_TOKEN_KEY = "TELEGRAM_BOT_API_TOKEN";
public static final String PROP_TOKEN_MORNY_KEY = "MORNY_TG_TOKEN";
private static final String THREAD_MORNY_INIT = "morny-init";
/**
* 程序入口也是参数处理器<br>
* <br>
@ -233,6 +235,7 @@ public class ServerMain {
logger.info("Parameter required has no value:\n --token.");
return;
}
Thread.currentThread().setName(THREAD_MORNY_INIT);
MornyCoeur.main(
api, api4File,
key, username,

View File

@ -14,6 +14,7 @@ import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.request.SendSticker;
import com.pengrad.telegrambot.request.SetMyCommands;
import org.jetbrains.annotations.NotNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
@ -60,8 +61,7 @@ public class MornyCommands {
register(
new ON(),
new Hello(),
new HelloOnStart(),
new Hello(), new HelloOnStart(),
new GetUsernameAndId(),
new EventHack(),
new Nbnhhsh(),
@ -71,7 +71,7 @@ public class MornyCommands {
new Version(),
new MornyRuntime(),
new Jrrp(),
new Exit()
new Exit(), new ExitAlias()
);
// 特殊的命令
@ -197,6 +197,11 @@ public class MornyCommands {
@Nonnull @Override public String getDescription () { return "关闭 Bot (仅可信成员)"; }
@Override public void execute (@Nonnull InputCommand command, @Nonnull Update event) { onCommandExitExec(event); }
}
private static class ExitAlias implements ISimpleCommand {
@Nonnull @Override public String getName () { return "quit"; }
@Nullable @Override public String[] getAliases () { return new String[]{"stop"}; }
@Override public void execute (@NotNull InputCommand command, @NotNull Update event) { onCommandExitExec(event); }
}
private static void onCommandExitExec (@Nonnull Update event) {
if (MornyCoeur.trustedInstance().isTrusted(event.message().from().id())) {
MornyCoeur.extra().exec(new SendSticker(

View File

@ -14,6 +14,7 @@ public class EventListeners {
@SuppressWarnings("unused") static final OnKuohuanhuanNeedSleep KUOHUANHUAN_NEED_SLEEP = new OnKuohuanhuanNeedSleep();
public static final OnUserRandoms USER_RANDOMS = new OnUserRandoms();
public static final OnCallMsgSend CALL_MSG_SEND = new OnCallMsgSend();
public static final OnMedicationNotifyApply MEDICATION_NOTIFY_APPLY = new OnMedicationNotifyApply();
public static void registerAllListeners () {
EventListenerManager.addListener(
@ -26,6 +27,7 @@ public class EventListeners {
INLINE_QUERY,
CALL_ME,
CALL_MSG_SEND,
MEDICATION_NOTIFY_APPLY,
EVENT_HACK_HANDLE
);
}

View File

@ -84,12 +84,12 @@ public class OnEventHackHandle extends EventListener {
@Override
public boolean onChannelPost (@Nonnull Update update) {
return onEventHacked(update, update.channelPost().chat().id(), update.channelPost().from().id());
return onEventHacked(update, update.channelPost().chat().id(), update.channelPost().chat().id());
}
@Override
public boolean onEditedChannelPost (@Nonnull Update update) {
return onEventHacked(update, update.editedChannelPost().chat().id(), update.editedChannelPost().from().id());
return onEventHacked(update, update.editedChannelPost().chat().id(), update.editedChannelPost().chat().id());
}
@Override

View File

@ -0,0 +1,28 @@
package cc.sukazyo.cono.morny.bot.event;
import cc.sukazyo.cono.morny.bot.api.EventListener;
import cc.sukazyo.cono.morny.daemon.MedicationTimer;
import cc.sukazyo.cono.morny.daemon.MornyDaemons;
import com.pengrad.telegrambot.model.Message;
import com.pengrad.telegrambot.model.Update;
import org.jetbrains.annotations.NotNull;
public class OnMedicationNotifyApply extends EventListener {
@Override
public boolean onEditedChannelPost (@NotNull Update update) {
return editedMessageProcess(update.editedChannelPost());
}
@Override
public boolean onEditedMessage (@NotNull Update update) {
return editedMessageProcess(update.editedMessage());
}
private boolean editedMessageProcess (Message edited) {
if (edited.chat().id() != MedicationTimer.NOTIFY_CHAT) return false;
MornyDaemons.medicationTimerInstance.refreshNotificationWrite(edited);
return true;
}
}

View File

@ -8,14 +8,19 @@ import com.pengrad.telegrambot.model.Update;
import javax.annotation.Nonnull;
import static cc.sukazyo.cono.morny.Log.logger;
public class OnTelegramCommand extends EventListener {
@Override
public boolean onMessage (@Nonnull Update event) {
if (event.message().text() == null || !event.message().text().startsWith("/")) {
if (event.message().text() == null || !event.message().text().startsWith("/") || event.message().text().startsWith("/ ")) {
logger.debug("not command");
return false; // 检测到非(命令格式)文本忽略掉命令处理
}
final InputCommand command = new InputCommand(event.message().text().substring(1));
if (!command.getCommand().matches("^\\w+$")) { logger.debug("not command");return false; }
logger.debug("is command");
if (command.getTarget() != null && !MornyCoeur.getUsername().equals(command.getTarget())) {
return true; // 检测到命令并非针对 morny退出整个事件处理链
}

View File

@ -41,7 +41,7 @@ public class OnUserSlashAction extends EventListener {
final String[] action = CommonCommand.format(text);
action[0] = action[0].substring(1);
if (action[0].matches("^[a-zA-Z_]+$")) {
if (action[0].matches("^\\w+(@\\w+)?$")) {
return false; // 忽略掉 Telegram 命令格式的输入
} else if (action[0].contains("/")) {
return false; // 忽略掉疑似目录格式的输入

View File

@ -1,19 +1,31 @@
package cc.sukazyo.cono.morny.daemon;
import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.data.TelegramStickers;
import com.pengrad.telegrambot.request.PinChatMessage;
import com.pengrad.telegrambot.request.SendSticker;
import cc.sukazyo.cono.morny.util.CommonFormatUtils;
import com.pengrad.telegrambot.model.Message;
import com.pengrad.telegrambot.model.MessageEntity;
import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.EditMessageText;
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.response.SendResponse;
import java.util.ArrayList;
import java.util.List;
import static cc.sukazyo.cono.morny.Log.logger;
public class MedicationTimer extends Thread {
public static final long NOTIFY_RECEIVE_CHAT = 5028252995L;
public static final long NOTIFY_CHAT = -1001729016815L;
public static final String NOTIFY_MESSAGE = "\uD83C\uDF65⏲";
private static final String DAEMON_THREAD_NAME = "TIMER_Medication";
private static final long LAST_NOTIFY_ID_NULL = -1L;
private long lastNotify = LAST_NOTIFY_ID_NULL;
MedicationTimer () {
super("TIMER_Medication");
super(DAEMON_THREAD_NAME);
}
@Override
@ -34,9 +46,25 @@ public class MedicationTimer extends Thread {
logger.info("MedicationTimer stopped");
}
private static void sendNotification () {
SendResponse m = MornyCoeur.extra().exec(new SendSticker(NOTIFY_RECEIVE_CHAT, TelegramStickers.ID_PROGYNOVA));
if (m.isOk()) MornyCoeur.extra().exec(new PinChatMessage(NOTIFY_RECEIVE_CHAT, m.message().messageId()));
private void sendNotification () {
final SendResponse resp = MornyCoeur.extra().exec(new SendMessage(NOTIFY_CHAT, NOTIFY_MESSAGE));
if (resp.isOk()) lastNotify = resp.message().messageId();
else lastNotify = LAST_NOTIFY_ID_NULL;
}
public void refreshNotificationWrite (Message edited) {
if (edited.messageId() != lastNotify) return;
final String editTime = CommonFormatUtils.formatDate(edited.editDate()*1000, 8);
ArrayList<MessageEntity> entities = new ArrayList<>();
if (edited.entities() != null) entities.addAll(List.of(edited.entities()));
entities.add(new MessageEntity(MessageEntity.Type.italic, edited.text().length() + "\n-- ".length(), editTime.length()));
EditMessageText sending = new EditMessageText(
NOTIFY_CHAT,
edited.messageId(),
edited.text() + "\n-- " + editTime + " --"
).parseMode(ParseMode.HTML).entities(entities.toArray(MessageEntity[]::new));
MornyCoeur.extra().exec(sending);
lastNotify = LAST_NOTIFY_ID_NULL;
}
private static long calcNextRoutineTimestamp () {

View File

@ -4,12 +4,11 @@ import static cc.sukazyo.cono.morny.Log.logger;
public class MornyDaemons {
static MedicationTimer medicationTimerInstance;
public static final MedicationTimer medicationTimerInstance = new MedicationTimer();
public static void start () {
logger.info("ALL Morny Daemons starting...");
TrackerDataManager.init();
medicationTimerInstance = new MedicationTimer();
medicationTimerInstance.start();
logger.info("Morny Daemons started.");
}