diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt index 8fc47f2a24..c1c935fb26 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt @@ -80,6 +80,8 @@ val openPeriodPollSecondsLimit = 5 .. 600 val membersLimit = 1 .. 99999 +const val botActionActualityTime: Seconds = 5 + // Made as lazy for correct work in K/JS val telegramInlineModeGifPermittedMimeTypes by lazy { listOf( diff --git a/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/SendActionDSL.kt b/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/SendActionDSL.kt new file mode 100644 index 0000000000..13e964debe --- /dev/null +++ b/tgbotapi.extensions.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/SendActionDSL.kt @@ -0,0 +1,76 @@ +package dev.inmo.tgbotapi.extensions.api.send + +import dev.inmo.micro_utils.coroutines.safely +import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions +import dev.inmo.tgbotapi.bot.TelegramBot +import dev.inmo.tgbotapi.requests.send.SendAction +import dev.inmo.tgbotapi.types.* +import dev.inmo.tgbotapi.types.actions.* +import dev.inmo.tgbotapi.types.chat.abstracts.Chat +import kotlinx.coroutines.* + +private const val refreshTime: MilliSeconds = (botActionActualityTime - 1) * 1000L +typealias TelegramBotActionCallback = suspend TelegramBot.() -> T + +suspend fun TelegramBot.withAction( + actionRequest: SendAction, + block: TelegramBotActionCallback +): T { + val botActionJob = supervisorScope { + launch { + while (isActive) { + delay(refreshTime) + safelyWithoutExceptions { + execute(actionRequest) + } + } + } + } + return try { + safely { block() } + } finally { + botActionJob.cancel() + } +} + +suspend fun TelegramBot.withAction( + chatId: ChatId, + action: BotAction, + block: TelegramBotActionCallback +) = withAction( + SendAction(chatId, action), + block +) + +suspend fun TelegramBot.withAction( + chat: Chat, + action: BotAction, + block: TelegramBotActionCallback +) = withAction( + chat.id, + action, + block +) + +suspend fun TelegramBot.withTypingAction(chatId: ChatId, block: TelegramBotActionCallback) = withAction(chatId, TypingAction, block) +suspend fun TelegramBot.withUploadPhotoAction(chatId: ChatId, block: TelegramBotActionCallback) = withAction(chatId, UploadPhotoAction, block) +suspend fun TelegramBot.withRecordVideoAction(chatId: ChatId, block: TelegramBotActionCallback) = withAction(chatId, RecordVideoAction, block) +suspend fun TelegramBot.withUploadVideoAction(chatId: ChatId, block: TelegramBotActionCallback) = withAction(chatId, UploadVideoAction, block) +suspend fun TelegramBot.withRecordAudioAction(chatId: ChatId, block: TelegramBotActionCallback) = withAction(chatId, RecordAudioAction, block) +suspend fun TelegramBot.withUploadAudioAction(chatId: ChatId, block: TelegramBotActionCallback) = withAction(chatId, UploadAudioAction, block) +suspend fun TelegramBot.withUploadDocumentAction(chatId: ChatId, block: TelegramBotActionCallback) = withAction(chatId, UploadDocumentAction, block) +suspend fun TelegramBot.withFindLocationAction(chatId: ChatId, block: TelegramBotActionCallback) = withAction(chatId, FindLocationAction, block) +suspend fun TelegramBot.withRecordVideoNoteAction(chatId: ChatId, block: TelegramBotActionCallback) = withAction(chatId, RecordVideoNoteAction, block) +suspend fun TelegramBot.withUploadVideoNoteAction(chatId: ChatId, block: TelegramBotActionCallback) = withAction(chatId, UploadVideoNoteAction, block) + + +suspend fun TelegramBot.withTypingAction(chat: Chat, block: TelegramBotActionCallback) = withAction(chat, TypingAction, block) +suspend fun TelegramBot.withUploadPhotoAction(chat: Chat, block: TelegramBotActionCallback) = withAction(chat, UploadPhotoAction, block) +suspend fun TelegramBot.withRecordVideoAction(chat: Chat, block: TelegramBotActionCallback) = withAction(chat, RecordVideoAction, block) +suspend fun TelegramBot.withUploadVideoAction(chat: Chat, block: TelegramBotActionCallback) = withAction(chat, UploadVideoAction, block) +suspend fun TelegramBot.withRecordAudioAction(chat: Chat, block: TelegramBotActionCallback) = withAction(chat, RecordAudioAction, block) +suspend fun TelegramBot.withUploadAudioAction(chat: Chat, block: TelegramBotActionCallback) = withAction(chat, UploadAudioAction, block) +suspend fun TelegramBot.withUploadDocumentAction(chat: Chat, block: TelegramBotActionCallback) = withAction(chat, UploadDocumentAction, block) +suspend fun TelegramBot.withFindLocationAction(chat: Chat, block: TelegramBotActionCallback) = withAction(chat, FindLocationAction, block) +suspend fun TelegramBot.withRecordVideoNoteAction(chat: Chat, block: TelegramBotActionCallback) = withAction(chat, RecordVideoNoteAction, block) +suspend fun TelegramBot.withUploadVideoNoteAction(chat: Chat, block: TelegramBotActionCallback) = withAction(chat, UploadVideoNoteAction, block)