Fetch data from frontend and add to response

Then you need to implement the payload system in your UI as well, which is very easy :slight_smile: But before, let’s talk about payloads.


It doesn’t matter what you are using to send the message. Whether it’s via API, Rasa X, or Rasa Shell, if you send Rasa a message of the format /intent{"entity_name": "entity_value"}, it’s a payload - you just send it like any regular message.

Maybe even make sure that if the user manually types an message, it does not start with / (unless you want it of course).

You can see payloads with buttons, but you only need to use double brackets in domain.yml. But make sure you are using double quotes inside the brackets - it should be a valid JSON.

You can read this post for more info about payloads and sending hidden payloads from your frontend app.


Now that you know about Rasa payloads, you can implement your own Frontend payloads.

Basically, if the frontend receives any message from Rasa starting with /, it should be ready to execute a custom function.

So, when Rasa sees query_battery_status, it should send back a message like /query_battery_status. When your app detects this message, it should send back /inform{"battery": "50%"} to Rasa, which will finally send “Your battery level is 50%” to the frontend.

Of course, you want to implement a system which does not display messages sent and recieved with /.

I will write the logic in Python since it is more concise and I don’t know Flutter:

def recieve(bot_message):
    if 'text' in bot_message and bot_message['text'][0] == '/': # message starts with '/'
        excutePayload(bot_message['text'][1:]) # send the payload name and don't display that messgae
    else: # message is regular
        display(message = bot_message, sender = 'bot')


def executePayload(payload):
    if payload == 'query_battery_status':
         battery_level = getBattery()
         send(user_message = '/inform{"battery": "'+battery_level+'%"}', hidden = True)


def send(user_message, hidden = False):
    sendToRasaViaApi(user_message)

    if not hidden:
        display(message = user_message, sender = 'bot')
1 Like