Dynamic multiple-choice


#1

Hello everyone,

I try to implement a dynamic single-choice option, I followed this link (https://stackoverflow.com/questions/51807404/botpress-how-to-reply-with-a-dynamic-multiple-choice)
But what I can’t figure out is how to get the value of the selected choice ? The value is not in state[‘skill-choice-ret’] where I thought it would have been
Cheers,

Babbel


#2

Hi @babbel! In the mentioned SO question, the OP is not using the “skill-choice”. The answer of a single_choice is (as far as I know) not stored anywhere as long as you don’t build any logic for it. You could use the “skill-choice” to handle the logic for you. Or you build your own logic, which could look as simple as follows:

//in actions.js
getSingleChoicePayload: async (state, event) => {
        let payload = event.raw.data.payload;
        event.bp.logger.debug("Custom Action (getSingleChoicePayload): got payload:", payload);
        if (payload) {
            // this is to lowerCase since for some reason the "value" for an option entered in the flow-editor is transformed to uppercase and it's not so intuitive to check for an uppercase value when the input is lowercase.
            payload = payload.toLowerCase();
        }
        else {
            console.warn("Payload empty - is the function called from a single-choice type of question?")
        }
        return {
            ...state,
            payload: payload
        }
    }

In your flow editor, you would then send the single_choice option onEnter, and then, in the onReceive of the same node you call the above method. As a result, in your transition part of the same node you can check for state.payload

Gotcha: the value entered as “value” for an option will be transformed by the system to uppercase, thus we take care of that in the action and check therefore for the same value we entered in the flow editor).

Btw: the cleanup you have to implement yourself too.

Hope this helps?

Cheers
Crixx


#3

It’s perfect, I adapted your solution to my flow and it works great ! Thank you