Share data between custom actions

Hi! I’m building a bot where in the first user interaction I make a third party API request to get product data. That’s done in one specific action.

But then I’d like to use the same data in another actions but I don’t know how to share or where to save the data collected from the first one.

Hi @mrepot33. You can set the details extracted by one action as slots. Then in your other action, you can extract the values of those slots using the method tracker.get_slot("slot_name")

Hi @Juste, I’ve already tried that but the slot was then empty. This is my code.

“data” is a big json containing a lot of data from an item that I’d like to share between actions, avoiding making third party API requests each time. Sharing this data across actions would be the ideal solution.

A workaround solution would be to use a cache (redis in this case) in order to store the data there, using the “itemid” as the key. Then in the other action I could retrieve the data by using that id. The problem is that “itemid” is then empty.

class ActionA(Action):
def name(self) -> Text:
    return "actiona"

def run(self, dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
    
    price = tracker.get_slot('price')      
    itemid = tracker.get_slot('itemid')
    data = rcache.get(itemid)
    if not data:
        data = get_item_data(itemid)
        rcache.mset({itemid: data})
    
    ....

    return [SlotSet('offer', offer)]

class ActionB(Action):

def name(self) -> Text:
    return "actionb"

def run(self, dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
    
    
    itemid = tracker.get_slot('itemid') #empty value
    ...

Using the method tracker.get_slot() you are extracting the values from tracker. That’s why, for this to work, your custom action actiona should return an event SlotSet for the itemid slot as well (just like you are doing it for the slot offer). You can do it by modifying the last line of actiona as follows:

return [SlotSet('offer', offer), SlotSet('itemid', data)]

Once you do that, the itemid should be saved as slot in the tracker as well and this slot then should be available for your actionb.