From d7a7e7153e38d09b7070dbc1cec6d2d742094e68 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 21 Apr 2023 23:21:15 +0600 Subject: [PATCH] add inline queries sample --- InlineQueriesBot/README.md | 9 +++ InlineQueriesBot/build.gradle | 56 +++++++++++++++ InlineQueriesBot/src/commonMain/kotlin/Bot.kt | 70 +++++++++++++++++++ .../src/jvmMain/kotlin/InlineQueriesBot.kt | 5 ++ .../src/nativeMain/kotlin/InlineQueriesBot.kt | 7 ++ gradle.properties | 2 +- settings.gradle | 2 + 7 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 InlineQueriesBot/README.md create mode 100644 InlineQueriesBot/build.gradle create mode 100644 InlineQueriesBot/src/commonMain/kotlin/Bot.kt create mode 100644 InlineQueriesBot/src/jvmMain/kotlin/InlineQueriesBot.kt create mode 100644 InlineQueriesBot/src/nativeMain/kotlin/InlineQueriesBot.kt diff --git a/InlineQueriesBot/README.md b/InlineQueriesBot/README.md new file mode 100644 index 0000000..4765c04 --- /dev/null +++ b/InlineQueriesBot/README.md @@ -0,0 +1,9 @@ +# RandomFileSenderBot + +This bot will send random file from input folder OR from bot working folder + +## Launch + +```bash +../gradlew run --args="BOT_TOKEN[ optional/folder/path]" +``` diff --git a/InlineQueriesBot/build.gradle b/InlineQueriesBot/build.gradle new file mode 100644 index 0000000..a574fb8 --- /dev/null +++ b/InlineQueriesBot/build.gradle @@ -0,0 +1,56 @@ +buildscript { + repositories { + mavenCentral() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +plugins { + id "org.jetbrains.kotlin.multiplatform" +} + +apply plugin: 'application' + +mainClassName="InlineQueriesBotKt" + + +kotlin { + def hostOs = System.getProperty("os.name") + def isMingwX64 = hostOs.startsWith("Windows") + def nativeTarget + if (hostOs == "Linux") nativeTarget = linuxX64("native") { binaries { executable() } } + else if (isMingwX64) nativeTarget = mingwX64("native") { binaries { executable() } } + else throw new GradleException("Host OS is not supported in Kotlin/Native.") + + jvm() + + sourceSets { + commonMain { + dependencies { + implementation kotlin('stdlib') + + api "dev.inmo:tgbotapi:$telegram_bot_api_version" + } + } + + nativeMain { + dependencies { + def engine + + if (hostOs == "Linux") engine = "curl" + else if (isMingwX64) engine = "winhttp" + else throw new GradleException("Host OS is not supported in Kotlin/Native.") + + api "io.ktor:ktor-client-$engine:$ktor_version" + } + } + } +} + +dependencies { + implementation 'io.ktor:ktor-client-logging-jvm:2.3.0' +} + diff --git a/InlineQueriesBot/src/commonMain/kotlin/Bot.kt b/InlineQueriesBot/src/commonMain/kotlin/Bot.kt new file mode 100644 index 0000000..64cbcb5 --- /dev/null +++ b/InlineQueriesBot/src/commonMain/kotlin/Bot.kt @@ -0,0 +1,70 @@ +import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions +import dev.inmo.tgbotapi.extensions.api.answers.answer +import dev.inmo.tgbotapi.extensions.api.bot.getMe +import dev.inmo.tgbotapi.extensions.api.send.reply +import dev.inmo.tgbotapi.extensions.api.telegramBot +import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onBaseInlineQuery +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onDeepLink +import dev.inmo.tgbotapi.requests.answers.InlineQueryResultsButton +import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResultArticle +import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputTextMessageContent +import dev.inmo.tgbotapi.types.inlineQueryAnswerResultsLimit +import dev.inmo.tgbotapi.utils.buildEntities + +/** + * This bot will send files inside of working directory OR from directory in the second argument. + * You may send /send_file command to this bot to get random file from the directory OR + * `/send_file $number` when you want to receive required number of files. For example, + * /send_file and `/send_file 1` will have the same effect - bot will send one random file. + * But if you will send `/send_file 5` it will choose 5 random files and send them as group + */ +suspend fun doInlineQueriesBot(token: String) { + val bot = telegramBot(token) + + bot.buildBehaviourWithLongPolling( + defaultExceptionsHandler = { it.printStackTrace() }, + ) { + onBaseInlineQuery { + val page = it.offset.toIntOrNull() ?: 0 + val results = (0 until inlineQueryAnswerResultsLimit.last).map { + (page * inlineQueryAnswerResultsLimit.last) + it + } + + answer( + it, + results = results.map { resultNumber -> + val resultAsString = resultNumber.toString() + InlineQueryResultArticle( + resultAsString, + "Title $resultNumber", + InputTextMessageContent( + buildEntities { + +"Result text of " + resultNumber.toString() + " result:\n" + +it.query + } + ), + description = "Description of $resultNumber result" + ) + }, + cachedTime = 0, + isPersonal = true, + button = InlineQueryResultsButton.Start( + "Text of button with page $page", + "deep_link_for_page_$page" + ), + nextOffset = (page + 1).toString() + ) + } + + onDeepLink { (message, deepLink) -> + reply(message, deepLink) + } + + allUpdatesFlow.subscribeSafelyWithoutExceptions(this) { + println(it) + } + + println(getMe()) + }.join() +} diff --git a/InlineQueriesBot/src/jvmMain/kotlin/InlineQueriesBot.kt b/InlineQueriesBot/src/jvmMain/kotlin/InlineQueriesBot.kt new file mode 100644 index 0000000..2d20c79 --- /dev/null +++ b/InlineQueriesBot/src/jvmMain/kotlin/InlineQueriesBot.kt @@ -0,0 +1,5 @@ +import dev.inmo.micro_utils.common.MPPFile + +suspend fun main(args: Array) { + doInlineQueriesBot(args.first()) +} diff --git a/InlineQueriesBot/src/nativeMain/kotlin/InlineQueriesBot.kt b/InlineQueriesBot/src/nativeMain/kotlin/InlineQueriesBot.kt new file mode 100644 index 0000000..8daeb70 --- /dev/null +++ b/InlineQueriesBot/src/nativeMain/kotlin/InlineQueriesBot.kt @@ -0,0 +1,7 @@ +import kotlinx.coroutines.runBlocking + +fun main(args: Array) { + runBlocking { + doInlineQueriesBot(args.first()) + } +} diff --git a/gradle.properties b/gradle.properties index 823884b..b2a959c 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ org.gradle.jvmargs=-Xmx2g kotlin_version=1.8.20 -telegram_bot_api_version=7.0.2 +telegram_bot_api_version=7.1.0-branch_7.1.0-build1602 micro_utils_version=0.17.8 serialization_version=1.5.0 ktor_version=2.3.0 diff --git a/settings.gradle b/settings.gradle index a665bb3..a76f386 100644 --- a/settings.gradle +++ b/settings.gradle @@ -37,3 +37,5 @@ include ":RightsChangerBot" include ":LiveLocationsBot" include ":StickerSetHandler" + +include ":InlineQueriesBot"