1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2025-09-07 17:19:24 +00:00

WAT A HUGE REFACTOR

This commit is contained in:
2025-07-12 15:42:48 +06:00
parent 586a61157d
commit 9e4d7dd9f9
292 changed files with 1337 additions and 509 deletions

View File

@@ -12,6 +12,7 @@ import io.ktor.client.engine.*
* @param ktorClientEngine Engine like [io.ktor.client.engine.cio.CIO]
* @param ktorClientConfig Config block for preconfiguring of bot [HttpClient]
*/
@ConsistentCopyVisibility
public data class BotBuilder internal constructor(
var proxy: ProxyConfig? = null,
var ktorClientEngineFactory: HttpClientEngineFactory<HttpClientEngineConfig>? = null,

View File

@@ -3,5 +3,4 @@ package dev.inmo.tgbotapi.extensions.api
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.local.Close
@Suppress("unused")
public suspend inline fun TelegramBot.close(): Boolean = execute(Close)
public suspend inline fun TelegramBot.executeClose(): Boolean = execute(Close)

View File

@@ -17,6 +17,7 @@ internal fun List<Update>.convertWithMediaGroupUpdates(): List<Update> {
for (update in this) {
val message = (update.data as? PossiblySentViaBotCommonMessage<*>) ?.let {
if (it.content is MediaGroupPartContent) {
@Suppress("UNCHECKED_CAST")
it as PossiblySentViaBotCommonMessage<MediaGroupPartContent>
} else {
null
@@ -48,10 +49,4 @@ internal fun List<Update>.convertWithMediaGroupUpdates(): List<Update> {
return resultUpdates
}
/**
* @return [EditMessageMediaGroupUpdate] in case if [this] is [EditMessageUpdate]. When [this] object is
* [EditChannelPostUpdate] instance - will return [EditChannelPostMediaGroupUpdate]
*
* @throws IllegalStateException
*/
internal fun BaseEditMessageUpdate.toEditMediaGroupUpdate() = this
internal fun BaseEditMessageUpdate.toEditMediaGroupUpdate(): BaseEditMessageUpdate = this

View File

@@ -1,7 +1,6 @@
package dev.inmo.tgbotapi.extensions.api
import dev.inmo.micro_utils.coroutines.LinkedSupervisorScope
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
import dev.inmo.tgbotapi.abstracts.Headed
import dev.inmo.tgbotapi.abstracts.HorizontallyAccured
import dev.inmo.tgbotapi.abstracts.Locationed
@@ -19,6 +18,7 @@ import dev.inmo.tgbotapi.types.location.Location
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.content.LiveLocationContent
import dev.inmo.tgbotapi.types.message.content.LocationContent
import dev.inmo.tgbotapi.utils.launchWithBotLogger
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.delay
@@ -63,7 +63,7 @@ public suspend fun TelegramBot.handleLiveLocation(
null
} else {
val scope = currentCoroutineContext().LinkedSupervisorScope()
scope.launchSafelyWithoutExceptions(start = CoroutineStart.LAZY) {
scope.launchWithBotLogger(start = CoroutineStart.LAZY) {
while (scope.isActive) {
delay(liveTimeMillis)
// Remove previous location message info to resend live location message

View File

@@ -1,3 +1,5 @@
@file:Suppress("KDocUnresolvedReference")
package dev.inmo.tgbotapi.extensions.api.edit.media
import dev.inmo.tgbotapi.bot.TelegramBot
@@ -45,7 +47,7 @@ public suspend fun TelegramBot.editMessageMedia(
* as a builder for that
*/
public suspend fun TelegramBot.editMessageMedia(
message: ContentMessage<out MediaContent>,
message: ContentMessage<MediaContent>,
media: TelegramFreeMedia,
businessConnectionId: BusinessConnectionId? = message.chat.id.businessConnectionId,
replyMarkup: InlineKeyboardMarkup? = null

View File

@@ -10,6 +10,7 @@ import dev.inmo.tgbotapi.types.message.content.MessageContent
/**
* This method will send [content] to the [chatId] as is
*/
@Suppress("UNCHECKED_CAST")
public suspend inline fun <T : MessageContent> TelegramBot.resend(
chatId: ChatIdentifier,
content: T,

View File

@@ -1,9 +1,7 @@
package dev.inmo.tgbotapi.extensions.api.send
import dev.inmo.micro_utils.coroutines.LinkedSupervisorScope
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.coroutines.safelyWithResult
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
import dev.inmo.micro_utils.coroutines.runCatchingLogging
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.send.SendAction
import dev.inmo.tgbotapi.types.*
@@ -21,6 +19,7 @@ public suspend fun <T> TelegramBot.withAction(
actionRequest: SendAction,
block: TelegramBotActionCallback<T>
): T {
@Suppress("WRONG_INVOCATION_KIND")
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
@@ -33,7 +32,7 @@ public suspend fun <T> TelegramBot.withAction(
delay(refreshTime)
}
}
val result = runCatchingSafely { block() }
val result = runCatchingLogging(logger = Log) { block() }
actionScope.coroutineContext.job.cancel()
return result.getOrThrow()
}

View File

@@ -1,11 +1,13 @@
package dev.inmo.tgbotapi.extensions.api.utils
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
import dev.inmo.kslog.common.KSLog
import dev.inmo.micro_utils.coroutines.launchLoggingDropExceptions
import dev.inmo.tgbotapi.extensions.api.InternalUtils.convertWithMediaGroupUpdates
import dev.inmo.tgbotapi.types.message.abstracts.PossiblyMediaGroupMessage
import dev.inmo.tgbotapi.types.update.abstracts.BaseMessageUpdate
import dev.inmo.tgbotapi.types.update.abstracts.Update
import dev.inmo.tgbotapi.updateshandlers.UpdateReceiver
import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog
import dev.inmo.tgbotapi.utils.extensions.accumulateByKey
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
@@ -19,7 +21,8 @@ import kotlinx.coroutines.launch
*/
public fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
output: UpdateReceiver<Update>,
mediaGroupsDebounceMillis: Long = 1000L
mediaGroupsDebounceMillis: Long = 1000L,
logger: KSLog = DefaultKTgBotAPIKSLog
): UpdateReceiver<Update> {
val updatesChannel = Channel<Update>(Channel.UNLIMITED)
val mediaGroupChannel = Channel<Pair<String, BaseMessageUpdate>>(Channel.UNLIMITED)
@@ -29,7 +32,7 @@ public fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
)
launch {
launchSafelyWithoutExceptions {
launchLoggingDropExceptions(logger = logger) {
for (update in updatesChannel) {
val data = update.data
when {
@@ -40,7 +43,7 @@ public fun CoroutineScope.updateHandlerWithMediaGroupsAdaptation(
}
}
}
launchSafelyWithoutExceptions {
launchLoggingDropExceptions(logger = logger) {
for ((_, mediaGroup) in mediaGroupAccumulatedChannel) {
mediaGroup.convertWithMediaGroupUpdates().forEach {
output(it)

View File

@@ -0,0 +1,11 @@
package dev.inmo.tgbotapi.extensions.api.verifications
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.verifications.RemoveChatVerification
import dev.inmo.tgbotapi.types.ChatIdentifier
public suspend fun TelegramBot.removeChatVerification(
chatId: ChatIdentifier
): Boolean = execute(
RemoveChatVerification(chatId)
)

View File

@@ -0,0 +1,11 @@
package dev.inmo.tgbotapi.extensions.api.verifications
import dev.inmo.tgbotapi.bot.TelegramBot
import dev.inmo.tgbotapi.requests.verifications.RemoveUserVerification
import dev.inmo.tgbotapi.types.UserId
public suspend fun TelegramBot.removeUserVerification(
userId: UserId
): Boolean = execute(
RemoveUserVerification(userId)
)