def get_menu_carousel(): """Creates a sample carousel rich card. Returns: A :obj: A BusinessMessagesCarouselCard object with three cards. """ inventory = get_inventory_data() card_content = [] for item in inventory['food']: card_content.append( BusinessMessagesCardContent( title=item['name'], description=item['price'], suggestions=[ BusinessMessagesSuggestion( reply=BusinessMessagesSuggestedReply( text='Add item', postbackData='{' + f'"action":"{CMD_ADD_ITEM}","item_name":"{item["id"]}"' + '}')) ], media=BusinessMessagesMedia( height=BusinessMessagesMedia.HeightValueValuesEnum.MEDIUM, contentInfo=BusinessMessagesContentInfo( fileUrl=item['image_url'], forceRefresh=False)))) return BusinessMessagesCarouselCard( cardContents=card_content, cardWidth=BusinessMessagesCarouselCard.CardWidthValueValuesEnum.MEDIUM)
def get_sample_carousel(): """Creates a sample carousel rich card. Returns: A :obj: A BusinessMessagesCarouselCard object with three cards. """ card_content = [] for i, sample_image in enumerate(SAMPLE_IMAGES): card_content.append( BusinessMessagesCardContent( title='Card #' + str(i), description='This is a sample card', suggestions=get_sample_suggestions(), media=BusinessMessagesMedia( height=BusinessMessagesMedia.HeightValueValuesEnum.MEDIUM, contentInfo=BusinessMessagesContentInfo( fileUrl=sample_image, forceRefresh=False)))) return BusinessMessagesCarouselCard( cardContents=card_content, cardWidth=BusinessMessagesCarouselCard.CardWidthValueValuesEnum.MEDIUM)
def get_cart_carousel(cart_items): ''' A function that returns the items in the shopping cart in a carousel. ''' card_content = [] for cart_entity in cart_items: card_content.append( BusinessMessagesCardContent( title=cart_entity.item.name, description=f'Quantity: {cart_entity.quantity}', suggestions=[ BusinessMessagesSuggestion( reply=BusinessMessagesSuggestedReply( text='➕', postbackData= f'{CMD_ADD_TO_CART}-{cart_entity.item.id}')), BusinessMessagesSuggestion( reply=BusinessMessagesSuggestedReply( text='➖', postbackData= f'remove_from_cart-{cart_entity.item.id}')), BusinessMessagesSuggestion( reply=BusinessMessagesSuggestedReply( text=MSG_REMOVE_ALL, postbackData= f'remove_all_from_cart-{cart_entity.item.id}')), ], media=BusinessMessagesMedia( height=BusinessMessagesMedia.HeightValueValuesEnum.MEDIUM, contentInfo=BusinessMessagesContentInfo( fileUrl=cart_entity.item.image_url, forceRefresh=False)))) return BusinessMessagesCarouselCard( cardContents=card_content, cardWidth=BusinessMessagesCarouselCard.CardWidthValueValuesEnum.MEDIUM)
def get_drink_menu_carousel(): ''' Creates the drink menu carousel rich card. Returns: A :obj: A BusinessMessagesCarouselCard object with three cards. ''' card_content = [] menu_items = Item.objects.filter(available=True, menu_type='D') for item in menu_items: card_content.append( BusinessMessagesCardContent( title=item.name, description=f'${item.price}{item.currency}', suggestions=get_menu_item_suggestions(item), media=BusinessMessagesMedia( height=BusinessMessagesMedia.HeightValueValuesEnum.MEDIUM, contentInfo=BusinessMessagesContentInfo( fileUrl=item.image_url, forceRefresh=False)))) return BusinessMessagesCarouselCard( cardContents=card_content, cardWidth=BusinessMessagesCarouselCard.CardWidthValueValuesEnum.MEDIUM)
def send_shopping_cart(conversation_id): """Sends a shopping cart to the user as a rich card carousel. Args: conversation_id (str): The unique id for this user and agent. """ credentials = service_account.Credentials.from_service_account_file( SERVICE_ACCOUNT_LOCATION) # Retrieve the inventory data inventory = get_inventory_data() # Pull the data from Google Datastore client = datastore.Client(credentials=credentials) key = client.key('ShoppingCart', conversation_id) result = client.get(key) shopping_cart_suggestions = [ BusinessMessagesSuggestion(reply=BusinessMessagesSuggestedReply( text='See total price', postbackData='show-cart-price')), BusinessMessagesSuggestion(reply=BusinessMessagesSuggestedReply( text='Empty the cart', postbackData='empty-cart')), BusinessMessagesSuggestion(reply=BusinessMessagesSuggestedReply( text='See the menu', postbackData=CMD_SHOW_PRODUCT_CATALOG)), ] if len(result.items()) == 0: message_obj = BusinessMessagesMessage( messageId=str(uuid.uuid4().int), representative=BOT_REPRESENTATIVE, text='There are no items in your shopping cart.', suggestions=shopping_cart_suggestions) send_message(message_obj, conversation_id) elif len(result.items()) == 1: for product_name, quantity in result.items(): product_id = get_id_by_product_name(product_name) fallback_text = ('You have one type of item in the shopping cart') rich_card = BusinessMessagesRichCard( standaloneCard=BusinessMessagesStandaloneCard( cardContent=BusinessMessagesCardContent( title=product_name, description=f'{quantity} in cart.', suggestions=[ BusinessMessagesSuggestion( reply=BusinessMessagesSuggestedReply( text='Remove one', postbackData='{' + f'"action":"{CMD_DEL_ITEM}","item_name":"{product_id}"' + '}')) ], media=BusinessMessagesMedia( height=BusinessMessagesMedia.HeightValueValuesEnum. MEDIUM, contentInfo=BusinessMessagesContentInfo( fileUrl=inventory['food'][product_id] ['image_url'], forceRefresh=False))))) message_obj = BusinessMessagesMessage( messageId=str(uuid.uuid4().int), representative=BOT_REPRESENTATIVE, richCard=rich_card, suggestions=shopping_cart_suggestions, fallback=fallback_text) send_message(message_obj, conversation_id) else: cart_carousel_items = [] # Iterate through the cart and generate a carousel of items for product_name, quantity in result.items(): product_id = get_id_by_product_name(product_name) cart_carousel_items.append( BusinessMessagesCardContent( title=product_name, description=f'{quantity} in cart.', suggestions=[ BusinessMessagesSuggestion( reply=BusinessMessagesSuggestedReply( text='Remove one', postbackData='{' + f'"action":"{CMD_DEL_ITEM}","item_name":"{product_id}"' + '}')) ], media=BusinessMessagesMedia( height=BusinessMessagesMedia.HeightValueValuesEnum. MEDIUM, contentInfo=BusinessMessagesContentInfo( fileUrl=inventory['food'][product_id]['image_url'], forceRefresh=False)))) rich_card = BusinessMessagesRichCard( carouselCard=BusinessMessagesCarouselCard( cardContents=cart_carousel_items, cardWidth=BusinessMessagesCarouselCard. CardWidthValueValuesEnum.MEDIUM)) fallback_text = '' # Construct a fallback text for devices that do not support carousels for card_content in rich_card.carouselCard.cardContents: fallback_text += ( card_content.title + '\n\n' + card_content.description + '\n\n' + card_content.media.contentInfo.fileUrl + '\n---------------------------------------------\n\n') message_obj = BusinessMessagesMessage( messageId=str(uuid.uuid4().int), representative=BOT_REPRESENTATIVE, richCard=rich_card, suggestions=shopping_cart_suggestions, fallback=fallback_text, ) send_message(message_obj, conversation_id)