chatbotrasa-nlurasarasa-corerasa-x

How to get latest bot response in rasa Chatbot?


How to get latest bot response with Rasa Chatbot?
For getting user input we use : tracker.latest_message['text']
So, what is syntax for getting latest bot response ?

Thanks


Solution

  • You can use the tracker.events list to fetch the latest bot event.

    bot_event = next(e for e in reversed(tracker.events) if e["event"] == "bot")
    

    This will go through the reversed list of events (making it from latest to oldest) and pick the first bot event using the next() function.

    The event will be have the following format:

    {'event': 'bot', 'timestamp': 1601789469.174273, 'text': 'Hey! How are you?', 'data': {'elements': None, 'quick_replies': None, 'buttons': None, 'attachment': None, 'image': None, 'custom': None}, 'metadata': {}}
    

    There you can simply take the 'text' param if you are only interested in the message.