Fetch data from frontend and add to response


Is there a way to fetch data from frontend and add that data to the response and then send it to the user?

For example, I have a Flutter app as my frontend. Now, if the user asks for their current battery status, I want to fetch that battery status using the Flutter plugin (which gives me a number) and send it to Rasa so that it can add it in response.

Welcome to the forum :slight_smile:

Do you know about payloads? They are messages with a specific format to force an intent and entities.

Instead of sending “Hello, my name is Anand”, you could send “/greet{“name”: “Anand”}”.

So you can send a message in secret to your bot like “/inform{“battery”: “50%”}”.

Thank you for your reply! I still have a few confusions. I’m using the Rest API channel to talk to the chatbot using the Flutter app. (basically sending API requests from Flutter)

Let’s say the user asks the chatbot the battery status and that is detected by Rasa as an intent called “query_battery_status”, now how would I let the frontend know that Rasa needs battery status after which the frontend could request an intent with a payload like you suggested earlier.

Also, is the payload thing documented somewhere so that I can read about it? Basically, I’m not sure how to send this via API call.

Thank you!

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):

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

Thank you so much for such a detailed reply :slightly_smiling_face: This is really helpful!

1 Like