first version of bot

This commit is contained in:
InsanusMokrassar 2022-09-07 20:13:39 +06:00
parent 4c4d4f6946
commit cec7719ad7
3 changed files with 51 additions and 43 deletions

View File

@ -18,6 +18,7 @@ plugins {
repositories { repositories {
mavenCentral() mavenCentral()
maven { url "https://jitpack.io" } maven { url "https://jitpack.io" }
mavenLocal()
} }
dependencies { dependencies {
@ -35,7 +36,7 @@ dependencies {
} }
application { application {
mainClassName = 'telegram_bot.AppKt' mainClassName = 'AppKt'
} }
java { java {

View File

@ -2,7 +2,7 @@
kotlin = "1.7.10" kotlin = "1.7.10"
tgbotapi = "3.2.0" tgbotapi = "3.2.0"
microutils = "0.12.7" microutils = "0.12.9"
imageboard = "2.5.2" imageboard = "2.5.2"
krontab = "0.8.0" krontab = "0.8.0"
kslog = "0.5.1" kslog = "0.5.1"

View File

@ -3,6 +3,7 @@ import dev.inmo.micro_utils.coroutines.*
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
import dev.inmo.micro_utils.repos.add import dev.inmo.micro_utils.repos.add
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
import dev.inmo.micro_utils.repos.cache.cache.KVCache
import dev.inmo.micro_utils.repos.cache.cached import dev.inmo.micro_utils.repos.cache.cached
import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo
import dev.inmo.micro_utils.repos.exposed.onetomany.ExposedKeyValuesRepo import dev.inmo.micro_utils.repos.exposed.onetomany.ExposedKeyValuesRepo
@ -69,7 +70,7 @@ suspend fun main(args: Array<String>) {
{ this }, { this },
{ ChatId(this) }, { ChatId(this) },
{ this }, { this },
).cached(FullKVCache(), scope = scope) ).cached(KVCache(128), scope = scope)
val chatsChangingMutex = Mutex() val chatsChangingMutex = Mutex()
val chatsSendingJobs = mutableMapOf<ChatId, Job>() val chatsSendingJobs = mutableMapOf<ChatId, Job>()
@ -79,14 +80,9 @@ suspend fun main(args: Array<String>) {
// in this lambda you will be able to call methods without "bot." prefix // in this lambda you will be able to call methods without "bot." prefix
val me = getMe() val me = getMe()
suspend fun refreshChatJob(chatId: ChatId, settings: ChatSettings?) { suspend fun triggerSendForChat(chatId: ChatId, settings: ChatSettings) {
val settings = settings ?: repo.get(chatId) val result = let {
chatsChangingMutex.withLock {
chatsSendingJobs[chatId] ?.cancel()
settings ?.let {
chatsSendingJobs[chatId] = settings.scheduler.asFlow().subscribeSafelyWithoutExceptions(scope) {
val result = mutableListOf<BoardImage>() val result = mutableListOf<BoardImage>()
let {
var i = 0 var i = 0
while (result.size < settings.count) { while (result.size < settings.count) {
val images = settings.makeRequest(i).takeIf { it.isNotEmpty() } ?: break val images = settings.makeRequest(i).takeIf { it.isNotEmpty() } ?: break
@ -97,35 +93,46 @@ suspend fun main(args: Array<String>) {
) )
i++ i++
} }
} val toDrop = (result.size - settings.count).takeIf { it > 0 } ?: return@let result
result.dropLast(toDrop)
}.takeIf { it.isNotEmpty() } ?: return
runCatchingSafely {
val urls = result.map { it.url.also(::println) }
chatsUrlsSeen.add(chatId, urls)
when { when {
result.isEmpty() -> return@subscribeSafelyWithoutExceptions urls.isEmpty() -> return@runCatchingSafely
result.size == 1 -> sendPhoto( urls.size == 1 -> sendPhoto(
chatId, chatId,
FileUrl(result.first().url) FileUrl(urls.first())
).also { )
result.forEach { chatsUrlsSeen.add(chatId, it.url) } settings.gallery -> urls.chunked(mediaCountInMediaGroup.last + 1).forEach {
}
settings.gallery -> result.chunked(mediaCountInMediaGroup.last + 1).forEach {
sendVisualMediaGroup( sendVisualMediaGroup(
chatId, chatId,
it.map { it.map {
TelegramMediaPhoto(FileUrl(it.url)) TelegramMediaPhoto(FileUrl(it))
} }
).also { _ -> )
it.forEach { chatsUrlsSeen.add(chatId, it.url) }
} }
} else -> urls.forEach {
else -> result.forEach {
sendPhoto( sendPhoto(
chatId, chatId,
FileUrl(it.url) FileUrl(it)
).also { _ -> )
chatsUrlsSeen.add(chatId, it.url)
} }
} }
}.onFailure {
triggerSendForChat(chatId, settings)
} }
} }
suspend fun refreshChatJob(chatId: ChatId, settings: ChatSettings?) {
val settings = settings ?: repo.get(chatId)
chatsChangingMutex.withLock {
chatsSendingJobs[chatId] ?.cancel()
settings ?.let {
chatsSendingJobs[chatId] = settings.scheduler.asFlow().subscribeSafelyWithoutExceptions(scope) {
triggerSendForChat(chatId, settings)
}
} }
} }
} }