Possible improvements, may be unestable

This commit is contained in:
Rubenlagus 2016-05-08 01:54:53 +02:00
parent f7d7ed14bb
commit d0950a23e1
2 changed files with 30 additions and 30 deletions

View File

@ -61,7 +61,7 @@ public class Message implements IBotApiObject {
@JsonProperty(FORWARDFROM_FIELD) @JsonProperty(FORWARDFROM_FIELD)
private User forwardFrom; ///< Optional. For forwarded messages, sender of the original message private User forwardFrom; ///< Optional. For forwarded messages, sender of the original message
@JsonProperty(FORWARDFROMCHAT_FIELD) @JsonProperty(FORWARDFROMCHAT_FIELD)
private Chat forwardedFromChat; ///< Optional. For messages forwarded from a channel, information about the original channel private Chat forwardFromChat; ///< Optional. For messages forwarded from a channel, information about the original channel
@JsonProperty(FORWARDDATE_FIELD) @JsonProperty(FORWARDDATE_FIELD)
private Integer forwardDate; ///< Optional. For forwarded messages, date the original message was sent private Integer forwardDate; ///< Optional. For forwarded messages, date the original message was sent
@JsonProperty(TEXT_FIELD) @JsonProperty(TEXT_FIELD)
@ -130,7 +130,7 @@ public class Message implements IBotApiObject {
} }
this.chat = new Chat(jsonObject.getJSONObject(CHAT_FIELD)); this.chat = new Chat(jsonObject.getJSONObject(CHAT_FIELD));
if (jsonObject.has(FORWARDFROMCHAT_FIELD)) { if (jsonObject.has(FORWARDFROMCHAT_FIELD)) {
this.forwardedFromChat = new Chat(jsonObject.getJSONObject(FORWARDFROMCHAT_FIELD)); this.forwardFromChat = new Chat(jsonObject.getJSONObject(FORWARDFROMCHAT_FIELD));
} }
if (jsonObject.has(FORWARDFROM_FIELD)) { if (jsonObject.has(FORWARDFROM_FIELD)) {
this.forwardFrom = new User(jsonObject.getJSONObject(FORWARDFROM_FIELD)); this.forwardFrom = new User(jsonObject.getJSONObject(FORWARDFROM_FIELD));
@ -373,8 +373,8 @@ public class Message implements IBotApiObject {
return location != null; return location != null;
} }
public Chat getForwardedFromChat() { public Chat getForwardFromChat() {
return forwardedFromChat; return forwardFromChat;
} }
@Override @Override
@ -388,8 +388,8 @@ public class Message implements IBotApiObject {
gen.writeNumberField(DATE_FIELD, date); gen.writeNumberField(DATE_FIELD, date);
} }
gen.writeObjectField(CHAT_FIELD, chat); gen.writeObjectField(CHAT_FIELD, chat);
if (forwardedFromChat != null) { if (forwardFromChat != null) {
gen.writeObjectField(FORWARDFROMCHAT_FIELD, forwardedFromChat); gen.writeObjectField(FORWARDFROMCHAT_FIELD, forwardFromChat);
} }
if (forwardFrom != null) { if (forwardFrom != null) {
gen.writeObjectField(FORWARDFROM_FIELD, forwardFrom); gen.writeObjectField(FORWARDFROM_FIELD, forwardFrom);
@ -485,7 +485,7 @@ public class Message implements IBotApiObject {
", date=" + date + ", date=" + date +
", chat=" + chat + ", chat=" + chat +
", forwardFrom=" + forwardFrom + ", forwardFrom=" + forwardFrom +
", forwardedFromChat=" + forwardedFromChat + ", forwardFromChat=" + forwardFromChat +
", forwardDate=" + forwardDate + ", forwardDate=" + forwardDate +
", text='" + text + '\'' + ", text='" + text + '\'' +
", audio=" + audio + ", audio=" + audio +

View File

@ -1,8 +1,8 @@
package org.telegram.telegrambots.updatesreceivers; package org.telegram.telegrambots.updatesreceivers;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.HttpPost; import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.entity.BufferedHttpEntity;
@ -34,7 +34,7 @@ import java.util.concurrent.TimeUnit;
*/ */
public class BotSession { public class BotSession {
private static final String LOGTAG = "BOTSESSION"; private static final String LOGTAG = "BOTSESSION";
private static final int SOCKET_TIMEOUT = 30 * 1000; private static final int SOCKET_TIMEOUT = 10 * 1000;
private final ITelegramLongPollingBot callback; private final ITelegramLongPollingBot callback;
private final ReaderThread readerThread; private final ReaderThread readerThread;
@ -55,6 +55,10 @@ public class BotSession {
this.handlerThread = new HandlerThread(); this.handlerThread = new HandlerThread();
handlerThread.setName(callback.getBotUsername() + " Executor"); handlerThread.setName(callback.getBotUsername() + " Executor");
this.handlerThread.start(); this.handlerThread.start();
httpclient = HttpClientBuilder.create()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.setConnectionTimeToLive(120, TimeUnit.SECONDS)
.build();
} }
public void close() public void close()
@ -79,32 +83,28 @@ public class BotSession {
public void run() { public void run() {
setPriority(Thread.MIN_PRIORITY); setPriority(Thread.MIN_PRIORITY);
while(running) { while(running) {
GetUpdates request = new GetUpdates();
request.setLimit(100);
request.setTimeout(20);
request.setOffset(lastReceivedUpdate + 1);
httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).setConnectionTimeToLive(20, TimeUnit.SECONDS).build();
String url = Constants.BASEURL + token + "/" + GetUpdates.PATH;
//config
RequestConfig defaultRequestConfig = RequestConfig.custom().build();
RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
.setSocketTimeout(SOCKET_TIMEOUT)
.setConnectTimeout(SOCKET_TIMEOUT)
.setConnectionRequestTimeout(SOCKET_TIMEOUT).build();
//http client
HttpPost httpPost = new HttpPost(url);
try { try {
GetUpdates request = new GetUpdates();
request.setLimit(100);
request.setTimeout(50);
request.setOffset(lastReceivedUpdate + 1);
String url = Constants.BASEURL + token + "/" + GetUpdates.PATH;
//config
RequestConfig defaultRequestConfig = RequestConfig.custom().build();
RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
.setSocketTimeout(SOCKET_TIMEOUT)
.setConnectTimeout(SOCKET_TIMEOUT)
.setConnectionRequestTimeout(SOCKET_TIMEOUT).build();
//http client
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("charset", StandardCharsets.UTF_8.name()); httpPost.addHeader("charset", StandardCharsets.UTF_8.name());
httpPost.setConfig(requestConfig); httpPost.setConfig(requestConfig);
httpPost.setEntity(new StringEntity(request.toJson().toString(), ContentType.APPLICATION_JSON)); httpPost.setEntity(new StringEntity(request.toJson().toString(), ContentType.APPLICATION_JSON));
HttpResponse response;
response = httpclient.execute(httpPost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht); try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8); HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
try { 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 InvalidObjectException(jsonObject.toString()); throw new InvalidObjectException(jsonObject.toString());