Skip to content

Commit b4e2d52

Browse files
add managedbotsbot sample
1 parent f829ce7 commit b4e2d52

3 files changed

Lines changed: 105 additions & 0 deletions

File tree

ManagedBotsBot/build.gradle

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
buildscript {
2+
repositories {
3+
mavenCentral()
4+
}
5+
6+
dependencies {
7+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
8+
}
9+
}
10+
11+
apply plugin: 'kotlin'
12+
apply plugin: 'application'
13+
14+
mainClassName="CustomBotKt"
15+
16+
17+
dependencies {
18+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
19+
20+
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
21+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import dev.inmo.kslog.common.KSLog
2+
import dev.inmo.kslog.common.LogLevel
3+
import dev.inmo.kslog.common.defaultMessageFormatter
4+
import dev.inmo.kslog.common.setDefaultKSLog
5+
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
6+
import dev.inmo.tgbotapi.extensions.api.bot.getMe
7+
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
8+
import dev.inmo.tgbotapi.extensions.api.send.reply
9+
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextData
10+
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildSubcontextInitialAction
11+
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
12+
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
13+
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
14+
import dev.inmo.tgbotapi.types.update.abstracts.Update
15+
import kotlinx.coroutines.CoroutineScope
16+
import kotlinx.coroutines.Dispatchers
17+
18+
private var BehaviourContextData.update: Update?
19+
get() = get("update") as? Update
20+
set(value) = set("update", value)
21+
22+
private var BehaviourContextData.commonMessage: CommonMessage<*>?
23+
get() = get("commonMessage") as? CommonMessage<*>
24+
set(value) = set("commonMessage", value)
25+
26+
/**
27+
* This place can be the playground for your code.
28+
*/
29+
suspend fun main(vararg args: String) {
30+
val botToken = args.first()
31+
32+
val isDebug = args.any { it == "debug" }
33+
val isTestServer = args.any { it == "testServer" }
34+
35+
if (isDebug) {
36+
setDefaultKSLog(
37+
KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
38+
println(defaultMessageFormatter(level, tag, message, throwable))
39+
}
40+
)
41+
}
42+
43+
telegramBotWithBehaviourAndLongPolling(
44+
botToken,
45+
CoroutineScope(Dispatchers.IO),
46+
testServer = isTestServer,
47+
builder = {
48+
includeMiddlewares {
49+
addMiddleware {
50+
doOnRequestReturnResult { result, request, _ ->
51+
println("Result of $request:\n\n$result")
52+
null
53+
}
54+
}
55+
}
56+
},
57+
subcontextInitialAction = buildSubcontextInitialAction {
58+
add {
59+
data.update = it
60+
}
61+
}
62+
) {
63+
// start here!!
64+
val me = getMe()
65+
println(me)
66+
67+
onCommand("start") {
68+
println(data.update)
69+
println(data.commonMessage)
70+
println(getChat(it.chat))
71+
}
72+
73+
onCommand("canManageBots") {
74+
val me = getMe()
75+
reply(it, if (me.canManageBots) "Yes" else "No")
76+
}
77+
78+
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
79+
println(it)
80+
}
81+
}.second.join()
82+
}

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,5 @@ include ":DraftsBot"
6969
include ":GiftsBot"
7070

7171
include ":TagsBot"
72+
73+
include ":ManagedBotsBot"

0 commit comments

Comments
 (0)