From 35eb9e71c6b88725bce8f78007ff1a8f6a4f757c Mon Sep 17 00:00:00 2001 From: MrBesen Date: Wed, 4 Nov 2020 17:17:18 +0100 Subject: [PATCH] support for custom apiurl --- .../java/de/mrbesen/telegram/TelegramAPI.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/mrbesen/telegram/TelegramAPI.java b/src/main/java/de/mrbesen/telegram/TelegramAPI.java index c9042b0..9c768e2 100644 --- a/src/main/java/de/mrbesen/telegram/TelegramAPI.java +++ b/src/main/java/de/mrbesen/telegram/TelegramAPI.java @@ -37,7 +37,7 @@ import java.util.Scanner; public class TelegramAPI implements Runnable { - private static final String API_URL = "https://api.telegram.org/bot"; + private static final String API_URL_DEFAULT = "https://api.telegram.org/bot"; private static final String TOKENREGEX = "^\\d{4,10}:[\\w-]{12,64}$"; private static final int TELEGRAMFILESIZELIMIT = 20000000;//20MB filesize https://core.telegram.org/bots/api#sending-files public static final String APIVERSION = "3.9";//Feb 29, 2020 @@ -52,6 +52,7 @@ public class TelegramAPI implements Runnable { private boolean longpolling = true; @Setter @Getter private boolean disableFeedback = false; + private String apiurl; private String helpmessage = "generic helppage\nuse TelegramAPI.setHelpText(java.lang.String) to change this."; @@ -86,17 +87,21 @@ public class TelegramAPI implements Runnable { * please only use if bot is not used in groups * @param apikey */ - public TelegramAPI(String apikey) { - this(apikey, ""); + + public TelegramAPI(String apikey) { this(null, apikey); } + + public TelegramAPI(String apiurl, String apikey) { + this(apiurl, apikey, ""); } - public TelegramAPI(String apikey, String botname) { + public TelegramAPI(String apiurl, String apikey, String botname) { + this.apiurl = (apiurl == null ? API_URL_DEFAULT : apiurl); this.botname = botname != null ? botname : ""; if (!apikey.matches(TOKENREGEX) ) { throw new IllegalArgumentException("Invalid API key: " + apikey); } - this.apikey = apikey; + this.apiurl += this.apikey = apikey; } public void start() { @@ -137,7 +142,7 @@ public class TelegramAPI implements Runnable { while(toomany) { toomany = false; ++trycount; - URL url = new URL(API_URL + apikey + "/" + request); + URL url = new URL(apiurl + "/" + request); HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); con.setDoInput(true); con.setDoOutput(true);