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

44 lines
1.6 KiB
Kotlin
Raw Normal View History

2020-10-04 11:32:50 +00:00
import dev.inmo.tgbotapi.extensions.api.bot.getMe
import dev.inmo.tgbotapi.extensions.api.send.media.sendMediaGroup
2020-11-18 06:35:45 +00:00
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
import dev.inmo.micro_utils.coroutines.safely
2020-10-04 11:32:50 +00:00
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
suspend fun activateResenderBot(
token: String,
print: (Any) -> Unit
) {
val bot = telegramBot(token)
print(bot.getMe())
supervisorScope {
val scope = this
2020-10-27 10:23:27 +00:00
bot.startGettingFlowsUpdatesByLongPolling(scope = scope) {
filterContentMessages<MessageContent>(scope).onEach {
it.content.createResends(it.chat.id, replyToMessageId = it.messageId).forEach {
2020-08-23 18:41:24 +00:00
bot.executeUnsafe(it) {
it.forEach(print)
} ?.also {
print(it)
}
}
}.launchIn(scope)
2020-08-23 18:41:24 +00:00
mediaGroupMessages(scope).onEach {
safely({ print(it.stackTraceToString()) }) {
println(it.chat)
bot.execute(it.createResend(it.chat ?: return@safely, replyTo = it.first().messageId)).also {
print(it)
}
}
}.launchIn(scope)
}
}
}