question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting into a loop

See original GitHub issue

Expected behavior there is a message block inside the callbackQuery(A). inside that message block we ask yes or no question with InlineKeyboardButton. for example, if the user chooses yes, it will go to callbackQuery(B). but if chooses no, callbackQuery(A) will be called again.

Actual behavior The problem starts when the user chooses no for the first time. after going throw all again, if he chooses no again, InlineKeyboardButton will popup twice!

To Reproduce

callbackQuery("A") {
    val chatId = callbackQuery.message?.chat?.id ?: return@callbackQuery
    bot.sendMessage(
        chatId = chatId,
        text = """some input""",
    )

    message {
        val inlineKeyboardMarkup = InlineKeyboardMarkup.create(
            listOf(
                InlineKeyboardButton.CallbackData(text = "no", callbackData = "A"),
                InlineKeyboardButton.CallbackData(text = "yes", callbackData = "B"),
            ),
        )
        bot.sendMessage(
            chatId = message.chat.id,
            text = """
            your choice
        """.trimIndent(),
            replyMarkup = inlineKeyboardMarkup
        )
    }
}

Additional context I think the possibility of my code being wrong is high but there is no place to ask my question, so I asked here so maybe, maybe, there is a bug.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
vjgarciag96commented, Jan 16, 2021

So… I guess you can implement that in several different ways. Would the next snippet do what you want to achieve (I guess you can just filter out things in the message handler when you are not interested on them):

sealed class OrderProcessState {
    object NotStarted : OrderProcessState()
    object AskingForOrderId : OrderProcessState()
    data class OrderIdReceived(val orderId: Int) : OrderProcessState()
    data class Completed(val order: Int) : OrderProcessState()
}

var orderProcessState: OrderProcessState = NotStarted

bot {
    token = ANY_TOKEN
    dispatch {
        command("start") {
            bot.sendMessage(
                chatId = message.chat.id,
                text = "How can I help you?",
                replyMarkup = InlineKeyboardMarkup.create(
                    listOf(
                        InlineKeyboardButton.CallbackData("Just do nothing", "Nothing"),
                        InlineKeyboardButton.CallbackData("Complete my order", "AskForOrderId")
                    )
                )
            )
        }

        callbackQuery("Nothing") {
            // Do nothing
        }

        callbackQuery("AskForOrderId") {
            val chatId = callbackQuery.message?.chat?.id ?: return@callbackQuery
            orderProcessState = AskingForOrderId
            bot.sendMessage(
                chatId = chatId,
                text = "What is your order id?"
            )
        }

        callbackQuery("OrderIdConfirmed") {
            val chatId = callbackQuery.message?.chat?.id ?: return@callbackQuery
            bot.sendMessage(chatId = chatId, text = "Awesome, order confirmed!")
        }

        message {
            when (orderProcessState) {
                NotStarted -> Unit // Ignore messages in this state
                is AskingForOrderId -> {
                    val orderId = message.text?.toInt() ?: return@message
                    orderProcessState = OrderIdReceived(orderId = orderId)
                    bot.sendMessage(
                        chatId = message.chat.id,
                        text = "Can you confirm your order id is $orderId?",
                        replyMarkup = InlineKeyboardMarkup.create(
                            listOf(
                                InlineKeyboardButton.CallbackData("Yes", "OrderIdConfirmed"),
                                InlineKeyboardButton.CallbackData("No", "AskForOrderId")
                            )
                        )
                    )
                }
                is OrderIdReceived -> Unit // Ignore messages in this state
                is OrderIdConfirmed -> Unit // Ignore messages in this state
            }
        }
    }
}.startPolling()
1reaction
vjgarciag96commented, Jan 15, 2021

@sinadarvi sorry for the delays in the response 🙏🏻, but I didn’t have time to check it.

I’ll try to take a look to it tomorrow and see what’s going on.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Are You Stuck In A Loop? - Medium
In Iran, when a couple is about to get married, the man essentially signs a document which mentions the amount of the dowry...
Read more >
The Cognitive Emotive Loop: What it is, Why it Keeps you ...
A cognitive/emotive loop is a repeating pattern where thoughts and beliefs produce feelings that fuel our rightness about our stories, that then ...
Read more >
If you got stuck in a loop, what should you do? - Quora
Control + C will break the execution of the loop and get you to the prompt. Alternatively you can use the Break key...
Read more >
to get stuck in a loop - WordReference Forums
It means "to get in a situation in which there is no way-out and in which the same things keep repeating themselves over...
Read more >
Human Existential Looping Problem (H.E.L.P.!)…
People who suffer with obsessive-compulsive disorder struggle with a loop of thoughts that drives repetitious behaviors.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found