From f50d2c335500523ccd323595a1625e9d2d6e286c Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 1 Jun 2019 17:12:57 +0800 Subject: [PATCH 1/8] start 0.16.0 --- CHANGELOG.md | 2 ++ build.gradle | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44a3e9be11..4e907a8be3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # TelegramBotAPI changelog +## 0.16.0 Bot API 4.3 + ## 0.15.0 * Old `UpdatesPoller` removed (was deprecated) diff --git a/build.gradle b/build.gradle index a09fe01bf9..ca230dc3c6 100644 --- a/build.gradle +++ b/build.gradle @@ -1,4 +1,4 @@ -project.version = "0.15.0" +project.version = "0.16.0" project.group = "com.github.insanusmokrassar" buildscript { From 5bc31695780630dee6326c00dc3f66ee0d31c3bb Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 1 Jun 2019 17:21:02 +0800 Subject: [PATCH 2/8] LoginURL and LoginURLInlineKeyboardButton --- CHANGELOG.md | 2 ++ .../TelegramBotAPI/types/Common.kt | 6 +++++ .../LoginURLInlineKeyboardButton.kt | 24 +++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/LoginURLInlineKeyboardButton.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e907a8be3..81e53c9afa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.16.0 Bot API 4.3 +* `LoginURL` and `LoginURLInlineKeyboardButton` has been added + ## 0.15.0 * Old `UpdatesPoller` removed (was deprecated) 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 055e4b0cd0..b0dc8b4388 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -84,6 +84,12 @@ const val lastErrorDateField = "last_error_date" const val lastErrorMessageField = "last_error_message" const val votesCountField = "voter_count" const val isClosedField = "is_closed" +const val loginUrlField = "login_url" +const val forwardTextField = "forward_text" +const val botUsernameField = "bot_username" + + +const val requestWriteAccessField = "request_write_access" const val photoUrlField = "photo_url" diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/LoginURLInlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/LoginURLInlineKeyboardButton.kt new file mode 100644 index 0000000000..fcf0bea1fa --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/LoginURLInlineKeyboardButton.kt @@ -0,0 +1,24 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons + +import com.github.insanusmokrassar.TelegramBotAPI.types.* +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable + +@Serializable +data class LoginURLInlineKeyboardButton( + override val text: String, + @SerialName(loginUrlField) + val loginUrl: LoginURL +) : InlineKeyboardButton + +@Serializable +data class LoginURL( + @SerialName(urlField) + val url: String, + @SerialName(forwardTextField) + val forwardText: String? = null, + @SerialName(botUsernameField) + val botUsername: String? = null, + @SerialName(requestWriteAccessField) + val requestWriteAccess: Boolean? = null +) From 4570df330bc93f9e881397900c44fed6353edb68 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 1 Jun 2019 17:28:37 +0800 Subject: [PATCH 3/8] reply_markup in messages --- CHANGELOG.md | 1 + .../types/message/ChannelMediaGroupMessage.kt | 4 +++- .../TelegramBotAPI/types/message/ChannelMessage.kt | 2 ++ .../types/message/CommonMediaGroupMessage.kt | 4 +++- .../types/message/CommonMessageImpl.kt | 2 ++ .../TelegramBotAPI/types/message/RawMessage.kt | 13 ++++++++++--- .../types/message/abstracts/AbleToBeMarkedUp.kt | 7 +++++++ .../types/message/abstracts/CommonMessage.kt | 1 + 8 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/AbleToBeMarkedUp.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 81e53c9afa..48d1ea66ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 0.16.0 Bot API 4.3 * `LoginURL` and `LoginURLInlineKeyboardButton` has been added +* `replyMarkup` field was added to the `CommonMessage` objects via `AbleToBeMarkedUp` interface ## 0.15.0 diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMediaGroupMessage.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMediaGroupMessage.kt index c127d3acbd..d66f9fca72 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMediaGroupMessage.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMediaGroupMessage.kt @@ -2,6 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message import com.github.insanusmokrassar.TelegramBotAPI.types.MediaGroupIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier +import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.MediaGroupMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message @@ -16,5 +17,6 @@ data class ChannelMediaGroupMessage( override val content: MediaGroupContent, override val editDate: DateTime?, override val forwarded: ForwardedMessage?, - override val replyTo: Message? + override val replyTo: Message?, + override val replyMarkup: InlineKeyboardMarkup? ) : MediaGroupMessage diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMessage.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMessage.kt index 2754550f26..1a2b11168b 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMessage.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/ChannelMessage.kt @@ -2,6 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message import com.github.insanusmokrassar.TelegramBotAPI.types.AuthorSignature import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier +import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.CommonMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.Message @@ -16,5 +17,6 @@ data class ChannelMessage( override val editDate: DateTime?, override val forwarded: ForwardedMessage?, override val replyTo: Message?, + override val replyMarkup: InlineKeyboardMarkup?, val authorSignature: AuthorSignature? ) : CommonMessage diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMediaGroupMessage.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMediaGroupMessage.kt index 77cad407a4..6095cc3daf 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMediaGroupMessage.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMediaGroupMessage.kt @@ -1,6 +1,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message import com.github.insanusmokrassar.TelegramBotAPI.types.* +import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MediaGroupContent @@ -15,5 +16,6 @@ data class CommonMediaGroupMessage( override val content: MediaGroupContent, override val editDate: DateTime?, override val forwarded: ForwardedMessage?, - override val replyTo: Message? + override val replyTo: Message?, + override val replyMarkup: InlineKeyboardMarkup? ) : MediaGroupMessage, FromUserMessage diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMessageImpl.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMessageImpl.kt index 94de3da542..e64ac56cea 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMessageImpl.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/CommonMessageImpl.kt @@ -2,6 +2,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier import com.github.insanusmokrassar.TelegramBotAPI.types.User +import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent @@ -17,5 +18,6 @@ data class CommonMessageImpl( override val editDate: DateTime?, override val forwarded: ForwardedMessage?, override val replyTo: Message?, + override val replyMarkup: InlineKeyboardMarkup?, val paymentInfo: PaymentInfo? ) : Message, CommonMessage, FromUserMessage \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt index b30b4dc58b..c8467d9e15 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/RawMessage.kt @@ -3,6 +3,7 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.message import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RawMessageEntities import com.github.insanusmokrassar.TelegramBotAPI.types.MessageEntity.RawMessageEntitiesSerializer +import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.chat.* import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.GroupChat import com.github.insanusmokrassar.TelegramBotAPI.types.files.* @@ -82,7 +83,9 @@ data class RawMessage( private val connected_website: String? = null, // passport property - private val passport_data: Unit? = null + private val passport_data: Unit? = null, + + private val reply_markup: InlineKeyboardMarkup? = null ) { @Transient private val content: MessageContent? by lazy { @@ -227,7 +230,8 @@ data class RawMessage( }, edit_date ?.asDate, forwarded, - reply_to_message ?.asMessage + reply_to_message ?.asMessage, + reply_markup ) else -> CommonMediaGroupMessage( messageId, @@ -242,7 +246,8 @@ data class RawMessage( }, edit_date ?.asDate, forwarded, - reply_to_message ?.asMessage + reply_to_message ?.asMessage, + reply_markup ) } } ?: when (chat) { @@ -254,6 +259,7 @@ data class RawMessage( edit_date ?.asDate, forwarded, reply_to_message ?.asMessage, + reply_markup, author_signature ) else -> CommonMessageImpl( @@ -265,6 +271,7 @@ data class RawMessage( edit_date ?.asDate, forwarded, reply_to_message ?.asMessage, + reply_markup, paymentInfo ) } diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/AbleToBeMarkedUp.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/AbleToBeMarkedUp.kt new file mode 100644 index 0000000000..aa0d969786 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/AbleToBeMarkedUp.kt @@ -0,0 +1,7 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts + +import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup + +interface AbleToBeMarkedUp { + val replyMarkup: InlineKeyboardMarkup? +} \ No newline at end of file diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/CommonMessage.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/CommonMessage.kt index 6a76684b95..ba53e0cfac 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/CommonMessage.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/message/abstracts/CommonMessage.kt @@ -6,4 +6,5 @@ interface CommonMessage : Message, AbleToBeForwardedMessage, AbleToBeEditedMessage, AbleToReplyMessage, + AbleToBeMarkedUp, ContentMessage From 85b3f8674675c5f1b43d39be73aef62c1fd938f2 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sat, 1 Jun 2019 17:31:48 +0800 Subject: [PATCH 4/8] fix in switchInlineQueryCurrentChat --- CHANGELOG.md | 1 + .../com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt | 1 + .../SwitchInlineQueryCurrentChatInlineKeyboardButton.kt | 3 ++- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 48d1ea66ab..e23c6f6230 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ * `LoginURL` and `LoginURLInlineKeyboardButton` has been added * `replyMarkup` field was added to the `CommonMessage` objects via `AbleToBeMarkedUp` interface +* `SwitchInlineQueryCurrentChatInlineKeyboardButton#switchInlineQueryCurrentChat` field fixed ## 0.15.0 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 b0dc8b4388..6ea61adc2b 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -87,6 +87,7 @@ const val isClosedField = "is_closed" const val loginUrlField = "login_url" const val forwardTextField = "forward_text" const val botUsernameField = "bot_username" +const val switchInlineQueryCurrentChatField = "switch_inline_query_current_chat" const val requestWriteAccessField = "request_write_access" diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryCurrentChatInlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryCurrentChatInlineKeyboardButton.kt index 731a3b4cca..fdfc663c49 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryCurrentChatInlineKeyboardButton.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryCurrentChatInlineKeyboardButton.kt @@ -1,11 +1,12 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons +import com.github.insanusmokrassar.TelegramBotAPI.types.switchInlineQueryCurrentChatField import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable @Serializable data class SwitchInlineQueryCurrentChatInlineKeyboardButton( override val text: String, - @SerialName("switch_inline_query_currentChat") + @SerialName(switchInlineQueryCurrentChatField) val switchInlineQueryCurrentChat: String ) : InlineKeyboardButton From 5e8bf82358c998f6f1db70bf728ee1690b093eaa Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 2 Jun 2019 22:02:12 +0800 Subject: [PATCH 5/8] InlineKeyboardButton as sealed class --- CHANGELOG.md | 1 + ...URLInlineKeyboardButton.kt => LoginURL.kt} | 10 +--- .../CallbackDataInlineKeyboardButton.kt | 14 ------ .../InlineKeyboardButton.kt | 47 +++++++++++++++++-- .../InlineKeyboardButtonSerializer.kt | 44 +++++++++++++++++ .../PayInlineKeyboardButton.kt | 10 ---- ...ineQueryCurrentChatInlineKeyboardButton.kt | 12 ----- .../SwitchInlineQueryInlineKeyboardButton.kt | 11 ----- .../URLInlineKeyboardButton.kt | 9 ---- 9 files changed, 90 insertions(+), 68 deletions(-) rename src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/{buttons/InlineKeyboardButtons/LoginURLInlineKeyboardButton.kt => LoginURL.kt} (55%) delete mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/CallbackDataInlineKeyboardButton.kt create mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt delete mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/PayInlineKeyboardButton.kt delete mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryCurrentChatInlineKeyboardButton.kt delete mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryInlineKeyboardButton.kt delete mode 100644 src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/URLInlineKeyboardButton.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index e23c6f6230..209f60bbb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * `LoginURL` and `LoginURLInlineKeyboardButton` has been added * `replyMarkup` field was added to the `CommonMessage` objects via `AbleToBeMarkedUp` interface * `SwitchInlineQueryCurrentChatInlineKeyboardButton#switchInlineQueryCurrentChat` field fixed +* `InlineKeyboardButton` now is sealed class and all its possible realisations are inside of its class file ## 0.15.0 diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/LoginURLInlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/LoginURL.kt similarity index 55% rename from src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/LoginURLInlineKeyboardButton.kt rename to src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/LoginURL.kt index fcf0bea1fa..13f223eebf 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/LoginURLInlineKeyboardButton.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/LoginURL.kt @@ -1,16 +1,8 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons +package com.github.insanusmokrassar.TelegramBotAPI.types -import com.github.insanusmokrassar.TelegramBotAPI.types.* import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable -@Serializable -data class LoginURLInlineKeyboardButton( - override val text: String, - @SerialName(loginUrlField) - val loginUrl: LoginURL -) : InlineKeyboardButton - @Serializable data class LoginURL( @SerialName(urlField) diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/CallbackDataInlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/CallbackDataInlineKeyboardButton.kt deleted file mode 100644 index 2610605f94..0000000000 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/CallbackDataInlineKeyboardButton.kt +++ /dev/null @@ -1,14 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons - -import com.github.insanusmokrassar.TelegramBotAPI.types.callbackDataField -import com.github.insanusmokrassar.TelegramBotAPI.types.textField -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable - -@Serializable -data class CallbackDataInlineKeyboardButton( - @SerialName(textField) - override val text: String, - @SerialName(callbackDataField) - val callbackData: String -) : InlineKeyboardButton diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt index 35741de405..22b1a27dad 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt @@ -1,10 +1,51 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons +import com.github.insanusmokrassar.TelegramBotAPI.types.* import kotlinx.serialization.* @Serializable(InlineKeyboardButtonSerializer::class) -interface InlineKeyboardButton { - val text: String +sealed class InlineKeyboardButton { + abstract val text: String } -object InlineKeyboardButtonSerializer : KSerializer by ContextSerializer(InlineKeyboardButton::class) +//TODO:: add check that this button first in a row (it MUST be first in a row) +@Serializable +data class PayInlineKeyboardButton( + override val text: String, + val pay: Boolean +) : InlineKeyboardButton() + +@Serializable +data class CallbackDataInlineKeyboardButton( + @SerialName(textField) + override val text: String, + @SerialName(callbackDataField) + val callbackData: String +) : InlineKeyboardButton() + +@Serializable +data class LoginURLInlineKeyboardButton( + override val text: String, + @SerialName(loginUrlField) + val loginUrl: LoginURL +) : InlineKeyboardButton() + +@Serializable +data class SwitchInlineQueryCurrentChatInlineKeyboardButton( + override val text: String, + @SerialName(switchInlineQueryCurrentChatField) + val switchInlineQueryCurrentChat: String +) : InlineKeyboardButton() + +@Serializable +data class SwitchInlineQueryInlineKeyboardButton( + override val text: String, + @SerialName("switch_inline_query") + val switchInlineQuery: String +) : InlineKeyboardButton() + +@Serializable +data class URLInlineKeyboardButton( + override val text: String, + val url: String +) : InlineKeyboardButton() diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt new file mode 100644 index 0000000000..2f289e39a0 --- /dev/null +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt @@ -0,0 +1,44 @@ +package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons + +import kotlinx.serialization.* +import kotlinx.serialization.internal.StringDescriptor +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonElementSerializer + +object InlineKeyboardButtonSerializer : KSerializer { + override val descriptor: SerialDescriptor = StringDescriptor.withName("com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons.InlineKeyboardButton") + + private val serializers = listOf( + CallbackDataInlineKeyboardButton.serializer(), + LoginURLInlineKeyboardButton.serializer(), + PayInlineKeyboardButton.serializer(), + SwitchInlineQueryInlineKeyboardButton.serializer(), + SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer(), + URLInlineKeyboardButton.serializer() + ) + + override fun deserialize(decoder: Decoder): InlineKeyboardButton { + val json = JsonElementSerializer.deserialize(decoder) + + serializers.forEach { + try { + return Json.nonstrict.fromJson(it, json) + } catch (e: SerializationException) { + e + } + } + + throw IllegalArgumentException("There is no known type of serializer for \"$json\" as inline button") + } + + override fun serialize(encoder: Encoder, obj: InlineKeyboardButton) { + when (obj) { + is CallbackDataInlineKeyboardButton -> CallbackDataInlineKeyboardButton.serializer().serialize(encoder, obj) + is LoginURLInlineKeyboardButton -> LoginURLInlineKeyboardButton.serializer().serialize(encoder, obj) + is PayInlineKeyboardButton -> PayInlineKeyboardButton.serializer().serialize(encoder, obj) + is SwitchInlineQueryInlineKeyboardButton -> SwitchInlineQueryInlineKeyboardButton.serializer().serialize(encoder, obj) + is SwitchInlineQueryCurrentChatInlineKeyboardButton -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer().serialize(encoder, obj) + is URLInlineKeyboardButton -> URLInlineKeyboardButton.serializer().serialize(encoder, obj) + } + } +} diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/PayInlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/PayInlineKeyboardButton.kt deleted file mode 100644 index edc357b468..0000000000 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/PayInlineKeyboardButton.kt +++ /dev/null @@ -1,10 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons - -import kotlinx.serialization.Serializable - -//TODO:: add check that this button first in a row (it MUST be first in a row) -@Serializable -data class PayInlineKeyboardButton( - override val text: String, - val pay: Boolean -) : InlineKeyboardButton diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryCurrentChatInlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryCurrentChatInlineKeyboardButton.kt deleted file mode 100644 index fdfc663c49..0000000000 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryCurrentChatInlineKeyboardButton.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons - -import com.github.insanusmokrassar.TelegramBotAPI.types.switchInlineQueryCurrentChatField -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable - -@Serializable -data class SwitchInlineQueryCurrentChatInlineKeyboardButton( - override val text: String, - @SerialName(switchInlineQueryCurrentChatField) - val switchInlineQueryCurrentChat: String -) : InlineKeyboardButton diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryInlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryInlineKeyboardButton.kt deleted file mode 100644 index a5134c6a8b..0000000000 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/SwitchInlineQueryInlineKeyboardButton.kt +++ /dev/null @@ -1,11 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons - -import kotlinx.serialization.SerialName -import kotlinx.serialization.Serializable - -@Serializable -data class SwitchInlineQueryInlineKeyboardButton( - override val text: String, - @SerialName("switch_inline_query") - val switchInlineQuery: String -) : InlineKeyboardButton diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/URLInlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/URLInlineKeyboardButton.kt deleted file mode 100644 index 5272e43f29..0000000000 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/URLInlineKeyboardButton.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons - -import kotlinx.serialization.Serializable - -@Serializable -data class URLInlineKeyboardButton( - override val text: String, - val url: String -) : InlineKeyboardButton From ae7365e9e9b7a83b2b187c97d89e6515b38e6789 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 2 Jun 2019 22:10:04 +0800 Subject: [PATCH 6/8] optimize deserialization of InlineKeyboardButton's --- .../TelegramBotAPI/types/Common.kt | 2 ++ .../InlineKeyboardButton.kt | 4 ++- .../InlineKeyboardButtonSerializer.kt | 33 ++++++++----------- 3 files changed, 19 insertions(+), 20 deletions(-) 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 6ea61adc2b..d6ce1db4da 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/Common.kt @@ -88,6 +88,7 @@ const val loginUrlField = "login_url" const val forwardTextField = "forward_text" const val botUsernameField = "bot_username" const val switchInlineQueryCurrentChatField = "switch_inline_query_current_chat" +const val switchInlineQueryField = "switch_inline_query" const val requestWriteAccessField = "request_write_access" @@ -199,6 +200,7 @@ const val resultsField = "results" const val certificateField = "certificate" const val questionField = "question" const val optionsField = "options" +const val payField = "pay" const val pointField = "point" const val xShiftField = "x_shift" diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt index 22b1a27dad..26809e7284 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButton.kt @@ -12,6 +12,7 @@ sealed class InlineKeyboardButton { @Serializable data class PayInlineKeyboardButton( override val text: String, + @SerialName(payField) val pay: Boolean ) : InlineKeyboardButton() @@ -40,12 +41,13 @@ data class SwitchInlineQueryCurrentChatInlineKeyboardButton( @Serializable data class SwitchInlineQueryInlineKeyboardButton( override val text: String, - @SerialName("switch_inline_query") + @SerialName(switchInlineQueryField) val switchInlineQuery: String ) : InlineKeyboardButton() @Serializable data class URLInlineKeyboardButton( override val text: String, + @SerialName(urlField) val url: String ) : InlineKeyboardButton() diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt index 2f289e39a0..4ee83f2133 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/buttons/InlineKeyboardButtons/InlineKeyboardButtonSerializer.kt @@ -1,34 +1,29 @@ package com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons +import com.github.insanusmokrassar.TelegramBotAPI.types.* import kotlinx.serialization.* import kotlinx.serialization.internal.StringDescriptor -import kotlinx.serialization.json.Json -import kotlinx.serialization.json.JsonElementSerializer +import kotlinx.serialization.json.* object InlineKeyboardButtonSerializer : KSerializer { override val descriptor: SerialDescriptor = StringDescriptor.withName("com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardButtons.InlineKeyboardButton") - private val serializers = listOf( - CallbackDataInlineKeyboardButton.serializer(), - LoginURLInlineKeyboardButton.serializer(), - PayInlineKeyboardButton.serializer(), - SwitchInlineQueryInlineKeyboardButton.serializer(), - SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer(), - URLInlineKeyboardButton.serializer() - ) + private fun resolveSerializer(json: JsonObject): KSerializer { + return when { + json[callbackDataField] != null -> CallbackDataInlineKeyboardButton.serializer() + json[loginUrlField] != null -> LoginURLInlineKeyboardButton.serializer() + json[payField] != null -> PayInlineKeyboardButton.serializer() + json[switchInlineQueryField] != null -> SwitchInlineQueryInlineKeyboardButton.serializer() + json[switchInlineQueryCurrentChatField] != null -> SwitchInlineQueryCurrentChatInlineKeyboardButton.serializer() + json[urlField] != null -> URLInlineKeyboardButton.serializer() + else -> throw IllegalArgumentException("Can't find correct serializer for inline button serialized as $json") + } + } override fun deserialize(decoder: Decoder): InlineKeyboardButton { val json = JsonElementSerializer.deserialize(decoder) - serializers.forEach { - try { - return Json.nonstrict.fromJson(it, json) - } catch (e: SerializationException) { - e - } - } - - throw IllegalArgumentException("There is no known type of serializer for \"$json\" as inline button") + return Json.nonstrict.fromJson(resolveSerializer(json.jsonObject), json) } override fun serialize(encoder: Encoder, obj: InlineKeyboardButton) { From 4e276846b47672f264e0bc2dd8ed6dd087669629 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 2 Jun 2019 22:57:52 +0800 Subject: [PATCH 7/8] String#asUsername -> String#toUsername --- CHANGELOG.md | 1 + .../insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 209f60bbb8..399724d00a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * `replyMarkup` field was added to the `CommonMessage` objects via `AbleToBeMarkedUp` interface * `SwitchInlineQueryCurrentChatInlineKeyboardButton#switchInlineQueryCurrentChat` field fixed * `InlineKeyboardButton` now is sealed class and all its possible realisations are inside of its class file +* `String#asUsername` method renamed to `String#toUsername` ## 0.15.0 diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt index dfaa6eb2c2..7877fe3fcd 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt @@ -32,7 +32,7 @@ data class Username( } } -fun String.asUsername(): Username = Username(this) +fun String.toUsername(): Username = Username(this) @Serializer(ChatIdentifier::class) internal class ChatIdentifierSerializer: KSerializer { From bdd1b9638a08f385224d66db59cfebd7031c847e Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Sun, 2 Jun 2019 22:59:24 +0800 Subject: [PATCH 8/8] toChatId extensions adding --- CHANGELOG.md | 1 + .../insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt | 2 ++ 2 files changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 399724d00a..3174454390 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ * `SwitchInlineQueryCurrentChatInlineKeyboardButton#switchInlineQueryCurrentChat` field fixed * `InlineKeyboardButton` now is sealed class and all its possible realisations are inside of its class file * `String#asUsername` method renamed to `String#toUsername` +* Several `toChatId` extensions added ## 0.15.0 diff --git a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt index 7877fe3fcd..c1d5928304 100644 --- a/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt +++ b/src/main/kotlin/com/github/insanusmokrassar/TelegramBotAPI/types/ChatIdentifier.kt @@ -20,6 +20,8 @@ val ChatId.link: String typealias UserId = ChatId fun Identifier.toChatId(): ChatId = ChatId(this) +fun Int.toChatId(): ChatId = toLong().toChatId() +fun Byte.toChatId(): ChatId = toLong().toChatId() @Serializable(ChatIdentifierSerializer::class) data class Username(