A java library to use in a Micronaut Telegram Bot

Build

This project requires Java 8 (min) and you can build it executing the following command:

TELEGRAM_TOKEN=aaaa:bbbbbb ./gradlew build

You will need a telegram Token (obtained from BotFather) to pass the test. You can provide it via command line (see previous example) or include it in src/test/resources/application.properties as telegram.token variable

1. src/test/resources/application.properties
telegram.token=aaaa:bbbbb (1)
1 This file is included into .gitignore so you’ll never upload the token to the repository

Send Messages

TelegramBot

TelegramBot is a Micronaut http-client interface to send messages to a channel or chat. It uses async capabilities from Micronaut so don’t forget to subscribe your calls

diag c42bb6b20f52efac76859f2b5e2677a6

Model

Following are some POJOS used to send information

diag 8ffb6d3ecd11ec16d83a1edce39180b4
1 A message to send
2 Show a menu with buttons
3 Show an inline menu with buttons

Example

For example to send a message to a user you’ll do something similar to:

    Message message = new Message();
    message.setChat_id("@aPublicChaneel");
    message.setText("hello world");
    telegramBot.sendMessage(message).blockingGet();

Receiving messages

Controller

To receive messages from the user you need to create a controller (not provided in this library by the moment) where Telegram will call with the information from the user

This controller is a tipical Micronaut controller but for security give them a not so easy path. It’s common to use the token as an entry point to it (but not mandaory)
@Controller('/my/bot/is/awesome/${telegram.token}')
class BotController{
    @Get('/')
    Single<Message>index( Update update ){
        // your logic
    }
}

Model

Telegram send to your controller an Update object where your can find all the information the user is sending to you:

diag cafaccff64d2a89142cbe3740c8df467

This information can come from a direct message from the user, so userMessage is filled, or from a callback keyboard sent previously from your bot, so callbackQuery is filled.