added possibility to work via HTTP proxy

This commit is contained in:
flicus 2016-03-11 14:27:24 +03:00
parent 5db2f3ddeb
commit 5950bbd94b
4 changed files with 89 additions and 0 deletions

View File

@ -1,6 +1,8 @@
package org.telegram.telegrambots;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
@ -13,6 +15,7 @@ import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.telegram.telegrambots.api.Constants;
import org.telegram.telegrambots.api.TelegramApiConfiguration;
import org.telegram.telegrambots.api.methods.SetWebhook;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.bots.TelegramWebhookBot;
@ -137,6 +140,13 @@ public class TelegramBotsApi {
String url = Constants.BASEURL + botToken + "/" + SetWebhook.PATH;
HttpPost httppost = new HttpPost(url);
if (TelegramApiConfiguration.getInstance().getProxy() != null) {
RequestConfig requestConfig = RequestConfig.custom()
.setProxy(TelegramApiConfiguration.getInstance().getProxy())
.build();
httppost.setConfig(requestConfig);
}
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody(SetWebhook.URL_FIELD, webHookURL);
if (publicCertificatePath != null) {

View File

@ -0,0 +1,32 @@
package org.telegram.telegrambots.api;
import org.apache.http.HttpHost;
/**
* Created by Sergey Skoptsov (flicus@gmail.com) on 11.03.2016.
*/
public class TelegramApiConfiguration {
private HttpHost proxy = null;
public static TelegramApiConfiguration getInstance() {
return Singleton.instance;
}
public HttpHost getProxy() {
return proxy;
}
public void setProxy(HttpHost proxy) {
this.proxy = proxy;
}
public void setProxy(String host, int port, String scheme) {
this.proxy = new HttpHost(host, port, scheme);
}
private static class Singleton {
public static TelegramApiConfiguration instance = new TelegramApiConfiguration();
}
}

View File

@ -2,6 +2,7 @@ package org.telegram.telegrambots.bots;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
@ -18,6 +19,7 @@ import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.telegram.telegrambots.TelegramApiException;
import org.telegram.telegrambots.api.Constants;
import org.telegram.telegrambots.api.TelegramApiConfiguration;
import org.telegram.telegrambots.api.methods.*;
import org.telegram.telegrambots.api.objects.File;
import org.telegram.telegrambots.api.objects.Message;
@ -185,6 +187,12 @@ public abstract class AbsSender {
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = getBaseUrl() + SendDocument.PATH;
HttpPost httppost = new HttpPost(url);
if (TelegramApiConfiguration.getInstance().getProxy() != null) {
RequestConfig requestConfig = RequestConfig.custom()
.setProxy(TelegramApiConfiguration.getInstance().getProxy())
.build();
httppost.setConfig(requestConfig);
}
if (sendDocument.isNewDocument()) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
@ -233,6 +241,12 @@ public abstract class AbsSender {
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = getBaseUrl() + SendPhoto.PATH;
HttpPost httppost = new HttpPost(url);
if (TelegramApiConfiguration.getInstance().getProxy() != null) {
RequestConfig requestConfig = RequestConfig.custom()
.setProxy(TelegramApiConfiguration.getInstance().getProxy())
.build();
httppost.setConfig(requestConfig);
}
if (sendPhoto.isNewPhoto()) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
@ -287,6 +301,12 @@ public abstract class AbsSender {
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = getBaseUrl() + SendVideo.PATH;
HttpPost httppost = new HttpPost(url);
if (TelegramApiConfiguration.getInstance().getProxy() != null) {
RequestConfig requestConfig = RequestConfig.custom()
.setProxy(TelegramApiConfiguration.getInstance().getProxy())
.build();
httppost.setConfig(requestConfig);
}
if (sendVideo.isNewVideo()) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
@ -348,6 +368,12 @@ public abstract class AbsSender {
CloseableHttpClient httpClient = HttpClients.createDefault();
String url = getBaseUrl() + SendSticker.PATH;
HttpPost httppost = new HttpPost(url);
if (TelegramApiConfiguration.getInstance().getProxy() != null) {
RequestConfig requestConfig = RequestConfig.custom()
.setProxy(TelegramApiConfiguration.getInstance().getProxy())
.build();
httppost.setConfig(requestConfig);
}
if (sendSticker.isNewSticker()) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
@ -396,6 +422,12 @@ public abstract class AbsSender {
CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
String url = getBaseUrl() + method.getPath();
HttpPost httppost = new HttpPost(url);
if (TelegramApiConfiguration.getInstance().getProxy() != null) {
RequestConfig requestConfig = RequestConfig.custom()
.setProxy(TelegramApiConfiguration.getInstance().getProxy())
.build();
httppost.setConfig(requestConfig);
}
httppost.addHeader("charset", "UTF-8");
httppost.setEntity(new StringEntity(method.toJson().toString(), ContentType.APPLICATION_JSON));
CloseableHttpResponse response = httpclient.execute(httppost);
@ -420,6 +452,12 @@ public abstract class AbsSender {
CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
String url = getBaseUrl() + method.getPath();
HttpPost httppost = new HttpPost(url);
if (TelegramApiConfiguration.getInstance().getProxy() != null) {
RequestConfig requestConfig = RequestConfig.custom()
.setProxy(TelegramApiConfiguration.getInstance().getProxy())
.build();
httppost.setConfig(requestConfig);
}
httppost.addHeader("charset", "UTF-8");
httppost.setEntity(new StringEntity(method.toJson().toString(), ContentType.APPLICATION_JSON));
CloseableHttpResponse response = httpclient.execute(httppost);

View File

@ -1,7 +1,9 @@
package org.telegram.telegrambots.updatesreceivers;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.BufferedHttpEntity;
@ -14,6 +16,7 @@ import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.telegram.telegrambots.api.Constants;
import org.telegram.telegrambots.api.TelegramApiConfiguration;
import org.telegram.telegrambots.api.methods.GetUpdates;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.ITelegramLongPollingBot;
@ -58,6 +61,12 @@ public class UpdatesThread {
CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).setConnectionTimeToLive(20, TimeUnit.SECONDS).build();
String url = Constants.BASEURL + token + "/" + GetUpdates.PATH;
HttpPost httpPost = new HttpPost(url);
if (TelegramApiConfiguration.getInstance().getProxy() != null) {
RequestConfig requestConfig = RequestConfig.custom()
.setProxy(TelegramApiConfiguration.getInstance().getProxy())
.build();
httpPost.setConfig(requestConfig);
}
try {
httpPost.addHeader("charset", "UTF-8");
httpPost.setEntity(new StringEntity(request.toJson().toString(), ContentType.APPLICATION_JSON));