简单log实现,完成tg连线,响应在线和退出命令,退出命令的简单身份验证

This commit is contained in:
A.C.Sukazyo Eyre 2021-11-06 20:43:56 +08:00
parent 646fca642a
commit aed6212ff1
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
6 changed files with 187 additions and 1 deletions

View File

@ -3,15 +3,19 @@ plugins {
} }
group 'cc.sukazyo' group 'cc.sukazyo'
version '1.0-dev' version '0.1'
repositories { repositories {
mavenCentral() mavenCentral()
} }
dependencies { dependencies {
implementation 'com.github.pengrad:java-telegram-bot-api:5.3.0'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
} }
test { test {

View File

@ -0,0 +1,25 @@
package cc.sukazyo.cono.morny;
public class Logger {
public static final Logger logger = new Logger();
public void info(String message) {
System.out.println(
"[INFO]" +
message.replaceAll("\\n", "\n[INFO]")
);
}
public void warn (String message) {
waring(message);
}
public void waring (String message) {
System.out.println(
"[WARN]" +
message.replaceAll("\\n", "\n[WARN]")
);
}
}

View File

@ -0,0 +1,49 @@
package cc.sukazyo.cono.morny;
import cc.sukazyo.cono.morny.bot.OnUpdate;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.request.GetMe;
import static cc.sukazyo.cono.morny.Logger.logger;
public class MornyCoeur {
private static TelegramBot account;
public static void main (String[] args) {
logger.info("System Starting");
logger.info("args key: " + args[0]);
account = login(args[0]);
logger.info("Bot login succeed.");
account.setUpdatesListener(OnUpdate::onNormalUpdate);
logger.info("System start complete");
}
public static TelegramBot login (String key) {
TelegramBot account = new TelegramBot(key);
logger.info("Trying to login...");
for (int i = 1; i < 4; i++) {
if (i != 1) logger.info("retrying...");
try {
logger.info("Succeed login to @" + account.execute(new GetMe()).user().username());
return account;
} catch (Exception e) {
e.printStackTrace(System.out);
logger.info("login failed.");
}
}
throw new RuntimeException("Login failed..");
}
public static TelegramBot getAccount () {
return account;
}
}

View File

@ -0,0 +1,20 @@
package cc.sukazyo.cono.morny;
import com.pengrad.telegrambot.model.ChatMember;
import com.pengrad.telegrambot.request.GetChatMember;
public class MornyTrusted {
public static final long TRUSTED_CHAT_ID = -1001541451710L;
public static boolean isTrusted (long userId) {
ChatMember chatMember = MornyCoeur.getAccount().execute(new GetChatMember(TRUSTED_CHAT_ID, userId)).chatMember();
return (
chatMember != null && (
chatMember.status() == ChatMember.Status.administrator ||
chatMember.status() == ChatMember.Status.creator
)
);
}
}

View File

@ -0,0 +1,67 @@
package cc.sukazyo.cono.morny.bot;
import cc.sukazyo.cono.morny.MornyCoeur;
import cc.sukazyo.cono.morny.MornyTrusted;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.request.SendSticker;
import static cc.sukazyo.cono.morny.Logger.logger;
public class OnCommandExecute {
private static final String ONLINE_STATUS_RETURN_STICKER_ID = "CAACAgEAAx0CW-CvvgAC5eBhhhODGRuu0pxKLwoQ3yMsowjviAACcycAAnj8xgVVU666si1utiIE";
private static final String HELLO_STICKER_ID = "CAACAgEAAxkBAAMnYYYWKNXO4ibo9dlsmDctHhhV6fIAAqooAAJ4_MYFJJhrHS74xUAiBA";
private static final String EXIT_STICKER_ID = "CAACAgEAAxkBAAMoYYYWt8UjvP0N405SAyvg2SQZmokAAkMiAAJ4_MYFw6yZLu06b-MiBA";
private static final String EXIT_403_STICKER_ID = "CAACAgEAAxkBAAMqYYYa_7hpXH6hMOYMX4Nh8AVYd74AAnQnAAJ4_MYFRdmmsQKLDZgiBA";
public static void searchForCommands (Update event) {
switch (event.message().text()) {
case "/o":
onCommandOnExec(event);
break;
case "/hi":
case "/hello":
onCommandHelloExec(event);
break;
case "/exit":
onCommandExitExec(event);
break;
default:
}
}
private static void onCommandOnExec (Update event) {
MornyCoeur.getAccount().execute(new SendSticker(
event.message().chat().id(),
ONLINE_STATUS_RETURN_STICKER_ID
).replyToMessageId(event.message().messageId())
);
}
private static void onCommandHelloExec (Update event) {
MornyCoeur.getAccount().execute(new SendSticker(
event.message().chat().id(),
HELLO_STICKER_ID
).replyToMessageId(event.message().messageId())
);
}
private static void onCommandExitExec (Update event) {
if (MornyTrusted.isTrusted(event.message().from().id())) {
MornyCoeur.getAccount().execute(new SendSticker(
event.message().chat().id(),
EXIT_STICKER_ID
).replyToMessageId(event.message().messageId())
);
logger.info("Morny exited by user @" + event.message().from().username());
System.exit(0);
} else {
MornyCoeur.getAccount().execute(new SendSticker(
event.message().chat().id(),
EXIT_403_STICKER_ID
).replyToMessageId(event.message().messageId())
);
}
}
}

View File

@ -0,0 +1,21 @@
package cc.sukazyo.cono.morny.bot;
import com.pengrad.telegrambot.UpdatesListener;
import com.pengrad.telegrambot.model.Update;
import java.util.List;
public class OnUpdate {
public static int onNormalUpdate (List<Update> updates) {
for (Update update : updates) {
if (update.message() != null) {
if (update.message().text() != null) {
new Thread(() -> OnCommandExecute.searchForCommands(update)).start();
}
}
}
return UpdatesListener.CONFIRMED_UPDATES_ALL;
}
}