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 # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid* hs_err_pid*
# unneeded files
.idea/dataSources.*
.idea/workspace.xml
# logs files # logs files
*.log *.log

View File

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

View File

@ -94,8 +94,7 @@ public class TelegramBotsApi {
* @throws TelegramApiException * @throws TelegramApiException
*/ */
private static void setWebhook(String webHookURL, String botToken, String publicCertificatePath, String publicCertificateName) throws TelegramApiException { private static void setWebhook(String webHookURL, String botToken, String publicCertificatePath, String publicCertificateName) throws TelegramApiException {
try { try (CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build()) {
CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
String url = Constants.BASEURL + botToken + "/" + SetWebhook.PATH; String url = Constants.BASEURL + botToken + "/" + SetWebhook.PATH;
HttpPost httppost = new HttpPost(url); HttpPost httppost = new HttpPost(url);
@ -106,13 +105,14 @@ public class TelegramBotsApi {
} }
HttpEntity multipart = builder.build(); HttpEntity multipart = builder.build();
httppost.setEntity(multipart); httppost.setEntity(multipart);
CloseableHttpResponse response = httpclient.execute(httppost); try (CloseableHttpResponse response = httpclient.execute(httppost)) {
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);
JSONObject jsonObject = new JSONObject(responseContent); JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) { if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
throw new TelegramApiException(webHookURL == null ? "Error removing old webhook" : "Error setting webhook", responseContent); throw new TelegramApiException(webHookURL == null ? "Error removing old webhook" : "Error setting webhook", responseContent);
}
} }
} catch (JSONException e) { } catch (JSONException e) {
throw new TelegramApiException("Error deserializing setWebhook method response", 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.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject; import org.json.JSONObject;
import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent;
import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup;
@ -52,7 +53,7 @@ public class InlineQueryResultArticle implements InlineQueryResult {
@JsonProperty(THUMBHEIGHT_FIELD) @JsonProperty(THUMBHEIGHT_FIELD)
private Integer thumbHeight; ///< Optional. Thumbnail height private Integer thumbHeight; ///< Optional. Thumbnail height
public String getType() { public static String getType() {
return type; return type;
} }
@ -150,8 +151,8 @@ public class InlineQueryResultArticle implements InlineQueryResult {
public JSONObject toJson() { public JSONObject toJson() {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put(TYPE_FIELD, this.type); jsonObject.put(TYPE_FIELD, type);
jsonObject.put(ID_FIELD, this.id); jsonObject.put(ID_FIELD, id);
jsonObject.put(TITLE_FIELD, this.title); jsonObject.put(TITLE_FIELD, this.title);
jsonObject.put(INPUTMESSAGECONTENT_FIELD, inputMessageContent.toJson()); jsonObject.put(INPUTMESSAGECONTENT_FIELD, inputMessageContent.toJson());
if (replyMarkup != null) { 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.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject; import org.json.JSONObject;
import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent;
import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup;
@ -51,7 +52,7 @@ public class InlineQueryResultGif implements InlineQueryResult {
@JsonProperty(REPLY_MARKUP_FIELD) @JsonProperty(REPLY_MARKUP_FIELD)
private InlineKeyboardMarkup replyMarkup; ///< Optional. Inline keyboard attached to the message private InlineKeyboardMarkup replyMarkup; ///< Optional. Inline keyboard attached to the message
public String getType() { public static String getType() {
return type; return type;
} }
@ -139,7 +140,7 @@ public class InlineQueryResultGif implements InlineQueryResult {
@Override @Override
public JSONObject toJson() { public JSONObject toJson() {
JSONObject jsonObject = new JSONObject(); JSONObject jsonObject = new JSONObject();
jsonObject.put(TYPE_FIELD, this.type); jsonObject.put(TYPE_FIELD, type);
jsonObject.put(ID_FIELD, this.id); jsonObject.put(ID_FIELD, this.id);
jsonObject.put(GIFURL_FIELD, this.gifUrl); jsonObject.put(GIFURL_FIELD, this.gifUrl);
if (gifWidth != null) { 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.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer; import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject; import org.json.JSONObject;
import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent; import org.telegram.telegrambots.api.objects.inlinequery.inputmessagecontent.InputMessageContent;
import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup; import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup;
@ -51,7 +52,7 @@ public class InlineQueryResultMpeg4Gif implements InlineQueryResult {
@JsonProperty(REPLY_MARKUP_FIELD) @JsonProperty(REPLY_MARKUP_FIELD)
private InlineKeyboardMarkup replyMarkup; ///< Optional. Inline keyboard attached to the message private InlineKeyboardMarkup replyMarkup; ///< Optional. Inline keyboard attached to the message
public String getType() { public static String getType() {
return type; return type;
} }

View File

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