From b7347570625826c6750973504612d0682a60506d Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 16 Feb 2026 18:24:29 +0600 Subject: [PATCH] add drafts flows --- tgbotapi.api/api/tgbotapi.api.api | 4 ++ .../extensions/api/send/SendMessageDraft.kt | 56 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/tgbotapi.api/api/tgbotapi.api.api b/tgbotapi.api/api/tgbotapi.api.api index 7800939be1..e0fdfb6d8e 100644 --- a/tgbotapi.api/api/tgbotapi.api.api +++ b/tgbotapi.api/api/tgbotapi.api.api @@ -1782,6 +1782,10 @@ public final class dev/inmo/tgbotapi/extensions/api/send/SendMessageDraftKt { public static final fun sendMessageDraft-PjdzSgQ (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Ldev/inmo/tgbotapi/types/chat/Chat;JLjava/lang/String;Ldev/inmo/tgbotapi/types/message/ParseMode;Ldev/inmo/tgbotapi/types/MessageThreadId;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static synthetic fun sendMessageDraft-PjdzSgQ$default (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Ldev/inmo/tgbotapi/types/IdChatIdentifier;JLjava/lang/String;Ldev/inmo/tgbotapi/types/message/ParseMode;Ldev/inmo/tgbotapi/types/MessageThreadId;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object; public static synthetic fun sendMessageDraft-PjdzSgQ$default (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Ldev/inmo/tgbotapi/types/chat/Chat;JLjava/lang/String;Ldev/inmo/tgbotapi/types/message/ParseMode;Ldev/inmo/tgbotapi/types/MessageThreadId;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object; + public static final fun sendMessageDraftFlow-85jglgs (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Ldev/inmo/tgbotapi/types/IdChatIdentifier;JLkotlinx/coroutines/flow/Flow;Ldev/inmo/tgbotapi/types/MessageThreadId;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static synthetic fun sendMessageDraftFlow-85jglgs$default (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Ldev/inmo/tgbotapi/types/IdChatIdentifier;JLkotlinx/coroutines/flow/Flow;Ldev/inmo/tgbotapi/types/MessageThreadId;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object; + public static final fun sendMessageDraftFlowWithTextAndParseMode (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Ldev/inmo/tgbotapi/types/IdChatIdentifier;JLkotlinx/coroutines/flow/Flow;Ldev/inmo/tgbotapi/types/MessageThreadId;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static synthetic fun sendMessageDraftFlowWithTextAndParseMode$default (Ldev/inmo/tgbotapi/bot/RequestsExecutor;Ldev/inmo/tgbotapi/types/IdChatIdentifier;JLkotlinx/coroutines/flow/Flow;Ldev/inmo/tgbotapi/types/MessageThreadId;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object; } public final class dev/inmo/tgbotapi/extensions/api/send/SendMessageKt { diff --git a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/SendMessageDraft.kt b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/SendMessageDraft.kt index 015ab5d879..45ca757da9 100644 --- a/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/SendMessageDraft.kt +++ b/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/SendMessageDraft.kt @@ -1,5 +1,7 @@ package dev.inmo.tgbotapi.extensions.api.send +import dev.inmo.kslog.common.logger +import dev.inmo.micro_utils.coroutines.runCatchingLogging import dev.inmo.tgbotapi.bot.TelegramBot import dev.inmo.tgbotapi.requests.send.SendMessageDraft import dev.inmo.tgbotapi.types.* @@ -11,6 +13,14 @@ import dev.inmo.tgbotapi.types.message.content.TextContent import dev.inmo.tgbotapi.types.message.textsources.TextSource import dev.inmo.tgbotapi.utils.EntitiesBuilderBody import dev.inmo.tgbotapi.utils.buildEntities +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.collect +import kotlinx.coroutines.flow.filter +import kotlinx.coroutines.flow.firstOrNull +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.takeWhile +import kotlin.js.JsName +import kotlin.jvm.JvmName public suspend fun TelegramBot.sendMessageDraft( chatId: IdChatIdentifier, @@ -28,6 +38,52 @@ public suspend fun TelegramBot.sendMessageDraft( ) ) +private suspend fun TelegramBot.sendMessageDraftFlow( + messagesFlow: Flow, +): Boolean { + val done = messagesFlow + .filter { draft -> + val sent = runCatchingLogging(logger = logger) { + execute(draft) + }.getOrElse { + false + } + + sent == false + } + .firstOrNull() + + return done == null +} + +public suspend fun TelegramBot.sendMessageDraftFlow( + chatId: IdChatIdentifier, + draftId: DraftId, + messagesFlow: Flow, + threadId: MessageThreadId? = chatId.threadId +): Boolean { + return sendMessageDraftFlow( + messagesFlow.map { + SendMessageDraft(chatId = chatId, draftId = draftId, entities = it, threadId = threadId) + } + ) +} + +@JvmName("sendMessageDraftFlowWithTextAndParseMode") +@JsName("sendMessageDraftFlowWithTextAndParseMode") +public suspend fun TelegramBot.sendMessageDraftFlow( + chatId: IdChatIdentifier, + draftId: DraftId, + messagesFlow: Flow>, + threadId: MessageThreadId? = chatId.threadId +): Boolean { + return sendMessageDraftFlow( + messagesFlow.map { + SendMessageDraft(chatId = chatId, draftId = draftId, text = it.first, parseMode = it.second, threadId = threadId) + } + ) +} + public suspend fun TelegramBot.sendMessageDraft( chat: Chat, draftId: DraftId,