Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor bot connecting with button on profile page
- Loading branch information
Showing
14 changed files
with
166 additions
and
68 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
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
38 changes: 0 additions & 38 deletions
38
src/main/java/com/habr/telegrambotmfa/botCommands/ConnectAccountCommand.java
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
src/main/java/com/habr/telegrambotmfa/config/WebSecurityConfig.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
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
1 change: 0 additions & 1 deletion
1
src/main/java/com/habr/telegrambotmfa/login/CustomAuthenticationProvider.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
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
25 changes: 25 additions & 0 deletions
25
src/main/java/com/habr/telegrambotmfa/models/ConnectTelegramTo.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,25 @@ | ||
package com.habr.telegrambotmfa.models; | ||
|
||
public class ConnectTelegramTo { | ||
private String checkString; | ||
private int userId; | ||
|
||
public ConnectTelegramTo() { | ||
} | ||
|
||
public String getCheckString() { | ||
return checkString; | ||
} | ||
|
||
public void setCheckString(String checkString) { | ||
this.checkString = checkString; | ||
} | ||
|
||
public int getUserId() { | ||
return userId; | ||
} | ||
|
||
public void setUserId(int userId) { | ||
this.userId = userId; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/habr/telegrambotmfa/repositories/ConnectTelegramRepository.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.habr.telegrambotmfa.repositories; | ||
|
||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Repository | ||
public class ConnectTelegramRepository { | ||
private Map<Integer, Long> repository = new HashMap<>(); // user id, chat id | ||
|
||
public void register(int userId, long chatId) { | ||
repository.put(userId, chatId); | ||
} | ||
|
||
public Long getChatIdByUserId(int userId) { | ||
return repository.get(userId); | ||
} | ||
|
||
public void removeByUserId(int userId) { | ||
repository.remove(userId); | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
src/main/java/com/habr/telegrambotmfa/services/TelegramService.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,37 @@ | ||
package com.habr.telegrambotmfa.services; | ||
|
||
import com.habr.telegrambotmfa.TelegramBot; | ||
import com.habr.telegrambotmfa.repositories.ConnectTelegramRepository; | ||
import com.habr.telegrambotmfa.repositories.UserRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
import org.telegram.telegrambots.meta.api.methods.send.SendMessage; | ||
import org.telegram.telegrambots.meta.exceptions.TelegramApiException; | ||
|
||
@Service | ||
public class TelegramService { | ||
private UserRepository userRepository; | ||
private ConnectTelegramRepository connectTelegramRepository; | ||
private TelegramBot telegramBot; | ||
|
||
@Autowired | ||
public TelegramService(UserRepository userRepository, ConnectTelegramRepository connectTelegramRepository, TelegramBot telegramBot) { | ||
this.userRepository = userRepository; | ||
this.connectTelegramRepository = connectTelegramRepository; | ||
this.telegramBot = telegramBot; | ||
} | ||
|
||
public long connectBot(int userId, int telegramUserId) { | ||
long chatId = connectTelegramRepository.getChatIdByUserId(telegramUserId); | ||
userRepository.connectBot(userId, chatId); | ||
connectTelegramRepository.removeByUserId(telegramUserId); | ||
return chatId; | ||
} | ||
|
||
public void sendMessage(String text, long chatId) throws TelegramApiException { | ||
var message = new SendMessage() | ||
.setText(text) | ||
.setChatId(chatId); | ||
telegramBot.execute(message); | ||
} | ||
} |
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