mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2026-06-06 15:47:17 +00:00
Compare commits
9 Commits
31.1.0
...
9746a068b7
| Author | SHA1 | Date | |
|---|---|---|---|
| 9746a068b7 | |||
| 9903e0e323 | |||
| 8268cd9bf4 | |||
| b4e2d52e7e | |||
| f829ce7281 | |||
| 20b2ae8175 | |||
| 29ad52b506 | |||
|
|
b848c6bfad | ||
| 6642b95af2 |
21
ManagedBotsBot/build.gradle
Normal file
21
ManagedBotsBot/build.gradle
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
apply plugin: 'kotlin'
|
||||||
|
apply plugin: 'application'
|
||||||
|
|
||||||
|
mainClassName="CustomBotKt"
|
||||||
|
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
|
|
||||||
|
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||||
|
}
|
||||||
143
ManagedBotsBot/src/main/kotlin/ManagedBotsBot.kt
Normal file
143
ManagedBotsBot/src/main/kotlin/ManagedBotsBot.kt
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
import dev.inmo.kslog.common.KSLog
|
||||||
|
import dev.inmo.kslog.common.LogLevel
|
||||||
|
import dev.inmo.kslog.common.defaultMessageFormatter
|
||||||
|
import dev.inmo.kslog.common.setDefaultKSLog
|
||||||
|
import dev.inmo.micro_utils.coroutines.subscribeLoggingDropExceptions
|
||||||
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.managed_bots.getManagedBotToken
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.managed_bots.replaceManagedBotToken
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.send
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextData
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildSubcontextInitialAction
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onManagedBotCreated
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onManagedBotUpdated
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.chatEventMessageOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.groupContentMessageOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.managedBotCreatedOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatReplyKeyboard
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.replyKeyboard
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.requestManagedBotButton
|
||||||
|
import dev.inmo.tgbotapi.types.Username
|
||||||
|
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonRequestManagedBot
|
||||||
|
import dev.inmo.tgbotapi.types.buttons.PreparedKeyboardButtonId
|
||||||
|
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
||||||
|
import dev.inmo.tgbotapi.types.request.RequestId
|
||||||
|
import dev.inmo.tgbotapi.types.toChatId
|
||||||
|
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
|
||||||
|
private var BehaviourContextData.update: Update?
|
||||||
|
get() = get("update") as? Update
|
||||||
|
set(value) = set("update", value)
|
||||||
|
|
||||||
|
private var BehaviourContextData.commonMessage: CommonMessage<*>?
|
||||||
|
get() = get("commonMessage") as? CommonMessage<*>
|
||||||
|
set(value) = set("commonMessage", value)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This place can be the playground for your code.
|
||||||
|
*/
|
||||||
|
suspend fun main(vararg args: String) {
|
||||||
|
val botToken = args.first()
|
||||||
|
|
||||||
|
val isDebug = args.any { it == "debug" }
|
||||||
|
val isTestServer = args.any { it == "testServer" }
|
||||||
|
|
||||||
|
if (isDebug) {
|
||||||
|
setDefaultKSLog(
|
||||||
|
KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
|
||||||
|
println(defaultMessageFormatter(level, tag, message, throwable))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
telegramBotWithBehaviourAndLongPolling(
|
||||||
|
botToken,
|
||||||
|
CoroutineScope(Dispatchers.IO),
|
||||||
|
testServer = isTestServer,
|
||||||
|
builder = {
|
||||||
|
includeMiddlewares {
|
||||||
|
addMiddleware {
|
||||||
|
doOnRequestReturnResult { result, request, _ ->
|
||||||
|
println("Result of $request:\n\n$result")
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
subcontextInitialAction = buildSubcontextInitialAction {
|
||||||
|
add {
|
||||||
|
data.update = it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
// start here!!
|
||||||
|
val me = getMe()
|
||||||
|
println(me)
|
||||||
|
|
||||||
|
onCommand("start") {
|
||||||
|
println(data.update)
|
||||||
|
println(data.commonMessage)
|
||||||
|
println(getChat(it.chat))
|
||||||
|
}
|
||||||
|
|
||||||
|
onCommand("canManageBots") {
|
||||||
|
val me = getMe()
|
||||||
|
reply(it, if (me.canManageBots) "Yes" else "No")
|
||||||
|
}
|
||||||
|
|
||||||
|
val requestId = RequestId(0)
|
||||||
|
onCommand("keyboard") {
|
||||||
|
reply(
|
||||||
|
it,
|
||||||
|
"Keyboard",
|
||||||
|
replyMarkup = flatReplyKeyboard(
|
||||||
|
resizeKeyboard = true,
|
||||||
|
oneTimeKeyboard = true,
|
||||||
|
) {
|
||||||
|
requestManagedBotButton(
|
||||||
|
"Add managed bot",
|
||||||
|
KeyboardButtonRequestManagedBot(
|
||||||
|
requestId = requestId,
|
||||||
|
suggestedName = "SampleName",
|
||||||
|
suggestedUsername = Username("@some_sample_bot")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
onManagedBotCreated {
|
||||||
|
reply(it, "Managed bot created successfully: ${it.chatEvent.bot}")
|
||||||
|
val token = getManagedBotToken(
|
||||||
|
it.chatEvent.bot.id.toChatId()
|
||||||
|
)
|
||||||
|
reply(it, "Token: $token")
|
||||||
|
}
|
||||||
|
|
||||||
|
onManagedBotUpdated {
|
||||||
|
send(it.user, "Managed bot has been updated: ${it.bot}")
|
||||||
|
val token = getManagedBotToken(
|
||||||
|
it.bot.id.toChatId()
|
||||||
|
)
|
||||||
|
send(it.user, "Token: $token")
|
||||||
|
}
|
||||||
|
|
||||||
|
onCommand("replaceToken") {
|
||||||
|
val reply = it.replyTo ?.chatEventMessageOrNull() ?: return@onCommand
|
||||||
|
val managedBotCreated = reply.chatEvent.managedBotCreatedOrNull() ?: return@onCommand
|
||||||
|
|
||||||
|
reply(it, "Token in replace update: ${replaceManagedBotToken(managedBotCreated.bot.id.toChatId())}")
|
||||||
|
}
|
||||||
|
|
||||||
|
allUpdatesFlow.subscribeLoggingDropExceptions(this) {
|
||||||
|
println(it)
|
||||||
|
}
|
||||||
|
}.second.join()
|
||||||
|
}
|
||||||
@@ -5,9 +5,9 @@ org.gradle.jvmargs=-Xmx3148m
|
|||||||
kotlin.daemon.jvmargs=-Xmx3g -Xms500m
|
kotlin.daemon.jvmargs=-Xmx3g -Xms500m
|
||||||
|
|
||||||
|
|
||||||
kotlin_version=2.2.21
|
kotlin_version=2.3.20
|
||||||
telegram_bot_api_version=31.1.0
|
telegram_bot_api_version=33.0.0
|
||||||
micro_utils_version=0.26.9
|
micro_utils_version=0.29.1
|
||||||
serialization_version=1.9.0
|
serialization_version=1.10.0
|
||||||
ktor_version=3.3.2
|
ktor_version=3.4.1
|
||||||
compose_version=1.8.2
|
compose_version=1.10.2
|
||||||
|
|||||||
@@ -69,3 +69,5 @@ include ":DraftsBot"
|
|||||||
include ":GiftsBot"
|
include ":GiftsBot"
|
||||||
|
|
||||||
include ":TagsBot"
|
include ":TagsBot"
|
||||||
|
|
||||||
|
include ":ManagedBotsBot"
|
||||||
|
|||||||
Reference in New Issue
Block a user