1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-14 22:05:28 +00:00
tgbotapi/tgbotapi.api/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/api/send/SendActionDSL.kt

231 lines
12 KiB
Kotlin
Raw Normal View History

2021-04-05 13:06:49 +00:00
package dev.inmo.tgbotapi.extensions.api.send
2021-10-01 13:38:22 +00:00
import dev.inmo.micro_utils.coroutines.safelyWithResult
import dev.inmo.micro_utils.coroutines.safelyWithoutExceptions
2021-04-05 13:06:49 +00:00
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.business_connection.BusinessConnectionId
2022-04-21 18:16:41 +00:00
import dev.inmo.tgbotapi.types.chat.Chat
2021-04-05 13:06:49 +00:00
import kotlinx.coroutines.*
2022-03-23 05:48:20 +00:00
import kotlin.contracts.*
2021-04-05 13:06:49 +00:00
private const val refreshTime: MilliSeconds = (botActionActualityTime - 1) * 1000L
typealias TelegramBotActionCallback<T> = suspend TelegramBot.() -> T
2022-03-23 05:48:20 +00:00
@OptIn(ExperimentalContracts::class)
2021-04-05 13:06:49 +00:00
suspend fun <T> TelegramBot.withAction(
actionRequest: SendAction,
block: TelegramBotActionCallback<T>
): T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
2022-06-25 17:06:41 +00:00
val botActionJob = CoroutineScope(currentCoroutineContext()).launch {
2021-09-08 16:19:08 +00:00
while (isActive) {
delay(refreshTime)
safelyWithoutExceptions {
execute(actionRequest)
2021-04-05 13:06:49 +00:00
}
}
}
2021-09-08 16:19:08 +00:00
val result = safelyWithResult { block() }
botActionJob.cancel()
return result.getOrThrow()
2021-04-05 13:06:49 +00:00
}
2022-03-23 05:48:20 +00:00
@OptIn(ExperimentalContracts::class)
2021-04-05 13:06:49 +00:00
suspend fun <T> TelegramBot.withAction(
2022-11-10 09:56:38 +00:00
chatId: IdChatIdentifier,
2021-04-05 13:06:49 +00:00
action: BotAction,
2022-12-30 14:49:47 +00:00
threadId: MessageThreadId? = chatId.threadId,
businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId,
2021-04-05 13:06:49 +00:00
block: TelegramBotActionCallback<T>
2022-03-23 05:48:20 +00:00
): T {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(
SendAction(chatId, action, threadId, businessConnectionId),
2022-03-23 05:48:20 +00:00
block
)
}
2021-04-05 13:06:49 +00:00
2022-03-23 05:48:20 +00:00
@OptIn(ExperimentalContracts::class)
2021-04-05 13:06:49 +00:00
suspend fun <T> TelegramBot.withAction(
chat: Chat,
action: BotAction,
2022-12-30 14:49:47 +00:00
threadId: MessageThreadId? = chat.id.threadId,
businessConnectionId: BusinessConnectionId? = chat.id.businessConnectionId,
2021-04-05 13:06:49 +00:00
block: TelegramBotActionCallback<T>
2022-03-23 05:48:20 +00:00
): T {
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(
chat.id,
action,
2022-12-30 14:49:47 +00:00
threadId,
businessConnectionId,
2022-03-23 05:48:20 +00:00
block
)
}
2021-04-05 13:06:49 +00:00
2022-03-23 05:48:20 +00:00
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withTypingAction(chatId: IdChatIdentifier, threadId: MessageThreadId? = chatId.threadId, businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId,block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chatId, TypingAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withUploadPhotoAction(chatId: IdChatIdentifier, threadId: MessageThreadId? = chatId.threadId, businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId,block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chatId, UploadPhotoAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withRecordVideoAction(chatId: IdChatIdentifier, threadId: MessageThreadId? = chatId.threadId, businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId,block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chatId, RecordVideoAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withUploadVideoAction(chatId: IdChatIdentifier, threadId: MessageThreadId? = chatId.threadId, businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId,block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chatId, UploadVideoAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withRecordVoiceAction(chatId: IdChatIdentifier, threadId: MessageThreadId? = chatId.threadId, businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId,block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chatId, RecordVoiceAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withUploadVoiceAction(chatId: IdChatIdentifier, threadId: MessageThreadId? = chatId.threadId, businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId,block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chatId, UploadVoiceAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withUploadDocumentAction(chatId: IdChatIdentifier, threadId: MessageThreadId? = chatId.threadId, businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chatId, UploadDocumentAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withFindLocationAction(chatId: IdChatIdentifier, threadId: MessageThreadId? = chatId.threadId, businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chatId, FindLocationAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withRecordVideoNoteAction(chatId: IdChatIdentifier, threadId: MessageThreadId? = chatId.threadId, businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chatId, RecordVideoNoteAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withUploadVideoNoteAction(chatId: IdChatIdentifier, threadId: MessageThreadId? = chatId.threadId, businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chatId, UploadVideoNoteAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withChooseStickerAction(chatId: IdChatIdentifier, threadId: MessageThreadId? = chatId.threadId, businessConnectionId: BusinessConnectionId? = chatId.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chatId, ChooseStickerAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
2021-04-05 13:06:49 +00:00
2022-03-23 05:48:20 +00:00
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withTypingAction(chat: Chat, threadId: MessageThreadId? = chat.id.threadId, businessConnectionId: BusinessConnectionId? = chat.id.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chat, TypingAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withUploadPhotoAction(chat: Chat, threadId: MessageThreadId? = chat.id.threadId, businessConnectionId: BusinessConnectionId? = chat.id.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chat, UploadPhotoAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withRecordVideoAction(chat: Chat, threadId: MessageThreadId? = chat.id.threadId, businessConnectionId: BusinessConnectionId? = chat.id.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chat, RecordVideoAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withUploadVideoAction(chat: Chat, threadId: MessageThreadId? = chat.id.threadId, businessConnectionId: BusinessConnectionId? = chat.id.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chat, UploadVideoAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withRecordVoiceAction(chat: Chat, threadId: MessageThreadId? = chat.id.threadId, businessConnectionId: BusinessConnectionId? = chat.id.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chat, RecordVoiceAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withUploadVoiceAction(chat: Chat, threadId: MessageThreadId? = chat.id.threadId, businessConnectionId: BusinessConnectionId? = chat.id.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chat, UploadVoiceAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withUploadDocumentAction(chat: Chat, threadId: MessageThreadId? = chat.id.threadId, businessConnectionId: BusinessConnectionId? = chat.id.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chat, UploadDocumentAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withFindLocationAction(chat: Chat, threadId: MessageThreadId? = chat.id.threadId, businessConnectionId: BusinessConnectionId? = chat.id.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chat, FindLocationAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withRecordVideoNoteAction(chat: Chat, threadId: MessageThreadId? = chat.id.threadId, businessConnectionId: BusinessConnectionId? = chat.id.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chat, RecordVideoNoteAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withUploadVideoNoteAction(chat: Chat, threadId: MessageThreadId? = chat.id.threadId, businessConnectionId: BusinessConnectionId? = chat.id.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chat, UploadVideoNoteAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}
@OptIn(ExperimentalContracts::class)
suspend fun <T> TelegramBot.withChooseStickerAction(chat: Chat, threadId: MessageThreadId? = chat.id.threadId, businessConnectionId: BusinessConnectionId? = chat.id.businessConnectionId, block: TelegramBotActionCallback<T>) : T {
2022-03-23 05:48:20 +00:00
contract {
callsInPlace(block, InvocationKind.EXACTLY_ONCE)
}
return withAction(chat, ChooseStickerAction, threadId, businessConnectionId, block)
2022-03-23 05:48:20 +00:00
}