|
|
@@ -0,0 +1,56 @@
|
|
|
+package com.izouma.awesomeAdmin.web;
|
|
|
+
|
|
|
+import com.izouma.awesomeAdmin.service.TelegramBotConfig;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.telegram.telegrambots.bots.TelegramLongPollingBot;
|
|
|
+import org.telegram.telegrambots.meta.api.methods.send.SendMessage;
|
|
|
+import org.telegram.telegrambots.meta.api.objects.Chat;
|
|
|
+import org.telegram.telegrambots.meta.api.objects.MessageEntity;
|
|
|
+import org.telegram.telegrambots.meta.api.objects.Update;
|
|
|
+import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
|
|
+
|
|
|
+@EnableConfigurationProperties(TelegramBotConfig.class)
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class TelegramBot extends TelegramLongPollingBot {
|
|
|
+
|
|
|
+ private final TelegramBotConfig config;
|
|
|
+
|
|
|
+ public TelegramBot(TelegramBotConfig config) {
|
|
|
+ super(config.getToken());
|
|
|
+ this.config = config;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onUpdateReceived(Update update) {
|
|
|
+ if (update.hasMessage() && update.getMessage().hasText()) {
|
|
|
+ String text = update.getMessage().getText();
|
|
|
+ log.info("Received message: {}", text);
|
|
|
+ Chat chat = update.getMessage().getChat();
|
|
|
+ log.info("Chat: id={} type={} title={}", chat.getId(), chat.getType(), chat.getTitle());
|
|
|
+ if (update.getMessage().getEntities() != null) {
|
|
|
+ for (MessageEntity entity : update.getMessage().getEntities()) {
|
|
|
+ log.info("Entity: type={} text={}", entity.getType(), entity.getText());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ SendMessage message = new SendMessage(); // Create a SendMessage object with mandatory fields
|
|
|
+ message.setChatId(update.getMessage().getChatId().toString());
|
|
|
+ message.setText("hello");
|
|
|
+
|
|
|
+ try {
|
|
|
+ execute(message); // Call method to send the message
|
|
|
+ } catch (TelegramApiException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getBotUsername() {
|
|
|
+ return config.getUsername();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|