How to call an action/command every time new information is received from user?

I’d like to call an external api each time I receive new information from a user. The idea beging that the api is called and provided the latest entity/slot information and updates a database.

I currently have an action (action_check_info) that is called according to the story and updates the infromation that I need it to. I want this to be called every time new information is received rather than controlled by a particular action.

class ActionCheckInfo(Action):
    def name(self) -> Text:
        return "action_check_info"

    def run(self,
            dispatcher: CollectingDispatcher,
            tracker: Tracker,
            domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

        info_to_check = tracker.slots['info']
        info_dict = infocheck.check_info(info_to_check)
        return [SlotSet("info_converted", str(info_dict))]

One approach would be to use forms. The FormAction would be invoked as slots are filled and you could use the slot_mapping call to make the API calls to update your data.

That works, thanks.

Just looking into this again, as the performance seemed slow.

I’ve actually placed my api call in required_slots rather than slot_mappings as I needed access to tracker to get the current slot values.

It seems that this function is called three times after receiving an entry (by forms.activate, forms.request_next_slot and log_form_slots) - this means that my api call is triggered three times too.

Is there some way of only calling it once per information gathered by the bot?

Maybe you can try to call the api in the validate_{slot_name} function ? When the user provides an information, you validate it (if needed) then call the api. If the user provides valid data, the bot won’t ask for the slot again, therefore the validate function only get executed once (i think).

1 Like