Merge branch 'dev' into starter-upgrade-2.2.0

This commit is contained in:
sokomishalov 2019-11-25 00:01:11 +05:30 committed by GitHub
commit 4f0e5f6af0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
36 changed files with 656 additions and 388 deletions

3
.gitignore vendored
View File

@ -41,3 +41,6 @@ copyright/
#File System specific files #File System specific files
.DS_STORE .DS_STORE
# Default ignored files
/Bots.iws

532
Bots.ipr

File diff suppressed because it is too large Load Diff

View File

@ -27,16 +27,16 @@ Just import add the library to your project with one of these options:
<dependency> <dependency>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId> <artifactId>telegrambots</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</dependency> </dependency>
``` ```
```gradle ```gradle
compile "org.telegram:telegrambots:4.4.0" compile "org.telegram:telegrambots:4.4.0.2"
``` ```
2. Using Jitpack from [here](https://jitpack.io/#rubenlagus/TelegramBots/4.4.0) 2. Using Jitpack from [here](https://jitpack.io/#rubenlagus/TelegramBots/4.4.0.2)
3. Download the jar(including all dependencies) from [here](https://mvnrepository.com/artifact/org.telegram/telegrambots/4.4.0) 3. Download the jar(including all dependencies) from [here](https://mvnrepository.com/artifact/org.telegram/telegrambots/4.4.0.2)
In order to use Long Polling mode, just create your own bot extending `org.telegram.telegrambots.bots.TelegramLongPollingBot`. In order to use Long Polling mode, just create your own bot extending `org.telegram.telegrambots.bots.TelegramLongPollingBot`.

View File

@ -1,3 +1,14 @@
### <a id="4.4.0.2"></a>4.4.0.2 ###
1. Use SLF4J
2. Support case-insensitive usernames
3. Add Ability toggles and export default abilities to their own class
4. Add state machine capability to AbilityBot via ReplyFlow
5. Support backup and recovery of db vars
6. Fixes: #602, #641, #652, #691
### <a id="4.4.0.1"></a>4.4.0.1 ###
1. Bug fix when importing the project
### <a id="4.4.0"></a>4.4.0 ### ### <a id="4.4.0"></a>4.4.0 ###
1. Update Api version [4.4](https://core.telegram.org/bots/api-changelog#july-29-2019) 1. Update Api version [4.4](https://core.telegram.org/bots/api-changelog#july-29-2019)
2. Removed BotLogger, replaced with [log4j2](https://logging.apache.org/log4j/2.x/) 2. Removed BotLogger, replaced with [log4j2](https://logging.apache.org/log4j/2.x/)

View File

@ -11,13 +11,13 @@ First you need ot get the library and add it to your project. There are few poss
<dependency> <dependency>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId> <artifactId>telegrambots</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</dependency> </dependency>
``` ```
* With **Gradle**: * With **Gradle**:
```groovy ```groovy
compile group: 'org.telegram', name: 'telegrambots', version: '4.4.0' compile group: 'org.telegram', name: 'telegrambots', version: '4.4.0.2'
``` ```
2. Don't like **Maven Central Repository**? It can also be taken from [Jitpack](https://jitpack.io/#rubenlagus/TelegramBots). 2. Don't like **Maven Central Repository**? It can also be taken from [Jitpack](https://jitpack.io/#rubenlagus/TelegramBots).

View File

@ -1,3 +1,6 @@
### <a id="4.4.0.2"></a>To version 4.4.0.2 ###
1. Logging framework has been replaced by slf4j, so now you'll need to manage your own implementation.
### <a id="4.0.0"></a>To version 4.0.0 ### ### <a id="4.0.0"></a>To version 4.0.0 ###
1. Replace removed method from AbsSender with `execute` requests. 1. Replace removed method from AbsSender with `execute` requests.
2. Everything under "Telegrambots-meta" has been moved to package `org.telegram.telegrambots.meta`. 2. Everything under "Telegrambots-meta" has been moved to package `org.telegram.telegrambots.meta`.

View File

@ -9,7 +9,7 @@ As with any Java project, you will need to set your dependencies.
<dependency> <dependency>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>telegrambots-abilities</artifactId> <artifactId>telegrambots-abilities</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</dependency> </dependency>
``` ```
* **Gradle** * **Gradle**

118
pom.xml
View File

@ -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>4.4.0</version> <version>4.4.0.2</version>
<modules> <modules>
<module>telegrambots</module> <module>telegrambots</module>
@ -26,20 +26,54 @@
</license> </license>
</licenses> </licenses>
<name>Bots</name>
<url>https://github.com/rubenlagus/TelegramBots</url>
<description>Easy to use library to create Telegram Bots</description>
<issueManagement>
<url>https://github.com/rubenlagus/TelegramBots/issues</url>
<system>GitHub Issues</system>
</issueManagement>
<scm>
<url>https://github.com/rubenlagus/TelegramBots</url>
<connection>scm:git:git://github.com/rubenlagus/TelegramBots.git</connection>
<developerConnection>scm:git:git@github.com:rubenlagus/TelegramBots.git</developerConnection>
</scm>
<developers>
<developer>
<email>rberlopez@gmail.com</email>
<name>Ruben Bermudez</name>
<url>https://github.com/rubenlagus</url>
<id>rubenlagus</id>
</developer>
</developers>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>
<properties> <properties>
<java.version>11</java.version> <java.version>11</java.version>
<maven.compiler.release>8</maven.compiler.release> <maven.compiler.release>8</maven.compiler.release>
<maven.compiler.source>${java.version}</maven.compiler.source> <maven.compiler.source>${java.version}</maven.compiler.source>
<maven.compiler.target>${java.version}</maven.compiler.target> <maven.compiler.target>${java.version}</maven.compiler.target>
<maven.deploy.skip>true</maven.deploy.skip> <junit.version>5.5.2</junit.version>
<mockito.version>3.1.0</mockito.version>
<junit.version>5.5.1</junit.version> <mockitojupiter.version>3.1.0</mockitojupiter.version>
<mockito.version>3.0.0</mockito.version> <jacksonbase.version>2.10.1</jacksonbase.version>
<mockitojupiter.version>3.0.0</mockitojupiter.version> <jackson.version>2.10.1</jackson.version>
<jacksonbase.version>2.9.9</jacksonbase.version> <slf4j.version>1.7.29</slf4j.version>
<jackson.version>2.9.9.1</jackson.version> <jakarta.annotation.version>1.3.5</jakarta.annotation.version>
<log4j.version>2.12.0</log4j.version>
</properties> </properties>
<dependencyManagement> <dependencyManagement>
@ -78,9 +112,15 @@
<version>${jackson.version}</version> <version>${jackson.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>log4j-core</artifactId> <artifactId>slf4j-api</artifactId>
<version>${log4j.version}</version> <version>${slf4j.version}</version>
</dependency>
<!-- Included to enforce common version-->
<dependency>
<groupId>jakarta.annotation</groupId>
<artifactId>jakarta.annotation-api</artifactId>
<version>${jakarta.annotation.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>
@ -108,9 +148,57 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.logging.log4j</groupId> <groupId>org.slf4j</groupId>
<artifactId>log4j-core</artifactId> <artifactId>slf4j-api</artifactId>
<version>${log4j.version}</version> <version>${slf4j.version}</version>
</dependency> </dependency>
</dependencies> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.8</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.0.0-M2</version>
<executions>
<execution>
<id>enforce-versions</id>
<goals>
<goal>enforce</goal>
</goals>
</execution>
</executions>
<configuration>
<rules>
<DependencyConvergence />
</rules>
</configuration>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -18,19 +18,19 @@ Usage
<dependency> <dependency>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>telegrambots-abilities</artifactId> <artifactId>telegrambots-abilities</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</dependency> </dependency>
``` ```
**Gradle** **Gradle**
```gradle ```gradle
compile "org.telegram:telegrambots-abilities:4.4.0" compile "org.telegram:telegrambots-abilities:4.4.0.2"
``` ```
**JitPack** - [JitPack](https://jitpack.io/#rubenlagus/TelegramBots/v4.4.0) **JitPack** - [JitPack](https://jitpack.io/#rubenlagus/TelegramBots/v4.4.0.2)
**Plain imports** - [Here](https://github.com/rubenlagus/TelegramBots/releases/tag/v4.4.0) **Plain imports** - [Here](https://github.com/rubenlagus/TelegramBots/releases/tag/v4.4.0.2)
Motivation Motivation
---------- ----------

View File

@ -7,7 +7,7 @@
<parent> <parent>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>Bots</artifactId> <artifactId>Bots</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</parent> </parent>
<artifactId>telegrambots-abilities</artifactId> <artifactId>telegrambots-abilities</artifactId>
@ -78,13 +78,14 @@
<commonslang.version>3.9</commonslang.version> <commonslang.version>3.9</commonslang.version>
<mapdb.version>3.0.7</mapdb.version> <mapdb.version>3.0.7</mapdb.version>
<log4j.version>2.12.1</log4j.version>
</properties> </properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId> <artifactId>telegrambots</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.commons</groupId> <groupId>org.apache.commons</groupId>
@ -102,6 +103,16 @@
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -3,9 +3,12 @@ package org.telegram.abilitybots.api.bot;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder; import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import org.apache.logging.log4j.LogManager; import com.google.common.collect.ListMultimap;
import org.apache.logging.log4j.Logger; import com.google.common.collect.Multimap;
import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.telegram.abilitybots.api.db.DBContext; import org.telegram.abilitybots.api.db.DBContext;
import org.telegram.abilitybots.api.objects.*; import org.telegram.abilitybots.api.objects.*;
import org.telegram.abilitybots.api.sender.DefaultSender; import org.telegram.abilitybots.api.sender.DefaultSender;
@ -79,7 +82,7 @@ import static org.telegram.abilitybots.api.util.AbilityUtils.*;
*/ */
@SuppressWarnings({"ConfusingArgumentToVarargsMethod", "UnusedReturnValue", "WeakerAccess", "unused", "ConstantConditions"}) @SuppressWarnings({"ConfusingArgumentToVarargsMethod", "UnusedReturnValue", "WeakerAccess", "unused", "ConstantConditions"})
public abstract class BaseAbilityBot extends DefaultAbsSender implements AbilityExtension { public abstract class BaseAbilityBot extends DefaultAbsSender implements AbilityExtension {
private static final Logger log = LogManager.getLogger(BaseAbilityBot.class); private static final Logger log = LoggerFactory.getLogger(BaseAbilityBot.class);
protected static final String DEFAULT = "default"; protected static final String DEFAULT = "default";
// DB objects // DB objects

View File

@ -3,12 +3,12 @@ package org.telegram.abilitybots.api.db;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.mapdb.Atomic; import org.mapdb.Atomic;
import org.mapdb.DB; import org.mapdb.DB;
import org.mapdb.DBMaker; import org.mapdb.DBMaker;
import org.mapdb.Serializer; import org.mapdb.Serializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.telegram.abilitybots.api.util.Pair; import org.telegram.abilitybots.api.util.Pair;
import java.io.IOException; import java.io.IOException;
@ -30,7 +30,7 @@ import static org.mapdb.Serializer.JAVA;
*/ */
@SuppressWarnings({"unchecked", "WeakerAccess"}) @SuppressWarnings({"unchecked", "WeakerAccess"})
public class MapDBContext implements DBContext { public class MapDBContext implements DBContext {
private static final Logger log = LogManager.getLogger(MapDBContext.class); private static final Logger log = LoggerFactory.getLogger(MapDBContext.class);
private final DB db; private final DB db;
private final ObjectMapper objectMapper; private final ObjectMapper objectMapper;

View File

@ -2,8 +2,8 @@ package org.telegram.abilitybots.api.objects;
import com.google.common.base.MoreObjects; import com.google.common.base.MoreObjects;
import com.google.common.base.Objects; import com.google.common.base.Objects;
import org.apache.logging.log4j.LogManager; import org.slf4j.Logger;
import org.apache.logging.log4j.Logger; import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.api.objects.Update;
import java.util.Arrays; import java.util.Arrays;
@ -35,7 +35,7 @@ import static org.apache.commons.lang3.StringUtils.*;
* @author Abbas Abou Daya * @author Abbas Abou Daya
*/ */
public final class Ability { public final class Ability {
private static final Logger log = LogManager.getLogger(Ability.class); private static final Logger log = LoggerFactory.getLogger(Ability.class);
private final String name; private final String name;
private final String info; private final String info;

View File

@ -1,7 +1,7 @@
package org.telegram.abilitybots.api.sender; package org.telegram.abilitybots.api.sender;
import org.apache.logging.log4j.LogManager; import org.slf4j.Logger;
import org.apache.logging.log4j.Logger; import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod; import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage; import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Message; import org.telegram.telegrambots.meta.api.objects.Message;
@ -19,7 +19,7 @@ import java.util.Optional;
* @author Abbas Abou Daya * @author Abbas Abou Daya
*/ */
public class SilentSender { public class SilentSender {
private static final Logger log = LogManager.getLogger(SilentSender.class); private static final Logger log = LoggerFactory.getLogger(SilentSender.class);
private final MessageSender sender; private final MessageSender sender;

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} [%t] %-5level %logger{36} - %msg%n</Property>
<Property name="APP">App</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
<ThresholdFilter level="OFF" onMatch="ACCEPT" onMismatch="DENY"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

View File

@ -15,7 +15,7 @@ Usage
<dependency> <dependency>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>telegrambots-chat-session-bot</artifactId> <artifactId>telegrambots-chat-session-bot</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</dependency> </dependency>
``` ```

View File

@ -7,7 +7,7 @@
<parent> <parent>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>Bots</artifactId> <artifactId>Bots</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</parent> </parent>
<artifactId>telegrambots-chat-session-bot</artifactId> <artifactId>telegrambots-chat-session-bot</artifactId>
@ -76,7 +76,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>
<shiro.version>1.4.1</shiro.version> <shiro.version>1.4.2</shiro.version>
</properties> </properties>
<dependencies> <dependencies>
@ -84,7 +84,7 @@
<dependency> <dependency>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId> <artifactId>telegrambots</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core --> <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core -->

View File

@ -16,12 +16,12 @@ Just import add the library to your project with one of these options:
<dependency> <dependency>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>telegrambotsextensions</artifactId> <artifactId>telegrambotsextensions</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</dependency> </dependency>
``` ```
2. Using Gradle: 2. Using Gradle:
```gradle ```gradle
compile "org.telegram:telegrambotsextensions:4.4.0" compile "org.telegram:telegrambotsextensions:4.4.0.2"
``` ```

View File

@ -7,7 +7,7 @@
<parent> <parent>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>Bots</artifactId> <artifactId>Bots</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</parent> </parent>
<artifactId>telegrambotsextensions</artifactId> <artifactId>telegrambotsextensions</artifactId>
@ -75,7 +75,7 @@
<dependency> <dependency>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId> <artifactId>telegrambots</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -28,10 +28,22 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
* Creates a TelegramLongPollingCommandBot using default options * Creates a TelegramLongPollingCommandBot using default options
* Use ICommandRegistry's methods on this bot to register commands * Use ICommandRegistry's methods on this bot to register commands
* *
* @param botUsername Username of the bot
*/ */
public TelegramLongPollingCommandBot(String botUsername) { public TelegramLongPollingCommandBot() {
this(ApiContext.getInstance(DefaultBotOptions.class), botUsername); this(ApiContext.getInstance(DefaultBotOptions.class));
}
/**
* Creates a TelegramLongPollingCommandBot using default options
* Use ICommandRegistry's methods on this bot to register commands
*
* @param botUsername Username of the bot
* @deprecated Overwrite {@link #getBotUsername() getBotUsername} instead
*/
@Deprecated
public TelegramLongPollingCommandBot(String botUsername){
this();
this.botUsername = botUsername;
} }
/** /**
@ -40,10 +52,9 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
* Use ICommandRegistry's methods on this bot to register commands * Use ICommandRegistry's methods on this bot to register commands
* *
* @param options Bot options * @param options Bot options
* @param botUsername Username of the bot
*/ */
public TelegramLongPollingCommandBot(DefaultBotOptions options, String botUsername) { public TelegramLongPollingCommandBot(DefaultBotOptions options) {
this(options, true, botUsername); this(options, true);
} }
/** /**
@ -53,12 +64,10 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
* @param options Bot options * @param options Bot options
* @param allowCommandsWithUsername true to allow commands with parameters (default), * @param allowCommandsWithUsername true to allow commands with parameters (default),
* false otherwise * false otherwise
* @param botUsername bot username of this bot
*/ */
public TelegramLongPollingCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername, String botUsername) { public TelegramLongPollingCommandBot(DefaultBotOptions options, boolean allowCommandsWithUsername) {
super(options); super(options);
this.botUsername = botUsername; this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, this.getBotUsername());
this.commandRegistry = new CommandRegistry(allowCommandsWithUsername, botUsername);
} }
@Override @Override
@ -143,9 +152,9 @@ public abstract class TelegramLongPollingCommandBot extends TelegramLongPollingB
* @return Bot username * @return Bot username
*/ */
@Override @Override
public final String getBotUsername() { public String getBotUsername(){
return botUsername; return this.botUsername;
} };
/** /**
* Process all updates, that are not commands. * Process all updates, that are not commands.

View File

@ -8,6 +8,7 @@ import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.regex.Pattern;
/** /**
* This class manages all the commands for a bot. You can register and deregister commands on demand * This class manages all the commands for a bot. You can register and deregister commands on demand
@ -122,7 +123,7 @@ public final class CommandRegistry implements ICommandRegistry {
*/ */
private String removeUsernameFromCommandIfNeeded(String command) { private String removeUsernameFromCommandIfNeeded(String command) {
if (allowCommandsWithUsername) { if (allowCommandsWithUsername) {
return command.replace("@" + botUsername, "").trim(); return command.replaceAll("(?i)@" + Pattern.quote(botUsername), "").trim();
} }
return command; return command;
} }

View File

@ -7,7 +7,7 @@
<parent> <parent>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>Bots</artifactId> <artifactId>Bots</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</parent> </parent>
<artifactId>telegrambots-meta</artifactId> <artifactId>telegrambots-meta</artifactId>
@ -70,10 +70,10 @@
<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>
<guice.version>4.2.2</guice.version> <guice.version>4.2.2</guice.version>
<jackson.version>2.9.9</jackson.version> <jackson.version>2.10.1</jackson.version>
<jacksonanotation.version>2.9.9</jacksonanotation.version> <jacksonanotation.version>2.10.1</jacksonanotation.version>
<json.version>20180813</json.version> <json.version>20180813</json.version>
<guava.version>28.0-jre</guava.version> <guava.version>28.1-jre</guava.version>
</properties> </properties>
<dependencies> <dependencies>

View File

@ -4,8 +4,8 @@ import com.google.inject.AbstractModule;
import com.google.inject.Guice; import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.google.inject.Singleton; import com.google.inject.Singleton;
import org.apache.logging.log4j.LogManager; import org.slf4j.Logger;
import org.apache.logging.log4j.Logger; import org.slf4j.LoggerFactory;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.HashMap; import java.util.HashMap;
@ -16,7 +16,7 @@ import java.util.Map;
* @version 1.0 * @version 1.0
*/ */
public class ApiContext { public class ApiContext {
private static final Logger log = LogManager.getLogger(ApiContext.class); private static final Logger log = LoggerFactory.getLogger(ApiContext.class);
private static final Object lock = new Object(); private static final Object lock = new Object();
private static Injector INJECTOR; private static Injector INJECTOR;

View File

@ -73,6 +73,38 @@ public class ChatPermissions implements BotApiObject {
return canPinMessages; return canPinMessages;
} }
public void setCanSendMessages(Boolean canSendMessages) {
this.canSendMessages = canSendMessages;
}
public void setGetCanSendMediaMessages(Boolean getCanSendMediaMessages) {
this.getCanSendMediaMessages = getCanSendMediaMessages;
}
public void setCanSendPolls(Boolean canSendPolls) {
this.canSendPolls = canSendPolls;
}
public void setCanSendOtherMessages(Boolean canSendOtherMessages) {
this.canSendOtherMessages = canSendOtherMessages;
}
public void setCanAddWebPagePreviews(Boolean canAddWebPagePreviews) {
this.canAddWebPagePreviews = canAddWebPagePreviews;
}
public void setCanChangeInfo(Boolean canChangeInfo) {
this.canChangeInfo = canChangeInfo;
}
public void setCanInviteUsers(Boolean canInviteUsers) {
this.canInviteUsers = canInviteUsers;
}
public void setCanPinMessages(Boolean canPinMessages) {
this.canPinMessages = canPinMessages;
}
@Override @Override
public String toString() { public String toString() {
return "ChatPermissions{" + return "ChatPermissions{" +

View File

@ -18,9 +18,9 @@
package org.telegram.telegrambots.meta.exceptions; package org.telegram.telegrambots.meta.exceptions;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONObject; import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.meta.api.objects.ApiResponse; import org.telegram.telegrambots.meta.api.objects.ApiResponse;
import org.telegram.telegrambots.meta.api.objects.ResponseParameters; import org.telegram.telegrambots.meta.api.objects.ResponseParameters;
@ -33,7 +33,7 @@ import java.io.IOException;
* Exception thrown when something goes wrong in the api * Exception thrown when something goes wrong in the api
*/ */
public class TelegramApiRequestException extends TelegramApiException { public class TelegramApiRequestException extends TelegramApiException {
private static final Logger log = LogManager.getLogger(TelegramApiRequestException.class); private static final Logger log = LoggerFactory.getLogger(TelegramApiRequestException.class);
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
private static final String ERRORDESCRIPTIONFIELD = "description"; private static final String ERRORDESCRIPTIONFIELD = "description";
@ -56,7 +56,7 @@ public class TelegramApiRequestException extends TelegramApiException {
try { try {
parameters = OBJECT_MAPPER.readValue(object.getJSONObject(PARAMETERSFIELD).toString(), ResponseParameters.class); parameters = OBJECT_MAPPER.readValue(object.getJSONObject(PARAMETERSFIELD).toString(), ResponseParameters.class);
} catch (IOException e) { } catch (IOException e) {
log.fatal(e.getLocalizedMessage(), e); log.error(e.getLocalizedMessage(), e);
} }
} }
} }

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} [%t] %-5level %logger{36} - %msg%n</Property>
<Property name="APP">App</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
<ThresholdFilter level="OFF" onMatch="ACCEPT" onMismatch="DENY"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

View File

@ -7,7 +7,7 @@
<parent> <parent>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>Bots</artifactId> <artifactId>Bots</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</parent> </parent>
<artifactId>telegrambots-spring-boot-starter</artifactId> <artifactId>telegrambots-spring-boot-starter</artifactId>
@ -70,7 +70,8 @@
<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>
<spring-boot.version>2.2.0.RELEASE</spring-boot.version>
<spring-boot.version>2.2.1.RELEASE</spring-boot.version>
</properties> </properties>
<dependencies> <dependencies>
@ -78,7 +79,7 @@
<dependency> <dependency>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId> <artifactId>telegrambots</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -103,7 +104,7 @@
<groupId>org.assertj</groupId> <groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId> <artifactId>assertj-core</artifactId>
<scope>test</scope> <scope>test</scope>
<version>3.13.1</version> <version>3.14.0</version>
</dependency> </dependency>
</dependencies> </dependencies>

View File

@ -1,7 +1,7 @@
package org.telegram.telegrambots.starter; package org.telegram.telegrambots.starter;
import org.apache.logging.log4j.LogManager; import org.slf4j.Logger;
import org.apache.logging.log4j.Logger; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.telegram.telegrambots.meta.TelegramBotsApi; import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
@ -21,7 +21,7 @@ import static java.lang.String.format;
* Receives all beand which are #LongPollingBot and #WebhookBot and register them in #TelegramBotsApi. * Receives all beand which are #LongPollingBot and #WebhookBot and register them in #TelegramBotsApi.
*/ */
public class TelegramBotInitializer implements InitializingBean { public class TelegramBotInitializer implements InitializingBean {
private static final Logger log = LogManager.getLogger(TelegramBotInitializer.class); private static final Logger log = LoggerFactory.getLogger(TelegramBotInitializer.class);
private final TelegramBotsApi telegramBotsApi; private final TelegramBotsApi telegramBotsApi;
private final List<LongPollingBot> longPollingBots; private final List<LongPollingBot> longPollingBots;

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} [%t] %-5level %logger{36} - %msg%n</Property>
<Property name="APP">App</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
<ThresholdFilter level="OFF" onMatch="ACCEPT" onMismatch="DENY"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

View File

@ -7,7 +7,7 @@
<parent> <parent>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>Bots</artifactId> <artifactId>Bots</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</parent> </parent>
<artifactId>telegrambots</artifactId> <artifactId>telegrambots</artifactId>
@ -70,12 +70,12 @@
<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>
<glassfish.version>2.29</glassfish.version> <glassfish.version>2.29.1</glassfish.version>
<jerseybundle.version>1.19.3</jerseybundle.version> <jerseybundle.version>1.19.3</jerseybundle.version>
<httpcompontents.version>4.5.9</httpcompontents.version> <httpcompontents.version>4.5.10</httpcompontents.version>
<json.version>20180813</json.version> <json.version>20180813</json.version>
<jackson.version>2.9.9</jackson.version> <jackson.version>2.10.1</jackson.version>
<jacksonanotation.version>2.9.9</jacksonanotation.version> <jacksonanotation.version>2.10.1</jacksonanotation.version>
<commonio.version>2.6</commonio.version> <commonio.version>2.6</commonio.version>
</properties> </properties>
@ -95,7 +95,7 @@
<dependency> <dependency>
<groupId>org.telegram</groupId> <groupId>org.telegram</groupId>
<artifactId>telegrambots-meta</artifactId> <artifactId>telegrambots-meta</artifactId>
<version>4.4.0</version> <version>4.4.0.2</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>
@ -105,12 +105,12 @@
<dependency> <dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId> <groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId> <artifactId>jackson-jaxrs-json-provider</artifactId>
<version>${jackson.version}</version> <version>${jacksonbase.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.module</groupId> <groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-jaxb-annotations</artifactId> <artifactId>jackson-module-jaxb-annotations</artifactId>
<version>2.9.9</version> <version>2.10.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.fasterxml.jackson.core</groupId> <groupId>com.fasterxml.jackson.core</groupId>

View File

@ -6,7 +6,6 @@ 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.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;
@ -16,7 +15,15 @@ import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder;
import org.telegram.telegrambots.facilities.filedownloader.TelegramFileDownloader; import org.telegram.telegrambots.facilities.filedownloader.TelegramFileDownloader;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod; import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.methods.groupadministration.SetChatPhoto; import org.telegram.telegrambots.meta.api.methods.groupadministration.SetChatPhoto;
import org.telegram.telegrambots.meta.api.methods.send.*; import org.telegram.telegrambots.meta.api.methods.send.SendAnimation;
import org.telegram.telegrambots.meta.api.methods.send.SendAudio;
import org.telegram.telegrambots.meta.api.methods.send.SendDocument;
import org.telegram.telegrambots.meta.api.methods.send.SendMediaGroup;
import org.telegram.telegrambots.meta.api.methods.send.SendPhoto;
import org.telegram.telegrambots.meta.api.methods.send.SendSticker;
import org.telegram.telegrambots.meta.api.methods.send.SendVideo;
import org.telegram.telegrambots.meta.api.methods.send.SendVideoNote;
import org.telegram.telegrambots.meta.api.methods.send.SendVoice;
import org.telegram.telegrambots.meta.api.methods.stickers.AddStickerToSet; import org.telegram.telegrambots.meta.api.methods.stickers.AddStickerToSet;
import org.telegram.telegrambots.meta.api.methods.stickers.CreateNewStickerSet; import org.telegram.telegrambots.meta.api.methods.stickers.CreateNewStickerSet;
import org.telegram.telegrambots.meta.api.methods.stickers.UploadStickerFile; import org.telegram.telegrambots.meta.api.methods.stickers.UploadStickerFile;
@ -58,9 +65,9 @@ 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 final CloseableHttpClient httpClient;
private volatile RequestConfig requestConfig; private final RequestConfig requestConfig;
private final TelegramFileDownloader telegramFileDownloader = new TelegramFileDownloader(this::getBotToken); private final TelegramFileDownloader telegramFileDownloader;
protected DefaultAbsSender(DefaultBotOptions options) { protected DefaultAbsSender(DefaultBotOptions options) {
super(); super();
@ -69,11 +76,14 @@ public abstract class DefaultAbsSender extends AbsSender {
this.options = options; this.options = options;
httpClient = TelegramHttpClientBuilder.build(options); httpClient = TelegramHttpClientBuilder.build(options);
this.telegramFileDownloader = new TelegramFileDownloader(httpClient, this::getBotToken);
configureHttpContext(); configureHttpContext();
requestConfig = options.getRequestConfig(); final RequestConfig configFromOptions = options.getRequestConfig();
if (requestConfig == null) { if (configFromOptions != null) {
requestConfig = RequestConfig.copy(RequestConfig.custom().build()) this.requestConfig = configFromOptions;
} else {
this.requestConfig = RequestConfig.copy(RequestConfig.custom().build())
.setSocketTimeout(SOCKET_TIMEOUT) .setSocketTimeout(SOCKET_TIMEOUT)
.setConnectTimeout(SOCKET_TIMEOUT) .setConnectTimeout(SOCKET_TIMEOUT)
.setConnectionRequestTimeout(SOCKET_TIMEOUT).build(); .setConnectionRequestTimeout(SOCKET_TIMEOUT).build();
@ -729,9 +739,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, options.getHttpContext())) { try (CloseableHttpResponse response = httpClient.execute(httppost, options.getHttpContext())) {
HttpEntity ht = response.getEntity(); return EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
return EntityUtils.toString(buf, StandardCharsets.UTF_8);
} }
} }

View File

@ -45,7 +45,8 @@ public class TelegramHttpClientBuilder {
case SOCKS5: case SOCKS5:
registry = RegistryBuilder.<ConnectionSocketFactory> create() registry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", new SocksConnectionSocketFactory()) .register("http", new SocksConnectionSocketFactory())
.register("https", new SocksSSLConnectionSocketFactory(SSLContexts.createSystemDefault())).build(); .register("https", new SocksSSLConnectionSocketFactory(SSLContexts.createSystemDefault()))
.build();
return new PoolingHttpClientConnectionManager(registry); return new PoolingHttpClientConnectionManager(registry);
} }
return null; return null;

View File

@ -2,25 +2,27 @@ package org.telegram.telegrambots.updatesreceivers;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.Inject; import com.google.inject.Inject;
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.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.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONException; import org.json.JSONException;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.meta.ApiConstants; import org.telegram.telegrambots.meta.ApiConstants;
import org.telegram.telegrambots.meta.api.methods.updates.GetUpdates; import org.telegram.telegrambots.meta.api.methods.updates.GetUpdates;
import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder; import org.telegram.telegrambots.meta.generics.BotOptions;
import org.telegram.telegrambots.meta.generics.*; import org.telegram.telegrambots.meta.generics.BotSession;
import org.telegram.telegrambots.meta.generics.LongPollingBot;
import org.telegram.telegrambots.meta.generics.UpdatesHandler;
import org.telegram.telegrambots.meta.generics.UpdatesReader;
import java.io.IOException; import java.io.IOException;
import java.io.InvalidObjectException; import java.io.InvalidObjectException;
@ -28,7 +30,11 @@ import java.net.SocketException;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.security.InvalidParameterException; import java.security.InvalidParameterException;
import java.util.*; import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentLinkedDeque; import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@ -40,7 +46,7 @@ import static org.telegram.telegrambots.Constants.SOCKET_TIMEOUT;
* Thread to request updates with active wait * Thread to request updates with active wait
*/ */
public class DefaultBotSession implements BotSession { public class DefaultBotSession implements BotSession {
private static final Logger log = LogManager.getLogger(DefaultBotSession.class); private static final Logger log = LoggerFactory.getLogger(DefaultBotSession.class);
private AtomicBoolean running = new AtomicBoolean(false); private AtomicBoolean running = new AtomicBoolean(false);
@ -132,6 +138,7 @@ public class DefaultBotSession implements BotSession {
return running.get(); return running.get();
} }
@SuppressWarnings("WeakerAccess")
private class ReaderThread extends Thread implements UpdatesReader { private class ReaderThread extends Thread implements UpdatesReader {
private final UpdatesSupplier updatesSupplier; private final UpdatesSupplier updatesSupplier;
@ -207,7 +214,7 @@ public class DefaultBotSession implements BotSession {
log.debug(e.getLocalizedMessage(), e); log.debug(e.getLocalizedMessage(), e);
interrupt(); interrupt();
} catch (Exception global) { } catch (Exception global) {
log.fatal(global.getLocalizedMessage(), global); log.error(global.getLocalizedMessage(), global);
try { try {
synchronized (lock) { synchronized (lock) {
lock.wait(exponentialBackOff.nextBackOffMillis()); lock.wait(exponentialBackOff.nextBackOffMillis());
@ -244,9 +251,7 @@ public class DefaultBotSession implements BotSession {
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, options.getHttpContext())) { try (CloseableHttpResponse response = httpclient.execute(httpPost, options.getHttpContext())) {
HttpEntity ht = response.getEntity(); String responseContent = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
if (response.getStatusLine().getStatusCode() >= 500) { if (response.getStatusLine().getStatusCode() >= 500) {
log.warn(responseContent); log.warn(responseContent);
@ -263,7 +268,7 @@ public class DefaultBotSession implements BotSession {
} }
} }
} catch (SocketException | InvalidObjectException | TelegramApiRequestException e) { } catch (SocketException | InvalidObjectException | TelegramApiRequestException e) {
log.fatal(e.getLocalizedMessage(), e); log.error(e.getLocalizedMessage(), e);
} catch (SocketTimeoutException e) { } catch (SocketTimeoutException e) {
log.info(e.getLocalizedMessage(), e); log.info(e.getLocalizedMessage(), e);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -309,7 +314,7 @@ public class DefaultBotSession implements BotSession {
log.debug(e.getLocalizedMessage(), e); log.debug(e.getLocalizedMessage(), e);
interrupt(); interrupt();
} catch (Exception e) { } catch (Exception e) {
log.fatal(e.getLocalizedMessage(), e); log.error(e.getLocalizedMessage(), e);
} }
} }
log.debug("Handler thread has being closed"); log.debug("Handler thread has being closed");

View File

@ -1,7 +1,7 @@
package org.telegram.telegrambots.updatesreceivers; package org.telegram.telegrambots.updatesreceivers;
import org.apache.logging.log4j.LogManager; import org.slf4j.Logger;
import org.apache.logging.log4j.Logger; import org.slf4j.LoggerFactory;
import org.telegram.telegrambots.meta.api.methods.BotApiMethod; import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
import org.telegram.telegrambots.meta.api.objects.Update; import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException; import org.telegram.telegrambots.meta.exceptions.TelegramApiValidationException;
@ -24,7 +24,7 @@ import java.util.concurrent.ConcurrentHashMap;
*/ */
@Path("callback") @Path("callback")
public class RestApi { public class RestApi {
private static final Logger log = LogManager.getLogger(RestApi.class); private static final Logger log = LoggerFactory.getLogger(RestApi.class);
private final ConcurrentHashMap<String, WebhookBot> callbacks = new ConcurrentHashMap<>(); private final ConcurrentHashMap<String, WebhookBot> callbacks = new ConcurrentHashMap<>();

View File

@ -1,9 +1,9 @@
package org.telegram.telegrambots.util; package org.telegram.telegrambots.util;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
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.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;
@ -11,19 +11,21 @@ import org.apache.http.util.EntityUtils;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.telegram.telegrambots.meta.ApiConstants;
import org.telegram.telegrambots.meta.api.methods.updates.DeleteWebhook;
import org.telegram.telegrambots.bots.DefaultAbsSender; import org.telegram.telegrambots.bots.DefaultAbsSender;
import org.telegram.telegrambots.bots.DefaultBotOptions; import org.telegram.telegrambots.bots.DefaultBotOptions;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder; import org.telegram.telegrambots.facilities.TelegramHttpClientBuilder;
import org.telegram.telegrambots.meta.ApiConstants;
import org.telegram.telegrambots.meta.api.methods.updates.DeleteWebhook;
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook; import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException; import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import static org.telegram.telegrambots.Constants.SOCKET_TIMEOUT;
public final class WebhookUtils { public final class WebhookUtils {
private WebhookUtils() { private WebhookUtils() {
@ -35,8 +37,16 @@ public final class WebhookUtils {
try (CloseableHttpClient httpclient = TelegramHttpClientBuilder.build(botOptions)) { try (CloseableHttpClient httpclient = TelegramHttpClientBuilder.build(botOptions)) {
String requestUrl = bot.getBaseUrl() + SetWebhook.PATH; String requestUrl = bot.getBaseUrl() + SetWebhook.PATH;
RequestConfig requestConfig = botOptions.getRequestConfig();
if (requestConfig == null) {
requestConfig = RequestConfig.copy(RequestConfig.custom().build())
.setSocketTimeout(SOCKET_TIMEOUT)
.setConnectTimeout(SOCKET_TIMEOUT)
.setConnectionRequestTimeout(SOCKET_TIMEOUT).build();
}
HttpPost httppost = new HttpPost(requestUrl); HttpPost httppost = new HttpPost(requestUrl);
httppost.setConfig(botOptions.getRequestConfig()); httppost.setConfig(requestConfig);
MultipartEntityBuilder builder = MultipartEntityBuilder.create(); MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addTextBody(SetWebhook.URL_FIELD, url); builder.addTextBody(SetWebhook.URL_FIELD, url);
if (botOptions.getMaxWebhookConnections() != null) { if (botOptions.getMaxWebhookConnections() != null) {
@ -54,9 +64,7 @@ public final class WebhookUtils {
HttpEntity multipart = builder.build(); HttpEntity multipart = builder.build();
httppost.setEntity(multipart); httppost.setEntity(multipart);
try (CloseableHttpResponse response = httpclient.execute(httppost, botOptions.getHttpContext())) { try (CloseableHttpResponse response = httpclient.execute(httppost, botOptions.getHttpContext())) {
HttpEntity ht = response.getEntity(); String responseContent = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
JSONObject jsonObject = new JSONObject(responseContent); JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean(ApiConstants.RESPONSE_FIELD_OK)) { if (!jsonObject.getBoolean(ApiConstants.RESPONSE_FIELD_OK)) {
throw new TelegramApiRequestException("Error setting webhook", jsonObject); throw new TelegramApiRequestException("Error setting webhook", jsonObject);

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Properties>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} [%t] %-5level %logger{36} - %msg%n</Property>
<Property name="APP">App</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
<ThresholdFilter level="OFF" onMatch="ACCEPT" onMismatch="DENY"/>
</Console>
</Appenders>
<Loggers>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>