Skip to content

patrickpichler/java-telegram-bot-api

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

163 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java API for Telegram Bots

Full support of all Bot API 2.1 functions

Download

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/

Usage

Create bot

TelegramBot bot = TelegramBotAdapter.build("BOT_TOKEN");

Execute methods

// 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.

Send message

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) {
    }
});

Keyboards

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")
        });

Getting response to sending methods

SendResponse sendResponse = bot.execute(new SendMessage(chatId, "text"));
Message message = sendResponse.message();

Get updates

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();

Get file

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

Inline mode

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")
);

About

Simple Java API for Telegram Bots

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • Java 100.0%