From ff2b56b3a9e00534cb0f5a1c8730c0c472852f7f Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 26 Feb 2019 09:45:56 +0800 Subject: [PATCH] add SetWebhook --- CHANGELOG.md | 5 ++ .../TelegramBotAPI/requests/SetWebhook.kt | 54 +++++++++++++++++++ .../requests/send/media/SendAnimation.kt | 2 +- .../requests/send/media/SendAudio.kt | 2 +- .../requests/send/media/SendDocument.kt | 2 +- .../requests/send/media/SendMediaGroup.kt | 2 +- .../requests/send/media/SendPhoto.kt | 2 +- .../requests/send/media/SendVideo.kt | 2 +- .../requests/send/media/SendVideoNote.kt | 2 +- .../requests/send/media/SendVoice.kt | 2 +- .../requests/send/media/base/Data.kt | 11 ++-- .../requests/send/media/base/DataRequest.kt | 5 ++ .../send/media/base/MultipartRequestImpl.kt | 2 +- .../TelegramBotAPI/types/Common.kt | 4 ++ 14 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/SetWebhook.kt create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/DataRequest.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 89cb348c14..2623171d98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## 0.12.0 +* Added `DataRequest` interface which replace `Data` interface +* `MultipartRequestImpl` now use `DataRequest` +* All requests which implements `Data` now implement `DataRequest` +* Added class `SetWebhook` and its factory + ## 0.11.0 * Kotlin `1.3.11` -> `1.3.21` diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/SetWebhook.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/SetWebhook.kt new file mode 100644 index 0000000000..11aed14b52 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/SetWebhook.kt @@ -0,0 +1,54 @@ +package com.github.insanusmokrassar.TelegramBotAPI.requests + +import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.* +import com.github.insanusmokrassar.TelegramBotAPI.types.* +import kotlinx.serialization.* +import kotlinx.serialization.internal.BooleanSerializer + +fun SetWebhook( + url: String, + certificate: InputFile, + maxAllowedConnections: Int? = null, + allowedUpdates: List? = null +) : Request { + val data = SetWebhook( + url, + certificate.fileId, + maxAllowedConnections, + allowedUpdates + ) + return when (certificate) { + is FileId -> data + is MultipartFile -> MultipartRequestImpl( + data, + mapOf(certificateField to certificate) + ) + } +} + +@Serializable +data class SetWebhook internal constructor( + @SerialName(urlField) + val url: String, + @SerialName(certificateField) + @Optional + val certificateFile: String? = null, + @SerialName(maxAllowedConnectionsField) + @Optional + val maxAllowedConnections: Int? = null, + @SerialName(allowedUpdatesField) + @Optional + val allowedUpdates: List? = null +) : DataRequest { + override fun method(): String = "setWebhook" + override fun resultSerializer(): KSerializer = BooleanSerializer + + init { + maxAllowedConnections ?.let { + if (it !in allowedConnectionsLength) { + throw IllegalArgumentException("Allowed connection for webhook must be in $allowedConnectionsLength range (but passed $it)") + } + } + } +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAnimation.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAnimation.kt index 175a63a7f6..7974b242a9 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAnimation.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAnimation.kt @@ -87,7 +87,7 @@ data class SendAnimationData internal constructor( @SerialName(replyMarkupField) @Optional override val replyMarkup: KeyboardMarkup? = null -) : Data, +) : DataRequest, SendMessageRequest, ReplyingMarkupSendMessageRequest, TextableSendMessageRequest, diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAudio.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAudio.kt index 9599b3a532..2e086813dd 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAudio.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendAudio.kt @@ -88,7 +88,7 @@ data class SendAudioData internal constructor( @SerialName(replyMarkupField) @Optional override val replyMarkup: KeyboardMarkup? = null -) : Data, +) : DataRequest, SendMessageRequest, ReplyingMarkupSendMessageRequest, TextableSendMessageRequest, diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendDocument.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendDocument.kt index 55e2c67357..dca1fc11c9 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendDocument.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendDocument.kt @@ -72,7 +72,7 @@ data class SendDocumentData internal constructor( @SerialName(replyMarkupField) @Optional override val replyMarkup: KeyboardMarkup? = null -) : Data, +) : DataRequest, SendMessageRequest, ReplyingMarkupSendMessageRequest, TextableSendMessageRequest, diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendMediaGroup.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendMediaGroup.kt index f13de99995..a945196b74 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendMediaGroup.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendMediaGroup.kt @@ -66,7 +66,7 @@ data class SendMediaGroupData internal constructor( @SerialName(replyToMessageIdField) @Optional override val replyToMessageId: MessageIdentifier? = null -) : Data>, +) : DataRequest>, SendMessageRequest> { @SerialName(mediaField) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendPhoto.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendPhoto.kt index 2d5abe34c7..92336917c0 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendPhoto.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendPhoto.kt @@ -58,7 +58,7 @@ data class SendPhotoData internal constructor( @SerialName(replyMarkupField) @Optional override val replyMarkup: KeyboardMarkup? = null -) : Data, +) : DataRequest, SendMessageRequest, ReplyingMarkupSendMessageRequest, TextableSendMessageRequest diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideo.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideo.kt index 884c22c12a..3097bf07c9 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideo.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideo.kt @@ -92,7 +92,7 @@ data class SendVideoData internal constructor( @SerialName(replyMarkupField) @Optional override val replyMarkup: KeyboardMarkup? = null -) : Data, +) : DataRequest, SendMessageRequest, ReplyingMarkupSendMessageRequest, TextableSendMessageRequest, diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideoNote.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideoNote.kt index 61d876b225..c3199c0c2f 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideoNote.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVideoNote.kt @@ -82,7 +82,7 @@ data class SendVideoNoteData internal constructor( @SerialName(replyMarkupField) @Optional override val replyMarkup: KeyboardMarkup? = null -) : Data, +) : DataRequest, SendMessageRequest, ReplyingMarkupSendMessageRequest, TextableSendMessageRequest, diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVoice.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVoice.kt index 175135b727..16ee8a3a20 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVoice.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/SendVoice.kt @@ -77,7 +77,7 @@ data class SendVoiceData internal constructor( @SerialName(replyMarkupField) @Optional override val replyMarkup: KeyboardMarkup? = null -) : Data, +) : DataRequest, SendMessageRequest, ReplyingMarkupSendMessageRequest, TextableSendMessageRequest, diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/Data.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/Data.kt index e2bd40e3d6..d8ff64cd37 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/Data.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/Data.kt @@ -1,5 +1,10 @@ package com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base -import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest - -interface Data : SimpleRequest \ No newline at end of file +@Deprecated( + "Renamed to DataRequest", + ReplaceWith( + "DataRequest", + "com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base.DataRequest" + ) +) +typealias Data = DataRequest diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/DataRequest.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/DataRequest.kt new file mode 100644 index 0000000000..996e3227ee --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/DataRequest.kt @@ -0,0 +1,5 @@ +package com.github.insanusmokrassar.TelegramBotAPI.requests.send.media.base + +import com.github.insanusmokrassar.TelegramBotAPI.requests.abstracts.SimpleRequest + +interface DataRequest : SimpleRequest \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/MultipartRequestImpl.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/MultipartRequestImpl.kt index bcb68d8306..bc5488a504 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/MultipartRequestImpl.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/requests/send/media/base/MultipartRequestImpl.kt @@ -9,7 +9,7 @@ import kotlinx.serialization.json.JsonObject /** * Will be used as SimpleRequest if */ -class MultipartRequestImpl, F: Files, R: Any>( +class MultipartRequestImpl, F: Files, R: Any>( val data: D, val files: F ) : MultipartRequest { diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt index 49fc39a4df..8f82575c3c 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -23,6 +23,7 @@ val userProfilePhotosRequestLimit = 0 .. 100 val chatTitleLength = 1 until 255 val chatDescriptionLength = 0 until 256 val inlineResultQueryIdLingth = 1 until 64 +val allowedConnectionsLength = 1 .. 100 val invoiceTitleLimit = 1 until 32 val invoiceDescriptionLimit = 1 until 256 @@ -68,6 +69,8 @@ const val isPersonalField = "is_personal" const val nextOffsetField = "next_offset" const val switchPmTextField = "switch_pm_text" const val switchPmParameterField = "switch_pm_parameter" +const val maxAllowedConnectionsField = "max_connections" +const val allowedUpdatesField = "allowed_updates" const val photoUrlField = "photo_url" @@ -172,6 +175,7 @@ const val pricesField = "prices" const val payloadField = "payload" const val vcardField = "vcard" const val resultsField = "results" +const val certificateField = "certificate" const val pointField = "point" const val xShiftField = "x_shift"