Support special webhooks

This commit is contained in:
Andrea Cavalli 2021-12-19 01:16:10 +01:00
parent 57f043e239
commit cc33d6641d
3 changed files with 53 additions and 0 deletions

10
.editorconfig Normal file
View File

@ -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

View File

@ -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<BotApiMethod<?>> onWebhookUpdatesReceived(List<Update> updates) {
List<BotApiMethod<?>> 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

View File

@ -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<Update> updates) {
if (callbacks.containsKey(botPath)) {
try {
List<BotApiMethod<?>> 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)