diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 00000000..1a424d53 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,10 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +indent_size = 4 +indent_style = space +insert_final_newline = false +max_line_length = 120 +tab_width = 4 diff --git a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/generics/WebhookBot.java b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/generics/WebhookBot.java index ca00bc35..d9565607 100644 --- a/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/generics/WebhookBot.java +++ b/telegrambots-meta/src/main/java/org/telegram/telegrambots/meta/generics/WebhookBot.java @@ -1,5 +1,7 @@ package org.telegram.telegrambots.meta.generics; +import java.util.ArrayList; +import java.util.List; import org.telegram.telegrambots.meta.api.methods.BotApiMethod; import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook; import org.telegram.telegrambots.meta.api.objects.Update; @@ -18,6 +20,21 @@ public interface WebhookBot extends TelegramBot { */ BotApiMethod onWebhookUpdateReceived(Update update); + /** + * This method is called when receiving multiple updates via webhook + * @param updates Updates received + */ + default List> onWebhookUpdatesReceived(List updates) { + List> results = new ArrayList<>(); + for (Update update : updates) { + BotApiMethod newResult = onWebhookUpdateReceived(update); + if (newResult != null) { + results.add(newResult); + } + } + return results; + } + /** * Execute setWebhook method to set up the url of the webhook * @throws TelegramApiRequestException In case of error executing the request diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/RestApi.java b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/RestApi.java index 2a764778..c67a1942 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/RestApi.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/RestApi.java @@ -1,5 +1,6 @@ package org.telegram.telegrambots.updatesreceivers; +import java.util.List; import lombok.extern.slf4j.Slf4j; import org.telegram.telegrambots.Constants; import org.telegram.telegrambots.meta.api.methods.BotApiMethod; @@ -57,6 +58,31 @@ public class RestApi { return Response.status(Response.Status.NOT_FOUND).build(); } + @POST + @Path("/{botPath}") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response updatesReceived(@PathParam("botPath") String botPath, List updates) { + if (callbacks.containsKey(botPath)) { + try { + List> responses = callbacks.get(botPath).onWebhookUpdatesReceived(updates); + if (responses != null) { + for (BotApiMethod response : responses) { + if (response != null) { + response.validate(); + } + } + } + return Response.ok(responses).build(); + } catch (TelegramApiValidationException e) { + log.error(e.getLocalizedMessage(), e); + return Response.serverError().build(); + } + } + + return Response.status(Response.Status.NOT_FOUND).build(); + } + @GET @Path("/{botPath}") @Produces(MediaType.APPLICATION_JSON)