mirror of
https://github.com/InsanusMokrassar/BooruGrabberTelegramBot.git
synced 2024-11-21 15:43:46 +00:00
first version of bot
This commit is contained in:
parent
4c4d4f6946
commit
cec7719ad7
@ -18,6 +18,7 @@ plugins {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
maven { url "https://jitpack.io" }
|
||||
mavenLocal()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@ -35,7 +36,7 @@ dependencies {
|
||||
}
|
||||
|
||||
application {
|
||||
mainClassName = 'telegram_bot.AppKt'
|
||||
mainClassName = 'AppKt'
|
||||
}
|
||||
|
||||
java {
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
kotlin = "1.7.10"
|
||||
tgbotapi = "3.2.0"
|
||||
microutils = "0.12.7"
|
||||
microutils = "0.12.9"
|
||||
imageboard = "2.5.2"
|
||||
krontab = "0.8.0"
|
||||
kslog = "0.5.1"
|
||||
|
@ -3,6 +3,7 @@ import dev.inmo.micro_utils.coroutines.*
|
||||
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
|
||||
import dev.inmo.micro_utils.repos.add
|
||||
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.exposed.keyvalue.ExposedKeyValueRepo
|
||||
import dev.inmo.micro_utils.repos.exposed.onetomany.ExposedKeyValuesRepo
|
||||
@ -69,7 +70,7 @@ suspend fun main(args: Array<String>) {
|
||||
{ this },
|
||||
{ ChatId(this) },
|
||||
{ this },
|
||||
).cached(FullKVCache(), scope = scope)
|
||||
).cached(KVCache(128), scope = scope)
|
||||
|
||||
val chatsChangingMutex = Mutex()
|
||||
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
|
||||
val me = getMe()
|
||||
|
||||
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) {
|
||||
suspend fun triggerSendForChat(chatId: ChatId, settings: ChatSettings) {
|
||||
val result = let {
|
||||
val result = mutableListOf<BoardImage>()
|
||||
let {
|
||||
var i = 0
|
||||
while (result.size < settings.count) {
|
||||
val images = settings.makeRequest(i).takeIf { it.isNotEmpty() } ?: break
|
||||
@ -97,35 +93,46 @@ suspend fun main(args: Array<String>) {
|
||||
)
|
||||
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 {
|
||||
result.isEmpty() -> return@subscribeSafelyWithoutExceptions
|
||||
result.size == 1 -> sendPhoto(
|
||||
urls.isEmpty() -> return@runCatchingSafely
|
||||
urls.size == 1 -> sendPhoto(
|
||||
chatId,
|
||||
FileUrl(result.first().url)
|
||||
).also {
|
||||
result.forEach { chatsUrlsSeen.add(chatId, it.url) }
|
||||
}
|
||||
settings.gallery -> result.chunked(mediaCountInMediaGroup.last + 1).forEach {
|
||||
FileUrl(urls.first())
|
||||
)
|
||||
settings.gallery -> urls.chunked(mediaCountInMediaGroup.last + 1).forEach {
|
||||
sendVisualMediaGroup(
|
||||
chatId,
|
||||
it.map {
|
||||
TelegramMediaPhoto(FileUrl(it.url))
|
||||
TelegramMediaPhoto(FileUrl(it))
|
||||
}
|
||||
).also { _ ->
|
||||
it.forEach { chatsUrlsSeen.add(chatId, it.url) }
|
||||
)
|
||||
}
|
||||
}
|
||||
else -> result.forEach {
|
||||
else -> urls.forEach {
|
||||
sendPhoto(
|
||||
chatId,
|
||||
FileUrl(it.url)
|
||||
).also { _ ->
|
||||
chatsUrlsSeen.add(chatId, it.url)
|
||||
FileUrl(it)
|
||||
)
|
||||
}
|
||||
}
|
||||
}.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user