Skip to content

kratess/TelegramBotAPI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 

Repository files navigation

TelegramBotAPI

Version 1.0

Docs

ALL methods on the official Telegram Bot API page

How to get updates from bot

There are 2 methods

  • Webhooks (Setup a callback server. Telegram will send request to our server)

  • Long polling (We will send periodic request to Telegram server)

  • For now only Long Polling can be used.

  • Complete the toString() method with json

Long Polling

bot.setUpdateListener(new UpdateEvent() {
  @Override
  public void onUpdate(Update update) {
    int hour = LocalDateTime.now().getHour();
    int minute = LocalDateTime.now().getMinute();
    int second = LocalDateTime.now().getSecond();

    String time = "[" + hour + ":" + minute + ":" + second + "]";

    System.out.println(time);
    System.out.println(update.toString());
  }
}, 1000, 100, 0, null);

Return a JSONObject

Send methods

ALL methods on the official Telegram Bot API page

TelegramBot bot = new TelegramBot("xxxxxxxxx:yzyzyzyzyzyzyzyzyz-yzyzyzyzyz");

First we need to create an instance of the bot using the unique token gived by Bot Father


Then we need to create the request with one of the available methods

SendMessage request = new SendMessage(chat_id, URLEncoder.encode("TEST message UTF-8", "UTF-8"));

So we must send it to the Telegram servers

Sync method:

bot.execute(request);

Async method:

bot.async_execute(request, new ExecuteEvent() {
    @Override
    public void onExecute(JSONObject result) {
        // NEXT
    }
});

The return of method execute is a JSONArray so we can read it

System.out.println( result.toString(4) );