Form action with custom logic for asking the required slot

Is it possible to ask the user about a slot based on some custom logic instead of a pre-defined template? I’m not talking about validating the slot after filling it, instead, I want to ask the user about a slot in different ways depending on some custom logic. How can I do so with form action?

Hi @saurabh-m523,

the SDK provides a method request_next_slot that you might want to overwrite:

# noinspection PyUnusedLocal
def request_next_slot(
    self,
    dispatcher: "CollectingDispatcher",
    tracker: "Tracker",
    domain: Dict[Text, Any],
) -> Optional[List[EventType]]:
    """Request the next slot and utter template if needed,
        else return None"""

    for slot in self.required_slots(tracker):
        if self._should_request_slot(tracker, slot):
            logger.debug(f"Request next slot '{slot}'")
            dispatcher.utter_message(template=f"utter_ask_{slot}", **tracker.slots)
            return [SlotSet(REQUESTED_SLOT, slot)]

    # no more required slots to fill
    return None

As you can see, it asks with utter_ask_{slot} for the slot - you can easily manipulate this for your needs!

Did that help?

Kind regards
Julian

1 Like

Hi @JulianGerhard!

Thanks for pointing me in that direction. That was helpful.

I have the same question, but i don’t understand from the solution how you can identify that it is the second time the bot has to ask this question and then adjust your utter_message

e.g.
Bot: Does that sound good to you? (First attempt: expecting yes or no)
User: Banana
Bot: Are you ready to go on then? (Second attempt, different message)
User: Yes / No / Banana …

Hi @Yardie83!

You can easily do so in the validation function of that slot (if your code is reaching the validation function then it’s probably because the bot has already asked for that slot), but, if for some reason, you don’t wanna do it there then:

The SDK’s tracker object stores all the events. You can look for the bot_uttered event that contains the first time message of that slot in the tracker.

You could use the events_after_latest_restart() method to give you all the events that have occurred till now since the latest restart event and then search for the event that interests you.

If that event is present in the tracker then it means the bot has already asked that question so now you can utter the second message instead.

Try printing the results of events_after_latest_restart() method, you’ll get what I’m trying to say. You can do this inside the request_next_slot() method.

Hope that helps.

cool, thanks for the quick reply. Appreciate it

1 Like