From 61cd461eff4ca107cb63b16693f6ad995af37ee0 Mon Sep 17 00:00:00 2001 From: Andrew Anfanik Date: Thu, 31 Oct 2024 18:01:59 +0200 Subject: [PATCH 1/3] Add CopyTextButton to InlineKeyboardButton --- .../pengrad/telegrambot/model/request/CopyTextButton.kt | 5 +++++ .../telegrambot/model/request/InlineKeyboardButton.kt | 8 +++++++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 library/src/main/java/com/pengrad/telegrambot/model/request/CopyTextButton.kt diff --git a/library/src/main/java/com/pengrad/telegrambot/model/request/CopyTextButton.kt b/library/src/main/java/com/pengrad/telegrambot/model/request/CopyTextButton.kt new file mode 100644 index 00000000..cc4057fb --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/request/CopyTextButton.kt @@ -0,0 +1,5 @@ +package com.pengrad.telegrambot.model.request + +data class CopyTextButton( + val text: String +) \ No newline at end of file diff --git a/library/src/main/java/com/pengrad/telegrambot/model/request/InlineKeyboardButton.kt b/library/src/main/java/com/pengrad/telegrambot/model/request/InlineKeyboardButton.kt index 59727554..644d0b72 100644 --- a/library/src/main/java/com/pengrad/telegrambot/model/request/InlineKeyboardButton.kt +++ b/library/src/main/java/com/pengrad/telegrambot/model/request/InlineKeyboardButton.kt @@ -16,7 +16,8 @@ data class InlineKeyboardButton @JvmOverloads constructor( @get:JvmName("switchInlineQueryChosenChat") var switchInlineQueryChosenChat: SwitchInlineQueryChosenChat? = null, @get:JvmName("callbackGame") var callbackGame: CallbackGame? = null, var pay: Boolean? = null, - @get:JvmName("webApp") var webApp: WebAppInfo? = null + @get:JvmName("webApp") var webApp: WebAppInfo? = null, + @get:JvmName("copyText") var copyText: CopyTextButton? = null, ) { fun url(url: String): InlineKeyboardButton { @@ -65,6 +66,11 @@ data class InlineKeyboardButton @JvmOverloads constructor( return this } + fun copyText(copyText : CopyTextButton): InlineKeyboardButton { + this.copyText = copyText + return this + } + fun isPay(): Boolean { return pay ?: false } From 125631239884831b0ad1dd77738dd8b6779089f1 Mon Sep 17 00:00:00 2001 From: Andrew Anfanik Date: Thu, 31 Oct 2024 18:09:15 +0200 Subject: [PATCH 2/3] Add allow_paid_broadcast parameter to send requests --- .../com/pengrad/telegrambot/request/AbstractSendRequest.java | 4 ++++ .../java/com/pengrad/telegrambot/request/CopyMessage.java | 4 ++++ .../java/com/pengrad/telegrambot/request/SendPaidMedia.java | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/library/src/main/java/com/pengrad/telegrambot/request/AbstractSendRequest.java b/library/src/main/java/com/pengrad/telegrambot/request/AbstractSendRequest.java index 4c292a56..801a3529 100644 --- a/library/src/main/java/com/pengrad/telegrambot/request/AbstractSendRequest.java +++ b/library/src/main/java/com/pengrad/telegrambot/request/AbstractSendRequest.java @@ -57,4 +57,8 @@ public T protectContent(boolean protectContent) { return add("protect_content", protectContent); } + public T allowPaidBroadcast(boolean allowPaidBroadcast) { + return add("allow_paid_broadcast", allowPaidBroadcast); + } + } diff --git a/library/src/main/java/com/pengrad/telegrambot/request/CopyMessage.java b/library/src/main/java/com/pengrad/telegrambot/request/CopyMessage.java index a4e74616..62cb0333 100644 --- a/library/src/main/java/com/pengrad/telegrambot/request/CopyMessage.java +++ b/library/src/main/java/com/pengrad/telegrambot/request/CopyMessage.java @@ -62,4 +62,8 @@ public CopyMessage protectContent(boolean protectContent) { return add("protect_content", protectContent); } + public CopyMessage allowPaidBroadcast(boolean allowPaidBroadcast) { + return add("allow_paid_broadcast", allowPaidBroadcast); + } + } diff --git a/library/src/main/java/com/pengrad/telegrambot/request/SendPaidMedia.java b/library/src/main/java/com/pengrad/telegrambot/request/SendPaidMedia.java index 6e7d0b64..dc64aa91 100644 --- a/library/src/main/java/com/pengrad/telegrambot/request/SendPaidMedia.java +++ b/library/src/main/java/com/pengrad/telegrambot/request/SendPaidMedia.java @@ -82,4 +82,8 @@ public SendPaidMedia replyMarkup(ForceReply replyMarkup) { return add("reply_markup", replyMarkup); } + public SendPaidMedia allowPaidBroadcast(boolean allowPaidBroadcast) { + return add("allow_paid_broadcast", allowPaidBroadcast); + } + } From c49e10973e17abbe664c4fb9362c978daa2fefc7 Mon Sep 17 00:00:00 2001 From: Andrew Anfanik Date: Thu, 31 Oct 2024 18:19:35 +0200 Subject: [PATCH 3/3] Add TransactionPartnerTelegramApi transaction partner, rewrite TransactionPartnerTypeAdapter to kotlin --- .../TransactionPartnerTelegramApi.java | 39 +++++++++++++++++++ .../pengrad/telegrambot/utility/BotUtils.java | 2 +- .../gson/TransactionPartnerTypeAdapter.java | 32 --------------- .../gson/TransactionPartnerTypeAdapter.kt | 31 +++++++++++++++ 4 files changed, 71 insertions(+), 33 deletions(-) create mode 100644 library/src/main/java/com/pengrad/telegrambot/model/stars/partner/TransactionPartnerTelegramApi.java delete mode 100644 library/src/main/java/com/pengrad/telegrambot/utility/gson/TransactionPartnerTypeAdapter.java create mode 100644 library/src/main/java/com/pengrad/telegrambot/utility/gson/TransactionPartnerTypeAdapter.kt diff --git a/library/src/main/java/com/pengrad/telegrambot/model/stars/partner/TransactionPartnerTelegramApi.java b/library/src/main/java/com/pengrad/telegrambot/model/stars/partner/TransactionPartnerTelegramApi.java new file mode 100644 index 00000000..fbc6bba4 --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/model/stars/partner/TransactionPartnerTelegramApi.java @@ -0,0 +1,39 @@ +package com.pengrad.telegrambot.model.stars.partner; + +import java.util.Objects; + +public class TransactionPartnerTelegramApi extends TransactionPartner { + + public final static String TYPE = "telegram_api"; + + private Integer request_count; + + public TransactionPartnerTelegramApi() { + super(TYPE); + } + + public Integer requestCount() { + return request_count; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof TransactionPartnerTelegramApi)) return false; + if (!super.equals(o)) return false; + TransactionPartnerTelegramApi that = (TransactionPartnerTelegramApi) o; + return Objects.equals(request_count, that.request_count); + } + + @Override + public int hashCode() { + return Objects.hash(super.hashCode(), request_count); + } + + @Override + public String toString() { + return "TransactionPartnerTelegramApi{" + + "request_count=" + request_count + + '}'; + } +} diff --git a/library/src/main/java/com/pengrad/telegrambot/utility/BotUtils.java b/library/src/main/java/com/pengrad/telegrambot/utility/BotUtils.java index 4f34db74..4df59f51 100644 --- a/library/src/main/java/com/pengrad/telegrambot/utility/BotUtils.java +++ b/library/src/main/java/com/pengrad/telegrambot/utility/BotUtils.java @@ -36,7 +36,7 @@ private BotUtils() {} .registerTypeAdapter(BackgroundType.class, new BackgroundTypeAdapter()) .registerTypeAdapter(BackgroundFill.class, new BackgroundFillAdapter()) .registerTypeAdapter(RevenueWithdrawalState.class, new RevenueWithdrawalStateTypeAdapter()) - .registerTypeAdapter(TransactionPartner.class, new TransactionPartnerTypeAdapter()) + .registerTypeAdapter(TransactionPartner.class, TransactionPartnerTypeAdapter.INSTANCE) .registerTypeAdapter(PaidMedia.class, new PaidMediaTypeAdapter()) .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES) .create(); diff --git a/library/src/main/java/com/pengrad/telegrambot/utility/gson/TransactionPartnerTypeAdapter.java b/library/src/main/java/com/pengrad/telegrambot/utility/gson/TransactionPartnerTypeAdapter.java deleted file mode 100644 index 036aff36..00000000 --- a/library/src/main/java/com/pengrad/telegrambot/utility/gson/TransactionPartnerTypeAdapter.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.pengrad.telegrambot.utility.gson; - -import com.google.gson.*; -import com.pengrad.telegrambot.model.stars.partner.*; -import com.pengrad.telegrambot.model.stars.withdrawal.RevenueWithdrawalState; -import com.pengrad.telegrambot.model.stars.withdrawal.RevenueWithdrawalStateFailed; -import com.pengrad.telegrambot.model.stars.withdrawal.RevenueWithdrawalStatePending; -import com.pengrad.telegrambot.model.stars.withdrawal.RevenueWithdrawalStateSucceeded; - -import java.lang.reflect.Type; - -public class TransactionPartnerTypeAdapter implements JsonDeserializer { - - @Override - public TransactionPartner deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException { - JsonObject object = element.getAsJsonObject(); - JsonPrimitive primitive = object.getAsJsonPrimitive("type"); - String discriminator = primitive != null ? primitive.getAsString() : "unknown"; - - if (TransactionPartnerUser.TYPE.equals(discriminator)) { - return context.deserialize(object, TransactionPartnerUser.class); - } else if (TransactionPartnerFragment.TYPE.equals(discriminator)) { - return context.deserialize(object, TransactionPartnerFragment.class); - } else if (TransactionPartnerTelegramAds.TYPE.equals(discriminator)) { - return context.deserialize(object, TransactionPartnerTelegramAds.class); - } else if (TransactionPartnerOther.TYPE.equals(discriminator)) { - return context.deserialize(object, TransactionPartnerOther.class); - } - - return new TransactionPartner(discriminator); - } -} diff --git a/library/src/main/java/com/pengrad/telegrambot/utility/gson/TransactionPartnerTypeAdapter.kt b/library/src/main/java/com/pengrad/telegrambot/utility/gson/TransactionPartnerTypeAdapter.kt new file mode 100644 index 00000000..5fef300a --- /dev/null +++ b/library/src/main/java/com/pengrad/telegrambot/utility/gson/TransactionPartnerTypeAdapter.kt @@ -0,0 +1,31 @@ +package com.pengrad.telegrambot.utility.gson + +import com.google.gson.* +import com.pengrad.telegrambot.model.stars.partner.* +import java.lang.reflect.Type + +object TransactionPartnerTypeAdapter : JsonDeserializer { + + private val typeMapping = mapOf( + TransactionPartnerUser.TYPE to TransactionPartnerUser::class, + TransactionPartnerFragment.TYPE to TransactionPartnerFragment::class, + TransactionPartnerTelegramAds.TYPE to TransactionPartnerTelegramAds::class, + TransactionPartnerTelegramApi.TYPE to TransactionPartnerTelegramApi::class, + TransactionPartnerOther.TYPE to TransactionPartnerOther::class + ) + + @Throws(JsonParseException::class) + override fun deserialize( + element: JsonElement, + type: Type, + context: JsonDeserializationContext + ): TransactionPartner { + val obj = element.asJsonObject + val discriminator = obj.getAsJsonPrimitive("type")?.asString ?: "unknown" + + return typeMapping[discriminator]?.let { + context.deserialize(obj, it.java) + } ?: TransactionPartner(discriminator) + } + +} \ No newline at end of file