1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
| import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import lombok.Setter; import lombok.SneakyThrows; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.web.reactive.function.client.WebClient; import org.telegram.telegrambots.meta.api.methods.botapimethods.BotApiMethod; import org.telegram.telegrambots.meta.api.methods.botapimethods.PartialBotApiMethod; import org.telegram.telegrambots.meta.api.methods.groupadministration.SetChatPhoto; import org.telegram.telegrambots.meta.api.methods.send.*; import org.telegram.telegrambots.meta.api.methods.stickers.*; import org.telegram.telegrambots.meta.api.methods.updates.SetWebhook; import org.telegram.telegrambots.meta.api.methods.updatingmessages.EditMessageMedia; import org.telegram.telegrambots.meta.api.objects.File; import org.telegram.telegrambots.meta.api.objects.media.*; import org.telegram.telegrambots.meta.api.objects.media.paid.InputPaidMedia; import org.telegram.telegrambots.meta.api.objects.media.paid.InputPaidMediaVideo; import org.telegram.telegrambots.meta.api.objects.message.Message; import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import java.io.IOException; import java.io.InputStream; import java.io.Serializable; import java.util.List; import java.util.concurrent.CompletableFuture;
@Component public class TGBots extends AbstractTelegramClient { @Setter private String Token; private String BaseUrl = null; @Qualifier("tgBotWebClient") private final WebClient webClient; private final ObjectMapper objectMapper;
public TGBots(WebClient webClient) { this.webClient = webClient; this.objectMapper = new CustomObjectMapper(); }
public TGBots setBaseUrl(String url) { this.BaseUrl = url; return this; }
private WebClient loadWebClient() { return this.BaseUrl == null ? webClient : webClient.mutate().baseUrl(this.BaseUrl).build(); }
@SneakyThrows @Override public <T extends Serializable, Method extends BotApiMethod<T>> CompletableFuture<T> executeAsync(Method method) throws TelegramApiException { if (method == null) { throw new TelegramApiException("Parameter method can not be null"); } else { method.validate();
String body = this.objectMapper.writeValueAsString(method); return this.sendRequest(method, body); } }
private <T extends Serializable, Method extends PartialBotApiMethod<T>> CompletableFuture<T> sendRequest(Method method, String body) { CompletableFuture<T> future = new CompletableFuture<>(); this.loadWebClient() .post() .uri("/bot{token}/{method}", Token, method.getMethod()) .contentType(MediaType.APPLICATION_JSON) .bodyValue(body) .retrieve() .bodyToMono(String.class) .doOnSuccess(response -> { try { LogUtils.debug(null, 0, "TG-API", method.getMethod(), response); T result = method.deserializeResponse(response); future.complete(result); } catch (Exception e) { future.completeExceptionally(e); } }) .doOnError(error -> { LogUtils.error(null, 0, "TG-API", method.getMethod(), error.getMessage()); future.completeExceptionally(error); }) .subscribe(); return future; } }
|