Support custom api url

This commit is contained in:
Ruben Bermudez 2017-05-11 03:02:54 +02:00
parent 48a7503c7f
commit db229bb66e
7 changed files with 17 additions and 7 deletions

View File

@ -3,8 +3,7 @@ package org.telegram.telegrambots;
/**
* @author Ruben Bermudez
* @version 1.0
* @brief Constants needed for Telegram Bots API
* @date 20 of June of 2015
* Constants needed for Telegram Bots API
*/
public class ApiConstants {
public static final String BASE_URL = "https://api.telegram.org/bot";

View File

@ -7,4 +7,5 @@ package org.telegram.telegrambots.generics;
* @date 29 of October of 2016
*/
public interface BotOptions {
String getBaseUrl();
}

View File

@ -17,7 +17,6 @@ import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.telegram.telegrambots.ApiConstants;
import org.telegram.telegrambots.api.methods.BotApiMethod;
import org.telegram.telegrambots.api.methods.send.*;
import org.telegram.telegrambots.api.objects.File;
@ -755,6 +754,6 @@ public abstract class DefaultAbsSender extends AbsSender {
}
protected String getBaseUrl() {
return ApiConstants.BASE_URL + getBotToken() + "/";
return options.getBaseUrl() + getBotToken() + "/";
}
}

View File

@ -1,6 +1,7 @@
package org.telegram.telegrambots.bots;
import org.apache.http.client.config.RequestConfig;
import org.telegram.telegrambots.ApiConstants;
import org.telegram.telegrambots.generics.BotOptions;
import org.telegram.telegrambots.updatesreceivers.ExponentialBackOff;
@ -17,10 +18,20 @@ public class DefaultBotOptions implements BotOptions {
private RequestConfig requestConfig;
private ExponentialBackOff exponentialBackOff;
private Integer maxWebhookConnections;
private String baseUrl;
private List<String> allowedUpdates;
public DefaultBotOptions() {
maxThreads = 1;
baseUrl = ApiConstants.BASE_URL;
}
public String getBaseUrl() {
return baseUrl;
}
public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}
public void setMaxThreads(int maxThreads) {

View File

@ -38,7 +38,7 @@ public abstract class TelegramLongPollingBot extends DefaultAbsSender implements
@Override
public void clearWebhook() throws TelegramApiRequestException {
try (CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build()) {
String url = ApiConstants.BASE_URL + getBotToken() + "/" + SetWebhook.PATH;
String url = getOptions().getBaseUrl() + getBotToken() + "/" + SetWebhook.PATH;
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(getOptions().getRequestConfig());
try (CloseableHttpResponse response = httpclient.execute(httpGet)) {

View File

@ -45,7 +45,7 @@ public abstract class TelegramWebhookBot extends DefaultAbsSender implements Web
@Override
public void setWebhook(String url, String publicCertificatePath) throws TelegramApiRequestException {
try (CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build()) {
String requestUrl = ApiConstants.BASE_URL + getBotToken() + "/" + SetWebhook.PATH;
String requestUrl = getBaseUrl() + getBotToken() + "/" + SetWebhook.PATH;
HttpPost httppost = new HttpPost(requestUrl);
httppost.setConfig(botOptions.getRequestConfig());

View File

@ -186,7 +186,7 @@ public class DefaultBotSession implements BotSession {
request.setAllowedUpdates(options.getAllowedUpdates());
}
String url = ApiConstants.BASE_URL + token + "/" + GetUpdates.PATH;
String url = options.getBaseUrl() + token + "/" + GetUpdates.PATH;
//http client
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("charset", StandardCharsets.UTF_8.name());