2016-01-14 23:09:19 +01:00
|
|
|
package org.telegram.telegrambots.bots;
|
2016-01-14 01:14:53 +01:00
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2016-09-24 22:37:25 +02:00
|
|
|
import org.apache.commons.io.FileUtils;
|
2016-01-14 01:14:53 +01:00
|
|
|
import org.apache.http.HttpEntity;
|
2016-05-11 15:24:40 +02:00
|
|
|
import org.apache.http.client.config.RequestConfig;
|
2016-01-14 01:14:53 +01:00
|
|
|
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;
|
|
|
|
import org.apache.http.entity.ContentType;
|
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
|
import org.apache.http.entity.mime.MultipartEntityBuilder;
|
|
|
|
import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
|
import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
|
import org.apache.http.util.EntityUtils;
|
2016-05-22 11:42:04 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.BotApiMethod;
|
2017-06-30 14:15:48 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.groupadministration.SetChatPhoto;
|
2017-05-03 01:40:55 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.send.*;
|
2016-01-14 23:09:19 +01:00
|
|
|
import org.telegram.telegrambots.api.objects.File;
|
|
|
|
import org.telegram.telegrambots.api.objects.Message;
|
2016-09-24 22:37:25 +02:00
|
|
|
import org.telegram.telegrambots.exceptions.TelegramApiException;
|
|
|
|
import org.telegram.telegrambots.exceptions.TelegramApiRequestException;
|
|
|
|
import org.telegram.telegrambots.exceptions.TelegramApiValidationException;
|
|
|
|
import org.telegram.telegrambots.updateshandlers.DownloadFileCallback;
|
2016-01-14 23:09:19 +01:00
|
|
|
import org.telegram.telegrambots.updateshandlers.SentCallback;
|
2016-01-14 01:14:53 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
2016-01-14 23:09:19 +01:00
|
|
|
import java.io.Serializable;
|
2016-09-24 22:37:25 +02:00
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
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.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
2016-05-11 15:24:40 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2016-01-14 01:14:53 +01:00
|
|
|
|
2017-03-27 00:49:08 +02:00
|
|
|
import static org.telegram.telegrambots.Constants.SOCKET_TIMEOUT;
|
|
|
|
|
2016-01-14 01:14:53 +01:00
|
|
|
/**
|
|
|
|
* @author Ruben Bermudez
|
|
|
|
* @version 1.0
|
2016-01-24 03:20:54 +01:00
|
|
|
* @brief Implementation of all the methods needed to interact with Telegram Servers
|
2016-01-14 23:09:19 +01:00
|
|
|
* @date 14 of January of 2016
|
2016-01-14 01:14:53 +01:00
|
|
|
*/
|
2016-05-09 02:27:49 +02:00
|
|
|
@SuppressWarnings("unused")
|
2016-12-03 21:17:03 +01:00
|
|
|
public abstract class DefaultAbsSender extends AbsSender {
|
2016-06-09 22:31:14 +02:00
|
|
|
private static final ContentType TEXT_PLAIN_CONTENT_TYPE = ContentType.create("text/plain", StandardCharsets.UTF_8);
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
private final ExecutorService exe;
|
|
|
|
private final ObjectMapper objectMapper = new ObjectMapper();
|
|
|
|
private final DefaultBotOptions options;
|
2016-05-11 15:24:40 +02:00
|
|
|
private volatile CloseableHttpClient httpclient;
|
|
|
|
private volatile RequestConfig requestConfig;
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
protected DefaultAbsSender(DefaultBotOptions options) {
|
|
|
|
super();
|
|
|
|
this.exe = Executors.newFixedThreadPool(options.getMaxThreads());
|
2016-07-21 21:28:13 +02:00
|
|
|
this.options = options;
|
2016-05-11 15:24:40 +02:00
|
|
|
httpclient = HttpClientBuilder.create()
|
|
|
|
.setSSLHostnameVerifier(new NoopHostnameVerifier())
|
|
|
|
.setConnectionTimeToLive(70, TimeUnit.SECONDS)
|
|
|
|
.setMaxConnTotal(100)
|
|
|
|
.build();
|
2016-07-21 21:28:13 +02:00
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
requestConfig = options.getRequestConfig();
|
2017-03-27 00:49:08 +02:00
|
|
|
|
|
|
|
if (requestConfig == null) {
|
|
|
|
requestConfig = RequestConfig.copy(RequestConfig.custom().build())
|
|
|
|
.setSocketTimeout(SOCKET_TIMEOUT)
|
|
|
|
.setConnectTimeout(SOCKET_TIMEOUT)
|
|
|
|
.setConnectionRequestTimeout(SOCKET_TIMEOUT).build();
|
|
|
|
}
|
2016-05-11 15:24:40 +02:00
|
|
|
}
|
2016-01-14 23:09:19 +01:00
|
|
|
|
2016-01-24 03:20:54 +01:00
|
|
|
/**
|
|
|
|
* Returns the token of the bot to be able to perform Telegram Api Requests
|
|
|
|
* @return Token of the bot
|
|
|
|
*/
|
2016-01-14 23:09:19 +01:00
|
|
|
public abstract String getBotToken();
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
public final DefaultBotOptions getOptions() {
|
2016-07-21 21:28:13 +02:00
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2016-04-11 02:53:53 +02:00
|
|
|
// Send Requests
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
public final java.io.File downloadFile(String filePath) throws TelegramApiException {
|
|
|
|
if(filePath == null || filePath.isEmpty()){
|
|
|
|
throw new TelegramApiException("Parameter file can not be null");
|
2016-05-11 15:24:40 +02:00
|
|
|
}
|
2016-11-17 02:41:12 +01:00
|
|
|
String url = File.getFileUrl(getBotToken(), filePath);
|
|
|
|
java.io.File output;
|
|
|
|
try {
|
|
|
|
output = java.io.File.createTempFile(Long.toString(System.currentTimeMillis()), ".tmp");
|
|
|
|
FileUtils.copyURLToFile(new URL(url), output);
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
throw new TelegramApiException("Wrong url for file: " + url);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new TelegramApiRequestException("Error downloading the file", e);
|
2016-05-11 15:24:40 +02:00
|
|
|
}
|
2016-01-14 23:09:19 +01:00
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
return output;
|
2016-08-12 21:23:30 +02:00
|
|
|
}
|
|
|
|
|
2016-09-24 22:37:25 +02:00
|
|
|
public final java.io.File downloadFile(File file) throws TelegramApiException {
|
|
|
|
if(file == null){
|
|
|
|
throw new TelegramApiException("Parameter file can not be null");
|
|
|
|
}
|
2016-11-17 02:41:12 +01:00
|
|
|
String url = file.getFileUrl(getBotToken());
|
2016-09-24 22:37:25 +02:00
|
|
|
java.io.File output;
|
|
|
|
try {
|
|
|
|
output = java.io.File.createTempFile(file.getFileId(), ".tmp");
|
|
|
|
FileUtils.copyURLToFile(new URL(url), output);
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
throw new TelegramApiException("Wrong url for file: " + url);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new TelegramApiRequestException("Error downloading the file", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
public final void downloadFileAsync(String filePath, DownloadFileCallback<String> callback) throws TelegramApiException {
|
|
|
|
if(filePath == null || filePath.isEmpty()){
|
|
|
|
throw new TelegramApiException("Parameter filePath can not be null");
|
2016-05-22 11:42:04 +02:00
|
|
|
}
|
2016-11-17 02:41:12 +01:00
|
|
|
if (callback == null) {
|
|
|
|
throw new TelegramApiException("Parameter callback can not be null");
|
2016-09-25 15:33:20 +02:00
|
|
|
}
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
exe.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
String url = File.getFileUrl(getBotToken(), filePath);
|
|
|
|
try {
|
|
|
|
java.io.File output = java.io.File.createTempFile(Long.toString(System.currentTimeMillis()), ".tmp");
|
|
|
|
FileUtils.copyURLToFile(new URL(url), output);
|
|
|
|
callback.onResult(filePath, output);
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
callback.onException(filePath, new TelegramApiException("Wrong url for file: " + url));
|
|
|
|
} catch (IOException e) {
|
|
|
|
callback.onException(filePath, new TelegramApiRequestException("Error downloading the file", e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-09-28 00:29:44 +02:00
|
|
|
}
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
public final void downloadFileAsync(File file, DownloadFileCallback<File> callback) throws TelegramApiException {
|
2016-09-24 22:37:25 +02:00
|
|
|
if(file == null){
|
|
|
|
throw new TelegramApiException("Parameter file can not be null");
|
|
|
|
}
|
|
|
|
if (callback == null) {
|
|
|
|
throw new TelegramApiException("Parameter callback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
exe.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2016-11-17 02:41:12 +01:00
|
|
|
String url = file.getFileUrl(getBotToken());
|
2016-09-24 22:37:25 +02:00
|
|
|
try {
|
|
|
|
java.io.File output = java.io.File.createTempFile(file.getFileId(), ".tmp");
|
|
|
|
FileUtils.copyURLToFile(new URL(url), output);
|
|
|
|
callback.onResult(file, output);
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
callback.onException(file, new TelegramApiException("Wrong url for file: " + url));
|
|
|
|
} catch (IOException e) {
|
|
|
|
callback.onException(file, new TelegramApiRequestException("Error downloading the file", e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-04-11 02:53:53 +02:00
|
|
|
// Specific Send Requests
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
@Override
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message sendDocument(SendDocument sendDocument) throws TelegramApiException {
|
2016-11-17 02:41:12 +01:00
|
|
|
if(sendDocument == null){
|
|
|
|
throw new TelegramApiException("Parameter sendDocument can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendDocument.validate();
|
2016-01-14 23:09:19 +01:00
|
|
|
String responseContent;
|
2016-01-14 01:14:53 +01:00
|
|
|
|
2016-05-11 15:24:40 +02:00
|
|
|
try {
|
2016-01-14 23:09:19 +01:00
|
|
|
String url = getBaseUrl() + SendDocument.PATH;
|
2016-01-14 01:14:53 +01:00
|
|
|
HttpPost httppost = new HttpPost(url);
|
2016-05-11 15:24:40 +02:00
|
|
|
httppost.setConfig(requestConfig);
|
2017-06-30 14:15:48 +02:00
|
|
|
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendDocument.CHATID_FIELD, sendDocument.getChatId());
|
2016-01-14 01:14:53 +01:00
|
|
|
if (sendDocument.isNewDocument()) {
|
2016-06-04 06:17:00 +02:00
|
|
|
if (sendDocument.getNewDocumentFile() != null) {
|
|
|
|
builder.addBinaryBody(SendDocument.DOCUMENT_FIELD, sendDocument.getNewDocumentFile());
|
|
|
|
} else if (sendDocument.getNewDocumentStream() != null) {
|
2016-08-07 05:47:23 +02:00
|
|
|
builder.addBinaryBody(SendDocument.DOCUMENT_FIELD, sendDocument.getNewDocumentStream(), ContentType.APPLICATION_OCTET_STREAM, sendDocument.getDocumentName());
|
2016-06-04 06:17:00 +02:00
|
|
|
} else {
|
|
|
|
builder.addBinaryBody(SendDocument.DOCUMENT_FIELD, new java.io.File(sendDocument.getDocument()), ContentType.APPLICATION_OCTET_STREAM, sendDocument.getDocumentName());
|
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
} else {
|
2017-06-30 14:15:48 +02:00
|
|
|
builder.addTextBody(SendDocument.DOCUMENT_FIELD, sendDocument.getDocument());
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2017-06-30 14:15:48 +02:00
|
|
|
if (sendDocument.getReplyMarkup() != null) {
|
|
|
|
builder.addTextBody(SendDocument.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendDocument.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE);
|
|
|
|
}
|
|
|
|
if (sendDocument.getReplyToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendDocument.REPLYTOMESSAGEID_FIELD, sendDocument.getReplyToMessageId().toString());
|
|
|
|
}
|
|
|
|
if (sendDocument.getCaption() != null) {
|
|
|
|
builder.addTextBody(SendDocument.CAPTION_FIELD, sendDocument.getCaption(), TEXT_PLAIN_CONTENT_TYPE);
|
|
|
|
}
|
|
|
|
if (sendDocument.getDisableNotification() != null) {
|
|
|
|
builder.addTextBody(SendDocument.DISABLENOTIFICATION_FIELD, sendDocument.getDisableNotification().toString());
|
|
|
|
}
|
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httppost.setEntity(multipart);
|
2016-01-14 01:14:53 +01:00
|
|
|
|
2016-05-11 15:24:40 +02:00
|
|
|
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
|
2016-05-09 02:44:26 +02:00
|
|
|
HttpEntity ht = response.getEntity();
|
|
|
|
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
|
|
|
|
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
} catch (IOException e) {
|
2016-01-14 23:09:19 +01:00
|
|
|
throw new TelegramApiException("Unable to send document", e);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-01-14 23:09:19 +01:00
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
return sendDocument.deserializeResponse(responseContent);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
@Override
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException {
|
2016-11-17 02:41:12 +01:00
|
|
|
if(sendPhoto == null){
|
|
|
|
throw new TelegramApiException("Parameter sendPhoto can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendPhoto.validate();
|
2016-01-14 23:09:19 +01:00
|
|
|
String responseContent;
|
2016-05-11 15:24:40 +02:00
|
|
|
try {
|
2016-01-14 23:09:19 +01:00
|
|
|
String url = getBaseUrl() + SendPhoto.PATH;
|
2016-01-14 01:14:53 +01:00
|
|
|
HttpPost httppost = new HttpPost(url);
|
2016-05-11 15:24:40 +02:00
|
|
|
httppost.setConfig(requestConfig);
|
2017-06-30 14:15:48 +02:00
|
|
|
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendPhoto.CHATID_FIELD, sendPhoto.getChatId());
|
2016-01-14 01:14:53 +01:00
|
|
|
if (sendPhoto.isNewPhoto()) {
|
2016-06-04 06:17:00 +02:00
|
|
|
if (sendPhoto.getNewPhotoFile() != null) {
|
|
|
|
builder.addBinaryBody(SendPhoto.PHOTO_FIELD, sendPhoto.getNewPhotoFile());
|
|
|
|
} else if (sendPhoto.getNewPhotoStream() != null) {
|
2016-08-07 05:47:23 +02:00
|
|
|
builder.addBinaryBody(SendPhoto.PHOTO_FIELD, sendPhoto.getNewPhotoStream(), ContentType.APPLICATION_OCTET_STREAM, sendPhoto.getPhotoName());
|
2016-06-04 06:17:00 +02:00
|
|
|
} else {
|
|
|
|
builder.addBinaryBody(SendPhoto.PHOTO_FIELD, new java.io.File(sendPhoto.getPhoto()), ContentType.APPLICATION_OCTET_STREAM, sendPhoto.getPhotoName());
|
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
} else {
|
2017-06-30 14:15:48 +02:00
|
|
|
builder.addTextBody(SendPhoto.PHOTO_FIELD, sendPhoto.getPhoto());
|
|
|
|
}
|
|
|
|
if (sendPhoto.getReplyMarkup() != null) {
|
|
|
|
builder.addTextBody(SendPhoto.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendPhoto.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE);
|
|
|
|
}
|
|
|
|
if (sendPhoto.getReplyToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendPhoto.REPLYTOMESSAGEID_FIELD, sendPhoto.getReplyToMessageId().toString());
|
|
|
|
}
|
|
|
|
if (sendPhoto.getCaption() != null) {
|
|
|
|
builder.addTextBody(SendPhoto.CAPTION_FIELD, sendPhoto.getCaption(), TEXT_PLAIN_CONTENT_TYPE);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2017-06-30 14:15:48 +02:00
|
|
|
if (sendPhoto.getDisableNotification() != null) {
|
|
|
|
builder.addTextBody(SendPhoto.DISABLENOTIFICATION_FIELD, sendPhoto.getDisableNotification().toString());
|
|
|
|
}
|
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httppost.setEntity(multipart);
|
2016-01-14 01:14:53 +01:00
|
|
|
|
2016-05-11 15:24:40 +02:00
|
|
|
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
|
2016-05-09 02:44:26 +02:00
|
|
|
HttpEntity ht = response.getEntity();
|
|
|
|
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
|
|
|
|
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
} catch (IOException e) {
|
2016-01-14 23:09:19 +01:00
|
|
|
throw new TelegramApiException("Unable to send photo", e);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-01-14 23:09:19 +01:00
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
return sendPhoto.deserializeResponse(responseContent);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
@Override
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message sendVideo(SendVideo sendVideo) throws TelegramApiException {
|
2016-11-17 02:41:12 +01:00
|
|
|
if(sendVideo == null){
|
|
|
|
throw new TelegramApiException("Parameter sendVideo can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendVideo.validate();
|
2016-01-14 23:09:19 +01:00
|
|
|
String responseContent;
|
2016-05-11 15:24:40 +02:00
|
|
|
try {
|
2016-01-14 23:09:19 +01:00
|
|
|
String url = getBaseUrl() + SendVideo.PATH;
|
2016-01-14 01:14:53 +01:00
|
|
|
HttpPost httppost = new HttpPost(url);
|
2016-05-11 15:24:40 +02:00
|
|
|
httppost.setConfig(requestConfig);
|
2017-06-30 14:15:48 +02:00
|
|
|
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendVideo.CHATID_FIELD, sendVideo.getChatId());
|
2016-01-14 01:14:53 +01:00
|
|
|
if (sendVideo.isNewVideo()) {
|
2016-06-04 06:17:00 +02:00
|
|
|
if (sendVideo.getNewVideoFile() != null) {
|
|
|
|
builder.addBinaryBody(SendVideo.VIDEO_FIELD, sendVideo.getNewVideoFile());
|
|
|
|
} else if (sendVideo.getNewVideoStream() != null) {
|
2016-08-07 05:47:23 +02:00
|
|
|
builder.addBinaryBody(SendVideo.VIDEO_FIELD, sendVideo.getNewVideoStream(), ContentType.APPLICATION_OCTET_STREAM, sendVideo.getVideoName());
|
2016-06-04 06:17:00 +02:00
|
|
|
} else {
|
|
|
|
builder.addBinaryBody(SendVideo.VIDEO_FIELD, new java.io.File(sendVideo.getVideo()), ContentType.APPLICATION_OCTET_STREAM, sendVideo.getVideoName());
|
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
} else {
|
2017-06-30 14:15:48 +02:00
|
|
|
builder.addTextBody(SendVideo.VIDEO_FIELD, sendVideo.getVideo());
|
|
|
|
}
|
|
|
|
if (sendVideo.getReplyMarkup() != null) {
|
|
|
|
builder.addTextBody(SendVideo.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendVideo.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2017-06-30 14:15:48 +02:00
|
|
|
if (sendVideo.getReplyToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendVideo.REPLYTOMESSAGEID_FIELD, sendVideo.getReplyToMessageId().toString());
|
|
|
|
}
|
|
|
|
if (sendVideo.getCaption() != null) {
|
|
|
|
builder.addTextBody(SendVideo.CAPTION_FIELD, sendVideo.getCaption(), TEXT_PLAIN_CONTENT_TYPE);
|
|
|
|
}
|
|
|
|
if (sendVideo.getDuration() != null) {
|
|
|
|
builder.addTextBody(SendVideo.DURATION_FIELD, sendVideo.getDuration().toString());
|
|
|
|
}
|
|
|
|
if (sendVideo.getWidth() != null) {
|
|
|
|
builder.addTextBody(SendVideo.WIDTH_FIELD, sendVideo.getWidth().toString());
|
|
|
|
}
|
|
|
|
if (sendVideo.getHeight() != null) {
|
|
|
|
builder.addTextBody(SendVideo.HEIGHT_FIELD, sendVideo.getHeight().toString());
|
|
|
|
}
|
|
|
|
if (sendVideo.getDisableNotification() != null) {
|
|
|
|
builder.addTextBody(SendVideo.DISABLENOTIFICATION_FIELD, sendVideo.getDisableNotification().toString());
|
|
|
|
}
|
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httppost.setEntity(multipart);
|
2016-01-14 01:14:53 +01:00
|
|
|
|
2016-05-11 15:24:40 +02:00
|
|
|
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
|
2016-05-09 02:44:26 +02:00
|
|
|
HttpEntity ht = response.getEntity();
|
|
|
|
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
|
|
|
|
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
} catch (IOException e) {
|
2016-01-14 23:09:19 +01:00
|
|
|
throw new TelegramApiException("Unable to send video", e);
|
|
|
|
}
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
return sendVideo.deserializeResponse(responseContent);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
|
2017-05-03 01:40:55 +02:00
|
|
|
@Override
|
|
|
|
public final Message sendVideoNote(SendVideoNote sendVideoNote) throws TelegramApiException {
|
|
|
|
if(sendVideoNote == null){
|
|
|
|
throw new TelegramApiException("Parameter sendVideoNote can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendVideoNote.validate();
|
|
|
|
String responseContent;
|
|
|
|
try {
|
|
|
|
String url = getBaseUrl() + SendVideoNote.PATH;
|
|
|
|
HttpPost httppost = new HttpPost(url);
|
|
|
|
httppost.setConfig(requestConfig);
|
2017-06-30 14:15:48 +02:00
|
|
|
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendVideoNote.CHATID_FIELD, sendVideoNote.getChatId());
|
2017-05-03 01:40:55 +02:00
|
|
|
if (sendVideoNote.isNewVideoNote()) {
|
|
|
|
if (sendVideoNote.getNewVideoNoteFile() != null) {
|
|
|
|
builder.addBinaryBody(SendVideoNote.VIDEONOTE_FIELD, sendVideoNote.getNewVideoNoteFile());
|
|
|
|
} else if (sendVideoNote.getNewVideoNoteStream() != null) {
|
|
|
|
builder.addBinaryBody(SendVideoNote.VIDEONOTE_FIELD, sendVideoNote.getNewVideoNoteStream(), ContentType.APPLICATION_OCTET_STREAM, sendVideoNote.getVideoNoteName());
|
|
|
|
} else {
|
|
|
|
builder.addBinaryBody(SendVideoNote.VIDEONOTE_FIELD, new java.io.File(sendVideoNote.getVideoNote()), ContentType.APPLICATION_OCTET_STREAM, sendVideoNote.getVideoNoteName());
|
|
|
|
}
|
|
|
|
} else {
|
2017-06-30 14:15:48 +02:00
|
|
|
builder.addTextBody(SendVideoNote.VIDEONOTE_FIELD, sendVideoNote.getVideoNote());
|
|
|
|
}
|
|
|
|
if (sendVideoNote.getReplyMarkup() != null) {
|
|
|
|
builder.addTextBody(SendVideoNote.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendVideoNote.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE);
|
|
|
|
}
|
|
|
|
if (sendVideoNote.getReplyToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendVideoNote.REPLYTOMESSAGEID_FIELD, sendVideoNote.getReplyToMessageId().toString());
|
|
|
|
}
|
|
|
|
if (sendVideoNote.getDuration() != null) {
|
|
|
|
builder.addTextBody(SendVideoNote.DURATION_FIELD, sendVideoNote.getDuration().toString());
|
2017-05-03 01:40:55 +02:00
|
|
|
}
|
2017-06-30 14:15:48 +02:00
|
|
|
if (sendVideoNote.getLength() != null) {
|
|
|
|
builder.addTextBody(SendVideoNote.LENGTH_FIELD, sendVideoNote.getLength().toString());
|
|
|
|
}
|
|
|
|
if (sendVideoNote.getDisableNotification() != null) {
|
|
|
|
builder.addTextBody(SendVideoNote.DISABLENOTIFICATION_FIELD, sendVideoNote.getDisableNotification().toString());
|
|
|
|
}
|
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httppost.setEntity(multipart);
|
|
|
|
|
2017-05-03 01:40:55 +02:00
|
|
|
|
|
|
|
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 note", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return sendVideoNote.deserializeResponse(responseContent);
|
|
|
|
}
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
@Override
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message sendSticker(SendSticker sendSticker) throws TelegramApiException {
|
2016-11-17 02:41:12 +01:00
|
|
|
if(sendSticker == null){
|
|
|
|
throw new TelegramApiException("Parameter sendSticker can not be null");
|
|
|
|
}
|
2016-01-14 23:09:19 +01:00
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
sendSticker.validate();
|
|
|
|
String responseContent;
|
2016-05-11 15:24:40 +02:00
|
|
|
try {
|
2016-01-14 23:09:19 +01:00
|
|
|
String url = getBaseUrl() + SendSticker.PATH;
|
2016-01-14 01:14:53 +01:00
|
|
|
HttpPost httppost = new HttpPost(url);
|
2016-05-11 15:24:40 +02:00
|
|
|
httppost.setConfig(requestConfig);
|
2017-06-30 14:15:48 +02:00
|
|
|
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendSticker.CHATID_FIELD, sendSticker.getChatId());
|
2016-01-14 01:14:53 +01:00
|
|
|
if (sendSticker.isNewSticker()) {
|
2016-06-04 06:17:00 +02:00
|
|
|
if (sendSticker.getNewStickerFile() != null) {
|
|
|
|
builder.addBinaryBody(SendSticker.STICKER_FIELD, sendSticker.getNewStickerFile());
|
|
|
|
} else if (sendSticker.getNewStickerStream() != null) {
|
2016-08-07 05:47:23 +02:00
|
|
|
builder.addBinaryBody(SendSticker.STICKER_FIELD, sendSticker.getNewStickerStream(), ContentType.APPLICATION_OCTET_STREAM, sendSticker.getStickerName());
|
2016-06-04 06:17:00 +02:00
|
|
|
} else {
|
|
|
|
builder.addBinaryBody(SendSticker.STICKER_FIELD, new java.io.File(sendSticker.getSticker()), ContentType.APPLICATION_OCTET_STREAM, sendSticker.getStickerName());
|
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
} else {
|
2017-06-30 14:15:48 +02:00
|
|
|
builder.addTextBody(SendSticker.STICKER_FIELD, sendSticker.getSticker());
|
|
|
|
}
|
|
|
|
if (sendSticker.getReplyMarkup() != null) {
|
|
|
|
builder.addTextBody(SendSticker.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendSticker.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE);
|
|
|
|
}
|
|
|
|
if (sendSticker.getReplyToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendSticker.REPLYTOMESSAGEID_FIELD, sendSticker.getReplyToMessageId().toString());
|
|
|
|
}
|
|
|
|
if (sendSticker.getDisableNotification() != null) {
|
|
|
|
builder.addTextBody(SendSticker.DISABLENOTIFICATION_FIELD, sendSticker.getDisableNotification().toString());
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2017-06-30 14:15:48 +02:00
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httppost.setEntity(multipart);
|
2016-01-14 01:14:53 +01:00
|
|
|
|
2016-05-11 15:24:40 +02:00
|
|
|
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
|
2016-05-09 02:44:26 +02:00
|
|
|
HttpEntity ht = response.getEntity();
|
|
|
|
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
|
|
|
|
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
} catch (IOException e) {
|
2016-01-14 23:09:19 +01:00
|
|
|
throw new TelegramApiException("Unable to send sticker", e);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
return sendSticker.deserializeResponse(responseContent);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
|
2016-01-24 03:20:54 +01:00
|
|
|
/**
|
|
|
|
* Sends a file using Send Audio method (https://core.telegram.org/bots/api#sendaudio)
|
|
|
|
* @param sendAudio Information to send
|
|
|
|
* @return If success, the sent Message is returned
|
|
|
|
* @throws TelegramApiException If there is any error sending the audio
|
|
|
|
*/
|
2016-11-17 02:41:12 +01:00
|
|
|
@Override
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message sendAudio(SendAudio sendAudio) throws TelegramApiException {
|
2016-11-17 02:41:12 +01:00
|
|
|
if(sendAudio == null){
|
|
|
|
throw new TelegramApiException("Parameter sendAudio can not be null");
|
|
|
|
}
|
|
|
|
sendAudio.validate();
|
2016-01-24 03:20:54 +01:00
|
|
|
String responseContent;
|
|
|
|
|
2016-05-11 15:24:40 +02:00
|
|
|
try {
|
2016-01-24 03:20:54 +01:00
|
|
|
String url = getBaseUrl() + SendAudio.PATH;
|
|
|
|
HttpPost httppost = new HttpPost(url);
|
2016-05-11 15:24:40 +02:00
|
|
|
httppost.setConfig(requestConfig);
|
2017-06-30 14:15:48 +02:00
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendAudio.CHATID_FIELD, sendAudio.getChatId());
|
2016-01-24 03:20:54 +01:00
|
|
|
if (sendAudio.isNewAudio()) {
|
2016-06-04 06:17:00 +02:00
|
|
|
if (sendAudio.getNewAudioFile() != null) {
|
|
|
|
builder.addBinaryBody(SendAudio.AUDIO_FIELD, sendAudio.getNewAudioFile());
|
|
|
|
} else if (sendAudio.getNewAudioStream() != null) {
|
2016-08-07 05:47:23 +02:00
|
|
|
builder.addBinaryBody(SendAudio.AUDIO_FIELD, sendAudio.getNewAudioStream(), ContentType.APPLICATION_OCTET_STREAM, sendAudio.getAudioName());
|
2016-06-04 06:17:00 +02:00
|
|
|
} else {
|
|
|
|
builder.addBinaryBody(SendAudio.AUDIO_FIELD, new java.io.File(sendAudio.getAudio()), ContentType.create("audio/mpeg"), sendAudio.getAudioName());
|
|
|
|
}
|
2016-01-24 03:20:54 +01:00
|
|
|
} else {
|
2017-06-30 14:15:48 +02:00
|
|
|
builder.addTextBody(SendAudio.AUDIO_FIELD, sendAudio.getAudio());
|
|
|
|
}
|
|
|
|
if (sendAudio.getReplyMarkup() != null) {
|
|
|
|
builder.addTextBody(SendAudio.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendAudio.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE);
|
|
|
|
}
|
|
|
|
if (sendAudio.getReplyToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendAudio.REPLYTOMESSAGEID_FIELD, sendAudio.getReplyToMessageId().toString());
|
|
|
|
}
|
|
|
|
if (sendAudio.getPerformer() != null) {
|
|
|
|
builder.addTextBody(SendAudio.PERFOMER_FIELD, sendAudio.getPerformer());
|
2016-01-24 03:20:54 +01:00
|
|
|
}
|
2017-06-30 14:15:48 +02:00
|
|
|
if (sendAudio.getTitle() != null) {
|
|
|
|
builder.addTextBody(SendAudio.TITLE_FIELD, sendAudio.getTitle());
|
|
|
|
}
|
|
|
|
if(sendAudio.getDuration() != null){
|
|
|
|
builder.addTextBody(SendAudio.DURATION_FIELD, sendAudio.getDuration().toString());
|
|
|
|
}
|
|
|
|
if (sendAudio.getDisableNotification() != null) {
|
|
|
|
builder.addTextBody(SendAudio.DISABLENOTIFICATION_FIELD, sendAudio.getDisableNotification().toString());
|
|
|
|
}
|
|
|
|
if (sendAudio.getCaption() != null) {
|
|
|
|
builder.addTextBody(SendAudio.CAPTION_FIELD, sendAudio.getCaption());
|
|
|
|
}
|
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httppost.setEntity(multipart);
|
|
|
|
|
2016-01-24 03:20:54 +01:00
|
|
|
|
2016-05-11 15:24:40 +02:00
|
|
|
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
|
2016-05-09 02:44:26 +02:00
|
|
|
HttpEntity ht = response.getEntity();
|
|
|
|
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
|
|
|
|
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
|
|
|
}
|
2016-01-24 03:20:54 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
throw new TelegramApiException("Unable to send sticker", e);
|
|
|
|
}
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
return sendAudio.deserializeResponse(responseContent);
|
2016-01-24 03:20:54 +01:00
|
|
|
}
|
|
|
|
|
2016-02-27 03:17:06 +01:00
|
|
|
/**
|
|
|
|
* Sends a voice note using Send Voice method (https://core.telegram.org/bots/api#sendvoice)
|
2016-06-03 19:22:13 +02:00
|
|
|
* For this to work, your audio must be in an .ogg file encoded with OPUS
|
2016-02-27 03:17:06 +01:00
|
|
|
* @param sendVoice Information to send
|
|
|
|
* @return If success, the sent Message is returned
|
|
|
|
* @throws TelegramApiException If there is any error sending the audio
|
|
|
|
*/
|
2016-11-17 02:41:12 +01:00
|
|
|
@Override
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message sendVoice(SendVoice sendVoice) throws TelegramApiException {
|
2016-11-17 02:41:12 +01:00
|
|
|
if(sendVoice == null){
|
|
|
|
throw new TelegramApiException("Parameter sendVoice can not be null");
|
|
|
|
}
|
|
|
|
sendVoice.validate();
|
2016-02-27 03:17:06 +01:00
|
|
|
String responseContent;
|
|
|
|
|
2016-05-11 15:24:40 +02:00
|
|
|
try {
|
2016-02-27 03:17:06 +01:00
|
|
|
String url = getBaseUrl() + SendVoice.PATH;
|
|
|
|
HttpPost httppost = new HttpPost(url);
|
2016-05-11 15:24:40 +02:00
|
|
|
httppost.setConfig(requestConfig);
|
2017-06-30 14:15:48 +02:00
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendVoice.CHATID_FIELD, sendVoice.getChatId());
|
2016-02-27 03:17:06 +01:00
|
|
|
if (sendVoice.isNewVoice()) {
|
2016-06-04 06:17:00 +02:00
|
|
|
if (sendVoice.getNewVoiceFile() != null) {
|
2016-06-09 22:52:08 +02:00
|
|
|
builder.addBinaryBody(SendVoice.VOICE_FIELD, sendVoice.getNewVoiceFile());
|
2016-06-04 06:17:00 +02:00
|
|
|
} else if (sendVoice.getNewVoiceStream() != null) {
|
2016-08-07 05:47:23 +02:00
|
|
|
builder.addBinaryBody(SendVoice.VOICE_FIELD, sendVoice.getNewVoiceStream(), ContentType.APPLICATION_OCTET_STREAM, sendVoice.getVoiceName());
|
2016-06-04 06:17:00 +02:00
|
|
|
} else {
|
2016-06-09 22:52:08 +02:00
|
|
|
builder.addBinaryBody(SendVoice.VOICE_FIELD, new java.io.File(sendVoice.getVoice()), ContentType.create("audio/ogg"), sendVoice.getVoiceName());
|
2016-06-04 06:17:00 +02:00
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
} else {
|
2017-06-30 14:15:48 +02:00
|
|
|
builder.addTextBody(SendVoice.VOICE_FIELD, sendVoice.getVoice());
|
|
|
|
}
|
|
|
|
if (sendVoice.getReplyMarkup() != null) {
|
|
|
|
builder.addTextBody(SendVoice.REPLYMARKUP_FIELD, objectMapper.writeValueAsString(sendVoice.getReplyMarkup()), TEXT_PLAIN_CONTENT_TYPE);
|
|
|
|
}
|
|
|
|
if (sendVoice.getReplyToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendVoice.REPLYTOMESSAGEID_FIELD, sendVoice.getReplyToMessageId().toString());
|
2016-02-27 03:17:06 +01:00
|
|
|
}
|
2017-06-30 14:15:48 +02:00
|
|
|
if (sendVoice.getDisableNotification() != null) {
|
|
|
|
builder.addTextBody(SendVoice.DISABLENOTIFICATION_FIELD, sendVoice.getDisableNotification().toString());
|
|
|
|
}
|
|
|
|
if (sendVoice.getDuration() != null) {
|
|
|
|
builder.addTextBody(SendVoice.DURATION_FIELD, sendVoice.getDuration().toString());
|
|
|
|
}
|
|
|
|
if (sendVoice.getCaption() != null) {
|
|
|
|
builder.addTextBody(SendVoice.CAPTION_FIELD, sendVoice.getCaption());
|
|
|
|
}
|
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httppost.setEntity(multipart);
|
2016-02-27 03:17:06 +01:00
|
|
|
|
2016-05-11 15:24:40 +02:00
|
|
|
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
|
2016-05-09 02:44:26 +02:00
|
|
|
HttpEntity ht = response.getEntity();
|
|
|
|
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
|
|
|
|
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
} catch (IOException e) {
|
2017-06-30 14:15:48 +02:00
|
|
|
throw new TelegramApiException("Unable to send voice", e);
|
2016-02-27 03:17:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
return sendVoice.deserializeResponse(responseContent);
|
2016-02-27 03:17:06 +01:00
|
|
|
}
|
|
|
|
|
2017-06-30 14:15:48 +02:00
|
|
|
@Override
|
|
|
|
public Boolean setChatPhoto(SetChatPhoto setChatPhoto) throws TelegramApiException {
|
|
|
|
if(setChatPhoto == null){
|
|
|
|
throw new TelegramApiException("Parameter setChatPhoto can not be null");
|
|
|
|
}
|
|
|
|
setChatPhoto.validate();
|
|
|
|
String responseContent;
|
|
|
|
|
|
|
|
try {
|
|
|
|
String url = getBaseUrl() + SetChatPhoto.PATH;
|
|
|
|
HttpPost httppost = new HttpPost(url);
|
|
|
|
httppost.setConfig(requestConfig);
|
|
|
|
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SetChatPhoto.CHATID_FIELD, setChatPhoto.getChatId());
|
|
|
|
if (setChatPhoto.getPhoto() != null) {
|
|
|
|
builder.addBinaryBody(SetChatPhoto.PHOTO_FIELD, setChatPhoto.getPhoto());
|
|
|
|
} else if (setChatPhoto.getPhotoStream() != null) {
|
|
|
|
builder.addBinaryBody(SetChatPhoto.PHOTO_FIELD, setChatPhoto.getPhotoStream(), ContentType.APPLICATION_OCTET_STREAM, setChatPhoto.getPhotoName());
|
|
|
|
}
|
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httppost.setEntity(multipart);
|
|
|
|
|
|
|
|
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 set chat photo", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return setChatPhoto.deserializeResponse(responseContent);
|
|
|
|
}
|
|
|
|
|
2016-04-11 02:53:53 +02:00
|
|
|
// Simplified methods
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
@Override
|
|
|
|
protected final <T extends Serializable, Method extends BotApiMethod<T>, Callback extends SentCallback<T>> void sendApiMethodAsync(Method method, Callback callback) {
|
2016-05-09 02:27:49 +02:00
|
|
|
//noinspection Convert2Lambda
|
2016-04-17 17:10:09 +02:00
|
|
|
exe.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
2016-05-11 15:24:40 +02:00
|
|
|
try {
|
2016-09-24 22:37:25 +02:00
|
|
|
method.validate();
|
2016-11-17 02:41:12 +01:00
|
|
|
String url = getBaseUrl() + method.getMethod();
|
2016-04-17 17:10:09 +02:00
|
|
|
HttpPost httppost = new HttpPost(url);
|
2016-05-11 15:24:40 +02:00
|
|
|
httppost.setConfig(requestConfig);
|
2016-04-17 17:10:09 +02:00
|
|
|
httppost.addHeader("charset", StandardCharsets.UTF_8.name());
|
2016-11-17 02:41:12 +01:00
|
|
|
httppost.setEntity(new StringEntity(objectMapper.writeValueAsString(method), ContentType.APPLICATION_JSON));
|
2016-05-09 02:44:26 +02:00
|
|
|
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
|
|
|
|
HttpEntity ht = response.getEntity();
|
|
|
|
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
|
|
|
|
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
2016-11-17 03:31:32 +01:00
|
|
|
try {
|
|
|
|
callback.onResult(method, method.deserializeResponse(responseContent));
|
|
|
|
} catch (TelegramApiRequestException e) {
|
|
|
|
callback.onError(method, e);
|
2016-05-09 02:44:26 +02:00
|
|
|
}
|
2016-04-17 17:10:09 +02:00
|
|
|
}
|
2016-09-24 22:37:25 +02:00
|
|
|
} catch (IOException | TelegramApiValidationException e) {
|
2016-04-17 17:10:09 +02:00
|
|
|
callback.onException(method, e);
|
|
|
|
}
|
|
|
|
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-01-14 23:09:19 +01:00
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
@Override
|
|
|
|
protected final <T extends Serializable, Method extends BotApiMethod<T>> T sendApiMethod(Method method) throws TelegramApiException {
|
2016-09-24 22:37:25 +02:00
|
|
|
method.validate();
|
2016-01-14 23:09:19 +01:00
|
|
|
String responseContent;
|
2016-05-11 15:24:40 +02:00
|
|
|
try {
|
2016-11-17 02:41:12 +01:00
|
|
|
String url = getBaseUrl() + method.getMethod();
|
2016-01-14 23:09:19 +01:00
|
|
|
HttpPost httppost = new HttpPost(url);
|
2016-05-11 15:24:40 +02:00
|
|
|
httppost.setConfig(requestConfig);
|
2016-04-12 19:53:21 +02:00
|
|
|
httppost.addHeader("charset", StandardCharsets.UTF_8.name());
|
2016-11-17 02:41:12 +01:00
|
|
|
httppost.setEntity(new StringEntity(objectMapper.writeValueAsString(method), ContentType.APPLICATION_JSON));
|
2016-05-09 02:44:26 +02:00
|
|
|
try (CloseableHttpResponse response = httpclient.execute(httppost)) {
|
|
|
|
HttpEntity ht = response.getEntity();
|
|
|
|
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
|
|
|
|
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
|
|
|
|
}
|
2016-01-14 23:09:19 +01:00
|
|
|
} catch (IOException e) {
|
2016-11-17 02:41:12 +01:00
|
|
|
throw new TelegramApiException("Unable to execute " + method.getMethod() + " method", e);
|
2016-01-14 23:09:19 +01:00
|
|
|
}
|
|
|
|
|
2016-11-17 02:41:12 +01:00
|
|
|
return method.deserializeResponse(responseContent);
|
2016-01-14 23:09:19 +01:00
|
|
|
}
|
|
|
|
|
2017-05-09 01:53:40 +02:00
|
|
|
protected String getBaseUrl() {
|
2017-05-11 03:02:54 +02:00
|
|
|
return options.getBaseUrl() + getBotToken() + "/";
|
2016-01-14 23:09:19 +01:00
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|