commit
036032a39c
@ -93,6 +93,12 @@ This library use [Telegram bot API](https://core.telegram.org/bots), you can fin
|
|||||||
## Questions or Suggestions
|
## Questions or Suggestions
|
||||||
Feel free to create issues [here](https://github.com/rubenlagus/TelegramBots/issues) as you need or join the [chat](https://telegram.me/JavaBotsApi)
|
Feel free to create issues [here](https://github.com/rubenlagus/TelegramBots/issues) as you need or join the [chat](https://telegram.me/JavaBotsApi)
|
||||||
|
|
||||||
|
## Powered by Intellij
|
||||||
|
<p align="center">
|
||||||
|
<a href="https://www.jetbrains.com"><img src="jetbrains.png" width="75"></a>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
|
@ -31,7 +31,7 @@ public class MyBot extends AbilityBot {
|
|||||||
|
|
||||||
Now you are able to set up your proxy
|
Now you are able to set up your proxy
|
||||||
|
|
||||||
#### without authentication
|
#### Without authentication
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public class Main {
|
public class Main {
|
||||||
@ -53,11 +53,10 @@ public class Main {
|
|||||||
// Set up Http proxy
|
// Set up Http proxy
|
||||||
DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class);
|
DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class);
|
||||||
|
|
||||||
HttpHost httpHost = new HttpHost(PROXY_HOST, PROXY_PORT);
|
botOptions.setProxyHost(PROXY_HOST);
|
||||||
|
botOptions.setProxyPort(PROXY_PORT);
|
||||||
RequestConfig requestConfig = RequestConfig.custom().setProxy(httpHost).setAuthenticationEnabled(false).build();
|
// Select proxy type: [HTTP|SOCKS4|SOCKS5] (default: NO_PROXY)
|
||||||
botOptions.setRequestConfig(requestConfig);
|
botOptions.setProxyType(DefaultBotOptions.ProxyType.SOCKS5);
|
||||||
botOptions.setHttpProxy(httpHost);
|
|
||||||
|
|
||||||
// Register your newly created AbilityBot
|
// Register your newly created AbilityBot
|
||||||
MyBot bot = new MyBot(BOT_TOKEN, BOT_NAME, botOptions);
|
MyBot bot = new MyBot(BOT_TOKEN, BOT_NAME, botOptions);
|
||||||
@ -89,6 +88,14 @@ public class Main {
|
|||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
// Create the Authenticator that will return auth's parameters for proxy authentication
|
||||||
|
Authenticator.setDefault(new Authenticator() {
|
||||||
|
@Override
|
||||||
|
protected PasswordAuthentication getPasswordAuthentication() {
|
||||||
|
return new PasswordAuthentication(PROXY_USER, PROXY_PASSWORD.toCharArray());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
ApiContextInitializer.init();
|
ApiContextInitializer.init();
|
||||||
|
|
||||||
// Create the TelegramBotsApi object to register your bots
|
// Create the TelegramBotsApi object to register your bots
|
||||||
@ -97,17 +104,10 @@ public class Main {
|
|||||||
// Set up Http proxy
|
// Set up Http proxy
|
||||||
DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class);
|
DefaultBotOptions botOptions = ApiContext.getInstance(DefaultBotOptions.class);
|
||||||
|
|
||||||
CredentialsProvider credsProvider = new BasicCredentialsProvider();
|
botOptions.setProxyHost(PROXY_HOST);
|
||||||
credsProvider.setCredentials(
|
botOptions.setProxyPort(PROXY_PORT);
|
||||||
new AuthScope(PROXY_HOST, PROXY_PORT),
|
// Select proxy type: [HTTP|SOCKS4|SOCKS5] (default: NO_PROXY)
|
||||||
new UsernamePasswordCredentials(PROXY_USER, PROXY_PASSWORD));
|
botOptions.setProxyType(DefaultBotOptions.ProxyType.SOCKS5);
|
||||||
|
|
||||||
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
|
// Register your newly created AbilityBot
|
||||||
MyBot bot = new MyBot(BOT_TOKEN, BOT_NAME, botOptions);
|
MyBot bot = new MyBot(BOT_TOKEN, BOT_NAME, botOptions);
|
||||||
@ -120,3 +120,5 @@ public class Main {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If you need something more complex than one proxy, then you can create more complex Authenticator that will check host and other parameters of proxy and return auth values based on them (for more information see code of java.net.Authenticator class)
|
||||||
|
BIN
jetbrains.png
Normal file
BIN
jetbrains.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 178 KiB |
2
pom.xml
2
pom.xml
@ -7,7 +7,7 @@
|
|||||||
<groupId>org.telegram</groupId>
|
<groupId>org.telegram</groupId>
|
||||||
<artifactId>Bots</artifactId>
|
<artifactId>Bots</artifactId>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<version>3.6.1</version>
|
<version>3.6.2</version>
|
||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>telegrambots</module>
|
<module>telegrambots</module>
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.telegram</groupId>
|
<groupId>org.telegram</groupId>
|
||||||
<artifactId>telegrambots-abilities</artifactId>
|
<artifactId>telegrambots-abilities</artifactId>
|
||||||
<version>3.6.1</version>
|
<version>3.6.2</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>Telegram Ability Bot</name>
|
<name>Telegram Ability Bot</name>
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.telegram</groupId>
|
<groupId>org.telegram</groupId>
|
||||||
<artifactId>telegrambotsextensions</artifactId>
|
<artifactId>telegrambotsextensions</artifactId>
|
||||||
<version>3.6.1</version>
|
<version>3.6.2</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>Telegram Bots Extensions</name>
|
<name>Telegram Bots Extensions</name>
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.telegram</groupId>
|
<groupId>org.telegram</groupId>
|
||||||
<artifactId>telegrambots-meta</artifactId>
|
<artifactId>telegrambots-meta</artifactId>
|
||||||
<version>3.6.1</version>
|
<version>3.6.2</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>Telegram Bots Meta</name>
|
<name>Telegram Bots Meta</name>
|
||||||
|
@ -25,7 +25,7 @@ Usage
|
|||||||
**Gradle**
|
**Gradle**
|
||||||
|
|
||||||
```gradle
|
```gradle
|
||||||
compile "org.telegram:telegrambots-spring-boot-starter:3.6"
|
compile "org.telegram:telegrambots-spring-boot-starter:3.6.1"
|
||||||
```
|
```
|
||||||
|
|
||||||
Motivation
|
Motivation
|
||||||
@ -39,8 +39,6 @@ Your main spring boot class should look like this:
|
|||||||
|
|
||||||
```java
|
```java
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
//Add this annotation to enable automatic bots initializing
|
|
||||||
@EnableTelegramBots
|
|
||||||
public class YourApplicationMainClass {
|
public class YourApplicationMainClass {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.telegram</groupId>
|
<groupId>org.telegram</groupId>
|
||||||
<artifactId>telegrambots-spring-boot-starter</artifactId>
|
<artifactId>telegrambots-spring-boot-starter</artifactId>
|
||||||
<version>3.6.1</version>
|
<version>3.6.2</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>Telegram Bots Spring Boot Starter</name>
|
<name>Telegram Bots Spring Boot Starter</name>
|
||||||
@ -60,7 +60,7 @@
|
|||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
<bots.version>3.6.1</bots.version>
|
<bots.version>3.6.1</bots.version>
|
||||||
<spring-boot.version>1.5.10.RELEASE</spring-boot.version>
|
<spring-boot.version>2.0.2.RELEASE</spring-boot.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
@ -75,24 +75,41 @@
|
|||||||
<artifactId>spring-boot</artifactId>
|
<artifactId>spring-boot</artifactId>
|
||||||
<version>${spring-boot.version}</version>
|
<version>${spring-boot.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-autoconfigure</artifactId>
|
<artifactId>spring-boot-autoconfigure</artifactId>
|
||||||
<version>${spring-boot.version}</version>
|
<version>${spring-boot.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-test</artifactId>
|
||||||
|
<version>${spring-boot.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
<version>3.9.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mockito</groupId>
|
<groupId>org.mockito</groupId>
|
||||||
<artifactId>mockito-all</artifactId>
|
<artifactId>mockito-all</artifactId>
|
||||||
<version>2.0.2-beta</version>
|
<version>2.0.2-beta</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit</artifactId>
|
||||||
<version>4.11</version>
|
<version>4.11</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
package org.telegram.telegrambots.starter;
|
|
||||||
|
|
||||||
import org.springframework.context.annotation.Import;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Imports configuration #TelegramBotStarterConfiguration in spring context.
|
|
||||||
*/
|
|
||||||
@Import(TelegramBotStarterConfiguration.class)
|
|
||||||
public @interface EnableTelegramBots {
|
|
||||||
}
|
|
@ -0,0 +1,45 @@
|
|||||||
|
package org.telegram.telegrambots.starter;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.telegram.telegrambots.TelegramBotsApi;
|
||||||
|
import org.telegram.telegrambots.exceptions.TelegramApiException;
|
||||||
|
import org.telegram.telegrambots.generics.LongPollingBot;
|
||||||
|
import org.telegram.telegrambots.generics.WebhookBot;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Receives all beand which are #LongPollingBot and #WebhookBot and register them in #TelegramBotsApi.
|
||||||
|
*/
|
||||||
|
public class TelegramBotInitializer implements InitializingBean {
|
||||||
|
|
||||||
|
private final TelegramBotsApi telegramBotsApi;
|
||||||
|
private final List<LongPollingBot> longPollingBots;
|
||||||
|
private final List<WebhookBot> webHookBots;
|
||||||
|
|
||||||
|
public TelegramBotInitializer(TelegramBotsApi telegramBotsApi,
|
||||||
|
List<LongPollingBot> longPollingBots,
|
||||||
|
List<WebhookBot> webHookBots) {
|
||||||
|
Objects.requireNonNull(telegramBotsApi);
|
||||||
|
Objects.requireNonNull(longPollingBots);
|
||||||
|
Objects.requireNonNull(webHookBots);
|
||||||
|
this.telegramBotsApi = telegramBotsApi;
|
||||||
|
this.longPollingBots = longPollingBots;
|
||||||
|
this.webHookBots = webHookBots;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() throws Exception {
|
||||||
|
try {
|
||||||
|
for (LongPollingBot bot : longPollingBots) {
|
||||||
|
telegramBotsApi.registerBot(bot);
|
||||||
|
}
|
||||||
|
for (WebhookBot bot : webHookBots) {
|
||||||
|
telegramBotsApi.registerBot(bot);
|
||||||
|
}
|
||||||
|
} catch (TelegramApiException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,70 +1,37 @@
|
|||||||
package org.telegram.telegrambots.starter;
|
package org.telegram.telegrambots.starter;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import java.util.Collections;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.telegram.telegrambots.TelegramBotsApi;
|
|
||||||
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
|
|
||||||
import org.telegram.telegrambots.generics.LongPollingBot;
|
|
||||||
import org.telegram.telegrambots.generics.WebhookBot;
|
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.telegram.telegrambots.TelegramBotsApi;
|
||||||
|
import org.telegram.telegrambots.generics.LongPollingBot;
|
||||||
|
import org.telegram.telegrambots.generics.WebhookBot;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Receives all beand which are #LongPollingBot and #WebhookBot and register them in #TelegramBotsApi.
|
|
||||||
* #TelegramBotsApi added to spring context as well
|
* #TelegramBotsApi added to spring context as well
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
|
@ConditionalOnProperty(prefix="telegrambots",name = "enabled", havingValue = "true", matchIfMissing = true)
|
||||||
public class TelegramBotStarterConfiguration {
|
public class TelegramBotStarterConfiguration {
|
||||||
|
|
||||||
|
|
||||||
private final List<LongPollingBot> longPollingBots;
|
|
||||||
private final List<WebhookBot> webHookBots;
|
|
||||||
|
|
||||||
private TelegramBotsApi telegramBotsApi;
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public void setTelegramBotsApi(TelegramBotsApi telegramBotsApi) {
|
|
||||||
this.telegramBotsApi = telegramBotsApi;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TelegramBotStarterConfiguration(@Autowired(required = false) List<LongPollingBot> longPollingBots,
|
|
||||||
@Autowired(required = false) List<WebhookBot> webHookBots) {
|
|
||||||
|
|
||||||
this.longPollingBots = longPollingBots;
|
|
||||||
this.webHookBots = webHookBots;
|
|
||||||
}
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void registerBots() {
|
|
||||||
Optional.ofNullable(longPollingBots).ifPresent(bots -> {
|
|
||||||
bots.forEach(bot -> {
|
|
||||||
try {
|
|
||||||
telegramBotsApi.registerBot(bot);
|
|
||||||
} catch (TelegramApiRequestException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
Optional.ofNullable(webHookBots).ifPresent(bots -> {
|
|
||||||
bots.forEach(bot -> {
|
|
||||||
try {
|
|
||||||
telegramBotsApi.registerBot(bot);
|
|
||||||
} catch (TelegramApiRequestException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(TelegramBotsApi.class)
|
@ConditionalOnMissingBean(TelegramBotsApi.class)
|
||||||
public TelegramBotsApi telegramBotsApi() {
|
public TelegramBotsApi telegramBotsApi() {
|
||||||
return new TelegramBotsApi();
|
return new TelegramBotsApi();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@ConditionalOnMissingBean
|
||||||
|
public TelegramBotInitializer telegramBotInitializer(TelegramBotsApi telegramBotsApi,
|
||||||
|
Optional<List<LongPollingBot>> longPollingBots,
|
||||||
|
Optional<List<WebhookBot>> webHookBots) {
|
||||||
|
return new TelegramBotInitializer(telegramBotsApi,
|
||||||
|
longPollingBots.orElseGet(Collections::emptyList),
|
||||||
|
webHookBots.orElseGet(Collections::emptyList));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1 @@
|
|||||||
|
org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.telegram.telegrambots.starter.TelegramBotStarterConfiguration
|
@ -1,66 +1,99 @@
|
|||||||
package org.telegram.telegrambots.starter;
|
package org.telegram.telegrambots.starter;
|
||||||
|
|
||||||
import com.google.common.collect.Lists;
|
|
||||||
import org.junit.Rule;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.mockito.Answers;
|
import static org.mockito.Mockito.*;
|
||||||
import org.mockito.Mock;
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
import org.mockito.junit.MockitoJUnit;
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
import org.mockito.junit.MockitoRule;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.telegram.telegrambots.TelegramBotsApi;
|
import org.telegram.telegrambots.TelegramBotsApi;
|
||||||
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
|
|
||||||
import org.telegram.telegrambots.generics.LongPollingBot;
|
import org.telegram.telegrambots.generics.LongPollingBot;
|
||||||
import org.telegram.telegrambots.generics.WebhookBot;
|
import org.telegram.telegrambots.generics.WebhookBot;
|
||||||
|
|
||||||
import static org.mockito.Matchers.any;
|
|
||||||
import static org.mockito.Mockito.*;
|
|
||||||
|
|
||||||
public class TestTelegramBotStarterConfiguration {
|
public class TestTelegramBotStarterConfiguration {
|
||||||
|
|
||||||
@Mock
|
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||||
private TelegramBotsApi telegramBotsApi;
|
.withConfiguration(AutoConfigurations.of(MockTelegramBotsApi.class, TelegramBotStarterConfiguration.class));
|
||||||
|
|
||||||
@Rule
|
@Test
|
||||||
public MockitoRule mockitoRule = MockitoJUnit.rule();
|
public void createMockTelegramBotsApiWithDefaultSettings() {
|
||||||
|
this.contextRunner.run((context) -> {
|
||||||
|
assertThat(context).hasSingleBean(TelegramBotsApi.class);
|
||||||
|
assertThat(context).hasSingleBean(TelegramBotInitializer.class);
|
||||||
|
assertThat(context).doesNotHaveBean(LongPollingBot.class);
|
||||||
|
assertThat(context).doesNotHaveBean(WebhookBot.class);
|
||||||
|
verifyNoMoreInteractions(context.getBean(TelegramBotsApi.class));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void TestRegisterBotsWithLongPollingBots() throws TelegramApiRequestException {
|
public void createOnlyLongPollingBot() {
|
||||||
when(telegramBotsApi.registerBot(any(LongPollingBot.class))).then(Answers.RETURNS_MOCKS.get());
|
this.contextRunner.withUserConfiguration(LongPollingBotConfig.class)
|
||||||
LongPollingBot longPollingBot = mock(LongPollingBot.class);
|
.run((context) -> {
|
||||||
TelegramBotStarterConfiguration configuration = new TelegramBotStarterConfiguration(Lists.newArrayList(longPollingBot), null);
|
assertThat(context).hasSingleBean(LongPollingBot.class);
|
||||||
configuration.setTelegramBotsApi(telegramBotsApi);
|
assertThat(context).doesNotHaveBean(WebhookBot.class);
|
||||||
|
|
||||||
configuration.registerBots();
|
TelegramBotsApi telegramBotsApi = context.getBean(TelegramBotsApi.class);
|
||||||
|
|
||||||
verify(telegramBotsApi, times(1)).registerBot(longPollingBot);
|
verify(telegramBotsApi, times(1)).registerBot( context.getBean(LongPollingBot.class) );
|
||||||
verifyNoMoreInteractions(telegramBotsApi);
|
verifyNoMoreInteractions(telegramBotsApi);
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void TestRegisterBotsWithWebhookBots() throws TelegramApiRequestException {
|
public void createOnlyWebhookBot() {
|
||||||
doNothing().when(telegramBotsApi).registerBot(any(WebhookBot.class));
|
this.contextRunner.withUserConfiguration(WebhookBotConfig.class)
|
||||||
WebhookBot webhookBot = mock(WebhookBot.class);
|
.run((context) -> {
|
||||||
TelegramBotStarterConfiguration configuration = new TelegramBotStarterConfiguration(null, Lists.newArrayList(webhookBot));
|
assertThat(context).hasSingleBean(WebhookBot.class);
|
||||||
configuration.setTelegramBotsApi(telegramBotsApi);
|
assertThat(context).doesNotHaveBean(LongPollingBot.class);
|
||||||
|
|
||||||
configuration.registerBots();
|
TelegramBotsApi telegramBotsApi = context.getBean(TelegramBotsApi.class);
|
||||||
|
|
||||||
verify(telegramBotsApi, times(1)).registerBot(webhookBot);
|
verify(telegramBotsApi, times(1)).registerBot( context.getBean(WebhookBot.class) );
|
||||||
verifyNoMoreInteractions(telegramBotsApi);
|
verifyNoMoreInteractions(telegramBotsApi);
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void TestRegisterBotsWithLongPollingBotsAndWebhookBots() throws TelegramApiRequestException {
|
public void createLongPoolingBotAndWebhookBot() {
|
||||||
doNothing().when(telegramBotsApi).registerBot(any(WebhookBot.class));
|
this.contextRunner.withUserConfiguration(LongPollingBotConfig.class, WebhookBotConfig.class)
|
||||||
LongPollingBot longPollingBot = mock(LongPollingBot.class);
|
.run((context) -> {
|
||||||
WebhookBot webhookBot = mock(WebhookBot.class);
|
assertThat(context).hasSingleBean(LongPollingBot.class);
|
||||||
TelegramBotStarterConfiguration configuration = new TelegramBotStarterConfiguration(Lists.newArrayList(longPollingBot), Lists.newArrayList(webhookBot));
|
assertThat(context).hasSingleBean(WebhookBot.class);
|
||||||
configuration.setTelegramBotsApi(telegramBotsApi);
|
|
||||||
|
|
||||||
configuration.registerBots();
|
TelegramBotsApi telegramBotsApi = context.getBean(TelegramBotsApi.class);
|
||||||
|
|
||||||
verify(telegramBotsApi, times(1)).registerBot(longPollingBot);
|
verify(telegramBotsApi, times(1)).registerBot( context.getBean(LongPollingBot.class) );
|
||||||
verify(telegramBotsApi, times(1)).registerBot(webhookBot);
|
verify(telegramBotsApi, times(1)).registerBot( context.getBean(WebhookBot.class) );
|
||||||
verifyNoMoreInteractions(telegramBotsApi);
|
//verifyNoMoreInteractions(telegramBotsApi);
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class MockTelegramBotsApi{
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public TelegramBotsApi telegramBotsApi() {
|
||||||
|
return mock(TelegramBotsApi.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class LongPollingBotConfig{
|
||||||
|
@Bean
|
||||||
|
public LongPollingBot longPollingBot() {
|
||||||
|
return mock(LongPollingBot.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
static class WebhookBotConfig{
|
||||||
|
@Bean
|
||||||
|
public WebhookBot webhookBot() {
|
||||||
|
return mock(WebhookBot.class);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>org.telegram</groupId>
|
<groupId>org.telegram</groupId>
|
||||||
<artifactId>telegrambots</artifactId>
|
<artifactId>telegrambots</artifactId>
|
||||||
<version>3.6.1</version>
|
<version>3.6.2</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>Telegram Bots</name>
|
<name>Telegram Bots</name>
|
||||||
|
@ -6,14 +6,11 @@ import org.apache.http.HttpEntity;
|
|||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
||||||
import org.apache.http.entity.BufferedHttpEntity;
|
import org.apache.http.entity.BufferedHttpEntity;
|
||||||
import org.apache.http.entity.ContentType;
|
import org.apache.http.entity.ContentType;
|
||||||
import org.apache.http.entity.StringEntity;
|
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.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;
|
||||||
@ -33,13 +30,13 @@ import org.telegram.telegrambots.updateshandlers.SentCallback;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import static org.telegram.telegrambots.Constants.SOCKET_TIMEOUT;
|
import static org.telegram.telegrambots.Constants.SOCKET_TIMEOUT;
|
||||||
|
|
||||||
@ -56,7 +53,7 @@ public abstract class DefaultAbsSender extends AbsSender {
|
|||||||
protected final ExecutorService exe;
|
protected final ExecutorService exe;
|
||||||
private final ObjectMapper objectMapper = new ObjectMapper();
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
private final DefaultBotOptions options;
|
private final DefaultBotOptions options;
|
||||||
private volatile CloseableHttpClient httpclient;
|
private volatile CloseableHttpClient httpClient;
|
||||||
private volatile RequestConfig requestConfig;
|
private volatile RequestConfig requestConfig;
|
||||||
|
|
||||||
protected DefaultAbsSender(DefaultBotOptions options) {
|
protected DefaultAbsSender(DefaultBotOptions options) {
|
||||||
@ -64,10 +61,10 @@ public abstract class DefaultAbsSender extends AbsSender {
|
|||||||
this.exe = Executors.newFixedThreadPool(options.getMaxThreads());
|
this.exe = Executors.newFixedThreadPool(options.getMaxThreads());
|
||||||
this.options = options;
|
this.options = options;
|
||||||
|
|
||||||
httpclient = TelegramHttpClientBuilder.build(options);
|
httpClient = TelegramHttpClientBuilder.build(options);
|
||||||
|
configureHttpContext();
|
||||||
|
|
||||||
requestConfig = options.getRequestConfig();
|
requestConfig = options.getRequestConfig();
|
||||||
|
|
||||||
if (requestConfig == null) {
|
if (requestConfig == null) {
|
||||||
requestConfig = RequestConfig.copy(RequestConfig.custom().build())
|
requestConfig = RequestConfig.copy(RequestConfig.custom().build())
|
||||||
.setSocketTimeout(SOCKET_TIMEOUT)
|
.setSocketTimeout(SOCKET_TIMEOUT)
|
||||||
@ -76,6 +73,22 @@ public abstract class DefaultAbsSender extends AbsSender {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void configureHttpContext() {
|
||||||
|
|
||||||
|
if (options.getProxyType() != DefaultBotOptions.ProxyType.NO_PROXY) {
|
||||||
|
InetSocketAddress socksaddr = new InetSocketAddress(options.getProxyHost(), options.getProxyPort());
|
||||||
|
options.getHttpContext().setAttribute("socketAddress", socksaddr);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.getProxyType() == DefaultBotOptions.ProxyType.SOCKS4) {
|
||||||
|
options.getHttpContext().setAttribute("socksVersion", 4);
|
||||||
|
}
|
||||||
|
if (options.getProxyType() == DefaultBotOptions.ProxyType.SOCKS5) {
|
||||||
|
options.getHttpContext().setAttribute("socksVersion", 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the token of the bot to be able to perform Telegram Api Requests
|
* Returns the token of the bot to be able to perform Telegram Api Requests
|
||||||
* @return Token of the bot
|
* @return Token of the bot
|
||||||
@ -734,7 +747,7 @@ public abstract class DefaultAbsSender extends AbsSender {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private String sendHttpPostRequest(HttpPost httppost) throws IOException {
|
private String sendHttpPostRequest(HttpPost httppost) throws IOException {
|
||||||
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
|
try (CloseableHttpResponse response = httpClient.execute(httppost, options.getHttpContext())) {
|
||||||
HttpEntity ht = response.getEntity();
|
HttpEntity ht = response.getEntity();
|
||||||
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
|
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
|
||||||
return EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
return EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
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.apache.http.client.protocol.HttpClientContext;
|
||||||
|
import org.apache.http.protocol.HttpContext;
|
||||||
import org.telegram.telegrambots.ApiConstants;
|
import org.telegram.telegrambots.ApiConstants;
|
||||||
import org.telegram.telegrambots.generics.BotOptions;
|
import org.telegram.telegrambots.generics.BotOptions;
|
||||||
import org.telegram.telegrambots.updatesreceivers.ExponentialBackOff;
|
import org.telegram.telegrambots.updatesreceivers.ExponentialBackOff;
|
||||||
@ -18,17 +18,27 @@ import java.util.List;
|
|||||||
public class DefaultBotOptions implements BotOptions {
|
public class DefaultBotOptions implements BotOptions {
|
||||||
private int maxThreads; ///< Max number of threads used for async methods executions (default 1)
|
private int maxThreads; ///< Max number of threads used for async methods executions (default 1)
|
||||||
private RequestConfig requestConfig;
|
private RequestConfig requestConfig;
|
||||||
|
private volatile HttpContext httpContext;
|
||||||
private ExponentialBackOff exponentialBackOff;
|
private ExponentialBackOff exponentialBackOff;
|
||||||
private Integer maxWebhookConnections;
|
private Integer maxWebhookConnections;
|
||||||
private String baseUrl;
|
private String baseUrl;
|
||||||
private List<String> allowedUpdates;
|
private List<String> allowedUpdates;
|
||||||
|
private ProxyType proxyType;
|
||||||
|
private String proxyHost;
|
||||||
|
private int proxyPort;
|
||||||
|
|
||||||
private CredentialsProvider credentialsProvider;
|
public enum ProxyType {
|
||||||
private HttpHost httpProxy;
|
NO_PROXY,
|
||||||
|
HTTP,
|
||||||
|
SOCKS4,
|
||||||
|
SOCKS5
|
||||||
|
}
|
||||||
|
|
||||||
public DefaultBotOptions() {
|
public DefaultBotOptions() {
|
||||||
maxThreads = 1;
|
maxThreads = 1;
|
||||||
baseUrl = ApiConstants.BASE_URL;
|
baseUrl = ApiConstants.BASE_URL;
|
||||||
|
httpContext = HttpClientContext.create();
|
||||||
|
proxyType = ProxyType.NO_PROXY;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -56,6 +66,14 @@ public class DefaultBotOptions implements BotOptions {
|
|||||||
return maxWebhookConnections;
|
return maxWebhookConnections;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HttpContext getHttpContext() {
|
||||||
|
return httpContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHttpContext(HttpContext httpContext) {
|
||||||
|
this.httpContext = httpContext;
|
||||||
|
}
|
||||||
|
|
||||||
public void setMaxWebhookConnections(Integer maxWebhookConnections) {
|
public void setMaxWebhookConnections(Integer maxWebhookConnections) {
|
||||||
this.maxWebhookConnections = maxWebhookConnections;
|
this.maxWebhookConnections = maxWebhookConnections;
|
||||||
}
|
}
|
||||||
@ -88,19 +106,27 @@ public class DefaultBotOptions implements BotOptions {
|
|||||||
this.exponentialBackOff = exponentialBackOff;
|
this.exponentialBackOff = exponentialBackOff;
|
||||||
}
|
}
|
||||||
|
|
||||||
public CredentialsProvider getCredentialsProvider() {
|
public ProxyType getProxyType() {
|
||||||
return credentialsProvider;
|
return proxyType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCredentialsProvider(CredentialsProvider credentialsProvider) {
|
public void setProxyType(ProxyType proxyType) {
|
||||||
this.credentialsProvider = credentialsProvider;
|
this.proxyType = proxyType;
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpHost getHttpProxy() {
|
public String getProxyHost() {
|
||||||
return httpProxy;
|
return proxyHost;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHttpProxy(HttpHost httpProxy) {
|
public void setProxyHost(String proxyHost) {
|
||||||
this.httpProxy = httpProxy;
|
this.proxyHost = proxyHost;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getProxyPort() {
|
||||||
|
return proxyPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setProxyPort(int proxyPort) {
|
||||||
|
this.proxyPort = proxyPort;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,12 +3,10 @@ package org.telegram.telegrambots.bots;
|
|||||||
import org.apache.http.HttpEntity;
|
import org.apache.http.HttpEntity;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
||||||
import org.apache.http.entity.BufferedHttpEntity;
|
import org.apache.http.entity.BufferedHttpEntity;
|
||||||
import org.apache.http.entity.ContentType;
|
import org.apache.http.entity.ContentType;
|
||||||
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.util.EntityUtils;
|
import org.apache.http.util.EntityUtils;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
|
@ -1,10 +1,19 @@
|
|||||||
package org.telegram.telegrambots.facilities;
|
package org.telegram.telegrambots.facilities;
|
||||||
|
|
||||||
|
import org.apache.http.config.Registry;
|
||||||
|
import org.apache.http.config.RegistryBuilder;
|
||||||
|
import org.apache.http.conn.HttpClientConnectionManager;
|
||||||
|
import org.apache.http.conn.socket.ConnectionSocketFactory;
|
||||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||||
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.impl.conn.PoolingHttpClientConnectionManager;
|
||||||
|
import org.apache.http.ssl.SSLContexts;
|
||||||
import org.telegram.telegrambots.bots.DefaultBotOptions;
|
import org.telegram.telegrambots.bots.DefaultBotOptions;
|
||||||
|
import org.telegram.telegrambots.facilities.proxysocketfactorys.HttpConnectionSocketFactory;
|
||||||
|
import org.telegram.telegrambots.facilities.proxysocketfactorys.HttpSSLConnectionSocketFactory;
|
||||||
|
import org.telegram.telegrambots.facilities.proxysocketfactorys.SocksSSLConnectionSocketFactory;
|
||||||
|
import org.telegram.telegrambots.facilities.proxysocketfactorys.SocksConnectionSocketFactory;
|
||||||
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@ -16,22 +25,30 @@ public class TelegramHttpClientBuilder {
|
|||||||
public static CloseableHttpClient build(DefaultBotOptions options) {
|
public static CloseableHttpClient build(DefaultBotOptions options) {
|
||||||
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
|
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create()
|
||||||
.setSSLHostnameVerifier(new NoopHostnameVerifier())
|
.setSSLHostnameVerifier(new NoopHostnameVerifier())
|
||||||
|
.setConnectionManager(createConnectionManager(options))
|
||||||
.setConnectionTimeToLive(70, TimeUnit.SECONDS)
|
.setConnectionTimeToLive(70, TimeUnit.SECONDS)
|
||||||
.setMaxConnTotal(100);
|
.setMaxConnTotal(100);
|
||||||
|
|
||||||
if (options.getHttpProxy() != null) {
|
|
||||||
|
|
||||||
httpClientBuilder.setProxy(options.getHttpProxy());
|
|
||||||
|
|
||||||
if (options.getCredentialsProvider() != null) {
|
|
||||||
httpClientBuilder
|
|
||||||
.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy())
|
|
||||||
.setDefaultCredentialsProvider(options.getCredentialsProvider());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
return httpClientBuilder.build();
|
return httpClientBuilder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static HttpClientConnectionManager createConnectionManager(DefaultBotOptions options) {
|
||||||
|
Registry<ConnectionSocketFactory> registry;
|
||||||
|
switch (options.getProxyType()) {
|
||||||
|
case NO_PROXY:
|
||||||
|
return null;
|
||||||
|
case HTTP:
|
||||||
|
registry = RegistryBuilder.<ConnectionSocketFactory> create()
|
||||||
|
.register("http", new HttpConnectionSocketFactory())
|
||||||
|
.register("https", new HttpSSLConnectionSocketFactory(SSLContexts.createSystemDefault())).build();
|
||||||
|
return new PoolingHttpClientConnectionManager(registry);
|
||||||
|
case SOCKS4:
|
||||||
|
case SOCKS5:
|
||||||
|
registry = RegistryBuilder.<ConnectionSocketFactory> create()
|
||||||
|
.register("http", new SocksConnectionSocketFactory())
|
||||||
|
.register("https", new SocksSSLConnectionSocketFactory(SSLContexts.createSystemDefault())).build();
|
||||||
|
return new PoolingHttpClientConnectionManager(registry);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
package org.telegram.telegrambots.facilities.proxysocketfactorys;
|
||||||
|
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
||||||
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.Proxy;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public class HttpConnectionSocketFactory extends PlainConnectionSocketFactory {
|
||||||
|
@Override
|
||||||
|
public Socket createSocket(final HttpContext context) throws IOException {
|
||||||
|
InetSocketAddress socketAddress = (InetSocketAddress) context.getAttribute("socketAddress");
|
||||||
|
Proxy proxy = new Proxy(Proxy.Type.HTTP, socketAddress);
|
||||||
|
return new Socket(proxy);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Socket connectSocket(
|
||||||
|
int connectTimeout,
|
||||||
|
Socket socket,
|
||||||
|
HttpHost host,
|
||||||
|
InetSocketAddress remoteAddress,
|
||||||
|
InetSocketAddress localAddress,
|
||||||
|
HttpContext context) throws IOException {
|
||||||
|
String hostName = host.getHostName();
|
||||||
|
int port = remoteAddress.getPort();
|
||||||
|
InetSocketAddress unresolvedRemote = InetSocketAddress.createUnresolved(hostName, port);
|
||||||
|
return super.connectSocket(connectTimeout, socket, host, unresolvedRemote, localAddress, context);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,40 @@
|
|||||||
|
package org.telegram.telegrambots.facilities.proxysocketfactorys;
|
||||||
|
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||||
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||||
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.Proxy;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public class HttpSSLConnectionSocketFactory extends SSLConnectionSocketFactory {
|
||||||
|
|
||||||
|
public HttpSSLConnectionSocketFactory(final SSLContext sslContext) {
|
||||||
|
super(sslContext, new NoopHostnameVerifier());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Socket createSocket(final HttpContext context) throws IOException {
|
||||||
|
InetSocketAddress socketAddress = (InetSocketAddress) context.getAttribute("socketAddress");
|
||||||
|
Proxy proxy = new Proxy(Proxy.Type.HTTP, socketAddress);
|
||||||
|
return new Socket(proxy);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Socket connectSocket(
|
||||||
|
int connectTimeout,
|
||||||
|
Socket socket,
|
||||||
|
HttpHost host,
|
||||||
|
InetSocketAddress remoteAddress,
|
||||||
|
InetSocketAddress localAddress,
|
||||||
|
HttpContext context) throws IOException {
|
||||||
|
String hostName = host.getHostName();
|
||||||
|
int port = remoteAddress.getPort();
|
||||||
|
InetSocketAddress unresolvedRemote = InetSocketAddress.createUnresolved(hostName, port);
|
||||||
|
return super.connectSocket(connectTimeout, socket, host, unresolvedRemote, localAddress, context);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package org.telegram.telegrambots.facilities.proxysocketfactorys;
|
||||||
|
|
||||||
|
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
|
||||||
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
import sun.net.SocksProxy;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.Proxy;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
public class SocksConnectionSocketFactory extends PlainConnectionSocketFactory {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Socket createSocket(final HttpContext context) throws IOException {
|
||||||
|
InetSocketAddress socksaddr = (InetSocketAddress) context.getAttribute("socketAddress");
|
||||||
|
int socksVersion = (Integer) context.getAttribute("socksVersion");
|
||||||
|
Proxy proxy = SocksProxy.create(socksaddr, socksVersion);
|
||||||
|
return new Socket(proxy);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Socket connectSocket(
|
||||||
|
int connectTimeout,
|
||||||
|
Socket socket,
|
||||||
|
HttpHost host,
|
||||||
|
InetSocketAddress remoteAddress,
|
||||||
|
InetSocketAddress localAddress,
|
||||||
|
HttpContext context) throws IOException {
|
||||||
|
String hostName = host.getHostName();
|
||||||
|
int port = remoteAddress.getPort();
|
||||||
|
InetSocketAddress unresolvedRemote = InetSocketAddress.createUnresolved(hostName, port);
|
||||||
|
return super.connectSocket(connectTimeout, socket, host, unresolvedRemote, localAddress, context);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,43 @@
|
|||||||
|
package org.telegram.telegrambots.facilities.proxysocketfactorys;
|
||||||
|
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
||||||
|
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
|
||||||
|
import org.apache.http.protocol.HttpContext;
|
||||||
|
import sun.net.SocksProxy;
|
||||||
|
|
||||||
|
import javax.net.ssl.SSLContext;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.InetSocketAddress;
|
||||||
|
import java.net.Proxy;
|
||||||
|
import java.net.Socket;
|
||||||
|
|
||||||
|
|
||||||
|
public class SocksSSLConnectionSocketFactory extends SSLConnectionSocketFactory {
|
||||||
|
|
||||||
|
public SocksSSLConnectionSocketFactory(final SSLContext sslContext) {
|
||||||
|
super(sslContext, new NoopHostnameVerifier());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Socket createSocket(final HttpContext context) throws IOException {
|
||||||
|
InetSocketAddress socksaddr = (InetSocketAddress) context.getAttribute("socketAddress");
|
||||||
|
int socksVersion = (Integer) context.getAttribute("socksVersion");
|
||||||
|
Proxy proxy = SocksProxy.create(socksaddr, socksVersion);
|
||||||
|
return new Socket(proxy);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Socket connectSocket(
|
||||||
|
int connectTimeout,
|
||||||
|
Socket socket,
|
||||||
|
HttpHost host,
|
||||||
|
InetSocketAddress remoteAddress,
|
||||||
|
InetSocketAddress localAddress,
|
||||||
|
HttpContext context) throws IOException {
|
||||||
|
String hostName = host.getHostName();
|
||||||
|
int port = remoteAddress.getPort();
|
||||||
|
InetSocketAddress unresolvedRemote = InetSocketAddress.createUnresolved(hostName, port);
|
||||||
|
return super.connectSocket(connectTimeout, socket, host, unresolvedRemote, localAddress, context);
|
||||||
|
}
|
||||||
|
}
|
@ -6,13 +6,10 @@ import org.apache.http.HttpEntity;
|
|||||||
import org.apache.http.client.config.RequestConfig;
|
import org.apache.http.client.config.RequestConfig;
|
||||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||||
import org.apache.http.client.methods.HttpPost;
|
import org.apache.http.client.methods.HttpPost;
|
||||||
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
||||||
import org.apache.http.entity.BufferedHttpEntity;
|
import org.apache.http.entity.BufferedHttpEntity;
|
||||||
import org.apache.http.entity.ContentType;
|
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.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;
|
||||||
@ -32,7 +29,6 @@ import java.nio.charset.StandardCharsets;
|
|||||||
import java.security.InvalidParameterException;
|
import java.security.InvalidParameterException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.ConcurrentLinkedDeque;
|
import java.util.concurrent.ConcurrentLinkedDeque;
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
import static org.telegram.telegrambots.Constants.SOCKET_TIMEOUT;
|
import static org.telegram.telegrambots.Constants.SOCKET_TIMEOUT;
|
||||||
|
|
||||||
@ -245,7 +241,7 @@ public class DefaultBotSession implements BotSession {
|
|||||||
httpPost.setConfig(requestConfig);
|
httpPost.setConfig(requestConfig);
|
||||||
httpPost.setEntity(new StringEntity(objectMapper.writeValueAsString(request), ContentType.APPLICATION_JSON));
|
httpPost.setEntity(new StringEntity(objectMapper.writeValueAsString(request), ContentType.APPLICATION_JSON));
|
||||||
|
|
||||||
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
|
try (CloseableHttpResponse response = httpclient.execute(httpPost, options.getHttpContext())) {
|
||||||
HttpEntity ht = response.getEntity();
|
HttpEntity ht = response.getEntity();
|
||||||
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
|
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
|
||||||
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
||||||
|
Loading…
Reference in New Issue
Block a user