From 0f2829945fb4741782867e73419b55b478be1233 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 31 Dec 2022 15:45:48 +0600 Subject: [PATCH] add topics example --- TopicsHandling/README.md | 9 ++ TopicsHandling/build.gradle | 21 +++ .../src/main/kotlin/TopicsHandling.kt | 145 ++++++++++++++++++ gradle.properties | 2 +- settings.gradle | 2 + 5 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 TopicsHandling/README.md create mode 100644 TopicsHandling/build.gradle create mode 100644 TopicsHandling/src/main/kotlin/TopicsHandling.kt diff --git a/TopicsHandling/README.md b/TopicsHandling/README.md new file mode 100644 index 0000000..660efad --- /dev/null +++ b/TopicsHandling/README.md @@ -0,0 +1,9 @@ +# HelloBot + +The main purpose of this bot is just to answer "Oh, hi, " and add user mention here + +## Launch + +```bash +../gradlew run --args="BOT_TOKEN" +``` diff --git a/TopicsHandling/build.gradle b/TopicsHandling/build.gradle new file mode 100644 index 0000000..2006fb3 --- /dev/null +++ b/TopicsHandling/build.gradle @@ -0,0 +1,21 @@ +buildscript { + repositories { + mavenCentral() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +apply plugin: 'kotlin' +apply plugin: 'application' + +mainClassName="TopicsHandlingKt" + + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + + implementation "dev.inmo:tgbotapi:$telegram_bot_api_version" +} diff --git a/TopicsHandling/src/main/kotlin/TopicsHandling.kt b/TopicsHandling/src/main/kotlin/TopicsHandling.kt new file mode 100644 index 0000000..d13a2d2 --- /dev/null +++ b/TopicsHandling/src/main/kotlin/TopicsHandling.kt @@ -0,0 +1,145 @@ +import com.benasher44.uuid.uuid4 +import dev.inmo.micro_utils.common.repeatOnFailure +import dev.inmo.micro_utils.coroutines.runCatchingSafely +import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions +import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands +import dev.inmo.tgbotapi.extensions.api.chat.forum.closeForumTopic +import dev.inmo.tgbotapi.extensions.api.chat.forum.closeGeneralForumTopic +import dev.inmo.tgbotapi.extensions.api.chat.forum.createForumTopic +import dev.inmo.tgbotapi.extensions.api.chat.forum.deleteForumTopic +import dev.inmo.tgbotapi.extensions.api.chat.forum.editForumTopic +import dev.inmo.tgbotapi.extensions.api.chat.forum.editGeneralForumTopic +import dev.inmo.tgbotapi.extensions.api.chat.forum.hideGeneralForumTopic +import dev.inmo.tgbotapi.extensions.api.chat.forum.reopenForumTopic +import dev.inmo.tgbotapi.extensions.api.chat.forum.reopenGeneralForumTopic +import dev.inmo.tgbotapi.extensions.api.chat.forum.unhideGeneralForumTopic +import dev.inmo.tgbotapi.extensions.api.send.reply +import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand +import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.flushAccumulatedUpdates +import dev.inmo.tgbotapi.types.BotCommand +import dev.inmo.tgbotapi.types.ForumTopic +import dev.inmo.tgbotapi.types.commands.BotCommandScope +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay + +suspend fun main(vararg args: String) { + telegramBotWithBehaviourAndLongPolling( + args.first(), + CoroutineScope(Dispatchers.Default), + defaultExceptionsHandler = { + it.printStackTrace() + } + ) { + flushAccumulatedUpdates() + allUpdatesFlow.subscribeSafelyWithoutExceptions(this) { + println(it) + } + onCommand("start_test_topics") { + val forumTopic = createForumTopic( + it.chat, + "Test", + ForumTopic.GREEN + ) + + reply(it, "Test topic has been created") + + delay(1000L) + editForumTopic( + it.chat.id, + forumTopic.messageThreadId, + "Test 01" + ) + + reply(it, "Test topic has changed its name to Test 01") + + delay(1000L) + closeForumTopic( + it.chat.id, + forumTopic.messageThreadId, + ) + + reply(it, "Test topic has been closed") + + delay(1000L) + reopenForumTopic( + it.chat.id, + forumTopic.messageThreadId, + ) + + reply(it, "Test topic has been reopened") + + delay(1000L) + deleteForumTopic( + it.chat.id, + forumTopic.messageThreadId, + ) + + reply(it, "Test topic has been deleted") + + delay(1000L) + hideGeneralForumTopic( + it.chat.id, + ) + + reply(it, "General topic has been hidden") + + delay(1000L) + unhideGeneralForumTopic( + it.chat.id + ) + + reply(it, "General topic has been shown") + + delay(1000L) + runCatchingSafely( + { _ -> + reopenGeneralForumTopic( + it.chat.id + ) + + closeGeneralForumTopic( + it.chat.id + ) + } + ) { + closeGeneralForumTopic( + it.chat.id + ) + } + + reply(it, "General topic has been closed") + + delay(1000L) + reopenGeneralForumTopic( + it.chat.id + ) + + reply(it, "General topic has been opened") + + delay(1000L) + editGeneralForumTopic( + it.chat.id, + uuid4().toString().take(10) + ) + + reply(it, "General topic has been renamed") + + delay(1000L) + editGeneralForumTopic( + it.chat.id, + "Main topic" + ) + + reply(it, "General topic has been renamed") + + delay(1000L) + } + + setMyCommands( + BotCommand("start_test_topics", "start test topics"), + scope = BotCommandScope.AllGroupChats + ) + }.second.join() +} diff --git a/gradle.properties b/gradle.properties index 946f855..ef854d5 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ org.gradle.jvmargs=-Xmx768m kotlin_version=1.7.22 -telegram_bot_api_version=5.0.0-branch_5.0.0-build1340 +telegram_bot_api_version=5.0.0-branch_5.0.0-build1342 micro_utils_version=0.16.4 serialization_version=1.4.1 ktor_version=2.2.1 diff --git a/settings.gradle b/settings.gradle index 4d94aff..5bad889 100644 --- a/settings.gradle +++ b/settings.gradle @@ -26,3 +26,5 @@ include ":ChatAvatarSetter" include ":WebApp" include ":FSMBot" + +include ":TopicsHandling"