Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat-payment' into feat-payment
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/main/resources/application.properties
  • Loading branch information
ivan0dyatlyukkk committed Oct 4, 2023
2 parents 5731675 + 6d1bcba commit 81328bc
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 7 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<maven.checkstyle.plugin.configLocation>checkstyle.xml</maven.checkstyle.plugin.configLocation>
<lombok.mapstruct.binding.version>0.2.0</lombok.mapstruct.binding.version>
<mapstruct.version>1.5.5.Final</mapstruct.version>
<telegram.version>6.5.0</telegram.version>
</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -82,6 +83,11 @@
<artifactId>stripe-java</artifactId>
<version>23.7.0</version>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>${telegram.version}</version>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.project.carsharingapp.config;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
public class TelegramBotConfig {
@Value("${bot.name}")
private String botName;

@Value("${bot.token}")
private String token;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.project.carsharingapp.config;

import com.project.carsharingapp.service.NotificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.meta.TelegramBotsApi;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession;

@Component
@RequiredArgsConstructor
public class TelegramBotInitializer {
private final NotificationService notificationService;

@EventListener({ContextRefreshedEvent.class})
public void init() throws TelegramApiException {
TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
telegramBotsApi.registerBot(notificationService);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.project.carsharingapp.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "Health management",
description = "Send message if application is running")
@RestController
@RequestMapping(value = "/health")
public class HealthController {
@GetMapping
public String checkHealth() {
return "Application is up and running";
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.project.carsharingapp.repository;

import com.project.carsharingapp.model.User;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.project.carsharingapp.service;

import com.project.carsharingapp.config.TelegramBotConfig;
import com.project.carsharingapp.model.User;
import com.project.carsharingapp.repository.UserRepository;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.telegram.telegrambots.bots.TelegramLongPollingBot;
import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;

@Component
@RequiredArgsConstructor
public class NotificationService extends TelegramLongPollingBot {
private final TelegramBotConfig config;
private final UserRepository userRepository;

@Override
public void onUpdateReceived(Update update) {
if (update.hasMessage() && update.getMessage().hasText()) {
String messageText = update.getMessage().getText();
Long chatId = update.getMessage().getChatId();
switch (messageText) {
case "/start":
startCommandReceived(chatId, update.getMessage().getChat().getFirstName());
break;
default:
break;
}
if (!messageText.startsWith("/")) {
saveUserChatId(chatId, update.getMessage().getText());
}
}
}

private void saveUserChatId(Long chatId, String text) {
Optional<User> optionalUser = userRepository.findByEmail(text);
if (optionalUser.isPresent()) {
User user = optionalUser.get();
user.setTelegramChatId(chatId);
userRepository.save(user);
sendMessage(chatId, "Success");
} else {
sendMessage(chatId, "can`t find user");
}
}

@Override
public String getBotUsername() {
return config.getBotName();
}

@Override
public String getBotToken() {
return config.getToken();
}

public void sendNotification(Long userId, String message) {
//need security
Long chatId = 0L;
sendMessage(chatId, message);
}

private void startCommandReceived(Long chatId, String firstName) {
String answer = "Hi, " + firstName + ", nice to meet you! \n"
+ "Enter your username to get started:";
sendMessage(chatId, answer);

}

private void sendMessage(Long chatId, String textMessage) {
SendMessage sendMessage = new SendMessage();
sendMessage.setChatId(chatId);
sendMessage.setText(textMessage);

try {
execute(sendMessage);
} catch (TelegramApiException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ public interface PaymentService {
Payment updateStatus(String sessionId);

List<Payment> getAll(Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ public PaymentAmountHandler getHandler(Payment.Type type) {
new NoSuchElementException("Can't find a PaymentAmountHandler "
+ "for such type as: " + type)
);

}

}
3 changes: 1 addition & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
spring.datasource.url=jdbc:mysql://localhost:3306/car_sharing_db?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root123
spring.datasource.password=redblack0root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

server.servlet.context-path=/api
spring.jpa.hibernate.ddl-auto=validate
spring.jpa.show-sql=true
spring.jpa.open-in-view=false


stripe.secret=sk_test_51NwluPLODs6ppc0Lt4vFUOmyqkOeMpnbizJwy\
BNFWQ0Y2rL79OtqPtydVF11PD2WoXiIkxCc3IrThcvWZkOmmn1e00ST12M2PI
app.host=localhost
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ databaseChangeLog:
- column:
name: telegram_chat_id
type: bigint
constraints:
nullable: false
- column:
name: is_deleted
type: boolean
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ stripe.secret=sk_test_51NwluPLODs6ppc0Lt4vFUOmyqkOeMpnbizJwy\
BNFWQ0Y2rL79OtqPtydVF11PD2WoXiIkxCc3IrThcvWZkOmmn1e00ST12M2PI
app.host=localhost
app.port=8080

bot.name=SharingCarBot
bot.token=6631383610:AAExM59fyeZyshmxsmupqUrAV9TcitH30vk

0 comments on commit 81328bc

Please sign in to comment.