From 9fa3690db68455849ec664cf691387e6f95d92c3 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 26 Aug 2022 12:21:24 +0600 Subject: [PATCH] add DeepLinkBot --- DeepLinksBot/README.md | 9 +++++ DeepLinksBot/build.gradle | 21 +++++++++++ DeepLinksBot/src/main/kotlin/DeepLinksBot.kt | 39 ++++++++++++++++++++ GetMeBot/build.gradle | 2 +- gradle.properties | 6 +-- settings.gradle | 2 + 6 files changed, 75 insertions(+), 4 deletions(-) create mode 100644 DeepLinksBot/README.md create mode 100644 DeepLinksBot/build.gradle create mode 100644 DeepLinksBot/src/main/kotlin/DeepLinksBot.kt diff --git a/DeepLinksBot/README.md b/DeepLinksBot/README.md new file mode 100644 index 0000000..eeb660d --- /dev/null +++ b/DeepLinksBot/README.md @@ -0,0 +1,9 @@ +# DeepLinksBot + +This bot will send you deeplink to this bot when you send some text message and react on the `start` button + +## Launch + +```bash +../gradlew run --args="BOT_TOKEN" +``` diff --git a/DeepLinksBot/build.gradle b/DeepLinksBot/build.gradle new file mode 100644 index 0000000..a18b59d --- /dev/null +++ b/DeepLinksBot/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="DeepLinksBotKt" + + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + + implementation "dev.inmo:tgbotapi:$telegram_bot_api_version" +} diff --git a/DeepLinksBot/src/main/kotlin/DeepLinksBot.kt b/DeepLinksBot/src/main/kotlin/DeepLinksBot.kt new file mode 100644 index 0000000..5614cbc --- /dev/null +++ b/DeepLinksBot/src/main/kotlin/DeepLinksBot.kt @@ -0,0 +1,39 @@ +import dev.inmo.micro_utils.coroutines.subscribeSafelySkippingExceptions +import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions +import dev.inmo.tgbotapi.bot.ktor.telegramBot +import dev.inmo.tgbotapi.extensions.api.bot.getMe +import dev.inmo.tgbotapi.extensions.api.send.reply +import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitDeepLinks +import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.* +import dev.inmo.tgbotapi.extensions.utils.formatting.makeTelegramDeepLink +import dev.inmo.tgbotapi.types.message.textsources.BotCommandTextSource + +/** + * This bot will send you deeplink to this bot when you send some text message and react on the `start` button + */ +suspend fun main(vararg args: String) { + val botToken = args.first() + + telegramBotWithBehaviourAndLongPolling(botToken) { + val me = bot.getMe() + println(me) + + onText( + initialFilter = { it.content.textSources.none { it is BotCommandTextSource } } // excluding messages with commands + ) { + reply(it, makeTelegramDeepLink(me.username, it.content.text)) + } + + onCommand("start", requireOnlyCommandInMessage = true) { // handling of `start` without args + reply(it, "Hi :) Send me any text and I will try hard to create deeplink for you") + } + onDeepLink { (it, deepLink) -> + reply(it, "Ok, I got deep link \"${deepLink}\" in trigger") + } + waitDeepLinks().subscribeSafelyWithoutExceptions(this) { (it, deepLink) -> + reply(it, "Ok, I got deep link \"${deepLink}\" in waiter") + println(triggersHolder.handleableCommandsHolder.handleable) + } + }.second.join() +} diff --git a/GetMeBot/build.gradle b/GetMeBot/build.gradle index 0188724..7f0036c 100644 --- a/GetMeBot/build.gradle +++ b/GetMeBot/build.gradle @@ -11,7 +11,7 @@ buildscript { apply plugin: 'kotlin' apply plugin: 'application' -mainClassName="HelloBotKt" +mainClassName="GetMeBotKt" dependencies { diff --git a/gradle.properties b/gradle.properties index c5519fa..3b2d1db 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.1.1 -micro_utils_version=0.12.1 -serialization_version=1.4.0-RC +telegram_bot_api_version=3.2.0 +micro_utils_version=0.12.4 +serialization_version=1.4.0 ktor_version=2.1.0 diff --git a/settings.gradle b/settings.gradle index 16604cb..529e18e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -6,6 +6,8 @@ include ":HelloBot" include ":GetMeBot" +include ":DeepLinksBot" + include ":FilesLoaderBot" include ":ResenderBot:ResenderBotLib"