This Java library implements all the methods and types in Telegram's Bot API including the new features in 4.1. Telegram is an instant messenger for various platforms.
If you find a bug or know some way to improve the library please report it to me as an issue or in private. Just don't ask me to add boiler plate stuff like getters and setters. There are lots of other Java libraries if you prefer that.
- Uses GET methods with URL query string where possible (all methods except when sending files as multipart/form-data)
- Supports getUpdates(), but I've only tested it with webhook so far
- Making requests when getting updates is not supported
- Full implementation of the API and hopefully quickly updated when new features are released
- Javadoc links to corresponding official documentation
- All types implemented as simple POJO classes with constructors and helper methods where necessary
- All methods implemented as Java methods with various parameter options
- A setSilent() method for bots who want to use disable_notification instead of having to set it each time you send a message
- Helper methods to make life easier, for instance
TgBotApi.sendReply()
,TgBotApi.downloadFile()
andTgBotApi.debug()
- Enums and constants for common values
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.nadam:tg-bot-api:2.0.0'
}
The project depends on GSON 2.8.2. I have currently only used it together with Google App Engine (which includes GSON), but it should be possible to use it with any platform.
Latest binaries available in the Releases section.
A pom.xml for Maven is included in the project. For other options just make sure you include GSON 2.8.2 or later.
If you are new to bots or bot development for Telegram check out the following links:
- Setup your bot using @BotFather as described in the links above to get your bot username and token
- Use @userinfobot or similar to get your user id
- For Google App Engine or similar, extend HttpServlet and implement the doPost() method.
- Optionally implement the ErrorListener interface.
- Create the
TgBotApi
object using your token and user id (it's good practice to put these values as constants in a separate file). - Parse the requests using
TgBotApi.parseFromWebhook()
to get theUpdate
object - Optionally send a reply using
TgBotApi.sendMessage()
orTgBotApi.sendReply()
public class ExampleServlet extends HttpServlet implements ErrorListener {
private TgBotApi api;
public ExampleServlet() {
super();
api = new TgBotApi(TOKEN, OWNER, this);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setStatus(200);
String messageText;
long chatId = 0;
try {
Update update = api.parseFromWebhook(req.getReader());
// Do something interesting,
// extract the chatId from the update object and
// decide an appropriate messageText to send as reply.
} catch (Exception e) {
api.debug(e);
return;
}
api.sendMessage(chatId, messageText);
}
@Override
public void onError(int errorCode, String description) {
api.debug(new Exception("ErrorCode " + errorCode + ", " + description));
}
}
The code for other platforms will look quite similar.
- TgBotApi Example @TgBotApiExampleBot
- Userinfobot @userinfobot
- Groupinfobot @groupinfobot
- Chat Bridge Bot - @chatbridgebot
- Json Dump Bot - @JsonDumpBot
- Jackpot Bot - @jackpot_bot
If you think the Bot API is too limiting you can use the Telegram API instead which is what ordinary Telegram clients use. You use it with an ordinary Telegram account or with a Bot account (using auth.importBotAuthorization).