How to get the last user input in Rasa

I can fetch the last user-inputted data from RASA chatbot using this method.

text = tracker.latest_message['text']

It’s working perfectly in my case. Now I want to fetch the text of a button pressed by the user. For example, if the user presses the allion button, I want my text variable to hold as text = "allion".

enter image description here

Is there any preferable solution?

Hi @shaiful019,

Its a tricky one, When you press the button allion the text will be printed on the front end you are using (Facebook or JS UI) and will never be sent to bot as a text request. The request will sent to bot will be your payload you have set on the button

So you can make your button payload to call an intent like cartype and write a story like this.

 * cartype{"car": "allion"}
       - action_car
       - slot{"carModel":"allion"}

declare the cartype intent in nlu.md

## intent:cartype
- [Allion](car)
- [axio](car)
- [prius](car)

and retrieve it in actions.py.

   class carModel(Action):
        def name(self) -> Text:
            return "action_car"
      
         def run(self, dispatcher: CollectingDispatcher,
                   tracker: Tracker,
                    domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
                car= tracker.get_slot('car')
                print("car", car)
                return [SlotSet("carModel", car)]

Hi @MuraliChandran14 , Appreciate your response. The problem is I don’t use any slot/entity here. I just use intent. The buttons you’re seeing in the picture is getting from API. Is there any simple way to get the text form of a button pressed by a user?

Hi @shaiful019, Can you post your actions.py. I will see what I can do.

Here’s my Action. If user press a button like allion, I need to fetch the text of this button and make another GET API request.

class ActionVehicleManufecture(Action):
def name(self):
    return "action_vehicle_model"

def run(self, dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
    
    manufacture = str(tracker.latest_message['text'])
    link = "http://mind.zantrik.com/api/A1/VehicleModel?Type=car&Manufacturer="+manufacture
    r = requests.get(link)
    response = r.text
    json_data = json.loads(response)
    l = []
    buttons = []
    for i in json_data["data"]:
        l.append(i["text"])
    for i in l:
        buttons.append({"title": i, "payload": "/vehicle_model"})

    dispatcher.utter_message(text="Please let me know you car Model name:", buttons=buttons)
    return []

If the user press allion button, the manufacture variable given below store “/vehicle_model” this value. I want to store “allion” as string.

manufacture = str(tracker.latest_message[‘text’])

hi @shaiful019,

To my knowledge you have to use slots to fill in the button input, you cannot get text using tracker.latest_message[‘text’]

for more reference you can refer below:

Hope this helps

Hi @shaiful019, I have the same problem as you. Did you manage to solve this?

Hey @tcthanh98, I solved this problem declaring my button like this way.

buttons = [
                {"title": "Battery", "payload": "Battery"},
                {"title": "Alternator", "payload": "Alternator"},
                {"title": "Sensor", "payload": "Sensor"},
                {"title": "Actuator", "payload": "Actuator"},
                {"title": "Relay", "payload": "Relay"},
                {"title": "Injector", "payload": "Injector"},
                {"title": "Self_starter", "payload": "Self_starter"}
            ]
1 Like

Thanks you. I will give it a try

Het @shaiful019 could you please share your custom actions code to get the button value, and how you are getting the button value using tracker.latest_message[‘text’]

Hi @sai4teja3, you can create a new post explaining what you need to get a much-updated answer!