Example #1
0
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker, domain: Dict[Text, Any]
            ) -> List[Dict[Text, Any]]:
        """Purchase all items in the cart.

        Args:
            dispatcher: Rasa CollectingDispatcher
            tracker: Rasa Tracker
            domain: Rasa domain

        Return:
            List[Dict[Text, Any]]: updated cart

        """
        receipt = ''
        user_cart = cart.get_user_cart(tracker.sender_id)
        if user_cart.cart_items:
            # TODO: implement actual checkout functionality
            user_cart.checkout()
            for item in user_cart.cart_items:
                receipt += f'{item.quantity}x {item.item_type.name}:\t\t${item.cost()}\n'
            receipt += f"TOTAL\t\t${user_cart.sum_cost()}"
            user_cart.clear()
            dispatcher.utter_message(template='utter_checkout_complete')
        else:
            dispatcher.utter_message(template='utter_empty_cart_no_checkout')

        return[SlotSet("readable_cart", user_cart.list_contents()), SlotSet("receipt", receipt)]
Example #2
0
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker, Domain: Dict[Text, Any]
            ) -> List[Dict[Text, Any]]:
        """Create cart object and load selected menu into cart object.

        Check if there is a menu corresponding to the store slot
        if not, utter error message. If menu exists, load and store in cart.

        Args:
            dispatcher: Rasa CollectingDispatcher
            tracker: Rasa Tracker
            domain: Rasa domain

        Return:
            List[Dict[Text, Any]]: SlotSet('cart')

        """
        user_cart = cart.get_user_cart(tracker.sender_id)
        curr_store = tracker.get_slot('store')
        if curr_store is None:
            dispatcher.utter_message(template="utter_no_store_selected")
            return [FollowupAction('action_listen')]
        elif user_cart.menu is not None and curr_store == user_cart.menu.name:
            return []
        else:
            # TODO: move menus to a more permanent shared folder
            menu_match = user_cart.get_matching_menu(curr_store, MENU_PATH)
            if menu_match is not None:
                user_cart.menu = cart.Menu.load_menu(menu_match, MENU_PATH)
            if menu_match is None or user_cart.menu is None:
                dispatcher.utter_message(template="utter_menu_not_found")
                return[SlotSet('store', None), FollowupAction('action_listen')]
        return[]
Example #3
0
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker, domain: Dict[Text, Any]
            ) -> List[Dict[Text, Any]]:
        """Get the index from target_cart_item, remove the item at that index.

        Args:
            dispatcher: Rasa CollectingDispatcher
            tracker: Rasa Tracker
            domain: Rasa domain

        Return:
            List[Dict[Text, Any]]: updated cart

        """
        user_cart = cart.get_user_cart(tracker.sender_id)
        to_remove = user_cart.get_matching_cart_item(tracker.get_slot("target_cart_item"))
        if to_remove:
            removed = user_cart.remove_item(to_remove)
            if removed:
                dispatcher.utter_message(template="utter_removed")
        else:
            dispatcher.utter_message(template="utter_cart_item_not_found")

        return [FollowupAction("action_clear_slots"), SlotSet("num_cart_items", len(user_cart.cart_items)),
                SlotSet("cart_total", user_cart.sum_cost())]
Example #4
0
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker, domain: Dict[Text, Any]
            ) -> List[Dict[Text, Any]]:
        """Read out cart contents.

        Check if current_cart is None, if so utter cart is empty
        Otherwise, read out current_cart.

        Args:
            dispatcher: Rasa CollectingDispatcher
            tracker: Rasa Tracker
            domain: Rasa domain

        Return:
            List[Dict[Text, Any]]: list of slotsets and followup actions

        """
        user_cart = cart.get_user_cart(tracker.sender_id)
        if user_cart.cart_items:
            readable_cart = ""
            dispatcher.utter_message(template="utter_cart_contents")
            for item in user_cart.cart_items:
                readable_cart += f'{item.quantity}x {item.item_type.name}:\t\t${item.cost()}\n'
            readable_cart += f"TOTAL\t\t${user_cart.sum_cost()}"
            dispatcher.utter_message(readable_cart)
        else:
            dispatcher.utter_message(template="utter_cart_is_empty")

        return []
Example #5
0
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker, domain: Dict[Text, Any]
            ) -> List[Dict[Text, Any]]:
        """Check that all 'options' are valid for the requested menu item.

        Args:
            dispatcher: Rasa collecting dispatcher
            tracker: Rasa tracker
            domain: Rasa domain

        Return:
            List[Text]: A list of matching values from the menu or an empty list

        """
        user_cart = cart.get_user_cart(tracker.sender_id)
        options = tracker.get_slot('options')
        if options == []:
            return [SlotSet('options', options)]
        else:  # stop-gap solution for error when adding a single add-on ['c','r','e','a','m']
            if len(options[0]) == 1:
                options = ["".join(options)]
            valid, invalid = user_cart.validate_options(tracker.get_slot('item'), tracker.get_slot('options'))
            if invalid:
                dispatcher.utter_message(f"I couldn't find a matching option for {cart.list_to_string(invalid, join='or')}")
                return [SlotSet('options', valid), FollowupAction('action_listen')]
            return [SlotSet('options', valid)]
Example #6
0
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker, domain: Dict[Text, Any]
            ) -> List[Dict[Text, Any]]:
        """Check that 'size' is a valid value (small, medium, or large).

        Args:
            dispatcher: Rasa collecting dispatcher
            tracker: Rasa tracker
            domain: Rasa domain

        Return:
            List[Dict[Text, Any]]: Set slots item and size

        """
        user_cart = cart.get_user_cart(tracker.sender_id)
        item_size_result = user_cart.validate_size(tracker.get_slot('item'), tracker.get_slot('size'))
        if not item_size_result[0]:
            dispatcher.utter_message(template='utter_invalid_item')
            return [SlotSet('item', None), SlotSet('size', None), FollowupAction('action_listen')]
        elif not item_size_result[1]:
            dispatcher.utter_message(template='utter_invalid_size')
            item_match = user_cart.menu.menu_items[user_cart.menu.get_matching_menu_item(tracker.get_slot('item'))]
            dispatcher.utter_message(item_match.describe_options('size'))
            return [SlotSet('item', item_size_result[0]), SlotSet('size', None), FollowupAction('action_listen')]
        return [SlotSet('item', item_size_result[0]), SlotSet('size', item_size_result[1])]
Example #7
0
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker, domain: Dict[Text, Any]
            ) -> List[Dict[Text, Any]]:
        """Get info from slots and create active item item_instance.

        Args:
            dispatcher: Rasa CollectingDispatcher
            tracker: Rasa Tracker
            domain: Rasa domain

        Return:
            List[Dict[Text, Any]]: list of slotsets and followup actions

        """
        # slots should all be validated at this point from validation step
        user_cart = cart.get_user_cart(tracker.sender_id)

        item = tracker.get_slot('item')
        size = tracker.get_slot('size')
        options = tracker.get_slot('options')
        user_cart.active_item = cart.ItemInstance.from_item_name(item, user_cart.menu)
        if tracker.get_slot('quantity'):
            user_cart.active_item.quantity = w2n.word_to_num(tracker.get_slot('quantity'))
        for opt in options:
            user_cart.active_item.options[opt] = user_cart.active_item.item_type.option_fields[opt]
            user_cart.active_item.options[opt].selected = True
        user_cart.active_item.options[size] = user_cart.active_item.item_type.option_fields[size]
        user_cart.active_item.options[size].selected = True

        return [SlotSet("item_to_add", user_cart.active_item.to_text()),
                SlotSet("image", user_cart.menu.menu_items[item].image)]
Example #8
0
 def run(self, dispatcher: CollectingDispatcher,
         tracker: Tracker, domain: Dict[Text, Any]
         ) -> List[Dict[Text, Any]]:
     """Find a matching item in the cart and set target_cart_item."""
     user_cart = cart.get_user_cart(tracker.sender_id)
     match = user_cart.get_matching_cart_item(tracker.get_slot('item'))
     if match:
         return [SlotSet('target_cart_item', match)]
     else:
         return [SlotSet('target_cart_item', None)]
Example #9
0
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker, domain: Dict[Text, Any]
            ) -> List[Dict[Text, Any]]:
        """Add item to cart.

        Args:
            dispatcher: Rasa CollectingDispatcher
            tracker: Rasa Tracker
            domain: Rasa domain

        Return:
            List[Dict[Text, Any]]: list of slotsets and followup actions

        """
        user_cart = cart.get_user_cart(tracker.sender_id)
        user_cart.add_item_instance(user_cart.active_item)
        return [FollowupAction("action_clear_slots"), SlotSet("num_cart_items", len(user_cart.cart_items)),
                SlotSet("cart_total", user_cart.sum_cost())]
Example #10
0
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker, domain: Dict[Text, Any]
            ) -> List[Dict[Text, Any]]:
        """Remove all items from the cart.

        Args:
            dispatcher: Rasa CollectingDispatcher
            tracker: Rasa Tracker
            domain: Rasa domain

        Return:
            List[Dict[Text, Any]]: updated cart

        """
        user_cart = cart.get_user_cart(tracker.sender_id)
        user_cart.clear()
        dispatcher.utter_message(template="utter_cart_cleared")

        return [SlotSet("num_cart_items", len(user_cart.cart_items)), SlotSet("cart_total", user_cart.sum_cost())]
Example #11
0
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker, domain: Dict[Text, Any]
            ) -> List[Dict[Text, Any]]:
        """Check that the requested item is in the menu.

        Args:
            value (Text): value of the slot that you are validating
            user_cart (cart.Cart): The cart object for the specific user

        Return:
            Optional[Text]: A matching value from the menu or None

        """
        user_cart = cart.get_user_cart(tracker.sender_id)
        match = user_cart.menu.get_matching_menu_item(tracker.get_slot('item'))
        if match:
            return [SlotSet('item', match)]
        else:
            dispatcher.utter_message(template="utter_item_not_found")
            return [SlotSet('item', None), FollowupAction('action_listen')]
Example #12
0
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker, domain: Dict[Text, Any]
            ) -> List[Dict[Text, Any]]:
        """Utter confirm add and display image + buttons.

        Args:
            dispatcher: Rasa CollectingDispatcher
            tracker: Rasa Tracker
            domain: Rasa domain

        Return:
            List[Dict[Text, Any]]: list of slotsets and followup actions

        """
        user_cart = cart.get_user_cart(tracker.sender_id)
        if not tracker.get_slot('item'):
            dispatcher.utter_message(template="utter_no_item_selected")
            return []
        dispatcher.utter_message(image=user_cart.active_item.item_type.image)
        dispatcher.utter_message(template="utter_confirm_add")

        return []
Example #13
0
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker, Domain: Dict[Text, Any]
            ) -> List[Dict[Text, Any]]:
        """Display each menu item name and price.

        Extract the menu from the menu slot (display error if no
        menu is selected). Display name and price.

        Args:
            dispatcher: Rasa CollectingDispatcher
            tracker: Rasa Tracker
            domain: Rasa domain

        Return:
            List[Dict[Text, Any]]: none

        """
        user_cart = cart.get_user_cart(tracker.sender_id)
        if not user_cart.menu:
            dispatcher.utter_message(template='utter_no_store_selected')
            return []
        if not tracker.get_slot('category'):
            dispatcher.utter_message(template='utter_no_category_selected')
            return []

        cat_match = user_cart.menu.get_matching_menu_category(tracker.get_slot('category'))
        if cat_match:
            description = user_cart.menu.describe_category(cat_match).split('\n')
            while len(description) > 30:
                chunk = '\n'.join(description[:30])
                description = description[30:]
                dispatcher.utter_message(chunk)
            dispatcher.utter_message('\n'.join(description))
        else:
            dispatcher.utter_message(template='utter_category_not_found')

        return []
Example #14
0
    def run(self, dispatcher: CollectingDispatcher,
            tracker: Tracker, Domain: Dict[Text, Any]
            ) -> List[Dict[Text, Any]]:
        """Display each menu item name and price.

        Extract the menu from the menu slot (display error if no
        menu is selected). Display name and price.

        Args:
            dispatcher: Rasa CollectingDispatcher
            tracker: Rasa Tracker
            domain: Rasa domain

        Return:
            List[Dict[Text, Any]]: none

        """
        user_cart = cart.get_user_cart(tracker.sender_id)
        if not user_cart.menu:
            dispatcher.utter_message(template='utter_no_store_selected')
        else:
            dispatcher.utter_message(user_cart.menu.describe_menu())

        return []