diff --git a/.github/workflows/mvn.yaml b/.github/workflows/mvn.yaml index 145b232..9d7cbc9 100644 --- a/.github/workflows/mvn.yaml +++ b/.github/workflows/mvn.yaml @@ -13,7 +13,7 @@ jobs: strategy: matrix: os: [ubuntu-20.04, macos-12] - java: [11, 17] + java: [17, 20] runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v2 diff --git a/.gitignore b/.gitignore index 629fde0..1e9291f 100644 --- a/.gitignore +++ b/.gitignore @@ -24,7 +24,7 @@ hs_err_pid* # for IntelliJidea users .idea - +*.iml **/.DS_Store target diff --git a/LICENSE b/LICENSE.txt similarity index 88% rename from LICENSE rename to LICENSE.txt index 4821d65..b7bba72 100644 --- a/LICENSE +++ b/LICENSE.txt @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Ivan Ivanchuk +Copyright (c) 2023 Blamer.io Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -14,8 +14,8 @@ copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +SOFTWARE. \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..36c850c --- /dev/null +++ b/pom.xml @@ -0,0 +1,157 @@ + + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.0.6 + + + io.blamer + bot + 0.0.1-SNAPSHOT + bot + Blamer telegram bot service + + 17 + 6.5.0 + 0.8.9 + 0.1.11 + 0.1.5 + 3.2.2 + 8.15 + 3.0.0 + + + + org.springframework.boot + spring-boot-starter-rsocket + + + org.springframework.boot + spring-boot-starter-webflux + + + org.telegram + telegrambots-spring-boot-starter + ${telegrambots-spring-boot-starter.version} + + + org.springframework.boot + spring-boot-devtools + runtime + true + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.projectlombok + lombok + true + + + org.springframework.boot + spring-boot-starter-test + test + + + io.projectreactor + reactor-test + test + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.projectlombok + lombok + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + 6018071095:AAEUVFjD3U3jNp1atDfVdJ2qxUsdkprCfMU + blamer_dev_io_bot + + + + + org.jacoco + jacoco-maven-plugin + ${jacoco-maven-plugin.version} + + + jacoco-initialize + + prepare-agent + + + + jacoco-check + test + + check + report + + + + + + + + + com.github.volodya-lombrozo + jtcop-maven-plugin + ${jtcop-maven-plugin.version} + + + + check + + + + + + ru.l3r8y + sa-tan + ${sa-tan.version} + + + + diff --git a/src/main/java/io/blamer/bot/BotApplication.java b/src/main/java/io/blamer/bot/BotApplication.java new file mode 100644 index 0000000..ce2c162 --- /dev/null +++ b/src/main/java/io/blamer/bot/BotApplication.java @@ -0,0 +1,37 @@ +/* + * MIT License + * + * Copyright (c) Copyright (c) 2023 Blamer.io + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package io.blamer.bot; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class BotApplication { + + public static void main(final String[] args) { + SpringApplication.run(BotApplication.class, args); + } + +} diff --git a/src/main/java/io/blamer/bot/bot/Bot.java b/src/main/java/io/blamer/bot/bot/Bot.java new file mode 100644 index 0000000..c8ab171 --- /dev/null +++ b/src/main/java/io/blamer/bot/bot/Bot.java @@ -0,0 +1,83 @@ +/* + * MIT License + * + * Copyright (c) Copyright (c) 2023 Blamer.io + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package io.blamer.bot.bot; + +import io.blamer.bot.configuration.BotConfiguration; +import lombok.SneakyThrows; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.telegram.telegrambots.bots.TelegramLongPollingBot; +import org.telegram.telegrambots.meta.api.methods.commands.SetMyCommands; +import org.telegram.telegrambots.meta.api.methods.send.SendMessage; +import org.telegram.telegrambots.meta.api.objects.Update; +import org.telegram.telegrambots.meta.api.objects.commands.scope.BotCommandScopeDefault; +import org.telegram.telegrambots.meta.exceptions.TelegramApiException; + +@Slf4j +@Service +public class Bot extends TelegramLongPollingBot { + + private final BotConfiguration configuration; + + public Bot(BotConfiguration configuration) throws TelegramApiException { + this.configuration = configuration; + this.execute( + new SetMyCommands( + configuration.commands(), + new BotCommandScopeDefault(), + null) + ); + } + + @SneakyThrows + @Override + public void onUpdateReceived(final Update update) { + if (update.hasMessage() && update.getMessage().hasText()) { + final String msg = update.getMessage().getText(); + final long chat = update.getMessage().getChatId(); + /* + * @todo #1 Remove this test implementation. + * Need to manage message command handling like it's described here: + * https://www.youtube.com/watch?v=61duchvKI6o&t=2349s + * */ + if (msg.equals("/start")) { + final SendMessage message = new SendMessage(); + message.setChatId(chat); + message.setText("Hi, I'm my name is Blamer!"); + this.execute(message); + } + } + } + + @Override + public String getBotUsername() { + return this.configuration.getName(); + } + + @Override + public String getBotToken() { + return this.configuration.getToken(); + } +} diff --git a/src/main/java/io/blamer/bot/bot/BotInitializer.java b/src/main/java/io/blamer/bot/bot/BotInitializer.java new file mode 100644 index 0000000..9d6d446 --- /dev/null +++ b/src/main/java/io/blamer/bot/bot/BotInitializer.java @@ -0,0 +1,46 @@ +/* + * MIT License + * + * Copyright (c) Copyright (c) 2023 Blamer.io + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package io.blamer.bot.bot; + +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 BotInitializer { + + private final Bot bot; + + @EventListener({ContextRefreshedEvent.class}) + public void initializeBot() throws TelegramApiException { + final TelegramBotsApi api = new TelegramBotsApi(DefaultBotSession.class); + api.registerBot(this.bot); + } +} diff --git a/src/main/java/io/blamer/bot/bot/package-info.java b/src/main/java/io/blamer/bot/bot/package-info.java new file mode 100644 index 0000000..27c1fd9 --- /dev/null +++ b/src/main/java/io/blamer/bot/bot/package-info.java @@ -0,0 +1,28 @@ +/* + * MIT License + * + * Copyright (c) Copyright (c) 2023 Blamer.io + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * Telegram bot package. + */ +package io.blamer.bot.bot; diff --git a/src/main/java/io/blamer/bot/configuration/BotConfiguration.java b/src/main/java/io/blamer/bot/configuration/BotConfiguration.java new file mode 100644 index 0000000..8a90ffa --- /dev/null +++ b/src/main/java/io/blamer/bot/configuration/BotConfiguration.java @@ -0,0 +1,81 @@ +/* + * MIT License + * + * Copyright (c) Copyright (c) 2023 Blamer.io + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package io.blamer.bot.configuration; + +import jakarta.annotation.PostConstruct; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.telegram.telegrambots.meta.api.objects.commands.BotCommand; + +import java.util.List; + +/** + * Configuration for {@link io.blamer.bot.bot.Bot}. + * Reads bot properties from application.yaml. + * + * @since 0.0.0 + */ +@Slf4j +@Data +@Configuration +@ConfigurationProperties("bot") +public class BotConfiguration { + + /** + * Token for Telegram Bots API. + */ + private String token; + + /** + * Username in telegram. + */ + private String name; + + /** + * A method for checking which configuration is loaded. + */ + @PostConstruct + void init() { + BotConfiguration.log.info( + "Bot configuration for '{}' loaded with token '{}'", + this.name, + this.token + ); + } + + /** + * Commands of bot with name and description. + * + * @return Description of commands as List + */ + public List commands() { + return List.of( + new BotCommand("/start", "Just starting command"), + new BotCommand("/token", "Set GitHub token to get updates") + ); + } +} diff --git a/src/main/java/io/blamer/bot/configuration/package-info.java b/src/main/java/io/blamer/bot/configuration/package-info.java new file mode 100644 index 0000000..759261e --- /dev/null +++ b/src/main/java/io/blamer/bot/configuration/package-info.java @@ -0,0 +1,28 @@ +/* + * MIT License + * + * Copyright (c) Copyright (c) 2023 Blamer.io + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * Telegram bot configuration package. + */ +package io.blamer.bot.configuration; diff --git a/src/main/java/io/blamer/bot/package-info.java b/src/main/java/io/blamer/bot/package-info.java new file mode 100644 index 0000000..56fa319 --- /dev/null +++ b/src/main/java/io/blamer/bot/package-info.java @@ -0,0 +1,28 @@ +/* + * MIT License + * + * Copyright (c) Copyright (c) 2023 Blamer.io + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * Root BotApplication package. + */ +package io.blamer.bot; diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml new file mode 100644 index 0000000..acd5675 --- /dev/null +++ b/src/main/resources/application.yaml @@ -0,0 +1,3 @@ +bot: + token: ${BOT_TOKEN} + name: ${BOT_NAME} diff --git a/src/test/java/io/blamer/bot/BotApplicationTest.java b/src/test/java/io/blamer/bot/BotApplicationTest.java new file mode 100644 index 0000000..735132f --- /dev/null +++ b/src/test/java/io/blamer/bot/BotApplicationTest.java @@ -0,0 +1,13 @@ +package io.blamer.bot; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class BotApplicationTest { + + @Test + void loadsContext() { + } + +}