-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/feat-payment' into feat-payment
# Conflicts: # src/main/resources/application.properties
- Loading branch information
Showing
11 changed files
with
152 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/com/project/carsharingapp/config/TelegramBotConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/project/carsharingapp/config/TelegramBotInitializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/project/carsharingapp/controller/HealthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/java/com/project/carsharingapp/repository/UserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
85 changes: 85 additions & 0 deletions
85
src/main/java/com/project/carsharingapp/service/NotificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters