Several improvements

This commit is contained in:
Rubenlagus 2016-05-09 02:44:26 +02:00
parent c42a23501d
commit 0f13e22917
7 changed files with 74 additions and 75 deletions

4
.gitignore vendored
View File

@ -22,10 +22,6 @@ buildNumber.properties
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# unneeded files
.idea/dataSources.*
.idea/workspace.xml
# logs files
*.log

View File

@ -121,8 +121,8 @@ public class BotLogger {
}
public static void log(Level level, String tag, String msg, Throwable thrown) {
logger.log(level, msg, thrown);
logToFile(level, msg, thrown);
logger.log(level, String.format("[%s] %s", tag, msg), thrown);
logToFile(level, tag, msg, thrown);
}
public static void severe(String tag, Throwable throwable) {
@ -201,7 +201,7 @@ public class BotLogger {
}
public static void finest(String msg, String tag, Throwable throwable) {
log(Level.FINEST, msg, throwable);
log(Level.FINEST, tag, msg, throwable);
}
public static void warn(String msg, String tag, Throwable throwable) {

View File

@ -94,8 +94,7 @@ public class TelegramBotsApi {
* @throws TelegramApiException
*/
private static void setWebhook(String webHookURL, String botToken, String publicCertificatePath, String publicCertificateName) throws TelegramApiException {
try {
CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
try (CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build()) {
String url = Constants.BASEURL + botToken + "/" + SetWebhook.PATH;
HttpPost httppost = new HttpPost(url);
@ -106,13 +105,14 @@ public class TelegramBotsApi {
}
HttpEntity multipart = builder.build();
httppost.setEntity(multipart);
CloseableHttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
throw new TelegramApiException(webHookURL == null ? "Error removing old webhook" : "Error setting webhook", responseContent);
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
throw new TelegramApiException(webHookURL == null ? "Error removing old webhook" : "Error setting webhook", responseContent);
}
}
} catch (JSONException e) {
throw new TelegramApiException("Error deserializing setWebhook method response", e);

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent;
import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup;
@ -52,7 +53,7 @@ public class InlineQueryResultArticle implements InlineQueryResult {
@JsonProperty(THUMBHEIGHT_FIELD)
private Integer thumbHeight; ///< Optional. Thumbnail height
public String getType() {
public static String getType() {
return type;
}
@ -150,8 +151,8 @@ public class InlineQueryResultArticle implements InlineQueryResult {
public JSONObject toJson() {
JSONObject jsonObject = new JSONObject();
jsonObject.put(TYPE_FIELD, this.type);
jsonObject.put(ID_FIELD, this.id);
jsonObject.put(TYPE_FIELD, type);
jsonObject.put(ID_FIELD, id);
jsonObject.put(TITLE_FIELD, this.title);
jsonObject.put(INPUTMESSAGECONTENT_FIELD, inputMessageContent.toJson());
if (replyMarkup != null) {

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent;
import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup;
@ -51,7 +52,7 @@ public class InlineQueryResultGif implements InlineQueryResult {
@JsonProperty(REPLY_MARKUP_FIELD)
private InlineKeyboardMarkup replyMarkup; ///< Optional. Inline keyboard attached to the message
public String getType() {
public static String getType() {
return type;
}
@ -139,7 +140,7 @@ public class InlineQueryResultGif implements InlineQueryResult {
@Override
public JSONObject toJson() {
JSONObject jsonObject = new JSONObject();
jsonObject.put(TYPE_FIELD, this.type);
jsonObject.put(TYPE_FIELD, type);
jsonObject.put(ID_FIELD, this.id);
jsonObject.put(GIFURL_FIELD, this.gifUrl);
if (gifWidth != null) {

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent;
import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup;
@ -51,7 +52,7 @@ public class InlineQueryResultMpeg4Gif implements InlineQueryResult {
@JsonProperty(REPLY_MARKUP_FIELD)
private InlineKeyboardMarkup replyMarkup; ///< Optional. Inline keyboard attached to the message
public String getType() {
public static String getType() {
return type;
}

View File

@ -382,8 +382,7 @@ public abstract class AbsSender {
public Message sendDocument(SendDocument sendDocument) throws TelegramApiException {
String responseContent;
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
String url = getBaseUrl() + SendDocument.PATH;
HttpPost httppost = new HttpPost(url);
if (sendDocument.isNewDocument()) {
@ -423,10 +422,11 @@ public abstract class AbsSender {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
}
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
try (CloseableHttpResponse response = httpClient.execute(httppost)) {
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) {
throw new TelegramApiException("Unable to send document", e);
}
@ -441,8 +441,7 @@ public abstract class AbsSender {
public Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException {
String responseContent;
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
String url = getBaseUrl() + SendPhoto.PATH;
HttpPost httppost = new HttpPost(url);
if (sendPhoto.isNewPhoto()) {
@ -482,10 +481,11 @@ public abstract class AbsSender {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
}
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
try (CloseableHttpResponse response = httpClient.execute(httppost)) {
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) {
throw new TelegramApiException("Unable to send photo", e);
}
@ -500,8 +500,7 @@ public abstract class AbsSender {
public Message sendVideo(SendVideo sendVideo) throws TelegramApiException {
String responseContent;
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
String url = getBaseUrl() + SendVideo.PATH;
HttpPost httppost = new HttpPost(url);
if (sendVideo.isNewVideo()) {
@ -559,10 +558,11 @@ public abstract class AbsSender {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
}
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
try (CloseableHttpResponse response = httpClient.execute(httppost)) {
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) {
throw new TelegramApiException("Unable to send video", e);
}
@ -578,8 +578,7 @@ public abstract class AbsSender {
public Message sendSticker(SendSticker sendSticker) throws TelegramApiException {
String responseContent;
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
String url = getBaseUrl() + SendSticker.PATH;
HttpPost httppost = new HttpPost(url);
if (sendSticker.isNewSticker()) {
@ -613,10 +612,11 @@ public abstract class AbsSender {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
}
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
try (CloseableHttpResponse response = httpClient.execute(httppost)) {
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) {
throw new TelegramApiException("Unable to send sticker", e);
}
@ -639,8 +639,7 @@ public abstract class AbsSender {
String responseContent;
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
String url = getBaseUrl() + SendAudio.PATH;
HttpPost httppost = new HttpPost(url);
@ -690,10 +689,11 @@ public abstract class AbsSender {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
}
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
try (CloseableHttpResponse response = httpClient.execute(httppost)) {
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) {
throw new TelegramApiException("Unable to send sticker", e);
}
@ -721,8 +721,7 @@ public abstract class AbsSender {
public Message sendVoice(SendVoice sendVoice) throws TelegramApiException {
String responseContent;
try {
CloseableHttpClient httpClient = HttpClients.createDefault();
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
String url = getBaseUrl() + SendVoice.PATH;
HttpPost httppost = new HttpPost(url);
@ -763,10 +762,11 @@ public abstract class AbsSender {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
}
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
try (CloseableHttpResponse response = httpClient.execute(httppost)) {
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) {
throw new TelegramApiException("Unable to send sticker", e);
}
@ -786,22 +786,22 @@ public abstract class AbsSender {
exe.submit(new Runnable() {
@Override
public void run() {
try {
CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
try (CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build()) {
String url = getBaseUrl() + method.getPath();
HttpPost httppost = new HttpPost(url);
httppost.addHeader("charset", StandardCharsets.UTF_8.name());
httppost.setEntity(new StringEntity(method.toJson().toString(), ContentType.APPLICATION_JSON));
CloseableHttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
callback.onError(method, jsonObject);
JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
callback.onError(method, jsonObject);
}
callback.onResult(method, jsonObject);
}
callback.onResult(method, jsonObject);
} catch (IOException e) {
callback.onException(method, e);
}
@ -812,16 +812,16 @@ public abstract class AbsSender {
private Serializable sendApiMethod(BotApiMethod method) throws TelegramApiException {
String responseContent;
try {
CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
try (CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build()) {
String url = getBaseUrl() + method.getPath();
HttpPost httppost = new HttpPost(url);
httppost.addHeader("charset", StandardCharsets.UTF_8.name());
httppost.setEntity(new StringEntity(method.toJson().toString(), ContentType.APPLICATION_JSON));
CloseableHttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
}
} catch (IOException e) {
throw new TelegramApiException("Unable to execute " + method.getPath() + " method", e);
}