Java API for Telegram Bots
Full support of all Bot API 2.1 functions
Gradle:
compile 'com.github.pengrad:java-telegram-bot-api:2.1.1'
Maven:
<dependency>
<groupId>com.github.pengrad</groupId>
<artifactId>java-telegram-bot-api</artifactId>
<version>2.1.1</version>
</dependency>
JAR-files:
https://oss.sonatype.org/content/repositories/releases/com/github/pengrad/java-telegram-bot-api/
TelegramBot bot = TelegramBotAdapter.build("BOT_TOKEN");
// sync
BaseResponse response = bot.execute(request);
// async
bot.execute(request, new Callback() {
@Override
public void onResponse(BaseRequest request, BaseResponse response) {
}
@Override
public void onFailure(BaseRequest request, IOException e) {
}
});
All request methods have the same names as original ones.
Required params should be passed in constructor.
Optional params can be added in chains.
SendResponse sendResponse = bot.execute(new SendMessage(chatId, "message text"));
SendResponse sendResponse = bot.execute(
new SendMessage(chatId, "message <b>bold</b> text")
.parseMode(ParseMode.HTML)
.replyMarkup(new ReplyKeyboardMarkup(new String[]{"button 1", "button 2"}))
);
bot.execute(new SendMessage("@mychannel", "message text"), new Callback<SendMessage, SendResponse>() {
@Override
public void onResponse(SendMessage request, SendResponse response) {
}
@Override
public void onFailure(SendMessage request, IOException e) {
}
});
Keyboard replyKeyboardMarkup = new ReplyKeyboardMarkup(
new String[]{"first row button1", "first row button2"},
new String[]{"second row button1", "second row button2"})
.oneTimeKeyboard(true) // optional
.resizeKeyboard(true) // optional
.selective(true); // optional
// keyboard button
Keyboard keyboard = new ReplyKeyboardMarkup(
new KeyboardButton[]{
new KeyboardButton("text"),
new KeyboardButton("contact").requestContact(true),
new KeyboardButton("location").requestLocation(true)
}
);
Keyboard forceReply = new ForceReply(isSelective); // or just new ForceReply();
Keyboard replyKeyboardHide = new ReplyKeyboardHide(); // new ReplyKeyboardHide(isSelective)
InlineKeyboardMarkup inlineKeyboard = new InlineKeyboardMarkup(
new InlineKeyboardButton[]{
new InlineKeyboardButton("url").url("url"),
new InlineKeyboardButton("callback_data").callbackData("callback_data"),
new InlineKeyboardButton("switch_inline_query").switchInlineQuery("switch_inline_query")
});
SendResponse sendResponse = bot.execute(new SendMessage(chatId, "text"));
Message message = sendResponse.message();
GetUpdatesResponse updatesResponse = bot.execute(new GetUpdates().limit(0).offset(0).timeout(0));
List<Update> updates = updatesResponse.updates();
...
Message message = update.message()
If using webhook, you can parse request to Message
Update update = BotUtils.parseUpdate(stringRequest); // from String
Update update = BotUtils.parseUpdate(reader); // from java.io.Reader
Message message = update.message();
GetFileResponse getFileResponse = bot.execute(new GetFile("fileId"));
File file = getFileResponse.file(); // com.pengrad.telegrambot.model.File
file.fileId();
file.filePath(); // relative path
file.fileSize();
To get downloading link as https://api.telegram.org/file/bot<token>/<file_path>
String fullPath = bot.getFullFilePath(file); // com.pengrad.telegrambot.model.File
Getting updates
GetUpdatesResponse updatesResponse = bot.execute(new GetUpdates());
List<Update> updates = updatesResponse.updates();
...
InlineQuery inlineQuery = update.inlineQuery();
If using webhook, you can parse request to InlineQuery
Update update = BotUtils.parseUpdate(stringRequest); // from String
Update update = BotUtils.parseUpdate(reader); // from java.io.Reader
InlineQuery inlineQuery = update.inlineQuery();
Build InlineQueryResult
InlineQueryResult r1 = new InlineQueryResultPhoto("id", "photoUrl", "thumbUrl");
InlineQueryResult r2 = new InlineQueryResultArticle("id", "title", "message text").thumbUrl("url");
InlineQueryResult r3 = new InlineQueryResultGif("id", "gifUrl", "thumbUrl");
InlineQueryResult r4 = new InlineQueryResultMpeg4Gif("id", "mpeg4Url", "thumbUrl");
InlineQueryResult r5 = new InlineQueryResultVideo("id", "videoUrl", InlineQueryResultVideo.MIME_VIDEO_MP4, "message text", "thumbUrl", "video title");
Send query
bot.execute(new AnswerInlineQuery(inlineQuery.id(), r1, r2, r3, r4, r5));
// or full
bot.execute(
new AnswerInlineQuery(inlineQuery.id(), new InlineQueryResult[]{r1, r2, r3, r4, r5})
.cacheTime(cacheTime)
.isPersonal(isPersonal)
.nextOffset("offset")
.switchPmParameter("pmParam")
.switchPmText("pmText")
);