2016-01-14 23:09:19 +01:00
|
|
|
package org.telegram.telegrambots.bots;
|
2016-01-14 01:14:53 +01:00
|
|
|
|
|
|
|
import org.apache.http.HttpEntity;
|
|
|
|
import org.apache.http.NameValuePair;
|
|
|
|
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.impl.client.HttpClients;
|
|
|
|
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-01-14 23:09:19 +01:00
|
|
|
import org.telegram.telegrambots.TelegramApiException;
|
2016-04-11 02:53:53 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.AnswerCallbackQuery;
|
2016-02-16 00:24:32 +01:00
|
|
|
import org.telegram.telegrambots.api.methods.AnswerInlineQuery;
|
|
|
|
import org.telegram.telegrambots.api.methods.BotApiMethod;
|
|
|
|
import org.telegram.telegrambots.api.methods.ForwardMessage;
|
2016-04-02 18:20:49 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.GetFile;
|
2016-02-16 00:24:32 +01:00
|
|
|
import org.telegram.telegrambots.api.methods.GetMe;
|
|
|
|
import org.telegram.telegrambots.api.methods.GetUserProfilePhotos;
|
2016-04-11 02:53:53 +02:00
|
|
|
import org.telegram.telegrambots.api.methods.groupadministration.KickChatMember;
|
|
|
|
import org.telegram.telegrambots.api.methods.groupadministration.UnbanChatMember;
|
|
|
|
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;
|
|
|
|
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;
|
|
|
|
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-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;
|
|
|
|
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-04-12 19:53:21 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
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-09 02:56:49 +02:00
|
|
|
import static org.telegram.telegrambots.Constants.ERRORCODEFIELD;
|
|
|
|
import static org.telegram.telegrambots.Constants.ERRORDESCRIPTIONFIELD;
|
|
|
|
|
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 {
|
|
|
|
private final ExecutorService exe = Executors.newSingleThreadExecutor();
|
|
|
|
|
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-04-11 02:53:53 +02:00
|
|
|
// Send Requests
|
|
|
|
|
2016-01-14 23:09:19 +01:00
|
|
|
public Message sendMessage(SendMessage sendMessage) throws TelegramApiException {
|
|
|
|
if (sendMessage == null) {
|
|
|
|
throw new TelegramApiException("Parameter sendMessage can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (Message) sendApiMethod(sendMessage);
|
|
|
|
}
|
|
|
|
|
2016-04-11 02:53:53 +02:00
|
|
|
public 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");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (Boolean) sendApiMethod(answerInlineQuery);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean sendChatAction(SendChatAction sendChatAction) throws TelegramApiException {
|
|
|
|
if (sendChatAction == null) {
|
|
|
|
throw new TelegramApiException("Parameter sendChatAction can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (Boolean) sendApiMethod(sendChatAction);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Message forwardMessage(ForwardMessage forwardMessage) throws TelegramApiException {
|
|
|
|
if (forwardMessage == null) {
|
|
|
|
throw new TelegramApiException("Parameter forwardMessage can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (Message) sendApiMethod(forwardMessage);
|
|
|
|
}
|
|
|
|
|
2016-04-07 14:36:13 +02:00
|
|
|
public 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-04-07 14:36:13 +02:00
|
|
|
return (Message) sendApiMethod(sendLocation);
|
2016-01-14 23:09:19 +01:00
|
|
|
}
|
|
|
|
|
2016-04-11 02:53:53 +02:00
|
|
|
public Message sendVenue(SendVenue sendVenue) throws TelegramApiException {
|
|
|
|
if (sendVenue == null) {
|
|
|
|
throw new TelegramApiException("Parameter sendVenue can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (Message) sendApiMethod(sendVenue);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Message sendContact(SendContact sendContact) throws TelegramApiException {
|
|
|
|
if (sendContact == null) {
|
|
|
|
throw new TelegramApiException("Parameter sendContact can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (Message) sendApiMethod(sendContact);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean kickMember(KickChatMember kickChatMember) throws TelegramApiException {
|
|
|
|
if (kickChatMember == null) {
|
|
|
|
throw new TelegramApiException("Parameter kickChatMember can not be null");
|
|
|
|
}
|
|
|
|
return (Boolean) sendApiMethod(kickChatMember);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean unbanMember(UnbanChatMember unbanChatMember) throws TelegramApiException {
|
|
|
|
if (unbanChatMember == null) {
|
|
|
|
throw new TelegramApiException("Parameter unbanChatMember can not be null");
|
|
|
|
}
|
|
|
|
return (Boolean) sendApiMethod(unbanChatMember);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Message editMessageText(EditMessageText editMessageText) throws TelegramApiException {
|
|
|
|
if (editMessageText == null) {
|
|
|
|
throw new TelegramApiException("Parameter editMessageText can not be null");
|
|
|
|
}
|
|
|
|
return (Message) sendApiMethod(editMessageText);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Message editMessageCaption(EditMessageCaption editMessageCaption) throws TelegramApiException {
|
|
|
|
if (editMessageCaption == null) {
|
|
|
|
throw new TelegramApiException("Parameter editMessageCaption can not be null");
|
|
|
|
}
|
|
|
|
return (Message) sendApiMethod(editMessageCaption);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Message editMessageReplyMarkup(EditMessageReplyMarkup editMessageReplyMarkup) throws TelegramApiException {
|
|
|
|
if (editMessageReplyMarkup == null) {
|
|
|
|
throw new TelegramApiException("Parameter editMessageReplyMarkup can not be null");
|
|
|
|
}
|
|
|
|
return (Message) sendApiMethod(editMessageReplyMarkup);
|
|
|
|
}
|
|
|
|
|
|
|
|
public Boolean answerCallbackQuery(AnswerCallbackQuery answerCallbackQuery) throws TelegramApiException {
|
|
|
|
if (answerCallbackQuery == null) {
|
|
|
|
throw new TelegramApiException("Parameter answerCallbackQuery can not be null");
|
|
|
|
}
|
|
|
|
return (Boolean) sendApiMethod(answerCallbackQuery);
|
|
|
|
}
|
|
|
|
|
2016-01-14 23:09:19 +01:00
|
|
|
public UserProfilePhotos getUserProfilePhotos(GetUserProfilePhotos getUserProfilePhotos) throws TelegramApiException {
|
|
|
|
if (getUserProfilePhotos == null) {
|
|
|
|
throw new TelegramApiException("Parameter getUserProfilePhotos can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
return (UserProfilePhotos) sendApiMethod(getUserProfilePhotos);
|
|
|
|
}
|
2016-04-11 02:53:53 +02:00
|
|
|
|
2016-03-25 19:15:11 +01:00
|
|
|
public File getFile(GetFile getFile) throws TelegramApiException{
|
|
|
|
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-04-11 02:53:53 +02:00
|
|
|
return (File) sendApiMethod(getFile);
|
2016-03-25 19:15:11 +01:00
|
|
|
}
|
2016-01-14 23:09:19 +01:00
|
|
|
|
|
|
|
public User getMe() throws TelegramApiException {
|
|
|
|
GetMe getMe = new GetMe();
|
|
|
|
|
|
|
|
return (User) sendApiMethod(getMe);
|
|
|
|
}
|
|
|
|
|
2016-04-11 02:53:53 +02:00
|
|
|
// Send Requests Async
|
|
|
|
|
2016-01-14 23:09:19 +01:00
|
|
|
public void sendMessageAsync(SendMessage sendMessage, SentCallback<Message> sentCallback) throws TelegramApiException {
|
|
|
|
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-04-11 02:53:53 +02:00
|
|
|
public 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendChatActionAsync(SendChatAction sendChatAction, SentCallback<Boolean> sentCallback) throws TelegramApiException {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void forwardMessageAsync(ForwardMessage forwardMessage, SentCallback<Message> sentCallback) throws TelegramApiException {
|
|
|
|
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-05-09 02:27:49 +02:00
|
|
|
public 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-04-11 02:53:53 +02:00
|
|
|
public void sendVenueAsync(SendVenue sendVenue, SentCallback<Message> sentCallback) throws TelegramApiException {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendContactAsync(SendContact sendContact, SentCallback<Message> sentCallback) throws TelegramApiException {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void kickMemberAsync(KickChatMember kickChatMember, SentCallback<Boolean> sentCallback) throws TelegramApiException {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void unbanMemberAsync(UnbanChatMember unbanChatMember, SentCallback<Boolean> sentCallback) throws TelegramApiException {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void editMessageTextAsync(EditMessageText editMessageText, SentCallback<Message> sentCallback) throws TelegramApiException {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void editMessageCaptionAsync(EditMessageCaption editMessageCaption, SentCallback<Message> sentCallback) throws TelegramApiException {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void editMessageReplyMarkup(EditMessageReplyMarkup editMessageReplyMarkup, SentCallback<Message> sentCallback) throws TelegramApiException {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void answerCallbackQueryAsync(AnswerCallbackQuery answerCallbackQuery, SentCallback<Boolean> sentCallback) throws TelegramApiException {
|
|
|
|
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-01-14 23:09:19 +01:00
|
|
|
public void getUserProfilePhotosAsync(GetUserProfilePhotos getUserProfilePhotos, SentCallback<UserProfilePhotos> sentCallback) throws TelegramApiException {
|
|
|
|
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-04-11 02:53:53 +02:00
|
|
|
public void getFileAsync(GetFile getFile, SentCallback<File> sentCallback) throws TelegramApiException {
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
sendApiMethodAsync(getFile, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-01-14 23:09:19 +01:00
|
|
|
public void getMeAsync(SentCallback<User> sentCallback) throws TelegramApiException {
|
|
|
|
if (sentCallback == null) {
|
|
|
|
throw new TelegramApiException("Parameter sentCallback can not be null");
|
|
|
|
}
|
|
|
|
|
|
|
|
GetMe getMe = new GetMe();
|
|
|
|
sendApiMethodAsync(getMe, sentCallback);
|
|
|
|
}
|
|
|
|
|
2016-04-11 02:53:53 +02:00
|
|
|
// Specific Send Requests
|
|
|
|
|
2016-01-14 23:09:19 +01:00
|
|
|
public Message sendDocument(SendDocument sendDocument) throws TelegramApiException {
|
|
|
|
String responseContent;
|
2016-01-14 01:14:53 +01:00
|
|
|
|
2016-05-09 02:44:26 +02:00
|
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
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);
|
|
|
|
if (sendDocument.isNewDocument()) {
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendDocument.CHATID_FIELD, sendDocument.getChatId());
|
2016-01-14 23:09:19 +01:00
|
|
|
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
|
|
|
if (sendDocument.getReplayMarkup() != null) {
|
|
|
|
builder.addTextBody(SendDocument.REPLYMARKUP_FIELD, sendDocument.getReplayMarkup().toJson().toString());
|
|
|
|
}
|
|
|
|
if (sendDocument.getReplayToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendDocument.REPLYTOMESSAGEID_FIELD, sendDocument.getReplayToMessageId().toString());
|
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
if (sendDocument.getCaption() != null) {
|
|
|
|
builder.addTextBody(SendDocument.CAPTION_FIELD, sendDocument.getCaption());
|
|
|
|
}
|
|
|
|
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()));
|
|
|
|
if (sendDocument.getReplayMarkup() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendDocument.REPLYMARKUP_FIELD, sendDocument.getReplayMarkup().toString()));
|
|
|
|
}
|
|
|
|
if (sendDocument.getReplayToMessageId() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendDocument.REPLYTOMESSAGEID_FIELD, sendDocument.getReplayToMessageId().toString()));
|
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
if (sendDocument.getCaption() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendDocument.CAPTION_FIELD, sendDocument.getCaption()));
|
|
|
|
}
|
|
|
|
if (sendDocument.getReplayToMessageId() != null) {
|
|
|
|
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-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 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-05-09 02:56:49 +02:00
|
|
|
throw new TelegramApiException("Error at sendDocument", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD));
|
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-01-14 23:09:19 +01:00
|
|
|
public Message sendPhoto(SendPhoto sendPhoto) throws TelegramApiException {
|
|
|
|
String responseContent;
|
2016-05-09 02:44:26 +02:00
|
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
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);
|
|
|
|
if (sendPhoto.isNewPhoto()) {
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendPhoto.CHATID_FIELD, sendPhoto.getChatId());
|
2016-01-14 23:09:19 +01:00
|
|
|
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
|
|
|
if (sendPhoto.getReplayMarkup() != null) {
|
|
|
|
builder.addTextBody(SendPhoto.REPLYMARKUP_FIELD, sendPhoto.getReplayMarkup().toJson().toString());
|
|
|
|
}
|
|
|
|
if (sendPhoto.getReplayToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendPhoto.REPLYTOMESSAGEID_FIELD, sendPhoto.getReplayToMessageId().toString());
|
|
|
|
}
|
|
|
|
if (sendPhoto.getCaption() != null) {
|
|
|
|
builder.addTextBody(SendPhoto.CAPTION_FIELD, sendPhoto.getCaption());
|
|
|
|
}
|
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()));
|
|
|
|
if (sendPhoto.getReplayMarkup() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendPhoto.REPLYMARKUP_FIELD, sendPhoto.getReplayMarkup().toString()));
|
|
|
|
}
|
|
|
|
if (sendPhoto.getReplayToMessageId() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendPhoto.REPLYTOMESSAGEID_FIELD, sendPhoto.getReplayToMessageId().toString()));
|
|
|
|
}
|
|
|
|
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-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 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-05-09 02:56:49 +02:00
|
|
|
throw new TelegramApiException("Error at sendPhoto", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD));
|
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-01-14 23:09:19 +01:00
|
|
|
public Message sendVideo(SendVideo sendVideo) throws TelegramApiException {
|
|
|
|
String responseContent;
|
2016-05-09 02:44:26 +02:00
|
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
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);
|
|
|
|
if (sendVideo.isNewVideo()) {
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendVideo.CHATID_FIELD, sendVideo.getChatId());
|
2016-01-14 23:09:19 +01:00
|
|
|
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
|
|
|
if (sendVideo.getReplayMarkup() != null) {
|
|
|
|
builder.addTextBody(SendVideo.REPLYMARKUP_FIELD, sendVideo.getReplayMarkup().toJson().toString());
|
|
|
|
}
|
|
|
|
if (sendVideo.getReplayToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendVideo.REPLYTOMESSAGEID_FIELD, sendVideo.getReplayToMessageId().toString());
|
|
|
|
}
|
|
|
|
if (sendVideo.getCaption() != null) {
|
|
|
|
builder.addTextBody(SendVideo.CAPTION_FIELD, sendVideo.getCaption());
|
|
|
|
}
|
|
|
|
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()));
|
|
|
|
if (sendVideo.getReplayMarkup() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVideo.REPLYMARKUP_FIELD, sendVideo.getReplayMarkup().toString()));
|
|
|
|
}
|
|
|
|
if (sendVideo.getReplayToMessageId() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVideo.REPLYTOMESSAGEID_FIELD, sendVideo.getReplayToMessageId().toString()));
|
|
|
|
}
|
|
|
|
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-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 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-05-09 02:56:49 +02:00
|
|
|
throw new TelegramApiException("Error at sendVideo", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD));
|
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-01-14 23:09:19 +01:00
|
|
|
public Message sendSticker(SendSticker sendSticker) throws TelegramApiException {
|
|
|
|
String responseContent;
|
|
|
|
|
2016-05-09 02:44:26 +02:00
|
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
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);
|
|
|
|
if (sendSticker.isNewSticker()) {
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendSticker.CHATID_FIELD, sendSticker.getChatId());
|
2016-01-14 23:09:19 +01:00
|
|
|
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
|
|
|
if (sendSticker.getReplayMarkup() != null) {
|
|
|
|
builder.addTextBody(SendSticker.REPLYMARKUP_FIELD, sendSticker.getReplayMarkup().toJson().toString());
|
|
|
|
}
|
|
|
|
if (sendSticker.getReplayToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendSticker.REPLYTOMESSAGEID_FIELD, sendSticker.getReplayToMessageId().toString());
|
|
|
|
}
|
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()));
|
|
|
|
if (sendSticker.getReplayMarkup() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendSticker.REPLYMARKUP_FIELD, sendSticker.getReplayMarkup().toString()));
|
|
|
|
}
|
|
|
|
if (sendSticker.getReplayToMessageId() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendSticker.REPLYTOMESSAGEID_FIELD, sendSticker.getReplayToMessageId().toString()));
|
|
|
|
}
|
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-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 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-05-09 02:56:49 +02:00
|
|
|
throw new TelegramApiException("Error at sendSticker", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD));
|
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
|
|
|
|
*/
|
|
|
|
public Message sendAudio(SendAudio sendAudio) throws TelegramApiException {
|
|
|
|
String responseContent;
|
|
|
|
|
2016-05-09 02:44:26 +02:00
|
|
|
|
|
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
2016-01-24 03:20:54 +01:00
|
|
|
String url = getBaseUrl() + SendAudio.PATH;
|
|
|
|
HttpPost httppost = new HttpPost(url);
|
|
|
|
|
|
|
|
if (sendAudio.isNewAudio()) {
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendAudio.CHATID_FIELD, sendAudio.getChatId());
|
2016-02-16 00:24:32 +01:00
|
|
|
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
|
|
|
if (sendAudio.getReplayMarkup() != null) {
|
|
|
|
builder.addTextBody(SendAudio.REPLYMARKUP_FIELD, sendAudio.getReplayMarkup().toJson().toString());
|
|
|
|
}
|
|
|
|
if (sendAudio.getReplayToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendAudio.REPLYTOMESSAGEID_FIELD, sendAudio.getReplayToMessageId().toString());
|
|
|
|
}
|
|
|
|
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){
|
|
|
|
builder.addTextBody(SendAudio.DURATION_FIELD, sendAudio.getDuration().toString());
|
|
|
|
}
|
2016-02-27 03:17:06 +01:00
|
|
|
if (sendAudio.getDisableNotification() != null) {
|
|
|
|
builder.addTextBody(SendAudio.DISABLENOTIFICATION_FIELD, sendAudio.getDisableNotification().toString());
|
|
|
|
}
|
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()));
|
|
|
|
if (sendAudio.getReplayMarkup() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendAudio.REPLYMARKUP_FIELD, sendAudio.getReplayMarkup().toString()));
|
|
|
|
}
|
|
|
|
if (sendAudio.getReplayToMessageId() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendAudio.REPLYTOMESSAGEID_FIELD, sendAudio.getReplayToMessageId().toString()));
|
|
|
|
}
|
|
|
|
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-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-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-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-05-09 02:56:49 +02:00
|
|
|
throw new TelegramApiException("Error at sendAudio", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD));
|
2016-03-20 12:47:22 +01:00
|
|
|
}
|
|
|
|
|
2016-03-21 15:43:24 +01: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)
|
|
|
|
* @param sendVoice Information to send
|
|
|
|
* @return If success, the sent Message is returned
|
|
|
|
* @throws TelegramApiException If there is any error sending the audio
|
|
|
|
*/
|
|
|
|
public Message sendVoice(SendVoice sendVoice) throws TelegramApiException {
|
|
|
|
String responseContent;
|
|
|
|
|
2016-05-09 02:44:26 +02:00
|
|
|
try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
|
2016-02-27 03:17:06 +01:00
|
|
|
String url = getBaseUrl() + SendVoice.PATH;
|
|
|
|
HttpPost httppost = new HttpPost(url);
|
|
|
|
|
|
|
|
if (sendVoice.isNewVoice()) {
|
|
|
|
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
|
|
|
|
builder.addTextBody(SendVoice.CHATID_FIELD, sendVoice.getChatId());
|
|
|
|
builder.addBinaryBody(SendVoice.AUDIO_FIELD, new java.io.File(sendVoice.getAudio()), ContentType.create("audio/ogg"), sendVoice.getVoiceName());
|
|
|
|
if (sendVoice.getReplayMarkup() != null) {
|
|
|
|
builder.addTextBody(SendVoice.REPLYMARKUP_FIELD, sendVoice.getReplayMarkup().toJson().toString());
|
|
|
|
}
|
|
|
|
if (sendVoice.getReplayToMessageId() != null) {
|
|
|
|
builder.addTextBody(SendVoice.REPLYTOMESSAGEID_FIELD, sendVoice.getReplayToMessageId().toString());
|
|
|
|
}
|
|
|
|
if (sendVoice.getDisableNotification() != null) {
|
|
|
|
builder.addTextBody(SendVoice.DISABLENOTIFICATION_FIELD, sendVoice.getDisableNotification().toString());
|
|
|
|
}
|
|
|
|
if (sendVoice.getDuration() != null) {
|
|
|
|
builder.addTextBody(SendVoice.DURATION_FIELD, sendVoice.getDuration().toString());
|
|
|
|
}
|
|
|
|
HttpEntity multipart = builder.build();
|
|
|
|
httppost.setEntity(multipart);
|
|
|
|
} else {
|
|
|
|
List<NameValuePair> nameValuePairs = new ArrayList<>();
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVoice.CHATID_FIELD, sendVoice.getChatId()));
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVoice.AUDIO_FIELD, sendVoice.getAudio()));
|
|
|
|
if (sendVoice.getReplayMarkup() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVoice.REPLYMARKUP_FIELD, sendVoice.getReplayMarkup().toString()));
|
|
|
|
}
|
|
|
|
if (sendVoice.getReplayToMessageId() != null) {
|
|
|
|
nameValuePairs.add(new BasicNameValuePair(SendVoice.REPLYTOMESSAGEID_FIELD, sendVoice.getReplayToMessageId().toString()));
|
|
|
|
}
|
|
|
|
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-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-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-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-05-09 02:56:49 +02:00
|
|
|
throw new TelegramApiException("Error at sendVoice", jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD));
|
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-09 02:44:26 +02:00
|
|
|
try (CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build()) {
|
2016-04-17 17:10:09 +02:00
|
|
|
String url = getBaseUrl() + method.getPath();
|
|
|
|
HttpPost httppost = new HttpPost(url);
|
|
|
|
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
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
callback.onException(method, e);
|
|
|
|
}
|
|
|
|
|
2016-01-14 01:14:53 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2016-01-14 23:09:19 +01:00
|
|
|
|
|
|
|
private Serializable sendApiMethod(BotApiMethod method) throws TelegramApiException {
|
|
|
|
String responseContent;
|
2016-05-09 02:44:26 +02:00
|
|
|
try (CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build()) {
|
2016-01-14 23:09:19 +01:00
|
|
|
String url = getBaseUrl() + method.getPath();
|
|
|
|
HttpPost httppost = new HttpPost(url);
|
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-05-09 02:56:49 +02:00
|
|
|
throw new TelegramApiException("Error at " + method.getPath(), jsonObject.getString(ERRORDESCRIPTIONFIELD), jsonObject.getInt(ERRORCODEFIELD));
|
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
|
|
|
}
|