tgbotapi/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/actions/BotAction.kt

173 lines
5.5 KiB
Kotlin
Raw Normal View History

2020-10-04 11:06:30 +00:00
package dev.inmo.tgbotapi.types.actions
2018-12-26 08:07:24 +00:00
2021-09-17 10:50:41 +00:00
import dev.inmo.micro_utils.common.Warning
2022-08-05 18:39:46 +00:00
import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded
2021-09-12 09:32:36 +00:00
import dev.inmo.tgbotapi.utils.RiskFeature
2021-06-28 05:12:51 +00:00
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
2021-05-29 07:39:43 +00:00
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor
2020-08-18 06:50:11 +00:00
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
2018-12-26 08:07:24 +00:00
2020-05-11 14:44:03 +00:00
/**
* Use BotAction objects realisations to notify user about bot actions
*/
2018-12-26 08:07:24 +00:00
@Serializable(BotActionSerializer::class)
2022-08-05 10:31:39 +00:00
@ClassCastsIncluded
2021-05-29 07:39:43 +00:00
sealed interface BotAction {
val actionName: String
2018-12-26 08:07:24 +00:00
}
2021-05-29 09:34:14 +00:00
object BotActionSerializer: KSerializer<BotAction> {
2021-05-29 07:39:43 +00:00
override val descriptor: SerialDescriptor = String.serializer().descriptor
2020-03-22 07:37:01 +00:00
override fun serialize(encoder: Encoder, value: BotAction) {
encoder.encodeString(value.actionName)
2018-12-26 08:07:24 +00:00
}
2019-02-21 06:21:33 +00:00
override fun deserialize(decoder: Decoder): BotAction {
2019-11-29 06:25:22 +00:00
return when (val actionName = decoder.decodeString()) {
TypingAction.actionName -> TypingAction
UploadPhotoAction.actionName -> UploadPhotoAction
RecordVideoAction.actionName -> RecordVideoAction
UploadVideoAction.actionName -> UploadVideoAction
RecordVoiceAction.actionName -> RecordVoiceAction
UploadVoiceAction.actionName -> UploadVoiceAction
2019-11-29 06:25:22 +00:00
UploadDocumentAction.actionName -> UploadDocumentAction
FindLocationAction.actionName -> FindLocationAction
2020-02-06 17:06:29 +00:00
RecordVideoNoteAction.actionName -> RecordVideoNoteAction
UploadVideoNoteAction.actionName -> UploadVideoNoteAction
2021-11-08 12:36:17 +00:00
ChooseStickerAction.actionName -> ChooseStickerAction
2021-09-12 09:32:36 +00:00
else -> CustomBotAction(actionName)
2019-11-29 06:25:22 +00:00
}
2018-12-26 08:07:24 +00:00
}
}
/**
2020-05-11 14:44:03 +00:00
* Will notify user that bot is "typing" something
2018-12-26 08:07:24 +00:00
*/
@Serializable(BotActionSerializer::class)
2021-05-29 07:39:43 +00:00
object TypingAction : BotAction {
2018-12-26 08:07:24 +00:00
override val actionName: String = "typing"
}
2021-01-02 09:51:26 +00:00
inline val typing
get() = TypingAction
2021-01-02 10:06:00 +00:00
inline fun BotAction.asTyping() = this as? TypingAction
2018-12-26 08:07:24 +00:00
2020-05-11 14:44:03 +00:00
/**
* Will notify user that bot is uploading some photo
*/
2018-12-26 08:07:24 +00:00
@Serializable(BotActionSerializer::class)
2021-05-29 07:39:43 +00:00
object UploadPhotoAction : BotAction {
2018-12-26 08:07:24 +00:00
override val actionName: String = "upload_photo"
}
2021-01-02 09:51:26 +00:00
inline val uploadPhoto
get() = UploadPhotoAction
2021-01-02 10:06:00 +00:00
inline fun BotAction.asUploadPhoto() = this as? UploadPhotoAction
2018-12-26 08:07:24 +00:00
2020-05-11 14:44:03 +00:00
/**
* Will notify user that bot is recording some video
*/
2018-12-26 08:07:24 +00:00
@Serializable(BotActionSerializer::class)
2021-05-29 07:39:43 +00:00
object RecordVideoAction : BotAction {
2018-12-26 08:07:24 +00:00
override val actionName: String = "record_video"
}
2021-01-02 09:51:26 +00:00
inline val recordVideo
get() = RecordVideoAction
2021-01-02 10:06:00 +00:00
inline fun BotAction.asRecordVideo() = this as? RecordVideoAction
2018-12-26 08:07:24 +00:00
2020-05-11 14:44:03 +00:00
/**
* Will notify user that bot is uploading some photo
*/
2018-12-26 08:07:24 +00:00
@Serializable(BotActionSerializer::class)
2021-05-29 07:39:43 +00:00
object UploadVideoAction : BotAction {
2018-12-26 08:07:24 +00:00
override val actionName: String = "upload_video"
}
2021-01-02 09:51:26 +00:00
inline val uploadVideo
get() = UploadVideoAction
2021-01-02 10:06:00 +00:00
inline fun BotAction.asUploadVideo() = this as? UploadVideoAction
2018-12-26 08:07:24 +00:00
2021-04-26 14:52:20 +00:00
/**
* Will notify user that bot is recording some audio
*/
@Serializable(BotActionSerializer::class)
2021-05-29 07:39:43 +00:00
object RecordVoiceAction : BotAction {
2021-04-26 14:52:20 +00:00
override val actionName: String = "record_voice"
}
inline val recordVoice
get() = RecordVoiceAction
inline fun BotAction.asRecordVoice() = this as? RecordVoiceAction
/**
* Will notify user that bot is uploading some audio
*/
@Serializable(BotActionSerializer::class)
2021-05-29 07:39:43 +00:00
object UploadVoiceAction : BotAction {
2021-04-26 14:52:20 +00:00
override val actionName: String = "upload_voice"
}
inline val uploadVoice
get() = UploadVoiceAction
inline fun BotAction.asUploadVoice() = this as? UploadVoiceAction
2020-05-11 14:44:03 +00:00
/**
* Will notify user that bot is uploading some document
*/
2018-12-26 08:07:24 +00:00
@Serializable(BotActionSerializer::class)
2021-05-29 07:39:43 +00:00
object UploadDocumentAction : BotAction {
2018-12-26 08:07:24 +00:00
override val actionName: String = "upload_document"
}
2021-01-02 09:51:26 +00:00
inline val uploadDocument
get() = UploadDocumentAction
2021-01-02 10:06:00 +00:00
inline fun BotAction.asUploadDocument() = this as? UploadDocumentAction
2018-12-26 08:07:24 +00:00
2020-05-11 14:44:03 +00:00
/**
* Will notify user that bot is trying to find location
*/
2018-12-26 08:07:24 +00:00
@Serializable(BotActionSerializer::class)
2021-05-29 07:39:43 +00:00
object FindLocationAction : BotAction {
2018-12-26 08:07:24 +00:00
override val actionName: String = "find_location"
}
2021-01-02 09:51:26 +00:00
inline val findLocation
get() = FindLocationAction
2021-01-02 10:06:00 +00:00
inline fun BotAction.asFindLocation() = this as? FindLocationAction
2020-02-06 17:06:29 +00:00
2020-05-11 14:44:03 +00:00
/**
* Will notify user that bot is recording video note
*/
2020-02-06 17:06:29 +00:00
@Serializable(BotActionSerializer::class)
2021-05-29 07:39:43 +00:00
object RecordVideoNoteAction : BotAction {
2020-02-06 17:06:29 +00:00
override val actionName: String = "record_video_note"
}
2021-01-02 09:51:26 +00:00
inline val recordVideoNote
get() = RecordVideoNoteAction
2021-01-02 10:06:00 +00:00
inline fun BotAction.asRecordVideoNote() = this as? RecordVideoNoteAction
2020-02-06 17:06:29 +00:00
2020-05-11 14:44:03 +00:00
/**
* Will notify user that bot is uploading video note
*/
2020-02-06 17:06:29 +00:00
@Serializable(BotActionSerializer::class)
2021-05-29 07:39:43 +00:00
object UploadVideoNoteAction : BotAction {
2020-02-06 17:06:29 +00:00
override val actionName: String = "upload_video_note"
}
2021-01-02 09:51:26 +00:00
inline val uploadVideoNote
get() = UploadVideoNoteAction
2021-01-02 10:06:00 +00:00
inline fun BotAction.asUploadVideoNote() = this as? UploadVideoNoteAction
2021-09-12 09:32:36 +00:00
2021-11-08 12:36:17 +00:00
/**
* Will notify user that bot is uploading video note
*/
@Serializable(BotActionSerializer::class)
object ChooseStickerAction : BotAction {
override val actionName: String = "choose_sticker"
}
inline val chooseSticker
get() = ChooseStickerAction
inline fun BotAction.asChooseStickerAction() = this as? ChooseStickerAction
2021-09-12 09:32:36 +00:00
@Serializable(BotActionSerializer::class)
2021-09-17 10:50:41 +00:00
@Warning("Use this action only in case you are pretty sure that there are no other action for your needs")
class CustomBotAction @RiskFeature("Usage of this action may lead to errors") constructor(
override val actionName: String
) : BotAction