From 538167dfa9cc6a1d81dd691396b0f2ab7edd15ef Mon Sep 17 00:00:00 2001 From: Eyre_S Date: Wed, 1 Jun 2022 12:40:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BA=20local=20bot=20api=20server=20?= =?UTF-8?q?=E8=AE=BE=E5=AE=9A=E6=8F=90=E4=BE=9B=E6=94=AF=E6=8C=81=20#33?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../cono/morny/GradleProjectConfigures.java | 4 +- .../cc/sukazyo/cono/morny/MornyCoeur.java | 40 +++++++++++++++++-- .../cc/sukazyo/cono/morny/ServerMain.java | 40 ++++++++++++++----- 4 files changed, 70 insertions(+), 16 deletions(-) diff --git a/gradle.properties b/gradle.properties index ab4a9cd..5a613bb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ ## Core -VERSION = 0.7.0.15 +VERSION = 0.7.0.16 CODENAME = fuzhou diff --git a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java index f471d62..da03946 100644 --- a/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java +++ b/src/main/java/cc/sukazyo/cono/morny/GradleProjectConfigures.java @@ -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.15"; + public static final String VERSION = "0.7.0.16"; public static final String CODENAME = "fuzhou"; - public static final long COMPILE_TIMESTAMP = 1653968380239L; + public static final long COMPILE_TIMESTAMP = 1654058419036L; } diff --git a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java index f010985..ac5a6dd 100644 --- a/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java +++ b/src/main/java/cc/sukazyo/cono/morny/MornyCoeur.java @@ -8,6 +8,7 @@ import cc.sukazyo.cono.morny.daemon.MornyDaemons; import cc.sukazyo.cono.morny.daemon.TrackerDataManager; import cc.sukazyo.untitled.telegram.api.extra.ExtraAction; import com.pengrad.telegrambot.TelegramBot; +import com.pengrad.telegrambot.impl.FileApi; import com.pengrad.telegrambot.model.User; import com.pengrad.telegrambot.request.GetMe; @@ -82,6 +83,7 @@ public class MornyCoeur { * 单位为毫秒 */ private MornyCoeur ( + @Nullable String botApi, @Nullable String botApi4File, @Nonnull String botKey, @Nullable String botUsername, long master, long trustedChat, Set trustedRDinner, long latestEventTimestamp, @@ -98,7 +100,7 @@ public class MornyCoeur { } try { - final LogInResult loginResult = login(botKey, botUsername); + final LogInResult loginResult = login(botApi, botApi4File, botKey, botUsername); this.account = loginResult.account; this.username = loginResult.username; this.userid = loginResult.userid; @@ -135,6 +137,7 @@ public class MornyCoeur { * @see #MornyCoeur 程序初始化方法 */ public static void main ( + @Nullable String botApi, @Nullable String botApi4File, @Nonnull String botKey, @Nullable String botUsername, long master, long trustedChat, Set trustedRDinner, long latestEventTimestamp, boolean isAutomaticResetCommandList, boolean isRemoveCommandListWhenExit @@ -142,6 +145,7 @@ public class MornyCoeur { if (INSTANCE == null) { logger.info("Coeur Starting"); INSTANCE = new MornyCoeur( + botApi, botApi4File, botKey, botUsername, master, trustedChat, trustedRDinner, latestEventTimestamp, @@ -193,12 +197,42 @@ public class MornyCoeur { * 会通过 GetMe 动作验证是否连接上了 telegram api 服务器, * 同时也要求登录获得的 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 requireName 要求登录到的需要的 username,如果登陆后的 username 与此不同则会报错退出 * @return 成功登录后的 {@link TelegramBot} 对象 */ @Nonnull - private static LogInResult login (@Nonnull String key, @Nullable String requireName) { - final TelegramBot account = new TelegramBot(key); + private static LogInResult login ( + @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..."); for (int i = 1; i < 4; i++) { if (i != 1) logger.info("retrying..."); diff --git a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java index eb1ef08..d7ec325 100644 --- a/src/main/java/cc/sukazyo/cono/morny/ServerMain.java +++ b/src/main/java/cc/sukazyo/cono/morny/ServerMain.java @@ -43,6 +43,14 @@ public class ServerMain { * {@code --username} {@link MornyCoeur#getUsername() bot 的 username} 预定义 * *
  • + * {@code --api} 设定 {@link MornyCoeur#getAccount() bot client} 使用的 telegram bot api server。 + * 需要注意的是如果带有后缀 {@code /bot} 则会单独设定 api server + * 而不会适应性的同时为 {@code --api-files} 设定值。 + *
  • + *
  • + * {@code --api-files} 单独设定 {@link MornyCoeur#getAccount() bot client} 使用的 telegram bot file api server + *
  • + *
  • * {@code --no-hello} 不在主程序启动时输出用于欢迎消息的字符画。 * 与 {@code --only-hello} 参数不兼容 —— 会导致程序完全没有任何输出 *
  • @@ -60,14 +68,11 @@ public class ServerMain { * {@code --auto-cmd-remove} 使 morny 在关闭时自动依据程序本体删除 bot 的命令列表 * * - * 除去选项之外,第一个参数会被赋值为 bot 的 telegram bot api token, - * 第二个参数会被赋值为 bot 的 username 限定名。其余的参数会被认定为无法理解。
    - *
    + * 除去选项之外,第一个参数会被赋值为 bot 的 telegram bot api token, + * 第二个参数会被赋值为 bot 的 username 限定名。其余的参数会被认定为无法理解。
    * 自 {@code 0.4.2.3},token 和 username 的赋值已被选项组支持
    - * 使用参数所进行取值的 token 和 username 已被转移至 {@code --token} 和 {@code --username} 参数, - * 或许,直接参数赋值的支持将计划在 {@code 0.4.3} 标记废弃并在 {@code 0.5} 删除。 - * 但实际上这并不影响现在的使用,选项赋值目前仍属于测试功能
    - * 但请勿混用,这将使两个赋值出现混淆并产生不可知的结果 + * 自 {@code 0.5.0.4},旧的直接通过参数为 bot token & username 赋值的方式已被删除 + * 使用参数所进行取值的 token 和 username 已被转移至 {@code --token} 和 {@code --username} 参数
    * * @see MornyCoeur#main * @since 0.4.0.0 @@ -90,6 +95,8 @@ public class ServerMain { long trustedChat = -1001541451710L; boolean autoCmdList = false; boolean autoCmdRemove = false; + String api = null; + String api4File = null; for (int i = 0; i < args.length; i++) { @@ -151,6 +158,16 @@ public class ServerMain { autoCmdRemove = true; 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(""" ServerMain.java Loaded >>> - - version %s %s (%s)(%d)""", - MornySystem.VERSION, MornySystem.CODENAME.toUpperCase(), - MornySystem.getJarMd5(), GradleProjectConfigures.COMPILE_TIMESTAMP + - version %s (%s)(%d) + - Morny %s""", + MornySystem.VERSION, + MornySystem.getJarMd5(), GradleProjectConfigures.COMPILE_TIMESTAMP, + MornySystem.CODENAME.toUpperCase() )); //# @@ -215,6 +234,7 @@ public class ServerMain { return; } MornyCoeur.main( + api, api4File, key, username, master, trustedChat, trustedReadersOfDinner, outdatedBlock?System.currentTimeMillis():0,