2016-01-14 23:09:19 +01:00
|
|
|
package org.telegram.telegrambots.updatesreceivers;
|
2016-01-14 01:14:53 +01:00
|
|
|
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
import org.apache.http.HttpResponse;
|
2016-03-16 20:11:06 +01:00
|
|
|
import org.apache.http.client.config.RequestConfig;
|
2016-01-14 01:14:53 +01:00
|
|
|
import org.apache.http.client.methods.HttpPost;
|
|
|
|
import org.apache.http.conn.ssl.NoopHostnameVerifier;
|
|
|
|
import org.apache.http.entity.BufferedHttpEntity;
|
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
2016-04-12 19:53:21 +02:00
|
|
|
import org.telegram.telegrambots.Constants;
|
2016-04-11 02:53:53 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.updates.GetUpdates;
|
2016-01-14 23:09:19 +01:00
|
|
|
import org.telegram.telegrambots.api.objects.Update;
|
|
|
|
import org.telegram.telegrambots.bots.ITelegramLongPollingBot;
|
2016-01-14 01:14:53 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InvalidObjectException;
|
2016-04-12 19:53:21 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2016-01-14 01:14:53 +01:00
|
|
|
import java.util.concurrent.ConcurrentLinkedDeque;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Ruben Bermudez
|
|
|
|
* @version 1.0
|
|
|
|
* @brief Thread to request updates with active wait
|
|
|
|
* @date 20 of June of 2015
|
|
|
|
*/
|
2016-04-17 15:50:54 +02:00
|
|
|
public class BotSession {
|
2016-04-11 02:53:53 +02:00
|
|
|
private static final int SOCKET_TIMEOUT = 30 * 1000;
|
|
|
|
|
2016-01-14 23:09:19 +01:00
|
|
|
private final ITelegramLongPollingBot callback;
|
2016-01-14 01:14:53 +01:00
|
|
|
private final ReaderThread readerThread;
|
|
|
|
private final HandlerThread handlerThread;
|
2016-04-11 02:53:53 +02:00
|
|
|
private final ConcurrentLinkedDeque<Update> receivedUpdates = new ConcurrentLinkedDeque<>();
|
2016-04-12 19:53:21 +02:00
|
|
|
private final String token;
|
2016-01-14 01:14:53 +01:00
|
|
|
private int lastReceivedUpdate = 0;
|
2016-04-17 15:50:54 +02:00
|
|
|
private volatile boolean running = true;
|
|
|
|
private volatile CloseableHttpClient httpclient;
|
2016-01-14 01:14:53 +01:00
|
|
|
|
2016-04-17 15:50:54 +02:00
|
|
|
|
|
|
|
public BotSession(String token, ITelegramLongPollingBot callback) {
|
2016-01-14 01:14:53 +01:00
|
|
|
this.token = token;
|
|
|
|
this.callback = callback;
|
|
|
|
this.readerThread = new ReaderThread();
|
2016-04-17 15:50:54 +02:00
|
|
|
readerThread.setName(callback.getBotUsername() + " Telegram Connection");
|
2016-01-14 01:14:53 +01:00
|
|
|
this.readerThread.start();
|
|
|
|
this.handlerThread = new HandlerThread();
|
2016-04-17 15:50:54 +02:00
|
|
|
handlerThread.setName(callback.getBotUsername() + " Executor");
|
2016-01-14 01:14:53 +01:00
|
|
|
this.handlerThread.start();
|
|
|
|
}
|
2016-04-17 15:50:54 +02:00
|
|
|
|
|
|
|
public void close()
|
|
|
|
{
|
|
|
|
running = false;
|
|
|
|
if(httpclient != null)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
httpclient.close();
|
|
|
|
httpclient = null;
|
2016-04-17 16:54:08 +02:00
|
|
|
} catch (IOException ignored) {
|
2016-04-17 15:50:54 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
|
|
|
|
private class ReaderThread extends Thread {
|
2016-04-17 15:50:54 +02:00
|
|
|
|
|
|
|
@Override
|
2016-01-14 01:14:53 +01:00
|
|
|
public void run() {
|
|
|
|
setPriority(Thread.MIN_PRIORITY);
|
2016-04-17 15:50:54 +02:00
|
|
|
while(running) {
|
2016-01-14 01:14:53 +01:00
|
|
|
GetUpdates request = new GetUpdates();
|
|
|
|
request.setLimit(100);
|
|
|
|
request.setTimeout(20);
|
|
|
|
request.setOffset(lastReceivedUpdate + 1);
|
2016-04-17 15:50:54 +02:00
|
|
|
httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).setConnectionTimeToLive(20, TimeUnit.SECONDS).build();
|
2016-01-14 01:14:53 +01:00
|
|
|
String url = Constants.BASEURL + token + "/" + GetUpdates.PATH;
|
2016-03-16 20:11:06 +01:00
|
|
|
//config
|
|
|
|
RequestConfig defaultRequestConfig = RequestConfig.custom().build();
|
|
|
|
RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
|
2016-04-11 02:53:53 +02:00
|
|
|
.setSocketTimeout(SOCKET_TIMEOUT)
|
|
|
|
.setConnectTimeout(SOCKET_TIMEOUT)
|
|
|
|
.setConnectionRequestTimeout(SOCKET_TIMEOUT).build();
|
2016-03-16 20:11:06 +01:00
|
|
|
//http client
|
2016-01-14 01:14:53 +01:00
|
|
|
HttpPost httpPost = new HttpPost(url);
|
|
|
|
try {
|
2016-04-12 19:53:21 +02:00
|
|
|
httpPost.addHeader("charset", StandardCharsets.UTF_8.name());
|
2016-03-16 20:11:06 +01:00
|
|
|
httpPost.setConfig(requestConfig);
|
2016-01-14 01:14:53 +01:00
|
|
|
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);
|
2016-04-12 19:53:21 +02:00
|
|
|
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
2016-01-14 01:14:53 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
JSONObject jsonObject = new JSONObject(responseContent);
|
2016-04-12 19:53:21 +02:00
|
|
|
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
2016-01-14 01:14:53 +01:00
|
|
|
throw new InvalidObjectException(jsonObject.toString());
|
|
|
|
}
|
2016-04-12 19:53:21 +02:00
|
|
|
JSONArray jsonArray = jsonObject.getJSONArray(Constants.RESPONSEFIELDRESULT);
|
2016-01-14 01:14:53 +01:00
|
|
|
if (jsonArray.length() != 0) {
|
|
|
|
for (int i = 0; i < jsonArray.length(); i++) {
|
|
|
|
Update update = new Update(jsonArray.getJSONObject(i));
|
|
|
|
if (update.getUpdateId() > lastReceivedUpdate) {
|
|
|
|
lastReceivedUpdate = update.getUpdateId();
|
|
|
|
receivedUpdates.addFirst(update);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
synchronized (receivedUpdates) {
|
|
|
|
receivedUpdates.notifyAll();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
synchronized (this) {
|
|
|
|
this.wait(500);
|
|
|
|
}
|
2016-01-14 23:09:19 +01:00
|
|
|
} catch (InterruptedException ignored) {
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-14 23:09:19 +01:00
|
|
|
} catch (JSONException ignored) {
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-04-29 14:27:09 +02:00
|
|
|
} catch (Exception e) {
|
2016-04-28 09:15:11 +02:00
|
|
|
try {
|
|
|
|
synchronized (this) {
|
|
|
|
this.wait(500);
|
|
|
|
}
|
|
|
|
} catch (InterruptedException ignored) {
|
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private class HandlerThread extends Thread {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
setPriority(Thread.MIN_PRIORITY);
|
2016-04-17 15:50:54 +02:00
|
|
|
while(running) {
|
2016-01-14 01:14:53 +01:00
|
|
|
try {
|
|
|
|
Update update = receivedUpdates.pollLast();
|
|
|
|
if (update == null) {
|
|
|
|
synchronized (receivedUpdates) {
|
|
|
|
try {
|
|
|
|
receivedUpdates.wait();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
update = receivedUpdates.pollLast();
|
|
|
|
if (update == null) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
callback.onUpdateReceived(update);
|
2016-01-14 23:09:19 +01:00
|
|
|
} catch (Exception ignored) {
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|