
de.raysha.lib.telegram.bot.api.UnirestRequestExecutor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bot-api Show documentation
Show all versions of bot-api Show documentation
This is an java library for the Telegram-Bot-API.
The Bot-API is an HTTP-based interface created for
developers keen on building bots for Telegram.
package de.raysha.lib.telegram.bot.api;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import com.mashape.unirest.request.BaseRequest;
import de.raysha.lib.telegram.bot.api.exception.BotApiException;
import de.raysha.lib.telegram.bot.api.exception.BotException;
import org.json.JSONObject;
import java.io.File;
import java.util.Map;
/**
* Uses Unirest lib for making http requests
*/
public class UnirestRequestExecutor implements RequestExecutor {
private final String baseUrl;
public UnirestRequestExecutor(String baseUrl) {
this.baseUrl = baseUrl;
}
@Override
public String get(String action, Map parameters) throws BotException {
return sendAndHandleRequest(
Unirest.get(baseUrl + action)
.queryString(parameters));
}
@Override
public String post(String action, Map parameters) throws BotException {
return sendAndHandleRequest(
Unirest.post(baseUrl + action)
.fields(parameters));
}
@Override
public String post(String action, Map parameters, String fileName, File file) throws BotException {
return sendAndHandleRequest(
Unirest.post(baseUrl + action)
.queryString(parameters)
.field(fileName, file));
}
private String sendAndHandleRequest(BaseRequest request) throws BotException {
JSONObject jsonResult;
try {
jsonResult = request
.asJson().getBody().getObject();
} catch (UnirestException e) {
throw new BotException("Could not get a response.", e);
}
if(jsonResult.get("ok").equals(false)){
throw new BotApiException(
jsonResult.optInt("error_code", -1),
jsonResult.optString("error_type"),
jsonResult.optString("description"));
}
return jsonResult.get("result").toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy