Initial
This commit is contained in:
22
src/main/java/ru/ilug/telegram_bot/GetUpdates.java
Normal file
22
src/main/java/ru/ilug/telegram_bot/GetUpdates.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package ru.ilug.telegram_bot;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import jakarta.annotation.Nullable;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@JsonInclude(JsonInclude.Include.NON_NULL)
|
||||
public class GetUpdates {
|
||||
|
||||
@Nullable
|
||||
private Integer offset;
|
||||
@Nullable
|
||||
private Integer limit;
|
||||
@Nullable
|
||||
private Integer timeout;
|
||||
@Nullable
|
||||
@JsonProperty("allowed_updates")
|
||||
private String[] allowedUpdates;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package ru.ilug.telegram_bot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
public class MyTelegramBotApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MyTelegramBotApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
14
src/main/java/ru/ilug/telegram_bot/ResponseParameters.java
Normal file
14
src/main/java/ru/ilug/telegram_bot/ResponseParameters.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package ru.ilug.telegram_bot;
|
||||
|
||||
import jakarta.annotation.Nullable;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class ResponseParameters {
|
||||
|
||||
@Nullable
|
||||
private Integer migrateToChatId;
|
||||
@Nullable
|
||||
private Integer retryAfter;
|
||||
|
||||
}
|
||||
38
src/main/java/ru/ilug/telegram_bot/TelegramClient.java
Normal file
38
src/main/java/ru/ilug/telegram_bot/TelegramClient.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package ru.ilug.telegram_bot;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Component
|
||||
public class TelegramClient {
|
||||
|
||||
private final WebClient webClient;
|
||||
|
||||
public TelegramClient(@Value("${application.telegram.token}") String telegramToken) {
|
||||
webClient = WebClient.builder()
|
||||
.baseUrl("https://api.telegram.org/bot" + telegramToken)
|
||||
.defaultHeaders(headers -> {
|
||||
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
})
|
||||
.build();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Mono<TelegramResponse<List<UpdateDto>>> getUpdates(GetUpdates request) {
|
||||
return webClient.post()
|
||||
.uri("/getUpdates")
|
||||
.bodyValue(request)
|
||||
.retrieve()
|
||||
.bodyToMono(new ParameterizedTypeReference<>() {
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
22
src/main/java/ru/ilug/telegram_bot/TelegramResponse.java
Normal file
22
src/main/java/ru/ilug/telegram_bot/TelegramResponse.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package ru.ilug.telegram_bot;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import jakarta.annotation.Nullable;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class TelegramResponse<T> {
|
||||
|
||||
private boolean ok;
|
||||
@Nullable
|
||||
private String description;
|
||||
@Nullable
|
||||
private T result;
|
||||
|
||||
@JsonProperty("error_code")
|
||||
private int errorCode;
|
||||
|
||||
@Nullable
|
||||
private ResponseParameters parameters;
|
||||
|
||||
}
|
||||
50
src/main/java/ru/ilug/telegram_bot/TelegramService.java
Normal file
50
src/main/java/ru/ilug/telegram_bot/TelegramService.java
Normal file
@@ -0,0 +1,50 @@
|
||||
package ru.ilug.telegram_bot;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.event.ContextRefreshedEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Service;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class TelegramService {
|
||||
|
||||
private final TelegramClient telegramClient;
|
||||
|
||||
private Integer lastUpdateId;
|
||||
|
||||
@EventListener(ContextRefreshedEvent.class)
|
||||
public void onStart() {
|
||||
Mono.delay(Duration.ofMillis(3000))
|
||||
.flatMapMany(i -> update())
|
||||
.repeat()
|
||||
.subscribe();
|
||||
}
|
||||
|
||||
public Flux<UpdateDto> update() {
|
||||
GetUpdates request = new GetUpdates();
|
||||
request.setOffset(lastUpdateId);
|
||||
|
||||
return telegramClient.getUpdates(request)
|
||||
.doOnNext(response -> {
|
||||
if (!response.isOk()) {
|
||||
throw new RuntimeException(response.getDescription());
|
||||
}
|
||||
}).flatMapIterable(response -> {
|
||||
assert response.getResult() != null;
|
||||
return response.getResult();
|
||||
}).doOnNext(this::update);
|
||||
}
|
||||
|
||||
private void update(UpdateDto updateDto) {
|
||||
log.info("Get update with id: {}", updateDto.getUpdateId());
|
||||
lastUpdateId = updateDto.getUpdateId() + 1;
|
||||
}
|
||||
|
||||
}
|
||||
12
src/main/java/ru/ilug/telegram_bot/UpdateDto.java
Normal file
12
src/main/java/ru/ilug/telegram_bot/UpdateDto.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package ru.ilug.telegram_bot;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class UpdateDto {
|
||||
|
||||
@JsonProperty("update_id")
|
||||
private int updateId;
|
||||
|
||||
}
|
||||
3
src/main/resources/application.properties
Normal file
3
src/main/resources/application.properties
Normal file
@@ -0,0 +1,3 @@
|
||||
spring.application.name=my-telegram-bot
|
||||
|
||||
application.telegram.token=
|
||||
@@ -0,0 +1,13 @@
|
||||
package ru.ilug.telegram_bot;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class MyTelegramBotApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user