2016-01-14 23:09:19 +01:00
|
|
|
package org.telegram.telegrambots.bots;
|
2016-01-14 01:14:53 +01:00
|
|
|
|
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-07-21 21:28:13 +02:00
|
|
|
import org.apache.http.HttpHost;
|
2016-01-14 01:14:53 +01:00
|
|
|
import org.apache.http.NameValuePair;
|
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.entity.UrlEncodedFormEntity;
|
|
|
|
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.message.BasicNameValuePair;
|
|
|
|
import org.apache.http.util.EntityUtils;
|
|
|
|
import org.json.JSONObject;
|
2016-04-12 19:53:21 +02:00
|
|
|
import org.telegram.telegrambots.Constants;
|
2016-05-22 11:42:04 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.AnswerCallbackQuery;
|
|
|
|
import org.telegram.telegrambots.api.methods.AnswerInlineQuery;
|
|
|
|
import org.telegram.telegrambots.api.methods.BotApiMethod;
|
|
|
|
import org.telegram.telegrambots.api.methods.ForwardMessage;
|
|
|
|
import org.telegram.telegrambots.api.methods.GetFile;
|
|
|
|
import org.telegram.telegrambots.api.methods.GetMe;
|
|
|
|
import org.telegram.telegrambots.api.methods.GetUserProfilePhotos;
|
2016-09-25 15:33:20 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.games.GetGameHighScores;
|
|
|
|
import org.telegram.telegrambots.api.methods.games.SetGameScore;
|
2016-05-22 11:42:04 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.groupadministration.GetChat;
|
|
|
|
import org.telegram.telegrambots.api.methods.groupadministration.GetChatAdministrators;
|
|
|
|
import org.telegram.telegrambots.api.methods.groupadministration.GetChatMember;
|
|
|
|
import org.telegram.telegrambots.api.methods.groupadministration.GetChatMemberCount;
|
2016-04-11 02:53:53 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.groupadministration.KickChatMember;
|
2016-05-22 11:42:04 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.groupadministration.LeaveChat;
|
2016-04-11 02:53:53 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.groupadministration.UnbanChatMember;
|
2016-05-22 11:42:04 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.send.SendAudio;
|
|
|
|
import org.telegram.telegrambots.api.methods.send.SendChatAction;
|
|
|
|
import org.telegram.telegrambots.api.methods.send.SendContact;
|
|
|
|
import org.telegram.telegrambots.api.methods.send.SendDocument;
|
2016-09-28 00:29:44 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.send.SendGame;
|
2016-05-22 11:42:04 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.send.SendLocation;
|
|
|
|
import org.telegram.telegrambots.api.methods.send.SendMessage;
|
|
|
|
import org.telegram.telegrambots.api.methods.send.SendPhoto;
|
|
|
|
import org.telegram.telegrambots.api.methods.send.SendSticker;
|
|
|
|
import org.telegram.telegrambots.api.methods.send.SendVenue;
|
|
|
|
import org.telegram.telegrambots.api.methods.send.SendVideo;
|
|
|
|
import org.telegram.telegrambots.api.methods.send.SendVoice;
|
2016-08-12 21:23:30 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.updates.GetWebhookInfo;
|
2016-04-11 02:53:53 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.updatingmessages.EditMessageCaption;
|
|
|
|
import org.telegram.telegrambots.api.methods.updatingmessages.EditMessageReplyMarkup;
|
|
|
|
import org.telegram.telegrambots.api.methods.updatingmessages.EditMessageText;
|
2016-05-22 11:42:04 +02:00
|
|
|
import org.telegram.telegrambots.api.objects.Chat;
|
|
|
|
import org.telegram.telegrambots.api.objects.ChatMember;
|
2016-01-14 23:09:19 +01:00
|
|
|
import org.telegram.telegrambots.api.objects.File;
|
|
|
|
import org.telegram.telegrambots.api.objects.Message;
|
|
|
|
import org.telegram.telegrambots.api.objects.User;
|
|
|
|
import org.telegram.telegrambots.api.objects.UserProfilePhotos;
|
2016-08-12 21:23:30 +02:00
|
|
|
import org.telegram.telegrambots.api.objects.WebhookInfo;
|
2016-09-25 15:33:20 +02:00
|
|
|
import org.telegram.telegrambots.api.objects.games.GameHighScore;
|
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-09-24 22:37:25 +02:00
|
|
|
import java.text.MessageFormat;
|
2016-01-14 01:14:53 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @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-01-14 23:09:19 +01:00
|
|
|
public abstract class 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-01-14 23:09:19 +01:00
|
|
|
private final ExecutorService exe = Executors.newSingleThreadExecutor();
|
2016-07-21 21:28:13 +02:00
|
|
|
private final BotOptions options;
|
2016-05-11 15:24:40 +02:00
|
|
|
private volatile CloseableHttpClient httpclient;
|
|
|
|
private volatile RequestConfig requestConfig;
|
|
|
|
private static final int SOCKET_TIMEOUT = 75 * 1000;
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
AbsSender(BotOptions options) {
|
|
|
|
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
|
|
|
|
|
|
|
RequestConfig.Builder configBuilder = RequestConfig.copy(RequestConfig.custom().build());
|
|
|
|
if (options.hasProxy()) {
|
|
|
|
configBuilder.setProxy(new HttpHost(options.getProxyHost(), options.getProxyPort()));
|
|
|
|
}
|
|
|
|
|
|
|
|
requestConfig = configBuilder.setSocketTimeout(SOCKET_TIMEOUT)
|
2016-05-11 15:24:40 +02:00
|
|
|
.setConnectTimeout(SOCKET_TIMEOUT)
|
|
|
|
.setConnectionRequestTimeout(SOCKET_TIMEOUT).build();
|
|
|
|
}
|
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-07-21 21:28:13 +02:00
|
|
|
public final BotOptions getOptions() {
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
2016-04-11 02:53:53 +02:00
|
|
|
// Send Requests
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message sendMessage(SendMessage sendMessage) throws TelegramApiException {
|
2016-01-14 23:09:19 +01:00
|
|
|
if (sendMessage == null) {
|
|
|
|
throw new TelegramApiException("Parameter sendMessage can not be null");
|
|
|
|
}
|
|
|
|
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(sendMessage);
|
2016-01-14 23:09:19 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Boolean answerInlineQuery(AnswerInlineQuery answerInlineQuery) throws TelegramApiException {
|
2016-01-14 23:09:19 +01:00
|
|
|
if (answerInlineQuery == null) {
|
|
|
|
throw new TelegramApiException("Parameter answerInlineQuery can not be null");
|
|
|
|
}
|
|
|
|
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(answerInlineQuery);
|
2016-01-14 23:09:19 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Boolean sendChatAction(SendChatAction sendChatAction) throws TelegramApiException {
|
2016-01-14 23:09:19 +01:00
|
|
|
if (sendChatAction == null) {
|
|
|
|
throw new TelegramApiException("Parameter sendChatAction can not be null");
|
|
|
|
}
|
|
|
|
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(sendChatAction);
|
2016-01-14 23:09:19 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message forwardMessage(ForwardMessage forwardMessage) throws TelegramApiException {
|
2016-01-14 23:09:19 +01:00
|
|
|
if (forwardMessage == null) {
|
|
|
|
throw new TelegramApiException("Parameter forwardMessage can not be null");
|
|
|
|
}
|
|
|
|
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(forwardMessage);
|
2016-01-14 23:09:19 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message sendLocation(SendLocation sendLocation) throws TelegramApiException {
|
2016-01-14 23:09:19 +01:00
|
|
|
if (sendLocation == null) {
|
|
|
|
throw new TelegramApiException("Parameter sendLocation can not be null");
|
|
|
|
}
|
|
|
|
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(sendLocation);
|
2016-01-14 23:09:19 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message sendVenue(SendVenue sendVenue) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (sendVenue == null) {
|
|
|
|
throw new TelegramApiException("Parameter sendVenue can not be null");
|
|
|
|
}
|
|
|
|
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(sendVenue);
|
2016-04-11 02:53:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message sendContact(SendContact sendContact) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (sendContact == null) {
|
|
|
|
throw new TelegramApiException("Parameter sendContact can not be null");
|
|
|
|
}
|
|
|
|
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(sendContact);
|
2016-04-11 02:53:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Boolean kickMember(KickChatMember kickChatMember) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (kickChatMember == null) {
|
|
|
|
throw new TelegramApiException("Parameter kickChatMember can not be null");
|
|
|
|
}
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(kickChatMember);
|
2016-04-11 02:53:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Boolean unbanMember(UnbanChatMember unbanChatMember) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (unbanChatMember == null) {
|
|
|
|
throw new TelegramApiException("Parameter unbanChatMember can not be null");
|
|
|
|
}
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(unbanChatMember);
|
2016-04-11 02:53:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Boolean leaveChat(LeaveChat leaveChat) throws TelegramApiException {
|
2016-05-22 11:42:04 +02:00
|
|
|
if (leaveChat == null) {
|
|
|
|
throw new TelegramApiException("Parameter leaveChat can not be null");
|
|
|
|
}
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(leaveChat);
|
2016-05-22 11:42:04 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Chat getChat(GetChat getChat) throws TelegramApiException {
|
2016-05-22 11:42:04 +02:00
|
|
|
if (getChat == null) {
|
|
|
|
throw new TelegramApiException("Parameter getChat can not be null");
|
|
|
|
}
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(getChat);
|
2016-05-22 11:42:04 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final List<ChatMember> getChatAdministrators(GetChatAdministrators getChatAdministrators) throws TelegramApiException {
|
2016-05-22 11:42:04 +02:00
|
|
|
if (getChatAdministrators == null) {
|
|
|
|
throw new TelegramApiException("Parameter getChatAdministrators can not be null");
|
|
|
|
}
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(getChatAdministrators);
|
2016-05-22 11:42:04 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final ChatMember getChatMember(GetChatMember getChatMember) throws TelegramApiException {
|
2016-05-22 11:42:04 +02:00
|
|
|
if (getChatMember == null) {
|
|
|
|
throw new TelegramApiException("Parameter getChatMember can not be null");
|
|
|
|
}
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(getChatMember);
|
2016-05-22 11:42:04 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Integer getChatMemberCount(GetChatMemberCount getChatMemberCount) throws TelegramApiException {
|
2016-05-22 11:42:04 +02:00
|
|
|
if (getChatMemberCount == null) {
|
|
|
|
throw new TelegramApiException("Parameter getChatMemberCount can not be null");
|
|
|
|
}
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(getChatMemberCount);
|
2016-05-22 11:42:04 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message editMessageText(EditMessageText editMessageText) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (editMessageText == null) {
|
|
|
|
throw new TelegramApiException("Parameter editMessageText can not be null");
|
|
|
|
}
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(editMessageText);
|
2016-04-11 02:53:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message editMessageCaption(EditMessageCaption editMessageCaption) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (editMessageCaption == null) {
|
|
|
|
throw new TelegramApiException("Parameter editMessageCaption can not be null");
|
|
|
|
}
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(editMessageCaption);
|
2016-04-11 02:53:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message editMessageReplyMarkup(EditMessageReplyMarkup editMessageReplyMarkup) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (editMessageReplyMarkup == null) {
|
|
|
|
throw new TelegramApiException("Parameter editMessageReplyMarkup can not be null");
|
|
|
|
}
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(editMessageReplyMarkup);
|
2016-04-11 02:53:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Boolean answerCallbackQuery(AnswerCallbackQuery answerCallbackQuery) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (answerCallbackQuery == null) {
|
|
|
|
throw new TelegramApiException("Parameter answerCallbackQuery can not be null");
|
|
|
|
}
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(answerCallbackQuery);
|
2016-04-11 02:53:53 +02:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final UserProfilePhotos getUserProfilePhotos(GetUserProfilePhotos getUserProfilePhotos) throws TelegramApiException {
|
2016-01-14 23:09:19 +01:00
|
|
|
if (getUserProfilePhotos == null) {
|
|
|
|
throw new TelegramApiException("Parameter getUserProfilePhotos can not be null");
|
|
|
|
}
|
|
|
|
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(getUserProfilePhotos);
|
2016-01-14 23:09:19 +01:00
|
|
|
}
|
2016-04-11 02:53:53 +02:00
|
|
|
|
2016-09-24 22:37:25 +02:00
|
|
|
public final File getFile(GetFile getFile) throws TelegramApiException {
|
2016-05-11 15:24:40 +02:00
|
|
|
if(getFile == null){
|
|
|
|
throw new TelegramApiException("Parameter getFile can not be null");
|
|
|
|
}
|
|
|
|
else if(getFile.getFileId() == null){
|
|
|
|
throw new TelegramApiException("Attribute file_id in parameter getFile can not be null");
|
|
|
|
}
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(getFile);
|
2016-03-25 19:15:11 +01:00
|
|
|
}
|
2016-01-14 23:09:19 +01:00
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final User getMe() throws TelegramApiException {
|
2016-01-14 23:09:19 +01:00
|
|
|
GetMe getMe = new GetMe();
|
|
|
|
|
2016-05-27 04:43:13 +02:00
|
|
|
return sendApiMethod(getMe);
|
2016-01-14 23:09:19 +01:00
|
|
|
}
|
|
|
|
|
2016-08-12 21:23:30 +02:00
|
|
|
public final WebhookInfo getWebhookInfo() throws TelegramApiException {
|
|
|
|
GetWebhookInfo getWebhookInfo = new GetWebhookInfo();
|
|
|
|
return sendApiMethod(getWebhookInfo);
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
String url = MessageFormat.format(File.FILEBASEURL, getBotToken(), file.getFilePath());
|
|
|
|
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-09-25 15:33:20 +02:00
|
|
|
public final Serializable setGameScore(SetGameScore setGameScore) throws TelegramApiException {
|
|
|
|
if(setGameScore == null){
|
|
|
|
throw new TelegramApiException("Parameter setGameScore can not be null");
|
|
|
|
}
|
|
|
|
return sendApiMethod(setGameScore);
|
|
|
|
}
|
|
|
|
|
|
|
|
public final Serializable getGameHighScores(GetGameHighScores getGameHighScores) throws TelegramApiException {
|
|
|
|
if(getGameHighScores == null){
|
|
|
|
throw new TelegramApiException("Parameter getGameHighScores can not be null");
|
|
|
|
}
|
|
|
|
return sendApiMethod(getGameHighScores);
|
|
|
|
}
|
|
|
|
|
2016-09-28 00:29:44 +02:00
|
|
|
public final Message sendGame(SendGame sendGame) throws TelegramApiException {
|
|
|
|
if(sendGame == null){
|
|
|
|
throw new TelegramApiException("Parameter sendGame can not be null");
|
|
|
|
}
|
|
|
|
return sendApiMethod(sendGame);
|
|
|
|
}
|
|
|
|
|
2016-04-11 02:53:53 +02:00
|
|
|
// Send Requests Async
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void sendMessageAsync(SendMessage sendMessage, SentCallback<Message> sentCallback) throws TelegramApiException {
|
2016-01-14 23:09:19 +01:00
|
|
|
if (sendMessage == null) {
|
|
|
|
throw new TelegramApiException("Parameter sendMessage can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(sendMessage, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void answerInlineQueryAsync(AnswerInlineQuery answerInlineQuery, SentCallback<Boolean> sentCallback) throws TelegramApiException {
|
2016-01-14 23:09:19 +01:00
|
|
|
if (answerInlineQuery == null) {
|
|
|
|
throw new TelegramApiException("Parameter answerInlineQuery can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(answerInlineQuery, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void sendChatActionAsync(SendChatAction sendChatAction, SentCallback<Boolean> sentCallback) throws TelegramApiException {
|
2016-01-14 23:09:19 +01:00
|
|
|
if (sendChatAction == null) {
|
|
|
|
throw new TelegramApiException("Parameter sendChatAction can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(sendChatAction, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void forwardMessageAsync(ForwardMessage forwardMessage, SentCallback<Message> sentCallback) throws TelegramApiException {
|
2016-01-14 23:09:19 +01:00
|
|
|
if (forwardMessage == null) {
|
|
|
|
throw new TelegramApiException("Parameter forwardMessage can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(forwardMessage, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void sendLocationAsync(SendLocation sendLocation, SentCallback<Message> sentCallback) throws TelegramApiException {
|
2016-01-14 23:09:19 +01:00
|
|
|
if (sendLocation == null) {
|
|
|
|
throw new TelegramApiException("Parameter sendLocation can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(sendLocation, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void sendVenueAsync(SendVenue sendVenue, SentCallback<Message> sentCallback) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (sendVenue == null) {
|
|
|
|
throw new TelegramApiException("Parameter sendVenue can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(sendVenue, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void sendContactAsync(SendContact sendContact, SentCallback<Message> sentCallback) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (sendContact == null) {
|
|
|
|
throw new TelegramApiException("Parameter sendContact can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(sendContact, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void kickMemberAsync(KickChatMember kickChatMember, SentCallback<Boolean> sentCallback) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (kickChatMember == null) {
|
|
|
|
throw new TelegramApiException("Parameter kickChatMember can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(kickChatMember, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void unbanMemberAsync(UnbanChatMember unbanChatMember, SentCallback<Boolean> sentCallback) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (unbanChatMember == null) {
|
|
|
|
throw new TelegramApiException("Parameter unbanChatMember can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(unbanChatMember, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void leaveChatAsync(LeaveChat leaveChat, SentCallback<Boolean> sentCallback) throws TelegramApiException {
|
2016-05-22 11:42:04 +02:00
|
|
|
if (leaveChat == null) {
|
|
|
|
throw new TelegramApiException("Parameter leaveChat can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
sendApiMethodAsync(leaveChat, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void getChatAsync(GetChat getChat, SentCallback<Chat> sentCallback) throws TelegramApiException {
|
2016-05-22 11:42:04 +02:00
|
|
|
if (getChat == null) {
|
|
|
|
throw new TelegramApiException("Parameter getChat can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
sendApiMethodAsync(getChat, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void getChatAdministratorsAsync(GetChatAdministrators getChatAdministrators, SentCallback<ArrayList<ChatMember>> sentCallback) throws TelegramApiException {
|
2016-05-22 11:42:04 +02:00
|
|
|
if (getChatAdministrators == null) {
|
|
|
|
throw new TelegramApiException("Parameter getChatAdministrators can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
sendApiMethodAsync(getChatAdministrators, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void getChatMemberAsync(GetChatMember getChatMember, SentCallback<ChatMember> sentCallback) throws TelegramApiException {
|
2016-05-22 11:42:04 +02:00
|
|
|
if (getChatMember == null) {
|
|
|
|
throw new TelegramApiException("Parameter getChatMember can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
sendApiMethodAsync(getChatMember, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void getChatMemberCountAsync(GetChatMemberCount getChatMemberCount, SentCallback<Integer> sentCallback) throws TelegramApiException {
|
2016-05-22 11:42:04 +02:00
|
|
|
if (getChatMemberCount == null) {
|
|
|
|
throw new TelegramApiException("Parameter getChatMemberCount can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(getChatMemberCount, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void editMessageTextAsync(EditMessageText editMessageText, SentCallback<Message> sentCallback) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (editMessageText == null) {
|
|
|
|
throw new TelegramApiException("Parameter editMessageText can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(editMessageText, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void editMessageCaptionAsync(EditMessageCaption editMessageCaption, SentCallback<Message> sentCallback) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (editMessageCaption == null) {
|
|
|
|
throw new TelegramApiException("Parameter editMessageCaption can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(editMessageCaption, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void editMessageReplyMarkup(EditMessageReplyMarkup editMessageReplyMarkup, SentCallback<Message> sentCallback) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (editMessageReplyMarkup == null) {
|
|
|
|
throw new TelegramApiException("Parameter editMessageReplyMarkup can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(editMessageReplyMarkup, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void answerCallbackQueryAsync(AnswerCallbackQuery answerCallbackQuery, SentCallback<Boolean> sentCallback) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (answerCallbackQuery == null) {
|
|
|
|
throw new TelegramApiException("Parameter answerCallbackQuery can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(answerCallbackQuery, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void getUserProfilePhotosAsync(GetUserProfilePhotos getUserProfilePhotos, SentCallback<UserProfilePhotos> sentCallback) throws TelegramApiException {
|
2016-01-14 23:09:19 +01:00
|
|
|
if (getUserProfilePhotos == null) {
|
|
|
|
throw new TelegramApiException("Parameter getUserProfilePhotos can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(getUserProfilePhotos, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void getFileAsync(GetFile getFile, SentCallback<File> sentCallback) throws TelegramApiException {
|
2016-04-11 02:53:53 +02:00
|
|
|
if (getFile == null) {
|
|
|
|
throw new TelegramApiException("Parameter getFile can not be null");
|
2016-09-24 22:37:25 +02:00
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
2016-04-11 02:53:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(getFile, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final void getMeAsync(SentCallback<User> sentCallback) throws TelegramApiException {
|
2016-01-14 23:09:19 +01:00
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
GetMe getMe = new GetMe();
|
|
|
|
sendApiMethodAsync(getMe, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-08-12 21:23:30 +02:00
|
|
|
public final void getWebhookInfoAsync(SentCallback<WebhookInfo> sentCallback) throws TelegramApiException {
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
GetWebhookInfo getWebhookInfo = new GetWebhookInfo();
|
|
|
|
sendApiMethodAsync(getWebhookInfo, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-09-25 15:33:20 +02:00
|
|
|
public final void setGameScoreAsync(SetGameScore setGameScore, SentCallback<Serializable> sentCallback) throws TelegramApiException {
|
|
|
|
if (setGameScore == null) {
|
|
|
|
throw new TelegramApiException("Parameter setGameScore can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
sendApiMethodAsync(setGameScore, sentCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
public final void getGameHighScoresAsync(GetGameHighScores getGameHighScores, SentCallback<ArrayList<GameHighScore>> sentCallback) throws TelegramApiException {
|
|
|
|
if (getGameHighScores == null) {
|
|
|
|
throw new TelegramApiException("Parameter getGameHighScores can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
sendApiMethodAsync(getGameHighScores, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-09-28 00:29:44 +02:00
|
|
|
public final void sendGameAsync(SendGame sendGame, SentCallback<Message> sentCallback) throws TelegramApiException {
|
|
|
|
if (sendGame == null) {
|
|
|
|
throw new TelegramApiException("Parameter sendGame can not be null");
|
|
|
|
}
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
sendApiMethodAsync(sendGame, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-09-24 22:37:25 +02:00
|
|
|
public final void downloadFileAsync(File file, DownloadFileCallback callback) throws TelegramApiException {
|
|
|
|
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() {
|
|
|
|
String url = MessageFormat.format(File.FILEBASEURL, getBotToken(), file.getFilePath());
|
|
|
|
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-07-21 21:28:13 +02:00
|
|
|
public final Message sendDocument(SendDocument sendDocument) throws TelegramApiException {
|
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);
|
2016-01-14 01:14:53 +01:00
|
|
|
if (sendDocument.isNewDocument()) {
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendDocument.CHATID_FIELD, sendDocument.getChatId());
|
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-06-20 20:50:03 +02:00
|
|
|
if (sendDocument.getReplyMarkup() != null) {
|
|
|
|
builder.addTextBody(SendDocument.REPLYMARKUP_FIELD, sendDocument.getReplyMarkup().toJson().toString(), TEXT_PLAIN_CONTENT_TYPE);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendDocument.getReplyToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendDocument.REPLYTOMESSAGEID_FIELD, sendDocument.getReplyToMessageId().toString());
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
if (sendDocument.getCaption() != null) {
|
2016-06-09 22:31:14 +02:00
|
|
|
builder.addTextBody(SendDocument.CAPTION_FIELD, sendDocument.getCaption(), TEXT_PLAIN_CONTENT_TYPE);
|
2016-02-27 03:17:06 +01:00
|
|
|
}
|
|
|
|
if (sendDocument.getDisableNotification() != null) {
|
|
|
|
builder.addTextBody(SendDocument.DISABLENOTIFICATION_FIELD, sendDocument.getDisableNotification().toString());
|
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httppost.setEntity(multipart);
|
|
|
|
} else {
|
|
|
|
List<NameValuePair> nameValuePairs = new ArrayList<>();
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendDocument.CHATID_FIELD, sendDocument.getChatId()));
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendDocument.DOCUMENT_FIELD, sendDocument.getDocument()));
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendDocument.getReplyMarkup() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendDocument.REPLYMARKUP_FIELD, sendDocument.getReplyMarkup().toJson().toString()));
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendDocument.getReplyToMessageId() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendDocument.REPLYTOMESSAGEID_FIELD, sendDocument.getReplyToMessageId().toString()));
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
if (sendDocument.getCaption() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendDocument.CAPTION_FIELD, sendDocument.getCaption()));
|
|
|
|
}
|
2016-08-07 14:04:00 +02:00
|
|
|
if (sendDocument.getDisableNotification() != null) {
|
2016-02-27 03:17:06 +01:00
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendDocument.DISABLENOTIFICATION_FIELD, sendDocument.getDisableNotification().toString()));
|
|
|
|
}
|
2016-04-12 19:53:21 +02:00
|
|
|
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
|
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
|
|
|
|
|
|
|
JSONObject jsonObject = new JSONObject(responseContent);
|
2016-04-12 19:53:21 +02:00
|
|
|
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
2016-09-29 00:44:03 +02:00
|
|
|
throw new TelegramApiRequestException("Error at sendDocument", jsonObject);
|
2016-01-14 23:09:19 +01:00
|
|
|
}
|
|
|
|
|
2016-05-02 08:32:01 +02:00
|
|
|
return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT));
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException {
|
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);
|
2016-01-14 01:14:53 +01:00
|
|
|
if (sendPhoto.isNewPhoto()) {
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendPhoto.CHATID_FIELD, sendPhoto.getChatId());
|
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-06-20 20:50:03 +02:00
|
|
|
if (sendPhoto.getReplyMarkup() != null) {
|
|
|
|
builder.addTextBody(SendPhoto.REPLYMARKUP_FIELD, sendPhoto.getReplyMarkup().toJson().toString(), TEXT_PLAIN_CONTENT_TYPE);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendPhoto.getReplyToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendPhoto.REPLYTOMESSAGEID_FIELD, sendPhoto.getReplyToMessageId().toString());
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
if (sendPhoto.getCaption() != null) {
|
2016-06-09 22:31:14 +02:00
|
|
|
builder.addTextBody(SendPhoto.CAPTION_FIELD, sendPhoto.getCaption(), TEXT_PLAIN_CONTENT_TYPE);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
if (sendPhoto.getDisableNotification() != null) {
|
|
|
|
builder.addTextBody(SendPhoto.DISABLENOTIFICATION_FIELD, sendPhoto.getDisableNotification().toString());
|
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httppost.setEntity(multipart);
|
|
|
|
} else {
|
|
|
|
List<NameValuePair> nameValuePairs = new ArrayList<>();
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendPhoto.CHATID_FIELD, sendPhoto.getChatId()));
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendPhoto.PHOTO_FIELD, sendPhoto.getPhoto()));
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendPhoto.getReplyMarkup() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendPhoto.REPLYMARKUP_FIELD, sendPhoto.getReplyMarkup().toJson().toString()));
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendPhoto.getReplyToMessageId() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendPhoto.REPLYTOMESSAGEID_FIELD, sendPhoto.getReplyToMessageId().toString()));
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
if (sendPhoto.getCaption() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendPhoto.CAPTION_FIELD, sendPhoto.getCaption()));
|
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
if (sendPhoto.getDisableNotification() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendPhoto.DISABLENOTIFICATION_FIELD, sendPhoto.getDisableNotification().toString()));
|
|
|
|
}
|
2016-04-12 19:53:21 +02:00
|
|
|
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
|
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
|
|
|
|
|
|
|
JSONObject jsonObject = new JSONObject(responseContent);
|
2016-04-12 19:53:21 +02:00
|
|
|
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
2016-09-29 00:44:03 +02:00
|
|
|
throw new TelegramApiRequestException("Error at sendPhoto", jsonObject);
|
2016-01-14 23:09:19 +01:00
|
|
|
}
|
|
|
|
|
2016-05-02 08:32:01 +02:00
|
|
|
return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT));
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message sendVideo(SendVideo sendVideo) throws TelegramApiException {
|
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);
|
2016-01-14 01:14:53 +01:00
|
|
|
if (sendVideo.isNewVideo()) {
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendVideo.CHATID_FIELD, sendVideo.getChatId());
|
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-06-20 20:50:03 +02:00
|
|
|
if (sendVideo.getReplyMarkup() != null) {
|
|
|
|
builder.addTextBody(SendVideo.REPLYMARKUP_FIELD, sendVideo.getReplyMarkup().toJson().toString(), TEXT_PLAIN_CONTENT_TYPE);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendVideo.getReplyToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendVideo.REPLYTOMESSAGEID_FIELD, sendVideo.getReplyToMessageId().toString());
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
if (sendVideo.getCaption() != null) {
|
2016-06-09 22:31:14 +02:00
|
|
|
builder.addTextBody(SendVideo.CAPTION_FIELD, sendVideo.getCaption(), TEXT_PLAIN_CONTENT_TYPE);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
if (sendVideo.getDuration() != null) {
|
|
|
|
builder.addTextBody(SendVideo.DURATION_FIELD, sendVideo.getDuration().toString());
|
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
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());
|
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httppost.setEntity(multipart);
|
|
|
|
} else {
|
|
|
|
List<NameValuePair> nameValuePairs = new ArrayList<>();
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVideo.CHATID_FIELD, sendVideo.getChatId()));
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVideo.VIDEO_FIELD, sendVideo.getVideo()));
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendVideo.getReplyMarkup() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVideo.REPLYMARKUP_FIELD, sendVideo.getReplyMarkup().toJson().toString()));
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendVideo.getReplyToMessageId() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVideo.REPLYTOMESSAGEID_FIELD, sendVideo.getReplyToMessageId().toString()));
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
if (sendVideo.getCaption() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVideo.CAPTION_FIELD, sendVideo.getCaption()));
|
|
|
|
}
|
|
|
|
if (sendVideo.getDuration() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVideo.DURATION_FIELD, sendVideo.getDuration().toString()));
|
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
if (sendVideo.getWidth() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVideo.WIDTH_FIELD, sendVideo.getWidth().toString()));
|
|
|
|
}
|
|
|
|
if (sendVideo.getHeight() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVideo.HEIGHT_FIELD, sendVideo.getHeight().toString()));
|
|
|
|
}
|
|
|
|
if (sendVideo.getDisableNotification() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVideo.DISABLENOTIFICATION_FIELD, sendVideo.getDisableNotification().toString()));
|
|
|
|
}
|
2016-04-12 19:53:21 +02:00
|
|
|
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject jsonObject = new JSONObject(responseContent);
|
2016-04-12 19:53:21 +02:00
|
|
|
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
2016-09-29 00:44:03 +02:00
|
|
|
throw new TelegramApiRequestException("Error at sendVideo", jsonObject);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-01-14 23:09:19 +01:00
|
|
|
|
2016-05-02 08:32:01 +02:00
|
|
|
return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT));
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
|
2016-07-21 21:28:13 +02:00
|
|
|
public final Message sendSticker(SendSticker sendSticker) throws TelegramApiException {
|
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() + 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);
|
2016-01-14 01:14:53 +01:00
|
|
|
if (sendSticker.isNewSticker()) {
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendSticker.CHATID_FIELD, sendSticker.getChatId());
|
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-06-20 20:50:03 +02:00
|
|
|
if (sendSticker.getReplyMarkup() != null) {
|
|
|
|
builder.addTextBody(SendSticker.REPLYMARKUP_FIELD, sendSticker.getReplyMarkup().toJson().toString(), TEXT_PLAIN_CONTENT_TYPE);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendSticker.getReplyToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendSticker.REPLYTOMESSAGEID_FIELD, sendSticker.getReplyToMessageId().toString());
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
if (sendSticker.getDisableNotification() != null) {
|
|
|
|
builder.addTextBody(SendSticker.DISABLENOTIFICATION_FIELD, sendSticker.getDisableNotification().toString());
|
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httppost.setEntity(multipart);
|
|
|
|
} else {
|
|
|
|
List<NameValuePair> nameValuePairs = new ArrayList<>();
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendSticker.CHATID_FIELD, sendSticker.getChatId()));
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendSticker.STICKER_FIELD, sendSticker.getSticker()));
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendSticker.getReplyMarkup() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendSticker.REPLYMARKUP_FIELD, sendSticker.getReplyMarkup().toJson().toString()));
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendSticker.getReplyToMessageId() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendSticker.REPLYTOMESSAGEID_FIELD, sendSticker.getReplyToMessageId().toString()));
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
if (sendSticker.getDisableNotification() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendSticker.DISABLENOTIFICATION_FIELD, sendSticker.getDisableNotification().toString()));
|
|
|
|
}
|
2016-04-12 19:53:21 +02:00
|
|
|
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject jsonObject = new JSONObject(responseContent);
|
2016-04-12 19:53:21 +02:00
|
|
|
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
2016-09-29 00:44:03 +02:00
|
|
|
throw new TelegramApiRequestException("Error at sendSticker", jsonObject);
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
|
2016-05-02 08:32:01 +02:00
|
|
|
return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT));
|
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-07-21 21:28:13 +02:00
|
|
|
public final Message sendAudio(SendAudio sendAudio) throws TelegramApiException {
|
2016-01-24 03:20:54 +01:00
|
|
|
String responseContent;
|
|
|
|
|
2016-05-09 02:44:26 +02:00
|
|
|
|
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);
|
2016-01-24 03:20:54 +01:00
|
|
|
if (sendAudio.isNewAudio()) {
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendAudio.CHATID_FIELD, sendAudio.getChatId());
|
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-06-20 20:50:03 +02:00
|
|
|
if (sendAudio.getReplyMarkup() != null) {
|
|
|
|
builder.addTextBody(SendAudio.REPLYMARKUP_FIELD, sendAudio.getReplyMarkup().toJson().toString(), TEXT_PLAIN_CONTENT_TYPE);
|
2016-01-24 03:20:54 +01:00
|
|
|
}
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendAudio.getReplyToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendAudio.REPLYTOMESSAGEID_FIELD, sendAudio.getReplyToMessageId().toString());
|
2016-01-24 03:20:54 +01:00
|
|
|
}
|
|
|
|
if (sendAudio.getPerformer() != null) {
|
|
|
|
builder.addTextBody(SendAudio.PERFOMER_FIELD, sendAudio.getPerformer());
|
|
|
|
}
|
|
|
|
if (sendAudio.getTitle() != null) {
|
|
|
|
builder.addTextBody(SendAudio.TITLE_FIELD, sendAudio.getTitle());
|
|
|
|
}
|
2016-03-15 22:58:43 +01:00
|
|
|
if(sendAudio.getDuration() != null){
|
2016-05-11 15:24:40 +02:00
|
|
|
builder.addTextBody(SendAudio.DURATION_FIELD, sendAudio.getDuration().toString());
|
2016-03-15 22:58:43 +01:00
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
if (sendAudio.getDisableNotification() != null) {
|
|
|
|
builder.addTextBody(SendAudio.DISABLENOTIFICATION_FIELD, sendAudio.getDisableNotification().toString());
|
|
|
|
}
|
2016-09-24 22:37:25 +02:00
|
|
|
if (sendAudio.getCaption() != null) {
|
|
|
|
builder.addTextBody(SendAudio.CAPTION_FIELD, sendAudio.getCaption());
|
|
|
|
}
|
2016-01-24 03:20:54 +01:00
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httppost.setEntity(multipart);
|
|
|
|
} else {
|
|
|
|
List<NameValuePair> nameValuePairs = new ArrayList<>();
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendAudio.CHATID_FIELD, sendAudio.getChatId()));
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendAudio.AUDIO_FIELD, sendAudio.getAudio()));
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendAudio.getReplyMarkup() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendAudio.REPLYMARKUP_FIELD, sendAudio.getReplyMarkup().toJson().toString()));
|
2016-01-24 03:20:54 +01:00
|
|
|
}
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendAudio.getReplyToMessageId() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendAudio.REPLYTOMESSAGEID_FIELD, sendAudio.getReplyToMessageId().toString()));
|
2016-01-24 03:20:54 +01:00
|
|
|
}
|
|
|
|
if (sendAudio.getPerformer() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendAudio.PERFOMER_FIELD, sendAudio.getPerformer()));
|
|
|
|
}
|
|
|
|
if (sendAudio.getTitle() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendAudio.TITLE_FIELD, sendAudio.getTitle()));
|
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
if (sendAudio.getDisableNotification() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendAudio.DISABLENOTIFICATION_FIELD, sendAudio.getDisableNotification().toString()));
|
|
|
|
}
|
2016-09-24 22:37:25 +02:00
|
|
|
if (sendAudio.getCaption() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendAudio.CAPTION_FIELD, sendAudio.getCaption()));
|
|
|
|
}
|
2016-04-12 19:53:21 +02:00
|
|
|
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject jsonObject = new JSONObject(responseContent);
|
|
|
|
|
2016-03-21 15:43:24 +01:00
|
|
|
/* if we got not an "ok" with false, we have a response like that
|
|
|
|
*
|
|
|
|
* {"description":"[Error]: Bad Request: chat not found","error_code":400,"ok":false}
|
|
|
|
*/
|
2016-04-12 19:53:21 +02:00
|
|
|
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
2016-09-29 00:44:03 +02:00
|
|
|
throw new TelegramApiRequestException("Error at sendAudio", jsonObject);
|
2016-03-20 12:47:22 +01:00
|
|
|
}
|
2016-05-11 15:24:40 +02:00
|
|
|
|
|
|
|
// and if not, we can expect a "result" section. and out of this can a new Message object be built
|
2016-04-12 19:53:21 +02:00
|
|
|
return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT));
|
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-07-21 21:28:13 +02:00
|
|
|
public final Message sendVoice(SendVoice sendVoice) throws TelegramApiException {
|
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);
|
2016-02-27 03:17:06 +01:00
|
|
|
if (sendVoice.isNewVoice()) {
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendVoice.CHATID_FIELD, sendVoice.getChatId());
|
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-06-20 20:50:03 +02:00
|
|
|
if (sendVoice.getReplyMarkup() != null) {
|
|
|
|
builder.addTextBody(SendVoice.REPLYMARKUP_FIELD, sendVoice.getReplyMarkup().toJson().toString(), TEXT_PLAIN_CONTENT_TYPE);
|
2016-02-27 03:17:06 +01:00
|
|
|
}
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendVoice.getReplyToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendVoice.REPLYTOMESSAGEID_FIELD, sendVoice.getReplyToMessageId().toString());
|
2016-02-27 03:17:06 +01: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());
|
|
|
|
}
|
2016-09-24 22:37:25 +02:00
|
|
|
if (sendVoice.getCaption() != null) {
|
|
|
|
builder.addTextBody(SendVoice.CAPTION_FIELD, sendVoice.getCaption());
|
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httppost.setEntity(multipart);
|
|
|
|
} else {
|
|
|
|
List<NameValuePair> nameValuePairs = new ArrayList<>();
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVoice.CHATID_FIELD, sendVoice.getChatId()));
|
2016-06-09 22:52:08 +02:00
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVoice.VOICE_FIELD, sendVoice.getVoice()));
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendVoice.getReplyMarkup() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVoice.REPLYMARKUP_FIELD, sendVoice.getReplyMarkup().toJson().toString()));
|
2016-02-27 03:17:06 +01:00
|
|
|
}
|
2016-06-20 20:50:03 +02:00
|
|
|
if (sendVoice.getReplyToMessageId() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVoice.REPLYTOMESSAGEID_FIELD, sendVoice.getReplyToMessageId().toString()));
|
2016-02-27 03:17:06 +01:00
|
|
|
}
|
|
|
|
if (sendVoice.getDisableNotification() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVoice.DISABLENOTIFICATION_FIELD, sendVoice.getDisableNotification().toString()));
|
|
|
|
}
|
|
|
|
if (sendVoice.getDuration() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVoice.DURATION_FIELD, sendVoice.getDuration().toString()));
|
|
|
|
}
|
2016-09-24 22:37:25 +02:00
|
|
|
if (sendVoice.getCaption() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVoice.CAPTION_FIELD, sendVoice.getCaption()));
|
|
|
|
}
|
2016-04-12 19:53:21 +02:00
|
|
|
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
|
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) {
|
|
|
|
throw new TelegramApiException("Unable to send sticker", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject jsonObject = new JSONObject(responseContent);
|
2016-04-12 19:53:21 +02:00
|
|
|
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
2016-09-29 00:44:03 +02:00
|
|
|
throw new TelegramApiRequestException("Error at sendVoice", jsonObject);
|
2016-02-27 03:17:06 +01:00
|
|
|
}
|
|
|
|
|
2016-05-02 08:32:01 +02:00
|
|
|
return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT));
|
2016-02-27 03:17:06 +01:00
|
|
|
}
|
|
|
|
|
2016-04-11 02:53:53 +02:00
|
|
|
// Simplified methods
|
|
|
|
|
2016-05-09 02:27:49 +02:00
|
|
|
private <T extends Serializable, Method extends BotApiMethod<T>, Callback extends SentCallback<T>> void sendApiMethodAsync(Method method, Callback callback) {
|
|
|
|
//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-04-17 17:10:09 +02:00
|
|
|
String url = getBaseUrl() + method.getPath();
|
|
|
|
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());
|
|
|
|
httppost.setEntity(new StringEntity(method.toJson().toString(), 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);
|
|
|
|
|
|
|
|
JSONObject jsonObject = new JSONObject(responseContent);
|
|
|
|
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
|
|
|
callback.onError(method, jsonObject);
|
|
|
|
}
|
|
|
|
callback.onResult(method, jsonObject);
|
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-05-27 04:43:13 +02:00
|
|
|
private <T extends Serializable> T sendApiMethod(BotApiMethod<T> 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-01-14 23:09:19 +01:00
|
|
|
String url = getBaseUrl() + method.getPath();
|
|
|
|
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-01-14 23:09:19 +01:00
|
|
|
httppost.setEntity(new StringEntity(method.toJson().toString(), 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) {
|
|
|
|
throw new TelegramApiException("Unable to execute " + method.getPath() + " method", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject jsonObject = new JSONObject(responseContent);
|
2016-04-12 19:53:21 +02:00
|
|
|
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
|
2016-09-29 00:44:03 +02:00
|
|
|
throw new TelegramApiRequestException("Error at " + method.getPath(), jsonObject);
|
2016-01-14 23:09:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return method.deserializeResponse(jsonObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getBaseUrl() {
|
|
|
|
return Constants.BASEURL + getBotToken() + "/";
|
|
|
|
}
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|