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

InlineKeyboardButton not working

Open LPirro opened this issue 4 years ago • 8 comments

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

tested on sendMessage()

LPirro avatar Nov 14 '20 18:11 LPirro

Any updates on this one?

nothingelsematters avatar Dec 19 '20 17:12 nothingelsematters

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

elbekD avatar Dec 20 '20 08:12 elbekD

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 avatar Mar 09 '21 18:03 nothingelsematters

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

nothingelsematters avatar Mar 24 '21 21:03 nothingelsematters

🌚

nothingelsematters avatar Apr 08 '21 16:04 nothingelsematters

👀

nothingelsematters avatar May 15 '21 15:05 nothingelsematters

@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.

romcooo avatar Nov 03 '21 08:11 romcooo

@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:

  • You cannot get button updates from getUpdates endpoint with allowed_updates parameter set to an empty list (Specify an empty list to receive all update types except chat_member (default)) or set to ["callback_query"]
  • Then send a request with allowed_updates set to ["message"] and then to ["callback_query"]
  • Done, everything works great again even with an empty list or without this optional parameter 🤷‍♂️

nothingelsematters avatar Nov 03 '21 09:11 nothingelsematters