Different response for each users types

Great :slight_smile:


What are Payloads

First, a payload is a way to force an intent with 0 or many entities. It’s in the form /intent{"entity1": "value1", "entity2": "value2"}.

For example, if you want to greet the bot and give it your name, instead of sending “Hello, my name is Chris”. you can send /greet{"name": "Chris"}.

This is basically what a button does as you can see here, but you can also send it as a message, and therefore via the webhook too.


Sending a Hidden Message to Rasa

Using the payload method, you can send a hidden message from your app to Rasa.

You surely have a function to send a message, and a function to display the user’s message on-screen once sent.

If these two features are actually in the same function, I recommend splitting them so that you can optionally call the display feature or not. Something like this:

function send(message, display = True) {
    sendUserMessage({message: message, sender: sender_id})

    if (display) {
        displayUserMessage(message)
    }
}

Sending a Hidden Payload to Rasa

The same way you can send a message, you can also send a payload.

Usually, the user enters a text message, and clicks on a “send” button. This button should call the above send() function with display = True.

Of course, the user can also send payloads, and you can block this feature on your app (maybe don’t send the user’s message if it starts with / in your button’s event handler).

What you want is only to allow the system itself to send payloads, and of course, you don’t want the user to see this. So, when you do that, call the above send() function with display = False.

This call can be done as soon as you get the user’s type, in whichever method - I won’t go into details here since you built the app and probably know how to retrieve the info you need.

Let’s say the user type is available as soon as the page is loaded. All you’ll have to do then is the following:

$(document).ready(function() {
    const user_type = document.getElementById('user_type').innerText // Or whatever method you use to get this info
    const payload = `/inform{"user_type": "${user_type}"}` // Be careful to enclose the entity value in quotes - It should be a valid JSON
   
    send(payload, False)
})

Of course, you would have to define the inform intent in the NLU and give at least two examples with the type entity, even if this will never be directly set by the user:

nlu:
- intent: inform
  examples: |
    - [type1](user_type)
    - [type2](user_type)
    - [English](language)
    - [french]{"entity": "language", "value": "French"}
    - [eng]{"entity": "language", "value": "English"}

Uttering a Sentence According to a Slot

Conditional Response Variations allow you to utter a sentence based on conditions. These conditions can be slot values!

There is more info in the link I mentioned right now, and don’t hesitate to ask for help, but you will basically do something like this:

responses:
  utter_greet:
    - condition:
        - type: slot
          name: user_type
          value: type1
      text: "Hi, type 1 user!"

    - condition:
        - type: slot
          name: user_type
          value: type 2
      text: "What's up, type 2 user?"

    - text: "Hey! I'm not sure what type of user you are..."
2 Likes