From 8dae811e0ad78f15faa6431590a0a4ae66c7bc03 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Tue, 5 Sep 2017 14:16:12 +0300 Subject: api: telegram bot is spring component --- juick-api/src/main/java/com/juick/api/TGBot.java | 18 ++++++----- .../api/configuration/ApiAppConfiguration.java | 2 +- .../juick/api/configuration/ApiInitializer.java | 3 +- .../api/configuration/TelegramConfiguration.java | 36 ---------------------- 4 files changed, 13 insertions(+), 46 deletions(-) delete mode 100644 juick-api/src/main/java/com/juick/api/configuration/TelegramConfiguration.java (limited to 'juick-api/src/main/java') diff --git a/juick-api/src/main/java/com/juick/api/TGBot.java b/juick-api/src/main/java/com/juick/api/TGBot.java index e67e3161..72097154 100644 --- a/juick-api/src/main/java/com/juick/api/TGBot.java +++ b/juick-api/src/main/java/com/juick/api/TGBot.java @@ -32,7 +32,9 @@ import com.pengrad.telegrambot.response.SendResponse; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; import org.springframework.web.socket.CloseStatus; import org.springframework.web.socket.PingMessage; import org.springframework.web.socket.TextMessage; @@ -40,6 +42,7 @@ import org.springframework.web.socket.WebSocketSession; import org.springframework.web.socket.client.WebSocketConnectionManager; import org.springframework.web.socket.handler.TextWebSocketHandler; +import javax.annotation.PostConstruct; import javax.inject.Inject; import java.io.IOException; import java.util.List; @@ -50,12 +53,15 @@ import static com.juick.formatters.PlainTextFormatter.formatUrl; /** * Created by vt on 12/05/16. */ +@Component public class TGBot extends TextWebSocketHandler { private static final Logger logger = LoggerFactory.getLogger(TGBot.class); private TelegramBot bot; private ObjectMapper ms; + @Value("${telegram_token}") + private String telegramToken; @Inject private TelegramService telegramService; @Inject @@ -65,15 +71,13 @@ public class TGBot extends TextWebSocketHandler { private WebSocketSession session; - private TGBot() { - throw new IllegalStateException(); - } - - public TGBot(String token) { - if (StringUtils.isBlank(token)) { + @PostConstruct + public void init() { + if (StringUtils.isBlank(telegramToken)) { + logger.info("telegram token is not set, exiting"); return; } - bot = TelegramBotAdapter.build(token); + bot = TelegramBotAdapter.build(telegramToken); ms = new ObjectMapper(); ms.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); ms.setSerializationInclusion(JsonInclude.Include.NON_NULL); diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java b/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java index cb5e617d..8bae8e93 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiAppConfiguration.java @@ -36,7 +36,7 @@ import javax.inject.Inject; @EnableScheduling @EnableAsync @PropertySource("classpath:juick.conf") -@ComponentScan(basePackages = "com.juick.api.controllers") +@ComponentScan(basePackages = "com.juick.api") @Import(BaseWebConfiguration.class) public class ApiAppConfiguration extends WebMvcConfigurationSupport { @Inject diff --git a/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java b/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java index 1b2e7850..fa025e56 100644 --- a/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java +++ b/juick-api/src/main/java/com/juick/api/configuration/ApiInitializer.java @@ -32,8 +32,7 @@ public class ApiInitializer extends AbstractAnnotationConfigDispatcherServletIni @Override protected Class[] getRootConfigClasses() { return new Class[]{ - ApiSecurityConfig.class, DataConfiguration.class, - TelegramConfiguration.class, MessengerConfiguration.class + ApiSecurityConfig.class, DataConfiguration.class, MessengerConfiguration.class }; } diff --git a/juick-api/src/main/java/com/juick/api/configuration/TelegramConfiguration.java b/juick-api/src/main/java/com/juick/api/configuration/TelegramConfiguration.java deleted file mode 100644 index c232de96..00000000 --- a/juick-api/src/main/java/com/juick/api/configuration/TelegramConfiguration.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (C) 2008-2017, Juick - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package com.juick.api.configuration; - -import com.juick.api.TGBot; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -/** - * Created by vital on 28.03.2017. - */ -@Configuration -public class TelegramConfiguration { - @Value("${telegram_token}") - private String telegramToken; - @Bean - public TGBot tgBot() { - return new TGBot(telegramToken); - } -} -- cgit v1.2.3