How to get values from payload in button after choosing an option?

Hi there, I want to get access to the payload values from my custom buttons. The main idea is to choose one option from the buttons, and then collect the payload values in a variable, in order to pass these to the next action.

The custom buttons are in a loop where from each items I collect the needed values for the “titles” and for the “payloads”. I want to achieve this in a dynamic way, since I have a lot of different variables with different length.

Here is my code until now:

    def run(self, dispatcher: CollectingDispatcher,
             tracker: Tracker, domain: Dict[Text, Any],
         ) -> Text:
       for element in request.question.items:
       for l in element['choices']:
       buttons.append({"title": "{}".format(l['label']), "payload": "{}{}".format(element['id'],l['id'])})

       return []

dispatcher.utter_button_message(message, buttons)

Again, my question is how to access the selected payload values. In rasa x I can see that the payload is being selected properly, for example: "11,true".

But I find no way to get these values into another variable.

Any help will be appreciated.

Thanks

2 Likes

Have you tried tracker.latest_message.get(‘text’)? This gets the latest message passed as input. Since when a button is pressed, the payload is sent to the NLU as input, I guess this could fetch the payload.

1 Like

Hi naag, Thank you for your answer. I did try that indeed, but I am only getting the last user input, as you said, the title of the buttons if you want. But I need the information behind that. If you see my code above again, I need an id which correspond to that element in the title.

I can see it is being called right in the payload, I just don’t know how to store this information in a variable in order to reuse it.

Any help please?

1 Like

Use slots for storing the payload and mention the same in stories

Could you give an example, how I can store the payload information in a slot based on my example above? That would be very helpful.

make a slot for example TEMP in domain file then in your first custom action use function SlotSet to set the slot value to desired value https://rasa.com/docs/rasa/api/events/ and then use that value in next actions using getSlots method https://rasa.com/docs/rasa/api/tracker/

@ramonrod If you want to obtain the payload values after the user presses the button, you’ll have to configure slots in your domain.yml file and set slot values in the buttons accordingly. When the user presses the button, the values mentioned in the payload are extracted and assigned to the respective slots. You can configure stories to act accordingly when a certain slot is set.

To get the value of a certain slot in your custom action, use tracker.get_slot(“slot_name”), where slot_name is the slot that you wish to extract.

Ensure that whichever entity value you intend to extract has a slot with the same name as the entity name. Check out RASA Slots

Follow this format to set slots in the button payload:

To set a single slot - “payload”:"/intent_name{/“slot1/”:/“value1/”}

To set multiple slots - “payload”:"/intent_name{/“slot1/”:/“value1/”,/“slot2/”:/“value2/”}

Note that you need to escape the double quotes with the backslash as this is parsed as a json and it is necessary for the double quote characters to be present for this to work.

1 Like

Hi naag, This sounds reasonable. I will give it a try.

Thanks for the advise.

Hey @ramonrod can you guide me how you have achieved saving button value in variable

Tried @naag’s solution but wasn’t lucky =/

In actions.py I have:

my_slot = tracker.get_slot("my_slot")
intents:
    - start
    - greet
    - goodbye

entities:
  - my_slot

slots: 
  my_slot:
    type: categorical
    values:
    - a
    - b
    - c
    - d

responses:  
  utter_start:
    - text: What would you like to do?
      buttons:
      - title: A
        payload: '/greet{"my_slot": "a"}'
      - title: B
        payload: '/greet{"my_slot": "b"}'
      - title: C
        payload: '/greet{"my_slot": "c"}'
      - title: D
        payload: '/greet{"my_slot": "d"}'

But my_slot is None always. What am I doing wrong?

Did you manage to solve this? Can you post the output of rasa shell --debug for this?

Hi,

Create intent for slot and train with some example.