Simple Java API for Telegram Bots
Full support of all Bot API functions
Download the latest version via Gradle:
compile 'com.github.pengrad:java-telegram-bot-api:1.3.2'
or Maven:
<dependency>
<groupId>com.github.pengrad</groupId>
<artifactId>java-telegram-bot-api</artifactId>
<version>1.3.2</version>
</dependency>
TelegramBot bot = TelegramBotAdapter.build("BOT_TOKEN");
All bot methods have the same signature as original ones.
You can pass null
as any Optional parameter
// short syntax
bot.sendMessage(chatId, "short message sending");
bot.sendMessage("@mychannel", "short message sending");
// full
bot.sendMessage(
chatId, // chat_id
"Hello _italic_ *bold*!", // text
ParseMode.Markdown, // Markdown text or null
false, // disable_web_page_preview
replyMessageId, // reply_to_message_id
new ReplyKeyboardMarkup(new String[]{"ok", "cancel"}).oneTimeKeyboard(true)); // keyboard
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 forceReply = new ForceReply(isSelective); // or just new ForceReply();
Keyboard replyKeyboardHide = new ReplyKeyboardHide(); // new ReplyKeyboardHide(isSelective)
SendResponse sendResponse = bot.sendMessage(chatId, "short message sending");
Message message = sendResponse.message();
3 options to sending files
// as String, resending existing file
String fileId;
// as File
InputFile.photo(file);
InputFile.audio(file);
InputFile.video(file);
InputFile.voice(file);
new InputFile("text/plain", file);
// as byte[]
InputFileBytes.photo(bytes);
InputFileBytes.audio(bytes);
InputFileBytes.video(bytes);
InputFileBytes.voice(bytes);
new InputFileBytes("text/plain", bytes, "my-file.txt");
Examples
// Photo
String fileId = // resending fileId
bot.sendPhoto(chatId, fileId, "caption", null, null);
bot.sendPhoto(chatId, InputFile.photo(imageFile), "caption", replyMessageId, new ForceReply());
bot.sendPhoto(chatId, InputFileBytes.photo(bytes), "caption", null, new ReplyKeyboardHide());
// Same options for all types
// Audio
bot.sendAudio(chatId, InputFile.audio(audioFile), duration, performer, title, null, null);
// Video
bot.sendVideo(chatId, InputFile.video(videoFile), duration, "caption", null, null);
// Document
bot.sendDocument(chatId, new InputFile("text/plain", docFile), null, null);
// Sticker
bot.sendSticker(chatId, stickerId, null, null);
// Voice
bot.sendVoice(chatId, InputFileBytes.voice(bytes), duration, null, null);
bot.sendChatAction(chatId, ChatAction.find_location);
bot.sendChatAction(chatId, ChatAction.typing);
bot.sendChatAction(chatId, ChatAction.record_audio);
bot.sendChatAction(chatId, ChatAction.record_video);
bot.sendChatAction("@channel", ChatAction.upload_audio);
bot.sendChatAction("@channel", ChatAction.upload_document);
bot.sendChatAction("@channel", ChatAction.upload_photo);
bot.sendChatAction("@channel", ChatAction.upload_video);
GetUpdatesResponse updatesResponse = bot.getUpdates(offset, limit, timeout);
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.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("fileId");
String fullPath = bot.getFullFilePath(file); // com.pengrad.telegrambot.model.File
Getting updates
GetUpdatesResponse updatesResponse = bot.getUpdates(offset, limit, timeout);
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.answerInlineQuery(inlineQuery.id(), r1, r2, r3, r4, r5);
// or full
bot.answerInlineQuery(inlineQuery.id(), new InlineQueryResult[]{r1, r2, r3, r4, r5}, cacheTime, isPersonal, nextOffset);