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

View File

@ -1,8 +1,8 @@
package org.telegram.telegrambots.updatesreceivers;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.conn.ssl.NoopHostnameVerifier;
import org.apache.http.entity.BufferedHttpEntity;
@ -34,7 +34,7 @@ import java.util.concurrent.TimeUnit;
*/
public class 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 ReaderThread readerThread;
@ -55,6 +55,10 @@ public class BotSession {
this.handlerThread = new HandlerThread();
handlerThread.setName(callback.getBotUsername() + " Executor");
this.handlerThread.start();
httpclient = HttpClientBuilder.create()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.setConnectionTimeToLive(120, TimeUnit.SECONDS)
.build();
}
public void close()
@ -79,11 +83,11 @@ public class BotSession {
public void run() {
setPriority(Thread.MIN_PRIORITY);
while(running) {
try {
GetUpdates request = new GetUpdates();
request.setLimit(100);
request.setTimeout(20);
request.setTimeout(50);
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();
@ -93,18 +97,14 @@ public class BotSession {
.setConnectionRequestTimeout(SOCKET_TIMEOUT).build();
//http client
HttpPost httpPost = new HttpPost(url);
try {
httpPost.addHeader("charset", StandardCharsets.UTF_8.name());
httpPost.setConfig(requestConfig);
httpPost.setEntity(new StringEntity(request.toJson().toString(), ContentType.APPLICATION_JSON));
HttpResponse response;
response = httpclient.execute(httpPost);
HttpEntity ht = response.getEntity();
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
try {
JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
throw new InvalidObjectException(jsonObject.toString());