From 068dc79ac8463a17ed4c64f68effecc2d5310402 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 13 Aug 2022 14:39:24 +0600 Subject: [PATCH] migration onto 3.1.0 and including new example --- .../src/commonMain/kotlin/ResenderBot.kt | 4 +- StickerInfoBot/StickerInfoBotLib/build.gradle | 33 +++++++++ .../src/commonMain/kotlin/StickerInfoBot.kt | 71 +++++++++++++++++++ .../src/jsMain/kotlin/StickerInfoBot.kt | 32 +++++++++ .../src/jsMain/resources/index.html | 16 +++++ StickerInfoBot/jvm_launcher/build.gradle | 21 ++++++ .../src/main/kotlin/StickerInfoBotBotJvm.kt | 5 ++ gradle.properties | 6 +- settings.gradle | 3 + 9 files changed, 187 insertions(+), 4 deletions(-) create mode 100644 StickerInfoBot/StickerInfoBotLib/build.gradle create mode 100644 StickerInfoBot/StickerInfoBotLib/src/commonMain/kotlin/StickerInfoBot.kt create mode 100644 StickerInfoBot/StickerInfoBotLib/src/jsMain/kotlin/StickerInfoBot.kt create mode 100644 StickerInfoBot/StickerInfoBotLib/src/jsMain/resources/index.html create mode 100644 StickerInfoBot/jvm_launcher/build.gradle create mode 100644 StickerInfoBot/jvm_launcher/src/main/kotlin/StickerInfoBotBotJvm.kt diff --git a/ResenderBot/ResenderBotLib/src/commonMain/kotlin/ResenderBot.kt b/ResenderBot/ResenderBotLib/src/commonMain/kotlin/ResenderBot.kt index 5791db6..1eb6d17 100644 --- a/ResenderBot/ResenderBotLib/src/commonMain/kotlin/ResenderBot.kt +++ b/ResenderBot/ResenderBotLib/src/commonMain/kotlin/ResenderBot.kt @@ -25,7 +25,9 @@ suspend fun activateResenderBot( ) { val chat = it.chat withTypingAction(chat) { - executeUnsafe(it.content.createResend(chat.id, replyToMessageId = it.messageId)) + executeUnsafe(it.content.createResend(chat.id, replyToMessageId = it.messageId)) { + it.forEach(print) + } } } onVisualGallery { diff --git a/StickerInfoBot/StickerInfoBotLib/build.gradle b/StickerInfoBot/StickerInfoBotLib/build.gradle new file mode 100644 index 0000000..1d40c3e --- /dev/null +++ b/StickerInfoBot/StickerInfoBotLib/build.gradle @@ -0,0 +1,33 @@ +buildscript { + repositories { + mavenCentral() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +plugins { + id "org.jetbrains.kotlin.multiplatform" +} + + +kotlin { + jvm() +// js(LEGACY) { + js(IR) { + browser() + binaries.executable() + } + + sourceSets { + commonMain { + dependencies { + implementation kotlin('stdlib') + + api "dev.inmo:tgbotapi:$telegram_bot_api_version" + } + } + } +} diff --git a/StickerInfoBot/StickerInfoBotLib/src/commonMain/kotlin/StickerInfoBot.kt b/StickerInfoBot/StickerInfoBotLib/src/commonMain/kotlin/StickerInfoBot.kt new file mode 100644 index 0000000..c6f4e08 --- /dev/null +++ b/StickerInfoBot/StickerInfoBotLib/src/commonMain/kotlin/StickerInfoBot.kt @@ -0,0 +1,71 @@ +import dev.inmo.micro_utils.coroutines.defaultSafelyWithoutExceptionHandler +import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions +import dev.inmo.tgbotapi.extensions.api.bot.getMe +import dev.inmo.tgbotapi.bot.ktor.telegramBot +import dev.inmo.tgbotapi.extensions.api.get.getCustomEmojiStickerOrNull +import dev.inmo.tgbotapi.extensions.api.get.getStickerSet +import dev.inmo.tgbotapi.extensions.api.send.* +import dev.inmo.tgbotapi.extensions.behaviour_builder.* +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.* +import dev.inmo.tgbotapi.extensions.utils.formatting.* +import dev.inmo.tgbotapi.types.StickerType +import dev.inmo.tgbotapi.types.message.textsources.* +import dev.inmo.tgbotapi.types.stickers.StickerSet +import kotlinx.coroutines.* + +fun StickerSet.buildInfo() = buildEntities { + bold("StickerSet name: ") + "${name}\n" + bold("StickerSet title: ") + "${title}\n" + bold( + when (stickerType) { + StickerType.CustomEmoji -> "Custom emoji" + StickerType.Mask -> "Mask" + StickerType.Regular -> "Regular" + is StickerType.Unknown -> "Unknown type \"${stickerType.type}\"" + } + ) + " sticker set with title " + bold(title) + " and name " + bold(name) +} + +suspend fun activateStickerInfoBot( + token: String, + print: (Any) -> Unit +) { + val bot = telegramBot(token) + + print(bot.getMe()) + + defaultSafelyWithoutExceptionHandler = { + it.printStackTrace() + } + + bot.buildBehaviourWithLongPolling(CoroutineScope(currentCoroutineContext() + SupervisorJob())) { + onText { + withTypingAction(it.chat) { + it.content.textSources.mapNotNull { + if (it is CustomEmojiTextSource) { + getCustomEmojiStickerOrNull(it.customEmojiId) ?.stickerSetName + } else { + null + } + }.distinct().map { + getStickerSet(it) + }.distinct().flatMap { + it.buildInfo() + regular("\n") + }.separateForText().map { entities -> + reply(it, entities) + } + } + } + onSticker { + val stickerSetInfo = getStickerSet(it.content.media) + reply( + it, + stickerSetInfo.buildInfo() + ) + } + + allUpdatesFlow.subscribeSafelyWithoutExceptions(this) { + println(it) + } + }.join() +} diff --git a/StickerInfoBot/StickerInfoBotLib/src/jsMain/kotlin/StickerInfoBot.kt b/StickerInfoBot/StickerInfoBotLib/src/jsMain/kotlin/StickerInfoBot.kt new file mode 100644 index 0000000..5474eaf --- /dev/null +++ b/StickerInfoBot/StickerInfoBotLib/src/jsMain/kotlin/StickerInfoBot.kt @@ -0,0 +1,32 @@ +import kotlinx.browser.document +import kotlinx.coroutines.* +import org.w3c.dom.* + +private val scope = CoroutineScope(Dispatchers.Default) + +fun main() { + document.addEventListener( + "DOMContentLoaded", + { + val botsContainer = document.getElementById("bots_container") ?: return@addEventListener + + (document.getElementById("bot_token_form") as? HTMLFormElement) ?.onsubmit = { + (document.getElementById("bot_token") as? HTMLInputElement) ?.value ?.let { token -> + val botContainer = document.createElement("div") as HTMLDivElement + botsContainer.append(botContainer) + + val infoDiv = document.createElement("div") as HTMLDivElement + botContainer.append(infoDiv) + + scope.launch { + activateStickerInfoBot(token) { + infoDiv.innerHTML = it.toString() + } + } + } + + false + } + } + ) +} diff --git a/StickerInfoBot/StickerInfoBotLib/src/jsMain/resources/index.html b/StickerInfoBot/StickerInfoBotLib/src/jsMain/resources/index.html new file mode 100644 index 0000000..a329509 --- /dev/null +++ b/StickerInfoBot/StickerInfoBotLib/src/jsMain/resources/index.html @@ -0,0 +1,16 @@ + + + + + Resender bot + + +
+ + +
+
Type your bot token to the input above to start its work
+ +
+ + diff --git a/StickerInfoBot/jvm_launcher/build.gradle b/StickerInfoBot/jvm_launcher/build.gradle new file mode 100644 index 0000000..fa0e12b --- /dev/null +++ b/StickerInfoBot/jvm_launcher/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="StickerInfoBotJvmKt" + + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + + implementation project(":StickerInfoBot:StickerInfoBotLib") +} diff --git a/StickerInfoBot/jvm_launcher/src/main/kotlin/StickerInfoBotBotJvm.kt b/StickerInfoBot/jvm_launcher/src/main/kotlin/StickerInfoBotBotJvm.kt new file mode 100644 index 0000000..2beb19c --- /dev/null +++ b/StickerInfoBot/jvm_launcher/src/main/kotlin/StickerInfoBotBotJvm.kt @@ -0,0 +1,5 @@ +suspend fun main(args: Array) { + activateStickerInfoBot(args.first()) { + println(it) + } +} diff --git a/gradle.properties b/gradle.properties index 1240109..3d9349a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ org.gradle.jvmargs=-Xmx768m kotlin_version=1.7.10 -telegram_bot_api_version=3.0.2 -micro_utils_version=0.12.0 +telegram_bot_api_version=3.1.0 +micro_utils_version=0.12.1 serialization_version=1.4.0-RC -ktor_version=2.0.3 +ktor_version=2.1.0 diff --git a/settings.gradle b/settings.gradle index d6102d8..16604cb 100644 --- a/settings.gradle +++ b/settings.gradle @@ -14,6 +14,9 @@ include ":ResenderBot:jvm_launcher" include ":KeyboardsBot:KeyboardsBotLib" include ":KeyboardsBot:jvm_launcher" +include ":StickerInfoBot:StickerInfoBotLib" +include ":StickerInfoBot:jvm_launcher" + include ":SlotMachineDetectorBot" include ":WebApp"