Support special webhooks
This commit is contained in:
parent
57f043e239
commit
cc33d6641d
10
.editorconfig
Normal file
10
.editorconfig
Normal 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
|
@ -1,5 +1,7 @@
|
|||||||
package org.telegram.telegrambots.meta.generics;
|
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.BotApiMethod;
|
||||||
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook;
|
import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook;
|
||||||
import org.telegram.telegrambots.meta.api.objects.Update;
|
import org.telegram.telegrambots.meta.api.objects.Update;
|
||||||
@ -18,6 +20,21 @@ public interface WebhookBot extends TelegramBot {
|
|||||||
*/
|
*/
|
||||||
BotApiMethod<?> onWebhookUpdateReceived(Update update);
|
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
|
* Execute setWebhook method to set up the url of the webhook
|
||||||
* @throws TelegramApiRequestException In case of error executing the request
|
* @throws TelegramApiRequestException In case of error executing the request
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package org.telegram.telegrambots.updatesreceivers;
|
package org.telegram.telegrambots.updatesreceivers;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.telegram.telegrambots.Constants;
|
import org.telegram.telegrambots.Constants;
|
||||||
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
|
import org.telegram.telegrambots.meta.api.methods.BotApiMethod;
|
||||||
@ -57,6 +58,31 @@ public class RestApi {
|
|||||||
return Response.status(Response.Status.NOT_FOUND).build();
|
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
|
@GET
|
||||||
@Path("/{botPath}")
|
@Path("/{botPath}")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user