Get updates using getUpdates, Webhook, or UpdatesListener
masterThere are three ways to receive updates from Telegram:
- getUpdates: Manually poll for updates. To confirm updates, use the
offsetparameter set tolast_processed_update_id + 1. - Webhook: Telegram sends requests to your server. Use
BotUtils.parseUpdate(stringRequest)orBotUtils.parseUpdate(reader)to convert the incoming request into anUpdateobject. - UpdatesListener: A high-level abstraction that runs a
getUpdatesloop for you. You implement theprocess(List<Update> updates)method and return an integer to indicate confirmation status.
// UpdatesListener example
bot.setUpdatesListener(new UpdatesListener() {
@Override
public int process(List<Update> updates) {
// process updates
return UpdatesListener.CONFIRMED_UPDATES_ALL;
}
}, new ExceptionHandler() {
@Override
public void onException(TelegramException e) {
// handle exception
}
});
// To stop receiving updates
bot.removeGetUpdatesListener();