-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTelegramBot.java
More file actions
125 lines (111 loc) · 4.53 KB
/
TelegramBot.java
File metadata and controls
125 lines (111 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package ivan.solscanbot.bot;
import ivan.solscanbot.exception.AddressAlreadyExistsException;
import ivan.solscanbot.exception.AddressNotMonitoredException;
import ivan.solscanbot.exception.ExceedsAmountOfAddressesException;
import ivan.solscanbot.exception.InvalidAddressException;
import ivan.solscanbot.exception.UserNotHaveAnyMonitoredAddressesException;
import ivan.solscanbot.service.TelegramService;
import org.springframework.beans.factory.annotation.Value;
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
public class TelegramBot extends TelegramLongPollingBot {
private final String token;
private final String username;
private final TelegramService telegramService;
public TelegramBot(
@Value("${telegram.bot.token}") String token,
@Value("${telegram.bot.username}") String username,
TelegramService telegramService
) {
this.token = token;
this.username = username;
this.telegramService = telegramService;
}
@Override
public String getBotUsername() {
return username;
}
@Override
public String getBotToken() {
return token;
}
@Override
public void onUpdateReceived(Update update) {
if (update.hasMessage() && update.getMessage().hasText()) {
String messageText = update.getMessage().getText();
long chatId = update.getMessage().getChatId();
if (messageText.startsWith("/help")) {
handleHelp(chatId);
} else if (messageText.startsWith("/add")) {
handleAddAddresses(chatId, messageText);
} else if (messageText.startsWith("/list")) {
handleListAddresses(chatId);
} else if (messageText.startsWith("/remove")) {
handleRemoveAddress(chatId, messageText);
} else if (messageText.startsWith("/portfolio")) {
handleGetPortfolios(chatId, messageText);
} else {
sendMessage(
chatId, "Unknown command. Use /help to get clarifications");
}
}
}
public void sendNotification(long chatId, String notification) {
sendMessage(chatId, notification);
}
private void handleHelp(long chatId) {
String message = telegramService.getHelp();
sendMessage(chatId, message);
}
private void handleGetPortfolios(long chatId, String messageText) {
try {
String message = telegramService.getPortfolios(chatId, messageText);
sendMessage(chatId, message);
} catch (InvalidAddressException | AddressNotMonitoredException
| ExceedsAmountOfAddressesException e) {
sendMessage(chatId, e.getMessage());
}
}
private void handleAddAddresses(long chatId, String messageText) {
try {
String message = telegramService.addAddresses(chatId, messageText);
sendMessage(chatId, message);
} catch (InvalidAddressException | AddressAlreadyExistsException
| ExceedsAmountOfAddressesException e) {
sendMessage(chatId, e.getMessage());
}
}
private void handleListAddresses(long chatId) {
try {
String message = telegramService.listAddresses(chatId);
sendMessage(chatId, message);
} catch (UserNotHaveAnyMonitoredAddressesException e) {
sendMessage(chatId, e.getMessage());
}
}
private void handleRemoveAddress(long chatId, String messageText) {
try {
String message = telegramService.removeAddresses(chatId, messageText);
sendMessage(chatId, message);
} catch (InvalidAddressException | AddressNotMonitoredException
| ExceedsAmountOfAddressesException e) {
sendMessage(chatId, e.getMessage());
}
}
private void sendMessage(long chatId, String text) {
SendMessage message = new SendMessage();
message.setChatId(String.valueOf(chatId));
message.setText(text);
message.enableMarkdown(true);
message.disableWebPagePreview();
try {
execute(message);
} catch (TelegramApiException e) {
throw new RuntimeException("Error while sending a message.", e);
}
}
}