添加 calling"安妮今天吃什么" 和其权限控制参数

> merged from major/0.6-hot-update/calling-last-dinner-get
- 添加对私聊的 "安妮今天吃什么" 事件处理
- 添加 --trusted-reader-dinner 参数用于指定一个用户可以获取"安妮今天吃什么"的数据
  - 参数可以重复指定,以便为多个账户授权
This commit is contained in:
A.C.Sukazyo Eyre 2022-05-10 23:10:38 +08:00
parent b5941494f6
commit 7ce151b9bf
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
6 changed files with 86 additions and 11 deletions

View File

@ -1,6 +1,6 @@
## Core
VERSION = 0.6.3.2
VERSION = 0.6.3.3
# 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.6.3.2";
public static final long COMPILE_TIMESTAMP = 1651806802187L;
public static final String VERSION = "0.6.3.3";
public static final long COMPILE_TIMESTAMP = 1652195187089L;
}

View File

@ -6,13 +6,12 @@ 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;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.DeleteMyCommands;
import com.pengrad.telegrambot.request.GetMe;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Set;
import static cc.sukazyo.cono.morny.Log.logger;
@ -58,6 +57,8 @@ public class MornyCoeur {
*/
public static final long coeurStartTimestamp = System.currentTimeMillis();
public static final long DINNER_CHAT_ID = -1001707106392L;
private record LogInResult(TelegramBot account, String username) { }
/**
@ -74,7 +75,8 @@ public class MornyCoeur {
*/
private MornyCoeur (
@Nonnull String botKey, @Nullable String botUsername,
long master, long trustedChat, long latestEventTimestamp,
long master, long trustedChat, Set<Long> trustedRDinner,
long latestEventTimestamp,
boolean isRemoveCommandListWhenExit
) {
@ -91,7 +93,7 @@ public class MornyCoeur {
final LogInResult loginResult = login(botKey, botUsername);
this.account = loginResult.account;
this.username = loginResult.username;
this.trusted = new MornyTrusted(master, trustedChat);
this.trusted = new MornyTrusted(master, trustedChat, trustedRDinner);
logger.info(String.format("""
trusted param set:
- master (id)
@ -122,14 +124,14 @@ public class MornyCoeur {
*/
public static void main (
@Nonnull String botKey, @Nullable String botUsername,
long master, long trustedChat, long latestEventTimestamp,
long master, long trustedChat, Set<Long> trustedRDinner, long latestEventTimestamp,
boolean isAutomaticResetCommandList, boolean isRemoveCommandListWhenExit
) {
if (INSTANCE == null) {
logger.info("System Starting");
INSTANCE = new MornyCoeur(
botKey, botUsername,
master, trustedChat,
master, trustedChat, trustedRDinner,
latestEventTimestamp,
isRemoveCommandListWhenExit
);

View File

@ -1,6 +1,8 @@
package cc.sukazyo.cono.morny;
import com.pengrad.telegrambot.model.ChatMember.Status;
import java.util.HashSet;
import java.util.Set;
/**
* 对用户进行身份权限验证的管理类
@ -19,9 +21,15 @@ public class MornyTrusted {
*/
public final long MASTER;
public MornyTrusted (long master, long trustedChatId) {
public final Set<Long> TRUSTED_READERS_OF_DINNER;
public MornyTrusted (long master, long trustedChatId, Set<Long> trustedRDinner) {
this.TRUSTED_CHAT_ID = trustedChatId;
this.MASTER = master;
this.TRUSTED_READERS_OF_DINNER = new HashSet<>(){{
this.add(master);
this.addAll(trustedRDinner);
}};
}
/**
@ -39,4 +47,8 @@ public class MornyTrusted {
return MornyCoeur.extra().isUserInGroup(userId, TRUSTED_CHAT_ID, Status.administrator);
}
public boolean isTrustedForDinnerRead (long userId) {
return TRUSTED_READERS_OF_DINNER.contains(userId);
}
}

View File

@ -4,6 +4,9 @@ import cc.sukazyo.cono.morny.util.CommonFormatUtils;
import javax.annotation.Nonnull;
import java.util.HashSet;
import java.util.Set;
import static cc.sukazyo.cono.morny.Log.logger;
/**
@ -75,6 +78,7 @@ public class ServerMain {
String username = null;
boolean outdatedBlock = false;
long master = 793274677L;
Set<Long> trustedReadersOfDinner = new HashSet<>();
long trustedChat = -1001541451710L;
boolean autoCmdList = false;
boolean autoCmdRemove = false;
@ -120,6 +124,10 @@ public class ServerMain {
trustedChat = Long.parseLong(args[i]);
continue;
}
case "--trusted-reader-dinner" -> {
trustedReadersOfDinner.add(Long.parseLong(args[i]));
continue;
}
case "--auto-cmd-list", "-ca" -> {
autoCmdList = true;
continue;
@ -165,7 +173,7 @@ public class ServerMain {
}
MornyCoeur.main(
key, username,
master, trustedChat,
master, trustedChat, trustedReadersOfDinner,
outdatedBlock?System.currentTimeMillis():0,
autoCmdList, autoCmdRemove
);

View File

@ -4,13 +4,18 @@ import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.MornyTrusted;
import cc.sukazyo.cono.morny.bot.api.EventListener;
import cc.sukazyo.cono.morny.data.TelegramStickers;
import cc.sukazyo.cono.morny.util.CommonFormatUtils;
import cc.sukazyo.untitled.telegram.api.formatting.TGToString;
import cc.sukazyo.untitled.util.telegram.formatting.MsgEscape;
import com.pengrad.telegrambot.model.Chat;
import com.pengrad.telegrambot.model.Message;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.ForwardMessage;
import com.pengrad.telegrambot.request.GetChat;
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.request.SendSticker;
import com.pengrad.telegrambot.response.SendResponse;
import javax.annotation.Nonnull;
@ -46,6 +51,8 @@ public class OnCallMe extends EventListener {
requestSteamJoin(update);
case "hana paresu", "花宫", "内群" ->
requestHanaParesuJoin(update);
case "dinner", "lunch", "breakfast", "meal", "eating", "安妮今天吃什么" ->
requestLastDinner(update);
default -> {
if (update.message().text().startsWith("cc::")) {
requestCustomCall(update);
@ -96,6 +103,52 @@ public class OnCallMe extends EventListener {
).parseMode(ParseMode.HTML));
}
/**
* 对访问最近一次的饭局的请求进行回复<br>
*
* @param event 执行呼叫的tg事件
*/
private static void requestLastDinner (Update event) {
boolean isAllowed = false;
Message lastDinnerData = null;
if (MornyCoeur.trustedInstance().isTrustedForDinnerRead(event.message().from().id())) {
lastDinnerData = MornyCoeur.extra().exec(new GetChat(MornyCoeur.DINNER_CHAT_ID)).chat().pinnedMessage();
SendResponse sendResp = MornyCoeur.extra().exec(new ForwardMessage(
event.message().from().id(),
lastDinnerData.forwardFromChat().id(),
lastDinnerData.forwardFromMessageId()
));
MornyCoeur.extra().exec(new SendMessage(
event.message().from().id(),
String.format("<i>on</i> <code>%s [UTC+8]</code>\n- <code>%s</code> <i>before</i>",
MsgEscape.escapeHtml(
CommonFormatUtils.formatDate((long)lastDinnerData.forwardDate()*1000, 8)
), MsgEscape.escapeHtml(
CommonFormatUtils.formatDuration(System.currentTimeMillis()-(long)lastDinnerData.forwardDate()*1000)
)
)
).replyToMessageId(sendResp.message().messageId()).parseMode(ParseMode.HTML));
isAllowed = true;
} else {
MornyCoeur.extra().exec(new SendSticker(
event.message().from().id(),
TelegramStickers.ID_403
).replyToMessageId(event.message().messageId()));
}
MornyCoeur.extra().exec(new SendMessage(
ME, String.format(
"""
request <b>Last Annie Dinner</b>
from %s
%s""",
TGToString.as(event.message().from()).fullnameRefHtml(),
isAllowed ? "Allowed and returned " + String.format(
"https://t.me/c/%d/%d", Math.abs(lastDinnerData.forwardFromChat().id()+1000000000000L), lastDinnerData.forwardFromMessageId()
) : "Forbidden by perm check."
)
).parseMode(ParseMode.HTML));
}
/**
* 执行自定义呼叫<br>
* 将会向 {@link #ME} 发送一个 request 数据消息和转发的原始请求消息<br>