kt-telegram-bot icon indicating copy to clipboard operation
kt-telegram-bot copied to clipboard

Parsing InlineKeyboardMarkup object by kotlinx.serialization.json.Json causes SerializationException

Open foxtrotdev opened this issue 2 years ago • 1 comments

I noticed that parsing InlineKeyboardMarkup object by kotlinx.serialization.json.Json causes SerializationException.

Crash log:

Unknown exception: kotlinx.serialization.SerializationException: Serializer for class 'Any' is not found.
Mark the class as @Serializable or provide the serializer explicitly.

Cause of problem:

private val markupToString = { a: Any -> toJson(a) } // com.elbekd.bot.internal.TelegramClient

Sample:

private val markupToString = { a: Any -> kotlinx.serialization.json.Json.encodeToString(a) }

val inlineKeyboardMarkup = InlineKeyboardMarkup(listOf(listOf(
    InlineKeyboardButton("Google", "https://www.google.com)
)))

val encodeToString = markupToString { inlineKeyboardMarkup } // crash!

It could be collect while sending photo:

telegramBot.sendPhoto(
    ChatId.IntegerId(TELEGRAM_CHANNEL),
    SendingFile(destinationFile),
    "Hello world",
    replyMarkup = inlineKeyboardMarkup
)

Library used:

implementation 'com.github.elbekD:kt-telegram-bot:2.1.7'

foxtrotdev avatar Oct 24 '22 10:10 foxtrotdev

This could fix the problem:

inline fun <reified T> markupToStringV2(a: T) : String = Json.encodeToString(a)

foxtrotdev avatar Oct 24 '22 11:10 foxtrotdev

thx. fixed in 2.1.8

elbekD avatar Oct 26 '22 04:10 elbekD