mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-12-22 08:37:18 +00:00
commit
acb382d3f7
@ -1,16 +1,5 @@
|
||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.closeForumTopic
|
||||
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.reopenForumTopic
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onForumTopicClosed
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.CustomEmojiId
|
||||
import dev.inmo.tgbotapi.types.ForumTopic
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
/**
|
||||
* This is one of the most easiest bot - it will just print information about itself
|
||||
|
9
InlineQueriesBot/README.md
Normal file
9
InlineQueriesBot/README.md
Normal file
@ -0,0 +1,9 @@
|
||||
# InlineQueriesBot
|
||||
|
||||
This bot will form the inline queries for you. For that feature you should explicitly enable inline queries in bot settings
|
||||
|
||||
## Launch
|
||||
|
||||
```bash
|
||||
../gradlew run --args="BOT_TOKEN"
|
||||
```
|
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'
|
||||
}
|
||||
|
67
InlineQueriesBot/src/commonMain/kotlin/Bot.kt
Normal file
67
InlineQueriesBot/src/commonMain/kotlin/Bot.kt
Normal file
@ -0,0 +1,67 @@
|
||||
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
|
||||
|
||||
/**
|
||||
* Thi bot will create inline query answers. You
|
||||
* should enable inline queries in bot settings
|
||||
*/
|
||||
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())
|
||||
}
|
||||
}
|
@ -4,12 +4,17 @@ import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||
import dev.inmo.tgbotapi.extensions.api.edit.edit
|
||||
import dev.inmo.tgbotapi.extensions.api.edit.editMessageText
|
||||
import dev.inmo.tgbotapi.extensions.api.edit.reply_markup.editMessageReplyMarkup
|
||||
import dev.inmo.tgbotapi.extensions.api.edit.text.editMessageText
|
||||
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.types.buttons.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.withContent
|
||||
import dev.inmo.tgbotapi.types.BotCommand
|
||||
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResultArticle
|
||||
import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputTextMessageContent
|
||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||
import dev.inmo.tgbotapi.utils.*
|
||||
import kotlinx.coroutines.*
|
||||
@ -55,6 +60,16 @@ fun InlineKeyboardBuilder.includePageButtons(page: Int, count: Int) {
|
||||
dataButton(">>", "$count $count")
|
||||
}
|
||||
}
|
||||
row {
|
||||
inlineQueryInChosenChatButton(
|
||||
"Send somebody page",
|
||||
query = "$page $count",
|
||||
allowUsers = true,
|
||||
allowBots = true,
|
||||
allowGroups = true,
|
||||
allowChannels = true,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun activateKeyboardsBot(
|
||||
@ -71,10 +86,8 @@ suspend fun activateKeyboardsBot(
|
||||
reply(
|
||||
message,
|
||||
replyMarkup = inlineKeyboard {
|
||||
row {
|
||||
includePageButtons(1, numberOfPages)
|
||||
}
|
||||
}
|
||||
) {
|
||||
regular("Your inline keyboard with $numberOfPages pages")
|
||||
}
|
||||
@ -92,15 +105,48 @@ suspend fun activateKeyboardsBot(
|
||||
return@onMessageDataCallbackQuery
|
||||
},
|
||||
replyMarkup = inlineKeyboard {
|
||||
row {
|
||||
includePageButtons(page, count)
|
||||
}
|
||||
}
|
||||
) {
|
||||
regular("This is $page of $count")
|
||||
}
|
||||
answer(it)
|
||||
}
|
||||
onInlineMessageIdDataCallbackQuery {
|
||||
val (page, count) = it.data.parsePageAndCount() ?: it.let {
|
||||
answer(it, "Unsupported data :(")
|
||||
return@onInlineMessageIdDataCallbackQuery
|
||||
}
|
||||
|
||||
editMessageText(
|
||||
it.inlineMessageId,
|
||||
replyMarkup = inlineKeyboard {
|
||||
includePageButtons(page, count)
|
||||
}
|
||||
) {
|
||||
regular("This is $page of $count")
|
||||
}
|
||||
answer(it)
|
||||
}
|
||||
|
||||
onBaseInlineQuery {
|
||||
val page = it.query.takeWhile { it.isDigit() }.toIntOrNull() ?: return@onBaseInlineQuery
|
||||
val count = it.query.removePrefix(page.toString()).dropWhile { !it.isDigit() }.takeWhile { it.isDigit() }.toIntOrNull() ?: return@onBaseInlineQuery
|
||||
|
||||
answer(
|
||||
it,
|
||||
results = listOf(
|
||||
InlineQueryResultArticle(
|
||||
it.query,
|
||||
"Send buttons",
|
||||
InputTextMessageContent("It is sent via inline mode inline buttons"),
|
||||
replyMarkup = inlineKeyboard {
|
||||
includePageButtons(page, count)
|
||||
}
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
onUnhandledCommand {
|
||||
reply(
|
||||
|
@ -1,13 +1,16 @@
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.micro_utils.ktor.server.createKtorServer
|
||||
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
||||
import dev.inmo.tgbotapi.extensions.api.answers.answerInlineQuery
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||
import dev.inmo.tgbotapi.extensions.api.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.makeTelegramStartattach
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
||||
import dev.inmo.tgbotapi.requests.answers.InlineQueryResultsButton
|
||||
import dev.inmo.tgbotapi.types.BotCommand
|
||||
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResultArticle
|
||||
import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputTextMessageContent
|
||||
@ -73,6 +76,7 @@ suspend fun main(vararg args: String) {
|
||||
bot.buildBehaviourWithLongPolling(
|
||||
defaultExceptionsHandler = { it.printStackTrace() }
|
||||
) {
|
||||
val me = getMe()
|
||||
onCommand("reply_markup") {
|
||||
reply(
|
||||
it,
|
||||
@ -97,6 +101,27 @@ suspend fun main(vararg args: String) {
|
||||
|
||||
)
|
||||
}
|
||||
onCommand("attachment_menu") {
|
||||
reply(
|
||||
it,
|
||||
"Button",
|
||||
replyMarkup = inlineKeyboard {
|
||||
row {
|
||||
webAppButton("Open WebApp", WebAppInfo(args[1]))
|
||||
}
|
||||
}
|
||||
|
||||
)
|
||||
}
|
||||
onBaseInlineQuery {
|
||||
answerInlineQuery(
|
||||
it,
|
||||
button = InlineQueryResultsButton.invoke(
|
||||
"Open webApp",
|
||||
WebAppInfo(args[1])
|
||||
)
|
||||
)
|
||||
}
|
||||
onUnhandledCommand {
|
||||
reply(
|
||||
it,
|
||||
@ -106,6 +131,9 @@ suspend fun main(vararg args: String) {
|
||||
}
|
||||
)
|
||||
}
|
||||
onWriteAccessAllowed(initialFilter = { it.chatEvent.webAppName != null }) {
|
||||
send(it.chat, "Thanks for adding ${it.chatEvent.webAppName} to the attachment menu")
|
||||
}
|
||||
setMyCommands(
|
||||
BotCommand("reply_markup", "Use to get reply markup keyboard with web app trigger"),
|
||||
BotCommand("inline", "Use to get inline keyboard with web app trigger"),
|
||||
|
@ -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
|
||||
micro_utils_version=0.17.8
|
||||
serialization_version=1.5.0
|
||||
ktor_version=2.3.0
|
||||
|
@ -37,3 +37,5 @@ include ":RightsChangerBot"
|
||||
include ":LiveLocationsBot"
|
||||
|
||||
include ":StickerSetHandler"
|
||||
|
||||
include ":InlineQueriesBot"
|
||||
|
Loading…
Reference in New Issue
Block a user