mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-11-22 16:23:54 +00:00
add inline queries sample
This commit is contained in:
parent
0b37acb7a9
commit
d7a7e7153e
9
InlineQueriesBot/README.md
Normal file
9
InlineQueriesBot/README.md
Normal file
@ -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]"
|
||||||
|
```
|
56
InlineQueriesBot/build.gradle
Normal file
56
InlineQueriesBot/build.gradle
Normal file
@ -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'
|
||||||
|
}
|
||||||
|
|
70
InlineQueriesBot/src/commonMain/kotlin/Bot.kt
Normal file
70
InlineQueriesBot/src/commonMain/kotlin/Bot.kt
Normal file
@ -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()
|
||||||
|
}
|
5
InlineQueriesBot/src/jvmMain/kotlin/InlineQueriesBot.kt
Normal file
5
InlineQueriesBot/src/jvmMain/kotlin/InlineQueriesBot.kt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
import dev.inmo.micro_utils.common.MPPFile
|
||||||
|
|
||||||
|
suspend fun main(args: Array<String>) {
|
||||||
|
doInlineQueriesBot(args.first())
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
|
|
||||||
|
fun main(args: Array<String>) {
|
||||||
|
runBlocking {
|
||||||
|
doInlineQueriesBot(args.first())
|
||||||
|
}
|
||||||
|
}
|
@ -5,7 +5,7 @@ org.gradle.jvmargs=-Xmx2g
|
|||||||
|
|
||||||
|
|
||||||
kotlin_version=1.8.20
|
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
|
micro_utils_version=0.17.8
|
||||||
serialization_version=1.5.0
|
serialization_version=1.5.0
|
||||||
ktor_version=2.3.0
|
ktor_version=2.3.0
|
||||||
|
@ -37,3 +37,5 @@ include ":RightsChangerBot"
|
|||||||
include ":LiveLocationsBot"
|
include ":LiveLocationsBot"
|
||||||
|
|
||||||
include ":StickerSetHandler"
|
include ":StickerSetHandler"
|
||||||
|
|
||||||
|
include ":InlineQueriesBot"
|
||||||
|
Loading…
Reference in New Issue
Block a user