1

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

Joffrey
  • 32,348
  • 6
  • 68
  • 100
Tobonaut
  • 2,245
  • 2
  • 26
  • 39

1 Answers1

2

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
                )
            )
        }
    }
}
  • Thanks, do you have a documentation page where all of the action types, etc. are listed? I was not able to find it :( – Tobonaut Oct 10 '22 at 09:39
  • 1
    You can use API Playground to create a sample API request. There you'll find all the possible parameter values, not only for button actions, but for any enumeration-like parameter of any API method. You can find API Playground in `Extensions` top-level menu. – Denis Zakharov Oct 11 '22 at 11:45