package dev.inmo.tgbotapi.extensions.api.send import dev.inmo.micro_utils.coroutines.safelyWithResult 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.* import kotlin.coroutines.coroutineContext private const val refreshTime: MilliSeconds = (botActionActualityTime - 1) * 1000L typealias TelegramBotActionCallback = suspend TelegramBot.() -> T suspend fun TelegramBot.withAction( actionRequest: SendAction, block: TelegramBotActionCallback ): T { val botActionJob = CoroutineScope(coroutineContext).launch { while (isActive) { delay(refreshTime) safelyWithoutExceptions { execute(actionRequest) } } } val result = safelyWithResult { block() } botActionJob.cancel() return result.getOrThrow() } 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.withRecordVoiceAction(chatId: ChatId, block: TelegramBotActionCallback) = withAction(chatId, RecordVoiceAction, block) suspend fun TelegramBot.withUploadVoiceAction(chatId: ChatId, block: TelegramBotActionCallback) = withAction(chatId, UploadVoiceAction, 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.withRecordVoiceAction(chat: Chat, block: TelegramBotActionCallback) = withAction(chat, RecordVoiceAction, block) suspend fun TelegramBot.withUploadVoiceAction(chat: Chat, block: TelegramBotActionCallback) = withAction(chat, UploadVoiceAction, 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)