private static HttpResponse<String> requestString(String path, Map<String, Object> params, File file) throws UnirestException { MultipartBody req = prepareRequest(path, params, file); // Execute and return response as String HttpResponse<String> resp = req.asString(); // Record lastRequestPath = path; lastResponse = resp; lastResponseBodyClass = String.class; return resp; }
/** * Refreshes a token */ private void refreshToken() { Optional.ofNullable(token) .map(t -> t.refresh_token) .ifPresent(r -> { MultipartBody post = Unirest.post(SPOTIFY_API_TOKEN).header(HttpHeaders.AUTHORIZATION, "Basic " + getBase64Authorization()) .field("grant_type", "refresh_token") .field("refresh_token", r); String body = null; try { body = post.asString().getBody(); logger().info("Getting token response with body [{}]", body); Optional.ofNullable(gson.fromJson(body, SpotifyToken.class)).ifPresent(t -> { token.access_token = t.access_token; setData(DATA_SPOTIFY_TOKEN, token); }); } catch (UnirestException e) { logger().error("couldn't get token via refresh token", e); } }); }
/** * Gets a token after the user has authorized the application * * @param code the code returned by Spotify * @param url the redirect uri that we used * @return */ private Optional<SpotifyToken> getToken(String code, String url) { try { logger().info("Getting token from code [{}] and state [{}]", code, url); MultipartBody post = Unirest.post(SPOTIFY_API_TOKEN).header(HttpHeaders.AUTHORIZATION, "Basic " + getBase64Authorization()) .field("grant_type", "authorization_code") .field("code", code) .field("redirect_uri", url); String body = post.asString().getBody(); logger().info("Getting token response with body [{}]", body); return Optional.ofNullable(gson.fromJson(body, SpotifyToken.class)); } catch (UnirestException e) { logger().error("Couldn't get Spotify token", e); return Optional.empty(); } }
/** * Gets a List of ChatMember objects for all the people who are admin in the chat * * @return A List of ChatMember objects for all the people who are admin in the chat */ default List<ChatMember> getChatAdministrators() { try { MultipartBody request = Unirest.post(getBotInstance().getBotAPIUrl() + "getChatAdministrators") .field("chat_id", getId(), "application/json; charset=utf8;"); HttpResponse<String> response = request.asString(); JSONObject jsonResponse = processResponse(response); if (jsonResponse != null && Utils.checkResponseStatus(jsonResponse)) { JSONArray jsonArray = jsonResponse.getJSONArray("result"); List<ChatMember> chatAdmins = new ArrayList<>(); for(int i = 0; i < jsonArray.length(); ++i) { JSONObject jsonObject = jsonArray.getJSONObject(i); chatAdmins.add(createChatMember(jsonObject)); } return chatAdmins; } } catch (UnirestException e) { e.printStackTrace(); } return null; }
public boolean deleteMessage(String chatId, Long messageId) { if(chatId != null && messageId != null) { HttpResponse<String> response; JSONObject jsonResponse = null; try { MultipartBody requests = Unirest.post(getBotAPIUrl() + "deleteMessage") .field("chat_id", chatId, "application/json; charset=utf8;") .field("message_id", messageId); response = requests.asString(); jsonResponse = Utils.processResponse(response); } catch (UnirestException e) { e.printStackTrace(); } if(jsonResponse != null) { if(jsonResponse.getBoolean("result")) return true; } } return false; }
response = uploadRequest.asString(); } catch (UnirestException e) { logger.error("Exception while connecting to Microcks backend", e);
/** * Use this method to unban a previously kicked user in a supergroup. The user will not return to the group * automatically, but will be able to join via link, etc. The bot must be an administrator in the group for * this to work * * @param chatId The ID of the chat that you want to unban the user from * @param userId The ID of the user that you want to unban from the chat * * @return True if the user was unbanned, otherwise False */ public boolean unbanChatMember(String chatId, int userId) { HttpResponse<String> response; JSONObject jsonResponse; try { MultipartBody request = Unirest.post(getBotAPIUrl() + "unbanChatMember") .field("chat_id", chatId, "application/json; charset=utf8;") .field("user_id", userId); response = request.asString(); jsonResponse = Utils.processResponse(response); if(jsonResponse != null) { if(jsonResponse.getBoolean("result")) return true; } } catch (UnirestException e) { e.printStackTrace(); } return false; }
/** * Use this method to kick a user from a group or a supergroup. In the case of supergroups, the user will not be * able to return to the group on their own using invite links, etc., unless unbanned first. The bot must be * an administrator in the group for this to work * * @param chatId The ID of the chat that you want to kick the user from * @param userId The ID of the user that you want to kick from the chat * * @return True if the user was kicked successfully, otherwise False */ public boolean kickChatMember(String chatId, int userId) { HttpResponse<String> response; JSONObject jsonResponse; try { MultipartBody request = Unirest.post(getBotAPIUrl() + "kickChatMember") .field("chat_id", chatId, "application/json; charset=utf8;") .field("user_id", userId); response = request.asString(); jsonResponse = Utils.processResponse(response); if(jsonResponse != null) { if(jsonResponse.getBoolean("result")) return true; } } catch (UnirestException e) { e.printStackTrace(); } return false; }
/** * A method used to get a Chat object via the chats ID * * @param chatID The Chat ID of the chat you want a Chat object of * * @return A Chat object or null if the chat does not exist or you don't have permission to get this chat */ public Chat getChat(String chatID) { try { MultipartBody request = Unirest.post(getBotAPIUrl() + "getChat") .field("chat_id", chatID, "application/json; charset=utf8;"); HttpResponse<String> response = request.asString(); JSONObject jsonResponse = Utils.processResponse(response); if (jsonResponse != null && Utils.checkResponseStatus(jsonResponse)) { JSONObject result = jsonResponse.getJSONObject("result"); return ChatImpl.createChat(result, this); } } catch (UnirestException e) { e.printStackTrace(); } return null; }
private JSONObject editMessageReplyMarkup(String chatId, Long messageId, String inlineMessageId, InlineReplyMarkup inlineReplyMarkup) { HttpResponse<String> response; JSONObject jsonResponse = null; try { MultipartBody requests = Unirest.post(getBotAPIUrl() + "editMessageReplyMarkup") .field("reply_markup", GSON.toJson(inlineReplyMarkup, InlineKeyboardMarkup.class), "application/json; charset=utf8;"); if(chatId != null) requests.field("chat_id", chatId, "application/json; charset=utf8;"); if(messageId != null) requests.field("message_id", messageId); if(inlineMessageId != null) requests.field("inline_message_id", inlineMessageId, "application/json; charset=utf8;"); response = requests.asString(); jsonResponse = Utils.processResponse(response); } catch (UnirestException e) { e.printStackTrace(); } return jsonResponse; }
/** * Gets the amount of people in the chat * * @return The amount of people in the chat */ default Integer getChatMembersCount() { try { MultipartBody request = Unirest.post(getBotInstance().getBotAPIUrl() + "getChatMembersCount") .field("chat_id", getId(), "application/json; charset=utf8;"); HttpResponse<String> response = request.asString(); JSONObject jsonResponse = processResponse(response); if (jsonResponse != null && Utils.checkResponseStatus(jsonResponse)) { return jsonResponse.getInt("result"); } } catch (UnirestException e) { e.printStackTrace(); } return null; }
private JSONObject editMessageCaption(String chatId, Long messageId, String inlineMessageId, String caption, InlineReplyMarkup inlineReplyMarkup) { HttpResponse<String> response; JSONObject jsonResponse = null; try { MultipartBody requests = Unirest.post(getBotAPIUrl() + "editMessageCaption") .field("caption", caption, "application/json; charset=utf8;"); if(chatId != null) requests.field("chat_id", chatId, "application/json; charset=utf8;"); if(messageId != null) requests.field("message_id", messageId); if(inlineMessageId != null) requests.field("inline_message_id", inlineMessageId, "application/json; charset=utf8;"); if(inlineReplyMarkup != null) requests.field("reply_markup", GSON.toJson(inlineReplyMarkup, InlineKeyboardMarkup.class), "application/json; charset=utf8;"); response = requests.asString(); jsonResponse = Utils.processResponse(response); } catch (UnirestException e) { e.printStackTrace(); } return jsonResponse; }
/** * If you call this method then the bot will leave this Chat if it is currently in it * * @return True if leaving the chat succeeded, otherwise False */ default boolean leaveChat() { try { MultipartBody request = Unirest.post(getBotInstance().getBotAPIUrl() + "leaveChat") .field("chat_id", getId(), "application/json; charset=utf8;"); HttpResponse<String> response = request.asString(); JSONObject jsonResponse = processResponse(response); if (jsonResponse != null && Utils.checkResponseStatus(jsonResponse)) { return jsonResponse.getBoolean("result"); } } catch (UnirestException e) { e.printStackTrace(); } return false; } }
response = body.asString(); } catch (UnirestException e) { throw new StorageException("Unable to store result attachment", e);
public List<GameHighScore> getGameHighScores(GameScoreRequest gameScoreRequest) { HttpResponse<String> response; JSONObject jsonResponse; try { MultipartBody request = Unirest.post(getBotAPIUrl() + "setGameScore") .field("user_id", gameScoreRequest.getUserId(), "application/json; charset=utf8;") .field("chat_id", gameScoreRequest.getChatId()) .field("message_id", gameScoreRequest.getMessageId()) .field("inline_message_id", gameScoreRequest.getInlineMessageId()); response = request.asString(); jsonResponse = Utils.processResponse(response); if(jsonResponse != null) { List<GameHighScore> highScores = new LinkedList<>(); for(Object object : jsonResponse.getJSONArray("result")) { JSONObject jsonObject = (JSONObject) object; highScores.add(GameHighScoreImpl.createGameHighscore(jsonObject)); } return highScores; } } catch (UnirestException e) { e.printStackTrace(); } return null; }
/** * Gets the ChatMember object for a specific user based on their ID in respect to this chat * * @param userID The ID of the user that you want the ChatMember object for * * @return The ChatMember object for this user or Null if the request failed */ default ChatMember getChatMember(long userID) { try { MultipartBody request = Unirest.post(getBotInstance().getBotAPIUrl() + "getChatMember") .field("chat_id", getId(), "application/json; charset=utf8;") .field("user_id", userID); HttpResponse<String> response = request.asString(); JSONObject jsonResponse = processResponse(response); if (jsonResponse != null && Utils.checkResponseStatus(jsonResponse)) { return createChatMember(jsonResponse.getJSONObject("result")); } } catch (UnirestException e) { e.printStackTrace(); } return null; }
private JSONObject editMessageText(String chatId, Long messageId, String inlineMessageId, String text, ParseMode parseMode, boolean disableWebPagePreview, InlineReplyMarkup inlineReplyMarkup) { HttpResponse<String> response; JSONObject jsonResponse = null; try { MultipartBody requests = Unirest.post(getBotAPIUrl() + "editMessageText") .field("text", text, "application/json; charset=utf8;") .field("disable_web_page_preview", disableWebPagePreview); if(chatId != null) requests.field("chat_id", chatId, "application/json; charset=utf8;"); if(messageId != null) requests.field("message_id", messageId); if(inlineMessageId != null) requests.field("inline_message_id", inlineMessageId, "application/json; charset=utf8;"); if(parseMode != null) requests.field("parse_mode", parseMode.getModeName(), "application/json; charset=utf8;"); if(inlineReplyMarkup != null) requests.field("reply_markup", GSON.toJson(inlineReplyMarkup, InlineKeyboardMarkup.class), "application/json; charset=utf8;"); response = requests.asString(); jsonResponse = Utils.processResponse(response); } catch (UnirestException e) { e.printStackTrace(); } return jsonResponse; }
response = requests.asString(); jsonResponse = Utils.processResponse(response);
.field("switch_pm_parameter", inlineQueryResponse.getSwitchPmParameter()); response = requests.asString(); jsonResponse = Utils.processResponse(response);
public GameScoreEditResponse setGameScore(SendableGameScore sendableGameScore) { HttpResponse<String> response; JSONObject jsonResponse; try { MultipartBody request = Unirest.post(getBotAPIUrl() + "setGameScore") .field("user_id", sendableGameScore.getUserId(), "application/json; charset=utf8;") .field("score", sendableGameScore.getScore()) .field("force", sendableGameScore.isForce()) .field("disable_edit_message", sendableGameScore.isDisableEditMessage()) .field("chat_id", sendableGameScore.getChatId()) .field("message_id", sendableGameScore.getMessageId()) .field("inline_message_id", sendableGameScore.getInlineMessageId()); response = request.asString(); jsonResponse = Utils.processResponse(response); if(jsonResponse != null) { return GameScoreEditResponseImpl.createGameScoreEditResponse(jsonResponse.getJSONObject("result"), this); } } catch (UnirestException e) { e.printStackTrace(); } return null; }