kt-telegram-bot
kt-telegram-bot copied to clipboard
InlineKeyboardButton not working
InlineKeyboardButton is not working. I'm getting Bad Request: BUTTON_TYPE_INVALID
from telegram
tested on sendMessage()
Any updates on this one?
@nothingelsematters can u please show your sample code? because I don't get such error in my sample case
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()
}
@elbekD could you please examine the example snippet I've provided, if that wouldn't be bothering for you?
🌚
👀
@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 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 withallowed_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 🤷♂️