Skip to content

Commit 89e1eec

Browse files
Merge pull request #358 from InsanusMokrassar/33.0.0
33.0.0
2 parents 29ad52b + 00ab078 commit 89e1eec

12 files changed

Lines changed: 363 additions & 34 deletions

File tree

BusinessConnectionsBot/src/main/kotlin/BusinessConnectionsBot.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@ suspend fun main(args: Array<String>) {
213213
firstName,
214214
secondName
215215
)
216+
}.map {
217+
true
216218
}.getOrElse { false }
217219
reply(it) {
218220
if (set) {
@@ -230,6 +232,8 @@ suspend fun main(args: Array<String>) {
230232
businessConnectionId,
231233
username
232234
)
235+
}.map {
236+
true
233237
}.getOrElse {
234238
it.printStackTrace()
235239
false
@@ -267,6 +271,8 @@ suspend fun main(args: Array<String>) {
267271
}
268272
val transferred = runCatching {
269273
transferBusinessAccountStars(businessConnectionId, count)
274+
}.map {
275+
true
270276
}.getOrElse {
271277
it.printStackTrace()
272278
false
@@ -310,6 +316,8 @@ suspend fun main(args: Array<String>) {
310316
businessConnectionId,
311317
bio
312318
)
319+
}.map {
320+
true
313321
}.getOrElse {
314322
it.printStackTrace()
315323
false
@@ -327,6 +335,8 @@ suspend fun main(args: Array<String>) {
327335
businessConnectionId,
328336
initialBio
329337
)
338+
}.map {
339+
true
330340
}.getOrElse {
331341
it.printStackTrace()
332342
false
@@ -358,6 +368,8 @@ suspend fun main(args: Array<String>) {
358368
),
359369
isPublic = isPublic
360370
)
371+
}.map {
372+
true
361373
}.getOrElse {
362374
it.printStackTrace()
363375
false
@@ -376,6 +388,8 @@ suspend fun main(args: Array<String>) {
376388
businessConnectionId,
377389
isPublic = isPublic
378390
)
391+
}.map {
392+
true
379393
}.getOrElse {
380394
it.printStackTrace()
381395
false
@@ -461,6 +475,8 @@ suspend fun main(args: Array<String>) {
461475

462476
val deleted = runCatching {
463477
deleteStory(businessConnectionId, replyTo.content.story.id)
478+
}.map {
479+
true
464480
}.getOrElse {
465481
it.printStackTrace()
466482
false

ChatAvatarSetter/src/main/kotlin/ChatAvatarSetter.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import dev.inmo.micro_utils.coroutines.runCatchingLogging
12
import dev.inmo.micro_utils.coroutines.runCatchingSafely
23
import dev.inmo.tgbotapi.bot.ktor.telegramBot
34
import dev.inmo.tgbotapi.extensions.api.chat.modify.setChatPhoto
@@ -15,17 +16,13 @@ suspend fun main(args: Array<String>) {
1516
bot.buildBehaviourWithLongPolling(scope = CoroutineScope(Dispatchers.IO)) {
1617
onPhoto {
1718
val bytes = downloadFile(it.content)
18-
runCatchingSafely {
19+
runCatchingLogging {
1920
setChatPhoto(
2021
it.chat.id,
2122
bytes.asMultipartFile("sample.jpg")
2223
)
23-
}.onSuccess { b ->
24-
if (b) {
25-
reply(it, "Done")
26-
} else {
27-
reply(it, "Something went wrong")
28-
}
24+
}.onSuccess { _ ->
25+
reply(it, "Done")
2926
}.onFailure { e ->
3027
e.printStackTrace()
3128

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: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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.subscribeLoggingDropExceptions
6+
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
7+
import dev.inmo.tgbotapi.extensions.api.bot.getMe
8+
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
9+
import dev.inmo.tgbotapi.extensions.api.managed_bots.getManagedBotToken
10+
import dev.inmo.tgbotapi.extensions.api.managed_bots.replaceManagedBotToken
11+
import dev.inmo.tgbotapi.extensions.api.send.reply
12+
import dev.inmo.tgbotapi.extensions.api.send.send
13+
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextData
14+
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildSubcontextInitialAction
15+
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
16+
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
17+
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onManagedBotCreated
18+
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onManagedBotUpdated
19+
import dev.inmo.tgbotapi.extensions.utils.chatEventMessageOrNull
20+
import dev.inmo.tgbotapi.extensions.utils.groupContentMessageOrNull
21+
import dev.inmo.tgbotapi.extensions.utils.managedBotCreatedOrNull
22+
import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatReplyKeyboard
23+
import dev.inmo.tgbotapi.extensions.utils.types.buttons.replyKeyboard
24+
import dev.inmo.tgbotapi.extensions.utils.types.buttons.requestManagedBotButton
25+
import dev.inmo.tgbotapi.types.Username
26+
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonRequestManagedBot
27+
import dev.inmo.tgbotapi.types.buttons.PreparedKeyboardButtonId
28+
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
29+
import dev.inmo.tgbotapi.types.request.RequestId
30+
import dev.inmo.tgbotapi.types.toChatId
31+
import dev.inmo.tgbotapi.types.update.abstracts.Update
32+
import kotlinx.coroutines.CoroutineScope
33+
import kotlinx.coroutines.Dispatchers
34+
35+
private var BehaviourContextData.update: Update?
36+
get() = get("update") as? Update
37+
set(value) = set("update", value)
38+
39+
private var BehaviourContextData.commonMessage: CommonMessage<*>?
40+
get() = get("commonMessage") as? CommonMessage<*>
41+
set(value) = set("commonMessage", value)
42+
43+
/**
44+
* This place can be the playground for your code.
45+
*/
46+
suspend fun main(vararg args: String) {
47+
val botToken = args.first()
48+
49+
val isDebug = args.any { it == "debug" }
50+
val isTestServer = args.any { it == "testServer" }
51+
52+
if (isDebug) {
53+
setDefaultKSLog(
54+
KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
55+
println(defaultMessageFormatter(level, tag, message, throwable))
56+
}
57+
)
58+
}
59+
60+
telegramBotWithBehaviourAndLongPolling(
61+
botToken,
62+
CoroutineScope(Dispatchers.IO),
63+
testServer = isTestServer,
64+
builder = {
65+
includeMiddlewares {
66+
addMiddleware {
67+
doOnRequestReturnResult { result, request, _ ->
68+
println("Result of $request:\n\n$result")
69+
null
70+
}
71+
}
72+
}
73+
},
74+
subcontextInitialAction = buildSubcontextInitialAction {
75+
add {
76+
data.update = it
77+
}
78+
}
79+
) {
80+
// start here!!
81+
val me = getMe()
82+
println(me)
83+
84+
onCommand("start") {
85+
println(data.update)
86+
println(data.commonMessage)
87+
println(getChat(it.chat))
88+
}
89+
90+
onCommand("canManageBots") {
91+
val me = getMe()
92+
reply(it, if (me.canManageBots) "Yes" else "No")
93+
}
94+
95+
val requestId = RequestId(0)
96+
onCommand("keyboard") {
97+
reply(
98+
it,
99+
"Keyboard",
100+
replyMarkup = flatReplyKeyboard(
101+
resizeKeyboard = true,
102+
oneTimeKeyboard = true,
103+
) {
104+
requestManagedBotButton(
105+
"Add managed bot",
106+
KeyboardButtonRequestManagedBot(
107+
requestId = requestId,
108+
suggestedName = "SampleName",
109+
suggestedUsername = Username("@some_sample_bot")
110+
)
111+
)
112+
}
113+
)
114+
}
115+
116+
onManagedBotCreated {
117+
reply(it, "Managed bot created successfully: ${it.chatEvent.bot}")
118+
val token = getManagedBotToken(
119+
it.chatEvent.bot.id.toChatId()
120+
)
121+
reply(it, "Token: $token")
122+
}
123+
124+
onManagedBotUpdated {
125+
send(it.user, "Managed bot has been updated: ${it.bot}")
126+
val token = getManagedBotToken(
127+
it.bot.id.toChatId()
128+
)
129+
send(it.user, "Token: $token")
130+
}
131+
132+
onCommand("replaceToken") {
133+
val reply = it.replyTo ?.chatEventMessageOrNull() ?: return@onCommand
134+
val managedBotCreated = reply.chatEvent.managedBotCreatedOrNull() ?: return@onCommand
135+
136+
reply(it, "Token in replace update: ${replaceManagedBotToken(managedBotCreated.bot.id.toChatId())}")
137+
}
138+
139+
allUpdatesFlow.subscribeLoggingDropExceptions(this) {
140+
println(it)
141+
}
142+
}.second.join()
143+
}

MyBot/src/main/kotlin/MyBot.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,21 +70,18 @@ suspend fun main(vararg args: String) {
7070

7171
draftMessagesChannel.send("Photo file have been downloaded. Start set my profile photo")
7272

73-
val setResult = setMyProfilePhoto(
73+
setMyProfilePhoto(
7474
InputProfilePhoto.Static(
7575
photoFile.asMultipartFile()
7676
)
7777
)
78-
if (setResult) {
79-
reply(commandMessage, "New photo have been set")
80-
}
78+
reply(commandMessage, "New photo have been set")
8179
}
8280

8381
onCommand("removeMyProfilePhoto") {
8482
runCatchingLogging {
85-
if (removeMyProfilePhoto()) {
86-
reply(it, "Photo have been removed")
87-
}
83+
removeMyProfilePhoto()
84+
reply(it, "Photo have been removed")
8885
}.onFailure { e ->
8986
e.printStackTrace()
9087
reply(it, "Something web wrong. See logs for details.")

0 commit comments

Comments
 (0)