Skip to content

hw7 #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

hw7 #10

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Compiled class file
*.class


# Log file
*.log

Expand All @@ -23,4 +24,8 @@
hs_err_pid*

# idea
.idea
FP/.idea/

# volumes
FP/migrations/postgres_data/
FP/rabbitmq/
110 changes: 110 additions & 0 deletions FP/bot/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.example</groupId>
<artifactId>FP</artifactId>
<version>1.0</version>
</parent>

<modelVersion>4.0.0</modelVersion>

<artifactId>bot</artifactId>

<properties>
<starter-validation.version>3.0.1</starter-validation.version>
<starter-web.version>2.7.6</starter-web.version>
</properties>

<dependencies>
<!--Swagger UI-->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<!--Swagger UI-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>${starter-validation.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${starter-web.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-indexer</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--Для бота-->
<dependency>
<groupId>com.github.pengrad</groupId>
<artifactId>java-telegram-bot-api</artifactId>
<version>6.6.0</version>
</dependency>
<!--Для бота-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</dependency>
<!--RabbitMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>3.0.6</version>
</dependency>
<!-- Database -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>scrapper</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
26 changes: 26 additions & 0 deletions FP/bot/src/main/java/ru/tinkoff/edu/java/bot/BotApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.tinkoff.edu.java.bot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.jdbc.core.JdbcTemplate;
import ru.tinkoff.edu.java.bot.configuration.records.ApplicationConfig;
import ru.tinkoff.edu.java.bot.handler.BotMain;

import javax.sql.DataSource;


@SpringBootApplication
@EnableConfigurationProperties(ApplicationConfig.class)
public class BotApplication
{
public static void main(String[] args){
var ctx = SpringApplication.run(BotApplication.class, args);
ApplicationConfig config = ctx.getBean(ApplicationConfig.class);
JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
BotMain bot = new BotMain(config.bot().token(), jdbcTemplate);
bot.start();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.tinkoff.edu.java.bot.api;

import org.springframework.web.bind.annotation.*;
import ru.tinkoff.edu.java.bot.model.LinkUpdate;

@RestController
@RequestMapping("/update")
public class BotController {

@PostMapping
public String updateChat(@RequestBody LinkUpdate update) {
return update.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package ru.tinkoff.edu.java.bot.api.exceptionHandler;

import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import ru.tinkoff.edu.java.bot.model.ApiErrorResponse;

@RestControllerAdvice
public class BotExceptionHandler {

private String getDescription(String message) {
ApiErrorResponse errorObj = new ApiErrorResponse(
message,
null,
null,
null,
null
);
return errorObj.description();
}

@ExceptionHandler(HttpMessageNotReadableException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String MessageNotReadable(HttpMessageNotReadableException Exception) {
return getDescription("Некорректные значения параметров или их нет!");
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public String MethodNotSupported(HttpRequestMethodNotSupportedException Exception) {
return getDescription("Метод не разрешен!");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ru.tinkoff.edu.java.bot.configuration;

import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import ru.tinkoff.edu.java.bot.configuration.records.ApplicationConfig;

import java.util.Collections;

@Configuration
public class RabbitMQConfiguration {

private final ApplicationConfig config;

public RabbitMQConfiguration(ApplicationConfig config) {
this.config = config;
}

@Bean
public DirectExchange exchange() {
return new DirectExchange(config.exchange());
}

@Bean
public Queue queue() {
return QueueBuilder.durable(config.queue())
.withArgument("x-dead-letter-exchange",config.queue() + ".dlq")
.build();
}

@Bean
public Binding binding(Queue queue, DirectExchange exchange) {
return BindingBuilder.bind(queue)
.to(exchange)
.with(config.routingKey());
}

@Bean
public DirectExchange dlqExchange() {
return new DirectExchange(config.exchange() + ".dlq");
}

@Bean
public Queue dlqQueue() {
return QueueBuilder.durable(config.queue() + ".dlq").build();
}

@Bean
public Binding dlqBinding() {
return BindingBuilder.bind(dlqQueue())
.to(dlqExchange())
.with(config.routingKey());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.tinkoff.edu.java.bot.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

@Configuration
public class jdbcBean {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.postgresql.Driver");
dataSource.setUrl("jdbc:postgresql://localhost:5432/postgres");
dataSource.setUsername("scrap_user");
dataSource.setPassword("hard_password");

return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate () {
return new JdbcTemplate (dataSource());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package ru.tinkoff.edu.java.bot.configuration.records;

import jakarta.validation.constraints.NotNull;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.validation.annotation.Validated;

@Validated
@EnableScheduling
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@ConfigurationProperties(prefix = "app", ignoreUnknownFields = false)
public record ApplicationConfig(
@NotNull String test,
@NotNull Scheduler scheduler,
String exchange,
String routingKey,
String queue,
Bot bot
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package ru.tinkoff.edu.java.bot.configuration.records;

public record Bot(String token, String name) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package ru.tinkoff.edu.java.bot.configuration.records;

import java.time.Duration;

public record Scheduler(Duration interval) {}
33 changes: 33 additions & 0 deletions FP/bot/src/main/java/ru/tinkoff/edu/java/bot/handler/BotMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ru.tinkoff.edu.java.bot.handler;

import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.request.SendMessage;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.jdbc.core.JdbcTemplate;
import ru.tinkoff.edu.java.bot.configuration.records.ApplicationConfig;


@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
@EnableConfigurationProperties(ApplicationConfig.class)
public class BotMain {

String token;
static TelegramBot bot;
private JdbcTemplate jdbcTemplate;

public BotMain(String token, JdbcTemplate jdbcTemplate) {
this.token = token;
this.jdbcTemplate = jdbcTemplate;
}

public void start() {
bot = new TelegramBot(token);
bot.setUpdatesListener(new Updater(bot, jdbcTemplate));
}

public void end() {
bot.removeGetUpdatesListener();
}
}
Loading