TelegramBotAPI-examples/ResenderBot/ResenderBotLib/src/commonMain/kotlin/ResenderBot.kt

43 lines
1.7 KiB
Kotlin
Raw Normal View History

2022-01-02 06:06:32 +00:00
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
2020-10-04 11:32:50 +00:00
import dev.inmo.tgbotapi.extensions.api.bot.getMe
2020-11-18 06:35:45 +00:00
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
import dev.inmo.tgbotapi.extensions.api.send.media.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
2021-09-21 17:22:13 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
2021-09-21 17:22:13 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.plus
2020-10-04 11:32:50 +00:00
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
import dev.inmo.tgbotapi.types.message.abstracts.MediaGroupMessage
import kotlinx.coroutines.*
import kotlin.coroutines.coroutineContext
suspend fun activateResenderBot(
token: String,
print: (Any) -> Unit
) {
val bot = telegramBot(token)
print(bot.getMe())
2021-11-11 06:34:52 +00:00
bot.buildBehaviourWithLongPolling(CoroutineScope(coroutineContext + SupervisorJob())) {
onContentMessage(
2021-09-21 17:22:13 +00:00
subcontextUpdatesFilter = MessageFilterByChat + BehaviourContextAndTwoTypesReceiver { it, _ -> it !is MediaGroupMessage<*> }
) {
2021-03-12 09:48:31 +00:00
executeUnsafe(it.content.createResend(it.chat.id, replyToMessageId = it.messageId))
}
onVisualGallery {
sendVisualMediaGroup(it.chat!!, it.map { it.content.toMediaGroupMemberInputMedia() })
}
onPlaylist {
sendPlaylist(it.chat!!, it.map { it.content.toMediaGroupMemberInputMedia() })
}
onDocumentsGroup {
sendDocumentsGroup(it.chat!!, it.map { it.content.toMediaGroupMemberInputMedia() })
}
2022-01-02 06:06:32 +00:00
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
println(it)
}
}.join()
}