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 send_rich_card(conversation_id): ''' Sends a sample rich card to the user. Args: conversation_id (str): The unique id for this user and agent. ''' fallback_text = ('Business Messages!!!\n\n' + 'This is an example rich card\n\n' + SAMPLE_IMAGES[0]) rich_card = BusinessMessagesRichCard( standaloneCard=BusinessMessagesStandaloneCard( cardContent=BusinessMessagesCardContent( title='Business Messages!!!', description='This is an example rich card', suggestions=get_sample_suggestions(), media=BusinessMessagesMedia( height=BusinessMessagesMedia.HeightValueValuesEnum.MEDIUM, contentInfo=BusinessMessagesContentInfo( fileUrl=SAMPLE_IMAGES[0], forceRefresh=False))))) message_obj = BusinessMessagesMessage(messageId=str(uuid.uuid4().int), representative=BOT_REPRESENTATIVE, richCard=rich_card, fallback=fallback_text) send_message(message_obj, conversation_id)
def send_business_hours_message(conv): ''' Sends a message to the user with the business hours of the business. Args: conv (Conversation): The conversation object tied to the user ''' rich_card = BusinessMessagesRichCard( standaloneCard=BusinessMessagesStandaloneCard( cardContent=BusinessMessagesCardContent( title='Business Hours', description=''' Sunday 8:00 AM - 8:00 PM \nMonday 8:00 AM - 8:00 PM Tuesday 8:00 AM - 8:00 PM \nWednesday 8:00 AM - 8:00 PM Thursday 8:00 AM - 8:00 PM \nFriday 8:00 AM - 8:00 PM Saturday 8:00 AM - 8:00 PM ''', ))) message_obj = BusinessMessagesMessage( messageId=str(uuid.uuid4().int), representative=BOT_REPRESENTATIVE, richCard=rich_card, fallback=('Business Hours...Open daily from 8 AM - 8 PM')) send_message(message_obj, conv.id) message_obj = BusinessMessagesMessage( messageId=str(uuid.uuid4().int), representative=BOT_REPRESENTATIVE, text='''Thanks for inquiring about our Business Hours. Please let us know how else we can help!''', suggestions=get_cart_suggestions()) send_message(message_obj, conv.id)
def send_online_shopping_info_message(conversation_id): ''' Sends a rich card with online shopping info to the user. Args: conversation_id (str): The unique id for this user and agent. ''' fallback_text = ('Online shopping will be available soon!') rich_card = BusinessMessagesRichCard( standaloneCard= BusinessMessagesStandaloneCard(cardContent=BusinessMessagesCardContent( title='Online shopping info!', description= 'Thanks for your business, we are located in SF near the Golden Gate Bridge. Online shopping is not yet available, please check back with us in a few days.', suggestions=[], media=BusinessMessagesMedia( height=BusinessMessagesMedia.HeightValueValuesEnum.MEDIUM, contentInfo=BusinessMessagesContentInfo( fileUrl=SAMPLE_IMAGES[4], forceRefresh=False))))) message_obj = BusinessMessagesMessage(messageId=str(uuid.uuid4().int), representative=BOT_REPRESENTATIVE, richCard=rich_card, fallback=fallback_text) send_message(message_obj, conversation_id) message_obj = BusinessMessagesMessage( messageId=str(uuid.uuid4().int), representative=BOT_REPRESENTATIVE, text='Let us know how else we can help you:', fallback='Please let us know how else we can help you.', suggestions=[ BusinessMessagesSuggestion(reply=BusinessMessagesSuggestedReply( text='Business hours', postbackData='business-hours-inquiry')), ]) send_message(message_obj, conversation_id)
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(conv): ''' Send the user their shopping cart. ''' cart_items = ShoppedItem.objects.filter(cart=conv.shopping_cart) if not conv.shopping_cart or len(cart_items) == 0: message_obj = BusinessMessagesMessage( messageId=str(uuid.uuid4().int), representative=BOT_REPRESENTATIVE, text=MSG_EMPTY_CART, suggestions=[ BusinessMessagesSuggestion( reply=BusinessMessagesSuggestedReply( text=MSG_SHOW_FOOD_MENU, postbackData=CMD_FOOD_MENU)), BusinessMessagesSuggestion( reply=BusinessMessagesSuggestedReply( text=MSG_SHOW_DRINKS_MENU, postbackData=CMD_DRINK_MENU)), BusinessMessagesSuggestion( reply=BusinessMessagesSuggestedReply( text=MSG_PENDING_ORDERS, postbackData=CMD_SHOW_PENDING_PICKUP)), ]) send_message(message_obj, conv.id) elif len(cart_items) == 1: fallback_text = ( f'Your shopping cart contains a {cart_items[0].item.name}') rich_card = BusinessMessagesRichCard( standaloneCard=BusinessMessagesStandaloneCard( cardContent=BusinessMessagesCardContent( title=cart_items[0].item.name, description=f'Quantity: {cart_items[0].quantity}', suggestions=[], media=BusinessMessagesMedia( height=BusinessMessagesMedia.HeightValueValuesEnum. MEDIUM, contentInfo=BusinessMessagesContentInfo( fileUrl=cart_items[0].item.image_url, forceRefresh=False))))) message_obj = BusinessMessagesMessage( messageId=str(uuid.uuid4().int), representative=BOT_REPRESENTATIVE, richCard=rich_card, fallback=fallback_text) send_message(message_obj, conv.id) message_obj = BusinessMessagesMessage( messageId=str(uuid.uuid4().int), representative=BOT_REPRESENTATIVE, text=f'''The total value of your shopping cart is ${cart_items[0].item.price * cart_items[0].quantity}.''', suggestions=get_cart_suggestions()) send_message(message_obj, conv.id) else: rich_card = BusinessMessagesRichCard( carouselCard=get_cart_carousel(cart_items)) # Construct a fallback text for devices that do not support carousels. fallback_text = '' 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, fallback=fallback_text) send_message(message_obj, conv.id) total_price = 0 for cart_item in cart_items: total_price = total_price + cart_item.item.price * cart_item.quantity message_obj = BusinessMessagesMessage( messageId=str(uuid.uuid4().int), representative=BOT_REPRESENTATIVE, text=f'The total value of your shopping cart is ${total_price}.', suggestions=get_cart_suggestions()) send_message(message_obj, conv.id)
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)