kotlinjetbrains-space

Open web page on click at a button in a JetBrains Space chat message using Kotlin SDK


I have a simple chat bot that renders "on this day" facts based on Wikipedia.

As you can see, there's a button on my UI. I want that if the user taps on it, a new browser tab opens with given Wikipedia url.

Until now, I only see the possibility to send other messages using buttons and there action (PostMessageAction).

Do you have any tip on how to achieve my feature idea?

Code

      private fun makeMessage(container: OnThisDay): ChatMessage {
            return message {
...
                section {
                    text("On this day (${container.date}) happened:")

                    container.happenings.reversed().forEach {
                        text(
                            size = MessageTextSize.SMALL,
                            content = "${it.year}:\n${it.description}",
                        )

                        controls {
                            wikipediaButton(it.wikipediaUrl) <-- BUTTON
                        }

                        divider()
                    }
                }
            }
        }

        private fun MessageControlGroupBuilder.wikipediaButton(urlString: String) {
            val action = PostMessageAction("a", "b") <-- NO other action possible?
            button("Open Wikipedia", action, MessageButtonStyle.SECONDARY)
        }

Screenshot Space Chat bot


Solution

  • You can use NavigateUrlAction for this. Here's an example:

    message {
        section {
            controls {
                button(
                    text = "Open Wikipedia",
                    style = MessageButtonStyle.PRIMARY,
                    action = NavigateUrlAction(
                        url = "https://wikipedia.org",
                        withBackUrl = false,
                        openInNewTab = true
                    )
                )
            }
        }
    }