Merge branch 'dev' of https://github.com/bvn13/TelegramBots into bvn13-dev
This commit is contained in:
commit
9733f35ef2
122
TelegramBots.wiki/Using-Http-Proxy.md
Normal file
122
TelegramBots.wiki/Using-Http-Proxy.md
Normal file
@ -0,0 +1,122 @@
|
|||||||
|
### Using HTTP proxy
|
||||||
|
|
||||||
|
HTTP proxy support implemented since version 3.6.1
|
||||||
|
|
||||||
|
First of all you need to override constructor with `DefaultBotOptions` argument while inheriting from `AbilityBot` or `TelegramLongPollingBot`
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class MyBot extends AbilityBot {
|
||||||
|
|
||||||
|
protected MyBot(String botToken, String botUsername, DefaultBotOptions botOptions) {
|
||||||
|
super(botToken, botUsername, botOptions);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int creatorId() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Ability pingPong() {
|
||||||
|
return Ability
|
||||||
|
.builder()
|
||||||
|
.name("ping")
|
||||||
|
.info("ping pong")
|
||||||
|
.locality(ALL)
|
||||||
|
.privacy(PUBLIC)
|
||||||
|
.action(ctx -> silent.send("pong", ctx.chatId()))
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Now you are able to set up your proxy
|
||||||
|
|
||||||
|
#### without authentication
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
private static String BOT_NAME = "My test bot";
|
||||||
|
private static String BOT_TOKEN = "..." /* your bot's token here */;
|
||||||
|
|
||||||
|
private static String PROXY_HOST = "..." /* proxy host */;
|
||||||
|
private static Integer PROXY_PORT = 3128 /* proxy port */;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
|
||||||
|
ApiContextInitializer.init();
|
||||||
|
|
||||||
|
// Create the TelegramBotsApi object to register your bots
|
||||||
|
TelegramBotsApi botsApi = new TelegramBotsApi();
|
||||||
|
|
||||||
|
// Set up Http proxy
|
||||||
|
DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class);
|
||||||
|
|
||||||
|
HttpHost httpHost = new HttpHost(PROXY_HOST, PROXY_PORT);
|
||||||
|
|
||||||
|
RequestConfig requestConfig = RequestConfig.custom().setProxy(httpHost).setAuthenticationEnabled(false).build();
|
||||||
|
botOptions.setRequestConfig(requestConfig);
|
||||||
|
botOptions.setHttpProxy(httpHost);
|
||||||
|
|
||||||
|
// Register your newly created AbilityBot
|
||||||
|
MyBot bot = new MyBot(BOT_TOKEN, BOT_NAME, botOptions);
|
||||||
|
|
||||||
|
botsApi.registerBot(bot);
|
||||||
|
|
||||||
|
} catch (TelegramApiException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
#### With authentication
|
||||||
|
|
||||||
|
```java
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
private static String BOT_NAME = "My test bot";
|
||||||
|
private static String BOT_TOKEN = "..." /* your bot's token here */;
|
||||||
|
|
||||||
|
private static String PROXY_HOST = "..." /* proxy host */;
|
||||||
|
private static Integer PROXY_PORT = 3128 /* proxy port */;
|
||||||
|
private static String PROXY_USER = "..." /* proxy user */;
|
||||||
|
private static String PROXY_PASSWORD = "..." /* proxy password */;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
|
||||||
|
ApiContextInitializer.init();
|
||||||
|
|
||||||
|
// Create the TelegramBotsApi object to register your bots
|
||||||
|
TelegramBotsApi botsApi = new TelegramBotsApi();
|
||||||
|
|
||||||
|
// Set up Http proxy
|
||||||
|
DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class);
|
||||||
|
|
||||||
|
CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
||||||
|
credsProvider.setCredentials(
|
||||||
|
new AuthScope(PROXY_HOST, PROXY_PORT),
|
||||||
|
new UsernamePasswordCredentials(PROXY_USER, PROXY_PASSWORD));
|
||||||
|
|
||||||
|
HttpHost httpHost = new HttpHost(PROXY_HOST, PROXY_PORT);
|
||||||
|
|
||||||
|
RequestConfig requestConfig = RequestConfig.custom().setProxy(httpHost).setAuthenticationEnabled(true).build();
|
||||||
|
botOptions.setRequestConfig(requestConfig);
|
||||||
|
botOptions.setCredentialsProvider(credsProvider);
|
||||||
|
botOptions.setHttpProxy(httpHost);
|
||||||
|
|
||||||
|
// Register your newly created AbilityBot
|
||||||
|
MyBot bot = new MyBot(BOT_TOKEN, BOT_NAME, botOptions);
|
||||||
|
|
||||||
|
botsApi.registerBot(bot);
|
||||||
|
|
||||||
|
} catch (TelegramApiException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
@ -1,6 +1,7 @@
|
|||||||
* Users guide
|
* Users guide
|
||||||
* [[Getting Started]]
|
* [[Getting Started]]
|
||||||
* [[Errors Handling]]
|
* [[Errors Handling]]
|
||||||
|
* [[Using HTTP Proxy]]
|
||||||
* [[FAQ]]
|
* [[FAQ]]
|
||||||
* AbilityBot
|
* AbilityBot
|
||||||
* [[Simple Example]]
|
* [[Simple Example]]
|
||||||
|
@ -13,6 +13,7 @@ import org.apache.http.entity.StringEntity;
|
|||||||
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.telegram.telegrambots.api.methods.BotApiMethod;
|
import org.telegram.telegrambots.api.methods.BotApiMethod;
|
||||||
import org.telegram.telegrambots.api.methods.groupadministration.SetChatPhoto;
|
import org.telegram.telegrambots.api.methods.groupadministration.SetChatPhoto;
|
||||||
@ -26,6 +27,7 @@ import org.telegram.telegrambots.api.objects.media.InputMedia;
|
|||||||
import org.telegram.telegrambots.exceptions.TelegramApiException;
|
import org.telegram.telegrambots.exceptions.TelegramApiException;
|
||||||
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
|
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
|
||||||
import org.telegram.telegrambots.exceptions.TelegramApiValidationException;
|
import org.telegram.telegrambots.exceptions.TelegramApiValidationException;
|
||||||
|
import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder;
|
||||||
import org.telegram.telegrambots.updateshandlers.DownloadFileCallback;
|
import org.telegram.telegrambots.updateshandlers.DownloadFileCallback;
|
||||||
import org.telegram.telegrambots.updateshandlers.SentCallback;
|
import org.telegram.telegrambots.updateshandlers.SentCallback;
|
||||||
|
|
||||||
@ -61,11 +63,8 @@ public abstract class DefaultAbsSender extends AbsSender {
|
|||||||
super();
|
super();
|
||||||
this.exe = Executors.newFixedThreadPool(options.getMaxThreads());
|
this.exe = Executors.newFixedThreadPool(options.getMaxThreads());
|
||||||
this.options = options;
|
this.options = options;
|
||||||
httpclient = HttpClientBuilder.create()
|
|
||||||
.setSSLHostnameVerifier(new NoopHostnameVerifier())
|
httpclient = TelegramHttpClientBuilder.build(options);
|
||||||
.setConnectionTimeToLive(70, TimeUnit.SECONDS)
|
|
||||||
.setMaxConnTotal(100)
|
|
||||||
.build();
|
|
||||||
|
|
||||||
requestConfig = options.getRequestConfig();
|
requestConfig = options.getRequestConfig();
|
||||||
|
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package org.telegram.telegrambots.bots;
|
package org.telegram.telegrambots.bots;
|
||||||
|
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.client.CredentialsProvider;
|
||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.telegram.telegrambots.ApiConstants;
|
import org.telegram.telegrambots.ApiConstants;
|
||||||
import org.telegram.telegrambots.generics.BotOptions;
|
import org.telegram.telegrambots.generics.BotOptions;
|
||||||
@ -21,6 +23,9 @@ public class DefaultBotOptions implements BotOptions {
|
|||||||
private String baseUrl;
|
private String baseUrl;
|
||||||
private List<String> allowedUpdates;
|
private List<String> allowedUpdates;
|
||||||
|
|
||||||
|
private CredentialsProvider credentialsProvider;
|
||||||
|
private HttpHost httpProxy;
|
||||||
|
|
||||||
public DefaultBotOptions() {
|
public DefaultBotOptions() {
|
||||||
maxThreads = 1;
|
maxThreads = 1;
|
||||||
baseUrl = ApiConstants.BASE_URL;
|
baseUrl = ApiConstants.BASE_URL;
|
||||||
@ -82,4 +87,20 @@ public class DefaultBotOptions implements BotOptions {
|
|||||||
public void setExponentialBackOff(ExponentialBackOff exponentialBackOff) {
|
public void setExponentialBackOff(ExponentialBackOff exponentialBackOff) {
|
||||||
this.exponentialBackOff = exponentialBackOff;
|
this.exponentialBackOff = exponentialBackOff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CredentialsProvider getCredentialsProvider() {
|
||||||
|
return credentialsProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCredentialsProvider(CredentialsProvider credentialsProvider) {
|
||||||
|
this.credentialsProvider = credentialsProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public HttpHost getHttpProxy() {
|
||||||
|
return httpProxy;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHttpProxy(HttpHost httpProxy) {
|
||||||
|
this.httpProxy = httpProxy;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ import org.telegram.telegrambots.ApiConstants;
|
|||||||
import org.telegram.telegrambots.ApiContext;
|
import org.telegram.telegrambots.ApiContext;
|
||||||
import org.telegram.telegrambots.api.methods.updates.SetWebhook;
|
import org.telegram.telegrambots.api.methods.updates.SetWebhook;
|
||||||
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
|
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
|
||||||
|
import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder;
|
||||||
import org.telegram.telegrambots.generics.LongPollingBot;
|
import org.telegram.telegrambots.generics.LongPollingBot;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -36,7 +37,7 @@ public abstract class TelegramLongPollingBot extends DefaultAbsSender implements
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clearWebhook() throws TelegramApiRequestException {
|
public void clearWebhook() throws TelegramApiRequestException {
|
||||||
try (CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build()) {
|
try (CloseableHttpClient httpclient = TelegramHttpClientBuilder.build(getOptions())) {
|
||||||
String url = getOptions().getBaseUrl() + getBotToken() + "/" + SetWebhook.PATH;
|
String url = getOptions().getBaseUrl() + getBotToken() + "/" + SetWebhook.PATH;
|
||||||
HttpGet httpGet = new HttpGet(url);
|
HttpGet httpGet = new HttpGet(url);
|
||||||
httpGet.setConfig(getOptions().getRequestConfig());
|
httpGet.setConfig(getOptions().getRequestConfig());
|
||||||
|
@ -17,6 +17,7 @@ import org.telegram.telegrambots.ApiConstants;
|
|||||||
import org.telegram.telegrambots.ApiContext;
|
import org.telegram.telegrambots.ApiContext;
|
||||||
import org.telegram.telegrambots.api.methods.updates.SetWebhook;
|
import org.telegram.telegrambots.api.methods.updates.SetWebhook;
|
||||||
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
|
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
|
||||||
|
import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder;
|
||||||
import org.telegram.telegrambots.generics.WebhookBot;
|
import org.telegram.telegrambots.generics.WebhookBot;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -44,7 +45,7 @@ public abstract class TelegramWebhookBot extends DefaultAbsSender implements Web
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setWebhook(String url, String publicCertificatePath) throws TelegramApiRequestException {
|
public void setWebhook(String url, String publicCertificatePath) throws TelegramApiRequestException {
|
||||||
try (CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build()) {
|
try (CloseableHttpClient httpclient = TelegramHttpClientBuilder.build(getOptions())) {
|
||||||
String requestUrl = getBaseUrl() + SetWebhook.PATH;
|
String requestUrl = getBaseUrl() + SetWebhook.PATH;
|
||||||
|
|
||||||
HttpPost httppost = new HttpPost(requestUrl);
|
HttpPost httppost = new HttpPost(requestUrl);
|
||||||
|
@ -0,0 +1,37 @@
|
|||||||
|
package org.telegram.telegrambots.facilities;
|
||||||
|
|
||||||
|
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
|
||||||
|
import org.telegram.telegrambots.bots.DefaultBotOptions;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by bvn13 on 17.04.2018.
|
||||||
|
*/
|
||||||
|
public class TelegramHttpClientBuilder {
|
||||||
|
|
||||||
|
public static CloseableHttpClient build(DefaultBotOptions options) {
|
||||||
|
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
|
||||||
|
.setSSLHostnameVerifier(new NoopHostnameVerifier())
|
||||||
|
.setConnectionTimeToLive(70, TimeUnit.SECONDS)
|
||||||
|
.setMaxConnTotal(100);
|
||||||
|
|
||||||
|
if (options.getHttpProxy() != null) {
|
||||||
|
|
||||||
|
httpClientBuilder.setProxy(options.getHttpProxy());
|
||||||
|
|
||||||
|
if (options.getCredentialsProvider() != null) {
|
||||||
|
httpClientBuilder
|
||||||
|
.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
|
||||||
|
.setDefaultCredentialsProvider(options.getCredentialsProvider());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return httpClientBuilder.build();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -12,6 +12,7 @@ import org.apache.http.entity.ContentType;
|
|||||||
import org.apache.http.entity.StringEntity;
|
import org.apache.http.entity.StringEntity;
|
||||||
import org.apache.http.impl.client.CloseableHttpClient;
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
import org.apache.http.impl.client.HttpClientBuilder;
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
|
||||||
import org.apache.http.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.telegram.telegrambots.ApiConstants;
|
import org.telegram.telegrambots.ApiConstants;
|
||||||
@ -19,6 +20,7 @@ import org.telegram.telegrambots.api.methods.updates.GetUpdates;
|
|||||||
import org.telegram.telegrambots.api.objects.Update;
|
import org.telegram.telegrambots.api.objects.Update;
|
||||||
import org.telegram.telegrambots.bots.DefaultBotOptions;
|
import org.telegram.telegrambots.bots.DefaultBotOptions;
|
||||||
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
|
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
|
||||||
|
import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder;
|
||||||
import org.telegram.telegrambots.generics.*;
|
import org.telegram.telegrambots.generics.*;
|
||||||
import org.telegram.telegrambots.logging.BotLogger;
|
import org.telegram.telegrambots.logging.BotLogger;
|
||||||
|
|
||||||
@ -147,11 +149,7 @@ public class DefaultBotSession implements BotSession {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public synchronized void start() {
|
public synchronized void start() {
|
||||||
httpclient = HttpClientBuilder.create()
|
httpclient = TelegramHttpClientBuilder.build(options);
|
||||||
.setSSLHostnameVerifier(new NoopHostnameVerifier())
|
|
||||||
.setConnectionTimeToLive(70, TimeUnit.SECONDS)
|
|
||||||
.setMaxConnTotal(100)
|
|
||||||
.build();
|
|
||||||
requestConfig = options.getRequestConfig();
|
requestConfig = options.getRequestConfig();
|
||||||
exponentialBackOff = options.getExponentialBackOff();
|
exponentialBackOff = options.getExponentialBackOff();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user