Loading repository data…
Loading repository data…
Eng-Fouad / repository
JTelegramBot is a Java library that wraps Telegram Bot API with a simpler API using Builder design pattern
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
JTelegramBot is a Java library that wraps Telegram Bot API with a simpler API using Builder design pattern.
TelegramBot library supports full functionality of Telegram Bot API 2.1 and it consists of 3 modules:
Other options will be added later.
See https://core.telegram.org/bots/api
UpdateHandler which contains callbacks methods called upon getting new updates from Telegram server. For simplicity, you can use SimpleUpdateHandler (which provides empty implementaions) and override the callback methods you need. UpdateHandler provides the following callback methods:onMessageReceived(TelegramBotApi telegramBotApi, int id, Message message): Invoked on receiving new incoming message of any kind — text, photo, sticker, etc, sticker, etc.onInlineQueryReceived(TelegramBotApi telegramBotApi, int id, InlineQuery inlineQuery): Invoked on receiving new incoming inline query.onChosenInlineResultReceived(TelegramBotApi telegramBotApi, int id, ChosenInlineResult chosenInlineResult): Invoked on receiving the result of an inline query that was chosen by a user and sent to their chat partner.onCallbackQueryReceived(TelegramBotApi telegramBotApi, int id, CallbackQuery callbackQuery): Invoked on receiving new incoming callback query.onGetUpdatesFailure(Exception e): Invoked in case of an exception occurs when trying to get the new update.Inside the callbck methods, wrap the low-level interface telegramBotApi with the high-level builder ApiBuilder. For example:
@Override
public void onMessageReceived(TelegramBotApi telegramBotApi, int id, Message message)
{
try
{
ApiBuilder.api(telegramBotApi)
.sendMessage("*This is a simple text message*")
.toChatId(message.getChat().getId())
.asReplyToMessage(message.getMessageId())
.asSilentMessage()
.parseMessageAs(ParseMode.MARKDOWN)
.execute();
}
catch(IOException e)
{
e.printStackTrace();
}
catch(NegativeResponseException e)
{
e.printStackTrace();
}
}
There are some builder classes that facilitates the creation of complex objects:
InlineKeyboardButtonBuilder builds InlineKeyboardButton[][].KeyboardButtonBuilder builds KeyboardButton[][].InlineQueryResultBuilder builds all of 19 types of InlineQueryResult.InputMessageContentBuilder builds all of 4 types of InputMessageContent.ReplyMarkupBuilder builds all of 4 types of ReplyMarkup.After implmenting the interface UpdateHandler, create a new instance of JTelegramBot as follows:
JTelegramBot bot = new JTelegramBot("BotName", API_TOKEN, updateHandler);
After that, just start the bot in Polling mode:
bot.start(); // blocking call
// or bot.startAsync(); non-blocking call
To start JTelegramBot in a Webhook mode, instead of starting bot directly (as in step 6), wrap it with WebhookServer:
WebhookServer webhookServer = new WebhookServer(bot, "example.com", TelegramPort.PORT_8443, "/random/path");
webhookServer.useGeneratedSelfSignedSslCertificate();
webhookServer.registerWebhook();
webhookServer.start();
Most of the APIs (methods) in this library throws 2 types of exceptions:
ApiBuilder APIsexecute() ends the methods chaining.ApiBuilder.api(TelegramBotApi telegramBotApi)