elbekD / kt-telegram-bot

Telegram Bot Library for Kotlin language
MIT License
103 stars 19 forks source link

InlineKeyboardButton not working #32

Closed LPirro closed 1 year ago

LPirro commented 3 years ago

InlineKeyboardButton is not working. I'm getting Bad Request: BUTTON_TYPE_INVALID from telegram

tested on sendMessage()

nothingelsematters commented 3 years ago

Any updates on this one?

elbekD commented 3 years ago

@nothingelsematters can u please show your sample code? because I don't get such error in my sample case

nothingelsematters commented 3 years ago

The first time I found out that buttons don't work was after Heroku deployment, they have been working successfully locally before.

Now I've faced the same issue locally with both long polling and web hook version. Could you point out what am I doing wrong?

import com.elbekD.bot.Bot
import com.elbekD.bot.server
import com.elbekD.bot.util.AllowedUpdate
import com.elbekD.bot.types.InlineKeyboardButton
import com.elbekD.bot.types.InlineKeyboardMarkup

fun main() {
    val token = Configuration.TELEGRAM_TOKEN
    val userName = Configuration.USER_NAME

    val bot = if (Configuration.IS_LONGPOLL) {
        println("polling")
        Bot.createPolling(userName, token) {
            allowedUpdates = listOf(AllowedUpdate.Message, AllowedUpdate.CallbackQuery)
        }
    } else {
        println("webhook")
        Bot.createWebhook(userName, token) {
            url = Configuration.APP_URL
            allowedUpdates = listOf(AllowedUpdate.Message, AllowedUpdate.CallbackQuery)

            server {
                host = Configuration.HOST
                port = Configuration.PORT
            }
        }
    }

    bot.onCommand("/start") { message, _ ->
        println("sendMessage")
        bot.sendMessage(
            message.chat.id,
            "sample",
            markup = InlineKeyboardMarkup(listOf(listOf(InlineKeyboardButton("sample", callback_data = "data"))))
        )
    }

    bot.onCallbackQuery("data") {
        println("Button worked")
        bot.answerCallbackQuery(it.id)
        val chatId = it.message?.chat?.id ?: return@onCallbackQuery
        bot.sendMessage(chatId, "Button worked")
    }

    bot.onAnyUpdate {
        println("UPD: $it")
    }

    bot.start()
}
nothingelsematters commented 3 years ago

@elbekD could you please examine the example snippet I've provided, if that wouldn't be bothering for you?

nothingelsematters commented 3 years ago

🌚

nothingelsematters commented 3 years ago

👀

romcooo commented 2 years ago

@nothingelsematters if this is still relevant, can you describe exactly how the code behaves for you? Do you get the same error as the author of the original issue? I remember having a similar issue with inline keyboards a couple months back, so I could try to help if it's still wanted.

nothingelsematters commented 2 years ago

@romcooo I've found out that it is not the library bug, but some telegram side magic.

I'm not completely sure, but this is why I think so:

I face this bug once in 1-2 months, the buttons attached to a message won't work. The problem is that button updates are not received by the bot. They are not even sent by Telegram Bot API since if you send a curl request on that endpoint, you won't get these updates.

Well, this is how I manage to fix this every time: