support for custom apiurl

This commit is contained in:
MrBesen 2020-11-04 17:17:18 +01:00 committed by mrbesen
parent a51a07c238
commit 35eb9e71c6
Signed by: MrBesen
GPG Key ID: 596B2350DCD67504
1 changed files with 11 additions and 6 deletions

View File

@ -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);