How to change payload and title of button dynamically and how to add number of buttons dynamically

in my action.py file

 if "bookOne" in product_group:
                buttons=[
                    {"payload": '/intent_language{"language":"german"}', "title":"German"},
                    {"payload": '/intent_language{"language":"english"}', "title":"English"},
                    {"payload": '/intent_language{"language":"spanish"}', "title":"Spanish"}
                ]
            elif "bookTwo" in product_group:
                buttons=[
                    {"payload": '/intent_language{"language":"german"}', "title":"German"},
                    {"payload": '/intent_language{"language":"english"}', "title":"English"},
                    {"payload": '/intent_language{"language":"spanish"}', "title":"Spanish"},
                    {"payload": '/intent_language{"language":"italian"}', "title":"Italian"},
                    {"payload": '/intent_language{"language":"french"}', "title":"French"},
                ]
            elif "bookThree" in product_group:
                buttons=[
                    {"payload": '/intent_language{"language":"german"}', "title":"German"},
                    {"payload": '/intent_language{"language":"english"}', "title":"English"}
                ]
            elif "bookfour" in product_group:
                buttons=[
                    {"payload": '/intent_language{"language":"german"}', "title":"German"},
                    {"payload": '/intent_language{"language":"english"}', "title":"English"}
                ]
            dispatcher.utter_message(text="In which language you want Data Sheet?", buttons=buttons)

Here, product_group is a list of books.

Some books are available in 4 languages, some are 3 and some are 2 languages.

what I want:

books = [bookOne, bookTwo, BookThree, BookFoue]
    if books in product_group:
                buttons=[
                    {"payload": '/intent_language{"language":"dynamic_langauge"}', "title":"dynamic_name"},
                    {"payload": '/intent_language{"language":"dynamic_langauge"}', "title":"dynamic_name"},
                    {"payload": '/intent_language{"language":"dynamic_langauge"}', "title":"dynamic_name"}
                ]

so here, if the book is available in 4 languages, then there should be 4 buttons, if it is available in 2, there should be only 2 buttons. If books are available in German and English then the payload and title of the button should be dynamic, German and English

In my book list, there are 10000 books, from 10000 I want to show only 500. Out of 500, some books are available in some specific language. How can many buttons, payload, and title be dynamic? I can not write for 500 books.

in which language book is available, that data coming from xlsx file. So, my idea is if the book is not available in English then the flag will be false and a button should not be added for the English language. if someone has a better idea then suggestions are welcome.

Heya @tanayraj !

can you please confirm, what is your current frontend i.e chat widget? and above code is displaying you some output, can you share the output?

This is a question about Python rather then Rasa, but you can do this:

books = get_books_from_file() # dynamically get a list of all available books in your XLSX

for book in books:
    if book in product_group:
        languages = get_languages_from_file(book) #  dynamically get a list of all available languages for this book in your XLSX
        buttons = []

        for language in languages:
            button = {"payload": '/intent_language{"language":"' + language + '"}', "title": language.title()
            buttons.append(button)

Compact form:

for book in get_books_from_file():
    if book in product_group:
        buttons = []
        for language in get_languages_from_file(book):
           buttons.append({"payload": '/intent_language{"language":"' + language + '"}', "title": language.title())

You have to implement get_books_from_file() and get_languages_from_file() yourself since I don’t have access to your data schema and therefore cannot write them. If you need help for that, please post on StackOverflow since this is a general coding problem unrelated to Rasa.

Hope this helps :slight_smile:

2 Likes

@tanayraj adding to Chris code :slight_smile:

buttons.append(button)
dispatcher.utter_message(text="text or template to show", buttons=buttons)

buttons.append({"payload": '/intent_language{"language":"' + language + '"}', "title": language.title())
dispatcher.utter_message(text="text or template to show", buttons=buttons)
2 Likes

Maybe you both are right. I am new to python as well as to rasa. I don’t know much about python. So, you are right this might be related to python only.

I will try your solutions.

Thank you @ChrisRahme

1 Like

Ya, i will try this. Thank you @nik202