This commit is contained in:
Rubenlagu 2016-04-12 19:53:21 +02:00 committed by Rubenlagus
parent 0b73abad50
commit b9d997fddc
20 changed files with 98 additions and 85 deletions

View File

@ -1,4 +1,4 @@
package org.telegram.telegrambots.api;
package org.telegram.telegrambots;
/**
* @author Ruben Bermudez
@ -8,4 +8,6 @@ package org.telegram.telegrambots.api;
*/
public class Constants {
public static final String BASEURL = "https://api.telegram.org/bot";
public static final String RESPONSEFIELDOK = "ok";
public static final String RESPONSEFIELDRESULT = "result";
}

View File

@ -12,7 +12,6 @@ import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;
import org.telegram.telegrambots.api.Constants;
import org.telegram.telegrambots.api.methods.updates.SetWebhook;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.bots.TelegramWebhookBot;
@ -21,6 +20,7 @@ import org.telegram.telegrambots.updatesreceivers.Webhook;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
/**
* @author Ruben Bermudez
@ -29,21 +29,16 @@ import java.io.IOException;
* @date 14 of January of 2016
*/
public class TelegramBotsApi {
private final boolean useWebhook; ///<
private final Webhook webhook; ///<
private final String extrenalUrl; ///<
private final String pathToCertificate; ///<
private final String publicCertificateName; ///<
private boolean useWebhook; ///<
private Webhook webhook; ///<
private String extrenalUrl; ///<
private String pathToCertificate; ///<
private String publicCertificateName; ///<
/**
*
*/
public TelegramBotsApi() {
this.useWebhook = false;
webhook = null;
extrenalUrl = null;
this.pathToCertificate = null;
this.publicCertificateName = null;
}
/**
@ -56,8 +51,6 @@ public class TelegramBotsApi {
public TelegramBotsApi(String keyStore, String keyStorePassword, String externalUrl, String internalUrl) throws TelegramApiException {
this.useWebhook = true;
this.extrenalUrl = fixExternalUrl(externalUrl);
this.pathToCertificate = null;
this.publicCertificateName = null;
webhook = new Webhook(keyStore, keyStorePassword, internalUrl);
webhook.startServer();
}
@ -86,7 +79,7 @@ public class TelegramBotsApi {
* @return
*/
private static String fixExternalUrl(String externalUrl) {
if (!externalUrl.endsWith("/")) {
if (externalUrl != null && !externalUrl.endsWith("/")) {
externalUrl = externalUrl + "/";
}
return externalUrl;
@ -116,9 +109,9 @@ public class TelegramBotsApi {
CloseableHttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
String responseContent = EntityUtils.toString(buf, "UTF-8");
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean("ok")) {
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
throw new TelegramApiException(webHookURL == null ? "Error removing old webhook" : "Error setting webhook", responseContent);
}
} catch (JSONException e) {

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import java.io.IOException;
@ -52,8 +53,8 @@ public class AnswerCallbackQuery extends BotApiMethod<Boolean> {
@Override
public Boolean deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return answer.getBoolean("result");
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return answer.getBoolean(Constants.RESPONSEFIELDRESULT);
}
return null;
}

View File

@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONArray;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.objects.inlinequery.result.InlineQueryResult;
import java.io.IOException;
@ -129,8 +130,8 @@ public class AnswerInlineQuery extends BotApiMethod<Boolean> {
@Override
public Boolean deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return answer.getBoolean("result");
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return answer.getBoolean(Constants.RESPONSEFIELDRESULT);
}
return null;
}

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.objects.Message;
import java.io.IOException;
@ -19,12 +20,12 @@ public class ForwardMessage extends BotApiMethod<Message> {
public static final String PATH = "forwardmessage";
public static final String CHATID_FIELD = "chat_id";
private String chatId; ///< Unique identifier for the chat to send the message to (or username for channels)
public static final String FROMCHATID_FIELD = "from_chat_id";
private Integer fromChatId; ///< Unique identifier for the chat where the original message was sent User or GroupChat id
public static final String MESSAGEID_FIELD = "message_id";
private Integer messageId; ///< Unique message identifier
public static final String DISABLENOTIFICATION_FIELD = "disable_notification";
private String chatId; ///< Unique identifier for the chat to send the message to (or username for channels)
private Integer fromChatId; ///< Unique identifier for the chat where the original message was sent User or GroupChat id
private Integer messageId; ///< Unique message identifier
/**
* Optional. Sends the message silently.
* iOS users will not receive a notification,
@ -111,8 +112,8 @@ public class ForwardMessage extends BotApiMethod<Message> {
@Override
public Message deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return new Message(answer.getJSONObject("result"));
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return new Message(answer.getJSONObject(Constants.RESPONSEFIELDRESULT));
}
return null;
}

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.objects.File;
import java.io.IOException;
@ -67,8 +68,8 @@ public class GetFile extends BotApiMethod<File> {
@Override
public File deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return new File(answer.getJSONObject("result"));
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return new File(answer.getJSONObject(Constants.RESPONSEFIELDRESULT));
}
return null;
}

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.objects.User;
import java.io.IOException;
@ -31,8 +32,8 @@ public class GetMe extends BotApiMethod<User> {
@Override
public User deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return new User(answer.getJSONObject("result"));
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return new User(answer.getJSONObject(Constants.RESPONSEFIELDRESULT));
}
return null;
}

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.objects.UserProfilePhotos;
import java.io.IOException;
@ -77,8 +78,8 @@ public class GetUserProfilePhotos extends BotApiMethod<UserProfilePhotos> {
@Override
public UserProfilePhotos deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return new UserProfilePhotos(answer.getJSONObject("result"));
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return new UserProfilePhotos(answer.getJSONObject(Constants.RESPONSEFIELDRESULT));
}
return null;
}

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.methods.BotApiMethod;
import java.io.IOException;
@ -64,8 +65,8 @@ public class KickChatMember extends BotApiMethod<Boolean> {
@Override
public Boolean deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return answer.getBoolean("result");
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return answer.getBoolean(Constants.RESPONSEFIELDRESULT);
}
return null;
}

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.methods.BotApiMethod;
import java.io.IOException;
@ -60,8 +61,8 @@ public class UnbanChatMember extends BotApiMethod<Boolean> {
@Override
public Boolean deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return answer.getBoolean("result");
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return answer.getBoolean(Constants.RESPONSEFIELDRESULT);
}
return null;
}

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.methods.BotApiMethod;
import java.io.IOException;
@ -55,8 +56,8 @@ public class SendChatAction extends BotApiMethod<Boolean> {
@Override
public Boolean deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return answer.getBoolean("result");
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return answer.getBoolean(Constants.RESPONSEFIELDRESULT);
}
return null;
}

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.methods.BotApiMethod;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard;
@ -107,8 +108,8 @@ public class SendContact extends BotApiMethod<Message> {
@Override
public Message deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return new Message(answer.getJSONObject("result"));
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return new Message(answer.getJSONObject(Constants.RESPONSEFIELDRESULT));
}
return null;
}

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.methods.BotApiMethod;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard;
@ -96,8 +97,8 @@ public class SendLocation extends BotApiMethod<Message> {
@Override
public Message deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return new Message(answer.getJSONObject("result"));
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return new Message(answer.getJSONObject(Constants.RESPONSEFIELDRESULT));
}
return null;
}

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.methods.BotApiMethod;
import org.telegram.telegrambots.api.methods.ParseMode;
import org.telegram.telegrambots.api.objects.Message;
@ -147,8 +148,8 @@ public class SendMessage extends BotApiMethod<Message> {
@Override
public Message deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return new Message(answer.getJSONObject("result"));
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return new Message(answer.getJSONObject(Constants.RESPONSEFIELDRESULT));
}
return null;
}

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.methods.BotApiMethod;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.replykeyboard.ReplyKeyboard;
@ -127,8 +128,8 @@ public class SendVenue extends BotApiMethod<Message> {
@Override
public Message deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return new Message(answer.getJSONObject("result"));
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return new Message(answer.getJSONObject(Constants.RESPONSEFIELDRESULT));
}
return null;
}

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.methods.BotApiMethod;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup;
@ -112,8 +113,8 @@ public class EditMessageCaption extends BotApiMethod<Message> {
@Override
public Message deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return new Message(answer.getJSONObject("result"));
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return new Message(answer.getJSONObject(Constants.RESPONSEFIELDRESULT));
}
return null;
}

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.methods.BotApiMethod;
import org.telegram.telegrambots.api.objects.Message;
import org.telegram.telegrambots.api.objects.replykeyboard.InlineKeyboardMarkup;
@ -99,8 +100,8 @@ public class EditMessageReplyMarkup extends BotApiMethod<Message> {
@Override
public Message deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return new Message(answer.getJSONObject("result"));
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return new Message(answer.getJSONObject(Constants.RESPONSEFIELDRESULT));
}
return null;
}

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.methods.BotApiMethod;
import org.telegram.telegrambots.api.methods.ParseMode;
import org.telegram.telegrambots.api.objects.Message;
@ -152,8 +153,8 @@ public class EditMessageText extends BotApiMethod<Message> {
@Override
public Message deserializeResponse(JSONObject answer) {
if (answer.getBoolean("ok")) {
return new Message(answer.getJSONObject("result"));
if (answer.getBoolean(Constants.RESPONSEFIELDOK)) {
return new Message(answer.getJSONObject(Constants.RESPONSEFIELDRESULT));
}
return null;
}

View File

@ -16,8 +16,8 @@ import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.TelegramApiException;
import org.telegram.telegrambots.api.Constants;
import org.telegram.telegrambots.api.methods.AnswerCallbackQuery;
import org.telegram.telegrambots.api.methods.AnswerInlineQuery;
import org.telegram.telegrambots.api.methods.BotApiMethod;
@ -49,6 +49,7 @@ import org.telegram.telegrambots.updateshandlers.SentCallback;
import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
@ -418,19 +419,19 @@ public abstract class AbsSender {
if (sendDocument.getReplayToMessageId() != null) {
nameValuePairs.add(new BasicNameValuePair(SendDocument.DISABLENOTIFICATION_FIELD, sendDocument.getDisableNotification().toString()));
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
}
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, "UTF-8");
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new TelegramApiException("Unable to send document", e);
}
JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean("ok")) {
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
throw new TelegramApiException("Error at sendDocument", jsonObject.getString("description"));
}
@ -477,19 +478,19 @@ public abstract class AbsSender {
if (sendPhoto.getDisableNotification() != null) {
nameValuePairs.add(new BasicNameValuePair(SendPhoto.DISABLENOTIFICATION_FIELD, sendPhoto.getDisableNotification().toString()));
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
}
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, "UTF-8");
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new TelegramApiException("Unable to send photo", e);
}
JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean("ok")) {
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
throw new TelegramApiException("Error at sendPhoto", jsonObject.getString("description"));
}
@ -554,19 +555,19 @@ public abstract class AbsSender {
if (sendVideo.getDisableNotification() != null) {
nameValuePairs.add(new BasicNameValuePair(SendVideo.DISABLENOTIFICATION_FIELD, sendVideo.getDisableNotification().toString()));
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
}
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, "UTF-8");
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new TelegramApiException("Unable to send video", e);
}
JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean("ok")) {
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
throw new TelegramApiException("Error at sendVideo", jsonObject.getString("description"));
}
@ -608,19 +609,19 @@ public abstract class AbsSender {
if (sendSticker.getDisableNotification() != null) {
nameValuePairs.add(new BasicNameValuePair(SendSticker.DISABLENOTIFICATION_FIELD, sendSticker.getDisableNotification().toString()));
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
}
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, "UTF-8");
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new TelegramApiException("Unable to send sticker", e);
}
JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean("ok")) {
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
throw new TelegramApiException("Error at sendSticker", jsonObject.getString("description"));
}
@ -685,13 +686,13 @@ public abstract class AbsSender {
if (sendAudio.getDisableNotification() != null) {
nameValuePairs.add(new BasicNameValuePair(SendAudio.DISABLENOTIFICATION_FIELD, sendAudio.getDisableNotification().toString()));
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
}
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, "UTF-8");
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new TelegramApiException("Unable to send sticker", e);
}
@ -702,12 +703,12 @@ public abstract class AbsSender {
*
* {"description":"[Error]: Bad Request: chat not found","error_code":400,"ok":false}
*/
if(!jsonObject.getBoolean("ok")){
throw new TelegramApiException("Error at sendAudio", jsonObject.getString("description"));
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
throw new TelegramApiException("Error at sendAudio", jsonObject.getString("description"));
}
// and if not, we can expect a "result" section. and out of this can a new Message object be built
return new Message(jsonObject.getJSONObject("result"));
return new Message(jsonObject.getJSONObject(Constants.RESPONSEFIELDRESULT));
}
/**
@ -758,19 +759,19 @@ public abstract class AbsSender {
if (sendVoice.getDuration() != null) {
nameValuePairs.add(new BasicNameValuePair(SendVoice.DURATION_FIELD, sendVoice.getDuration().toString()));
}
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, StandardCharsets.UTF_8));
}
CloseableHttpResponse response = httpClient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, "UTF-8");
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new TelegramApiException("Unable to send sticker", e);
}
JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean("ok")) {
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
throw new TelegramApiException("Error at sendVoice", jsonObject.getString("description"));
}
@ -785,15 +786,15 @@ public abstract class AbsSender {
CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
String url = getBaseUrl() + method.getPath();
HttpPost httppost = new HttpPost(url);
httppost.addHeader("charset", "UTF-8");
httppost.addHeader("charset", StandardCharsets.UTF_8.name());
httppost.setEntity(new StringEntity(method.toJson().toString(), ContentType.APPLICATION_JSON));
CloseableHttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
String responseContent = EntityUtils.toString(buf, "UTF-8");
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean("ok")) {
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
callback.onError(method, jsonObject);
}
callback.onResult(method, jsonObject);
@ -809,18 +810,18 @@ public abstract class AbsSender {
CloseableHttpClient httpclient = HttpClientBuilder.create().setSSLHostnameVerifier(new NoopHostnameVerifier()).build();
String url = getBaseUrl() + method.getPath();
HttpPost httppost = new HttpPost(url);
httppost.addHeader("charset", "UTF-8");
httppost.addHeader("charset", StandardCharsets.UTF_8.name());
httppost.setEntity(new StringEntity(method.toJson().toString(), ContentType.APPLICATION_JSON));
CloseableHttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
responseContent = EntityUtils.toString(buf, "UTF-8");
responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new TelegramApiException("Unable to execute " + method.getPath() + " method", e);
}
JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean("ok")) {
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
throw new TelegramApiException("Error at " + method.getPath(), jsonObject.getString("description"));
}

View File

@ -14,13 +14,14 @@ import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.telegram.telegrambots.api.Constants;
import org.telegram.telegrambots.Constants;
import org.telegram.telegrambots.api.methods.updates.GetUpdates;
import org.telegram.telegrambots.api.objects.Update;
import org.telegram.telegrambots.bots.ITelegramLongPollingBot;
import java.io.IOException;
import java.io.InvalidObjectException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.TimeUnit;
@ -37,8 +38,8 @@ public class UpdatesThread {
private final ReaderThread readerThread;
private final HandlerThread handlerThread;
private final ConcurrentLinkedDeque<Update> receivedUpdates = new ConcurrentLinkedDeque<>();
private final String token;
private int lastReceivedUpdate = 0;
private String token;
public UpdatesThread(String token, ITelegramLongPollingBot callback) {
this.token = token;
@ -69,7 +70,7 @@ public class UpdatesThread {
//http client
HttpPost httpPost = new HttpPost(url);
try {
httpPost.addHeader("charset", "UTF-8");
httpPost.addHeader("charset", StandardCharsets.UTF_8.name());
httpPost.setConfig(requestConfig);
httpPost.setEntity(new StringEntity(request.toJson().toString(), ContentType.APPLICATION_JSON));
HttpResponse response;
@ -77,14 +78,14 @@ public class UpdatesThread {
HttpEntity ht = response.getEntity();
BufferedHttpEntity buf = new BufferedHttpEntity(ht);
String responseContent = EntityUtils.toString(buf, "UTF-8");
String responseContent = EntityUtils.toString(buf, StandardCharsets.UTF_8);
try {
JSONObject jsonObject = new JSONObject(responseContent);
if (!jsonObject.getBoolean("ok")) {
if (!jsonObject.getBoolean(Constants.RESPONSEFIELDOK)) {
throw new InvalidObjectException(jsonObject.toString());
}
JSONArray jsonArray = jsonObject.getJSONArray("result");
JSONArray jsonArray = jsonObject.getJSONArray(Constants.RESPONSEFIELDRESULT);
if (jsonArray.length() != 0) {
for (int i = 0; i < jsonArray.length(); i++) {
Update update = new Update(jsonArray.getJSONObject(i));