code cleanup

optimized imports here and there, made CustomBot working as intended
This commit is contained in:
bpavuk
2024-07-10 19:23:36 +03:00
parent 9fb6570d21
commit 7964dc4eea
25 changed files with 141 additions and 196 deletions

View File

@@ -3,13 +3,17 @@ import dev.inmo.kslog.common.LogLevel
import dev.inmo.kslog.common.defaultMessageFormatter
import dev.inmo.kslog.common.setDefaultKSLog
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.tgbotapi.extensions.api.bot.getMe
import dev.inmo.tgbotapi.extensions.api.send.reply
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
import dev.inmo.tgbotapi.extensions.utils.asMessageUpdate
import dev.inmo.tgbotapi.extensions.utils.extensions.raw.from
import dev.inmo.tgbotapi.utils.PreviewFeature
import kotlinx.coroutines.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
/**
* The main purpose of this bot is just to answer "Oh, hi, " and add user mention here
* The main purpose of this bot is just to answer "Oh, hi, " and add user mention here.
* Also, this place can be the playground for your code.
*/
@OptIn(PreviewFeature::class)
suspend fun main(vararg args: String) {
@@ -26,8 +30,14 @@ suspend fun main(vararg args: String) {
}
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
val me = getMe()
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) { println(it) }
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) { update ->
update.asMessageUpdate()?.let { safeMessageUpdate ->
val user = safeMessageUpdate.data.from
val replyMessage = if (user != null) {
"Oh, hi, ${user.username ?: user.firstName}!"
} else "Oh, hi!"
reply(to = safeMessageUpdate.data, text = replyMessage)
}
}
}.second.join()
}