为 local bot api server 设定提供支持 #33

This commit is contained in:
A.C.Sukazyo Eyre 2022-06-01 12:40:28 +08:00
parent b35b0600dc
commit 538167dfa9
Signed by: Eyre_S
GPG Key ID: EFB47D98FE082FAD
4 changed files with 70 additions and 16 deletions

View File

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

View File

@ -4,7 +4,7 @@ package cc.sukazyo.cono.morny;
* the final field that will be updated by gradle automatically. * the final field that will be updated by gradle automatically.
*/ */
public class GradleProjectConfigures { public class GradleProjectConfigures {
public static final String VERSION = "0.7.0.15"; public static final String VERSION = "0.7.0.16";
public static final String CODENAME = "fuzhou"; public static final String CODENAME = "fuzhou";
public static final long COMPILE_TIMESTAMP = 1653968380239L; public static final long COMPILE_TIMESTAMP = 1654058419036L;
} }

View File

@ -8,6 +8,7 @@ import cc.sukazyo.cono.morny.daemon.MornyDaemons;
import cc.sukazyo.cono.morny.daemon.TrackerDataManager; import cc.sukazyo.cono.morny.daemon.TrackerDataManager;
import cc.sukazyo.untitled.telegram.api.extra.ExtraAction; import cc.sukazyo.untitled.telegram.api.extra.ExtraAction;
import com.pengrad.telegrambot.TelegramBot; import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.impl.FileApi;
import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.model.User;
import com.pengrad.telegrambot.request.GetMe; import com.pengrad.telegrambot.request.GetMe;
@ -82,6 +83,7 @@ public class MornyCoeur {
* 单位为毫秒 * 单位为毫秒
*/ */
private MornyCoeur ( private MornyCoeur (
@Nullable String botApi, @Nullable String botApi4File,
@Nonnull String botKey, @Nullable String botUsername, @Nonnull String botKey, @Nullable String botUsername,
long master, long trustedChat, Set<Long> trustedRDinner, long master, long trustedChat, Set<Long> trustedRDinner,
long latestEventTimestamp, long latestEventTimestamp,
@ -98,7 +100,7 @@ public class MornyCoeur {
} }
try { try {
final LogInResult loginResult = login(botKey, botUsername); final LogInResult loginResult = login(botApi, botApi4File, botKey, botUsername);
this.account = loginResult.account; this.account = loginResult.account;
this.username = loginResult.username; this.username = loginResult.username;
this.userid = loginResult.userid; this.userid = loginResult.userid;
@ -135,6 +137,7 @@ public class MornyCoeur {
* @see #MornyCoeur 程序初始化方法 * @see #MornyCoeur 程序初始化方法
*/ */
public static void main ( public static void main (
@Nullable String botApi, @Nullable String botApi4File,
@Nonnull String botKey, @Nullable String botUsername, @Nonnull String botKey, @Nullable String botUsername,
long master, long trustedChat, Set<Long> trustedRDinner, long latestEventTimestamp, long master, long trustedChat, Set<Long> trustedRDinner, long latestEventTimestamp,
boolean isAutomaticResetCommandList, boolean isRemoveCommandListWhenExit boolean isAutomaticResetCommandList, boolean isRemoveCommandListWhenExit
@ -142,6 +145,7 @@ public class MornyCoeur {
if (INSTANCE == null) { if (INSTANCE == null) {
logger.info("Coeur Starting"); logger.info("Coeur Starting");
INSTANCE = new MornyCoeur( INSTANCE = new MornyCoeur(
botApi, botApi4File,
botKey, botUsername, botKey, botUsername,
master, trustedChat, trustedRDinner, master, trustedChat, trustedRDinner,
latestEventTimestamp, latestEventTimestamp,
@ -193,12 +197,42 @@ public class MornyCoeur {
* 会通过 GetMe 动作验证是否连接上了 telegram api 服务器 * 会通过 GetMe 动作验证是否连接上了 telegram api 服务器
* 同时也要求登录获得的 username {@link #username} 声明值相等 * 同时也要求登录获得的 username {@link #username} 声明值相等
* *
* @param api bot client 将会连接到的 telegram bot api 位置
* @param api4File bot client 将会连接到的 telegram file api 位置如果不指定则会跟随 {@code api} 选项的设定
* @param key bot api-token * @param key bot api-token
* @param requireName 要求登录到的需要的 username如果登陆后的 username 与此不同则会报错退出
* @return 成功登录后的 {@link TelegramBot} 对象 * @return 成功登录后的 {@link TelegramBot} 对象
*/ */
@Nonnull @Nonnull
private static LogInResult login (@Nonnull String key, @Nullable String requireName) { private static LogInResult login (
final TelegramBot account = new TelegramBot(key); @Nullable String api, @Nullable String api4File,
@Nonnull String key, @Nullable String requireName
) {
final TelegramBot.Builder accountConfig = new TelegramBot.Builder(key);
boolean isCustomApi = false;
String apiUrlSet = "https://api.telegram.org/bot";
String api4FileUrlSet = FileApi.FILE_API;
if (api != null) {
api = api.endsWith("/") ? api.substring(0, api.length() - 1) : api;
accountConfig.apiUrl(apiUrlSet = api.endsWith("/bot")? api : api + "/bot");
isCustomApi = true;
}
if (api4File != null) {
api4File = api4File.endsWith("/") ? api4File : api4File + "/";
accountConfig.fileApiUrl(api4FileUrlSet = api4File.endsWith("/file/bot")? api4File : api4File + "/file/bot");
isCustomApi = true;
} else if (api != null && !api.endsWith("/bot")) {
accountConfig.fileApiUrl(api4FileUrlSet = api + "/file/bot");
}
if (isCustomApi) {
logger.info(String.format("""
Telegram Bot API set to :
- %s
- %s""",
apiUrlSet, api4FileUrlSet
));
}
final TelegramBot account = accountConfig.build();
logger.info("Trying to login..."); logger.info("Trying to login...");
for (int i = 1; i < 4; i++) { for (int i = 1; i < 4; i++) {
if (i != 1) logger.info("retrying..."); if (i != 1) logger.info("retrying...");

View File

@ -43,6 +43,14 @@ public class ServerMain {
* {@code --username} {@link MornyCoeur#getUsername() bot username} 预定义 * {@code --username} {@link MornyCoeur#getUsername() bot username} 预定义
* </li> * </li>
* <li> * <li>
* {@code --api} 设定 {@link MornyCoeur#getAccount() bot client} 使用的 telegram bot api server
* 需要注意的是如果带有后缀 {@code /bot} 则会单独设定 api server
* 而不会适应性的同时为 {@code --api-files} 设定值
* </li>
* <li>
* {@code --api-files} 单独设定 {@link MornyCoeur#getAccount() bot client} 使用的 telegram bot file api server
* </li>
* <li>
* {@code --no-hello} 不在主程序启动时输出用于欢迎消息的字符画 * {@code --no-hello} 不在主程序启动时输出用于欢迎消息的字符画
* {@code --only-hello} 参数不兼容 会导致程序完全没有任何输出 * {@code --only-hello} 参数不兼容 会导致程序完全没有任何输出
* </li> * </li>
@ -60,14 +68,11 @@ public class ServerMain {
* {@code --auto-cmd-remove} 使 morny 在关闭时自动依据程序本体删除 bot 的命令列表 * {@code --auto-cmd-remove} 使 morny 在关闭时自动依据程序本体删除 bot 的命令列表
* </li> * </li>
* </ul> * </ul>
* 除去选项之外第一个参数会被赋值为 bot telegram bot api token * <s>除去选项之外第一个参数会被赋值为 bot telegram bot api token</s>
* 第二个参数会被赋值为 bot username 限定名其余的参数会被认定为无法理解<br> * <s>第二个参数会被赋值为 bot username 限定名其余的参数会被认定为无法理解</s><br>
* <br>
* <b> {@code 0.4.2.3}token username 的赋值已被选项组支持</b><br> * <b> {@code 0.4.2.3}token username 的赋值已被选项组支持</b><br>
* 使用参数所进行取值的 token username 已被转移至 {@code --token} {@code --username} 参数 * <b> {@code 0.5.0.4}旧的直接通过参数为 bot token & username 赋值的方式已被删除</b>
* <u>或许直接参数赋值的支持将计划在 {@code 0.4.3} 标记废弃并在 {@code 0.5} 删除</u> * 使用参数所进行取值的 token username 已被转移至 {@code --token} {@code --username} 参数<br>
* <s>但实际上这并不影响现在的使用选项赋值目前仍属于测试功能</s><br>
* <b>但请勿混用</b>这将使两个赋值出现混淆并<b>产生不可知的结果</b>
* *
* @see MornyCoeur#main * @see MornyCoeur#main
* @since 0.4.0.0 * @since 0.4.0.0
@ -90,6 +95,8 @@ public class ServerMain {
long trustedChat = -1001541451710L; long trustedChat = -1001541451710L;
boolean autoCmdList = false; boolean autoCmdList = false;
boolean autoCmdRemove = false; boolean autoCmdRemove = false;
String api = null;
String api4File = null;
for (int i = 0; i < args.length; i++) { for (int i = 0; i < args.length; i++) {
@ -151,6 +158,16 @@ public class ServerMain {
autoCmdRemove = true; autoCmdRemove = true;
continue; continue;
} }
case "--api", "-a" -> {
i++;
api = args[i];
continue;
}
case "--api-files", "files-api", "-af" -> {
i++;
api4File = args[i];
continue;
}
} }
} }
@ -197,9 +214,11 @@ public class ServerMain {
logger.info(String.format(""" logger.info(String.format("""
ServerMain.java Loaded >>> ServerMain.java Loaded >>>
- version %s %s (%s)(%d)""", - version %s (%s)(%d)
MornySystem.VERSION, MornySystem.CODENAME.toUpperCase(), - Morny %s""",
MornySystem.getJarMd5(), GradleProjectConfigures.COMPILE_TIMESTAMP MornySystem.VERSION,
MornySystem.getJarMd5(), GradleProjectConfigures.COMPILE_TIMESTAMP,
MornySystem.CODENAME.toUpperCase()
)); ));
//# //#
@ -215,6 +234,7 @@ public class ServerMain {
return; return;
} }
MornyCoeur.main( MornyCoeur.main(
api, api4File,
key, username, key, username,
master, trustedChat, trustedReadersOfDinner, master, trustedChat, trustedReadersOfDinner,
outdatedBlock?System.currentTimeMillis():0, outdatedBlock?System.currentTimeMillis():0,