def run(self, dispatcher: CollectingDispatcher , tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: print('_____' + self.name()) shoe_model = tracker.get_slot(Entities.shoe_model) prefix_name = tracker.get_slot(Entities.prefix_name) print('gg') if not shoe_model: dispatcher.utter_message('{} chưa chọn đôi giày nào'.format(prefix_name)) return [FollowupAction('act_shoe_menu')] shoe_model = str(shoe_model).upper() query = f'select id as shoe_id, shoeModel, shoeName from mainapp_shoe where mainapp_shoe.shoeModel like "%{shoe_model}%";' shoes = SqlUtils.get_result(query, Shoe) # debug('\n_________act_set_shoe_id_________') # debug('query') # debug(query) # debug('model', shoe_model) # debug(len(shoes), 'KQ') # if len(shoes) > 0: # debug('shoe_id[0]', shoes[0].shoe_id) # debug() if len(shoes) > 0: return [SlotSet(Entities.shoe_id, str(shoes[0].shoe_id)), SlotSet(Entities.shoe_model, shoe_model)] else: return [SlotSet(Entities.shoe_model, shoe_model)]
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: print('_____' + self.name()) prefix_name = tracker.get_slot(Entities.customer_name) customer_name = tracker.get_slot(Entities.customer_name) bot_position = tracker.get_slot(Entities.bot_position) query = f'SELECT * FROM {Category.TABLE_NAME}' categories = SqlUtils.get_result(query, Category) if len(categories) == 0: message = 'Xin lỗi ' + prefix_name + customer_name + '. ' \ + bot_position + ' không tìm thấy category nào trong cơ sở dữ liệu.' dispatcher.utter_message(message) else: quick_reply_elements = [] for category in categories: element = QuickReplyElement( content_type=QuickReplyElement.TEXT, title=category.categoryName, payload=f'cho a xem loại giày {category.categoryName}') quick_reply_elements.append(element) quick_reply_template = QuickReplies( text_before_template= f'Hiện bên {bot_position} có những loại này. Mời {prefix_name} chọn:', list_quick_reply_elements=quick_reply_elements) dispatcher.utter_message( json_message=quick_reply_template.to_json_message()) return []
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: """ Trả về list cacs mau :param dispatcher: :param tracker: :param domain: :return: """ print('_____' + self.name()) prefix_name = tracker.get_slot(Entities.prefix_name) customer_name = tracker.get_slot(Entities.customer_name) bot_position = tracker.get_slot(Entities.bot_position) shoe_id = tracker.get_slot(Entities.shoe_id) shoe_model = tracker.get_slot(Entities.shoe_model) shoe_size = tracker.get_slot(Entities.shoe_size) # have not chosen shoe yet if shoe_model is None and shoe_id is None: err_message = f'Xin lỗi {customer_name}, {prefix_name} chưa chọn giày.' dispatcher.utter_message(text=err_message) return [FollowupAction('act_show_menu')] query = f''' select size from mainapp_shoe inner join mainapp_detailshoe on mainapp_detailshoe.shoe_id = mainapp_shoe.id where mainapp_shoe.id = {shoe_id} and mainapp_detailshoe.size = {shoe_size} and mainapp_detailshoe.quantityAvailable > 0; ''' # debug_print_content('query: ' + query) detail_shoes = SqlUtils.get_result(query, DetailShoe) if len(detail_shoes) == 0: err_message = f'Xin lỗi {prefix_name}{customer_name}, size này không có hàng ạ.' dispatcher.utter_message(text=err_message) return [FollowupAction('act_choose_size')] return [FollowupAction('act_add2cart_or_buy_now')]
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: prefix_name = tracker.get_slot(Entities.prefix_name) customer_name = tracker.get_slot(Entities.customer_name) bot_position = tracker.get_slot(Entities.bot_position) shoe_id = tracker.get_slot(Entities.shoe_id) shoe_model = tracker.get_slot(Entities.shoe_model) shoe_size = tracker.get_slot(Entities.shoe_size) color_id = tracker.get_slot(Entities.color_id) # have not chosen shoe yet if shoe_model is None and shoe_id is None: err_message = '{}, {} hãy chọn giày trước khi chọn màu.'.format( prefix_name, customer_name) dispatcher.utter_message(text=err_message) return [FollowupAction('act_show_menu')] query = f''' select size from mainapp_shoe inner join mainapp_detailshoe on mainapp_detailshoe.shoe_id = mainapp_shoe.id where mainapp_shoe.id = {shoe_id} and mainapp_detailshoe.color_id = {color_id} and mainapp_detailshoe.quantityAvailable > 0; ''' # debug_print_content('query: ' + query) detail_shoes = SqlUtils.get_result(query, DetailShoe) if len(detail_shoes) == 0: err_message = f'Xin lỗi {prefix_name}{customer_name}, màu này hiện không có hàng ạ.' dispatcher.utter_message(text=err_message) return [FollowupAction('act_choose_color')] return [FollowupAction('act_choose_size')]
def get_mapping_category2id() -> dict: """ :return: dict of types of shoes """ da = 1 luoi = 2 the_thao = 3 cao_co = 4 vai = 5 bata = 6 mapping_category = { 'leather': da, 'da': da, 'dà': da, 'dả': da, 'dá': da, 'dạ': da, 'dã': da, 'gia': da, 'ja': da, 'ra': da, 'lười': luoi, 'luoi': luoi, 'lườj': luoi, 'luoj': luoi, 'lazy': luoi, 'lây dy': luoi, 'lêy dy': luoi, 'ley dy': luoi, 'sport shoe': the_thao, 'thể thao': the_thao, 'the thao': the_thao, 'thethao': the_thao, 'thểthao': the_thao, 'sneaker': the_thao, 'sneakers': the_thao, 'cao cổ': cao_co, 'cao cỏ': cao_co, 'caocỏ': cao_co, 'caocổ': cao_co, 'cao co': cao_co, 'caoco': cao_co, 'kao cổ': cao_co, 'kout cổ': cao_co, 'cout cổ': cao_co, 'vải': vai, 'vài': vai, 'vái': vai, 'vãi': vai, 'vại': vai, 'vai': vai, 'pata': bata, 'bata': bata, 'bât': bata, 'ba ta': bata, 'bâta': bata, 'bâtâ': bata, 'bâ ta': bata, 'beta': bata, '3ta': bata, '3 ta': bata, } query = f'select id as category_id, categoryName from {Category.TABLE_NAME}' categories = SqlUtils.get_result(query, Category) dict_categories = dict() for category in categories: dict_categories[remove_accents( category.categoryName).lower()] = category.category_id mapping_category.update(dict_categories) return mapping_category
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: """ Trả về generic template 6 sản phẩm hot + quick replies đòi xem thêm :param dispatcher: :param tracker: :param domain: :return: """ print('_____' + self.name()) prefix_name = tracker.get_slot(Entities.prefix_name) customer_name = tracker.get_slot(Entities.customer_name) bot_position = tracker.get_slot(Entities.bot_position) query = f''' select distinct mainapp_shoe.id as shoe_id, mainapp_shoe.shoeName, mainapp_shoe.shoeModel, mainapp_shoe.shoeThumbnail, mainapp_shoe.dateCreated, mainapp_shoe.image_static, mainapp_shoe.viewCount, mainapp_shoe.quantitySold, mainapp_shoe.favouriteCount, mainapp_category.categoryName, mainapp_detailshoe.newPrice from mainapp_shoe inner join mainapp_detailshoe on mainapp_shoe.id = mainapp_detailshoe.shoe_id inner join mainapp_category on mainapp_shoe.category_id = mainapp_category.id where active = 1 order by dateCreated desc limit 0, 5; ''' shoes = SqlUtils.get_result(query, Shoe) shoes: List[Shoe] detail_shoes = SqlUtils.get_result(query, DetailShoe) if len(detail_shoes) == 0 or len(shoes) == 0: err_code = ErrorCode.ERR_IN_ACT_GET_NEW_SHOE err_message = f'Xin lỗi {prefix_name}{customer_name}, hệ thống đã xảy ra lỗi. error code={err_code}' dispatcher.utter_message(text=err_message) else: dispatcher.utter_message(text='Đây là 5 đôi bên ' + bot_position + ' mới nhập đó ' + prefix_name + customer_name) dispatcher.utter_message( text= f'Đôi mới nhất được nhập từ ngày {shoes[0].dateCreated}, mời {prefix_name} xem:' ) # horizontal_template_elements = [] # for index in range(len(shoes)): # shoe = shoes[index] # detail_shoe = detail_shoes[index] # # buttons = [ # PostBackButton( # title='Xem đôi này ' + shoe.shoeModel, # str_send_to_webhook='tôi muốn xem mẫu ' + shoe.shoeModel + ' id ' + str(shoe.shoe_id) # ), # UrlButton( # title='Xem trên website', # url_access=MyWebUrl.get_detail_shoe_url(shoe.shoe_id) # ), # ] # # element = HorizontalTemplateElement( # image_url=MyWebUrl.get_shoe_image_url(shoe.shoeThumbnail), # # image_url='https://www.w3schools.com/w3css/img_lights.jpg', # title=shoe.shoeName, # subtitle=price_format(detail_shoe.newPrice), # default_action=HorizontalTemplateElement.DefaultAction(MyWebUrl.get_detail_shoe_url(shoe.shoe_id)), # list_buttons=buttons, # ) # horizontal_template_elements.append(element) # horizontal_template = HorizontalTemplate(horizontal_template_elements) horizontal_template = HorizontalTemplate.from_shoes_detail_shoe( shoes=shoes, detail_shoes=detail_shoes) dispatcher.utter_message( json_message=horizontal_template.to_json_message()) # xem tiep quick_reply_elements = [ QuickReplyElement(QuickReplyElement.TEXT, 'Xem thêm', 'còn đôi nào khác k?'), QuickReplyElement(QuickReplyElement.TEXT, 'Thôi', 'tôi muốn xem menu'), ] quick_replies = QuickReplies( text_before_template='Xin hãy chọn một hành động', list_quick_reply_elements=quick_reply_elements) dispatcher.utter_message( json_message=quick_replies.to_json_message()) return []
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: print('_____' + self.name()) prefix_name = tracker.get_slot(Entities.prefix_name) customer_name = tracker.get_slot(Entities.customer_name) bot_position = tracker.get_slot(Entities.bot_position) shoe_category = tracker.get_slot(Entities.shoe_category) debug('\n_________action_show_shoes_by_category_________') debug('category ', shoe_category) if shoe_category is None or shoe_category.strip() == '': dispatcher.utter_message( text= f'Xin lỗi {prefix_name} chưa cung cấp tên loại giày muốn tìm.') return [] category_id = Category.get_id_by_name(shoe_category) debug('category_id ', category_id) if not category_id: message_not_found = 'Xin lỗi ' + prefix_name + customer_name \ + '. Hiện bên ' + bot_position + ' không kinh doanh mặt hàng này ạ' dispatcher.utter_message(text=message_not_found) return [] query = f''' select distinct mainapp_shoe.id as shoe_id, mainapp_shoe.shoeName, mainapp_shoe.shoeModel, mainapp_shoe.image_static, mainapp_shoe.shoeThumbnail, mainapp_category.categoryName, min(mainapp_detailshoe.newPrice) as newPrice from mainapp_shoe inner join mainapp_detailshoe on mainapp_shoe.id = mainapp_detailshoe.shoe_id inner join mainapp_category on mainapp_shoe.category_id = mainapp_category.id where active = 1 and mainapp_category.id = {category_id} group by mainapp_shoe.id, mainapp_shoe.shoeName, mainapp_shoe.shoeModel, mainapp_shoe.shoeThumbnail, mainapp_category.categoryName, mainapp_detailshoe.id limit 0, 5; ''' shoes, detail_shoes = SqlUtils.get_result(query, Shoe, DetailShoe) if len(shoes) == 0: message = 'Xin lỗi ' + prefix_name + customer_name \ + '. Hiện cửa hàng không kinh doanh đôi giày ' + shoe_category + ' nào ạ' dispatcher.utter_message(message) else: horizontal_template = HorizontalTemplate.from_shoes_detail_shoe( shoes=shoes, detail_shoes=detail_shoes) dispatcher.utter_message( json_message=horizontal_template.to_json_message()) # debug('query', query) # debug('tìm thấy ' + str(len(shoes)) + ' KQ') return []
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: """ Trả về list cacs mau :param dispatcher: :param tracker: :param domain: :return: """ print('_____' + self.name()) prefix_name = tracker.get_slot(Entities.prefix_name) customer_name = tracker.get_slot(Entities.customer_name) bot_position = tracker.get_slot(Entities.bot_position) shoe_id = tracker.get_slot(Entities.shoe_id) shoe_model = tracker.get_slot(Entities.shoe_model) # have not chosen shoe yet if shoe_model is None and shoe_id is None: err_message = f'Xin lỗi {customer_name}, {prefix_name} chưa chọn giày.' dispatcher.utter_message(text=err_message) return [] if shoe_id: query = f'call get_colors({shoe_id})' elif shoe_model: query = f'call get_colors_by_model("{shoe_model}")' debug_print_content('query: ' + query) colors = SqlUtils.get_result(query, Color) if len(colors) == 0: err_code = ErrorCode.ERR_IN_ACT_CHOSE_COLOR err_message = f'Xin lỗi {prefix_name}{customer_name}, hệ thống đã xảy ra lỗi. error={err_code}' dispatcher.utter_message(text=err_message) return [] # xem tiep quick_reply_elements = [] for color in colors: quick_reply_elements.append( QuickReplyElement( QuickReplyElement.TEXT, color.colorName, 'tôi lấy màu {}'.format(color.colorName.lower()), image_url=MyWebUrl.get_color_image('colors/red.png'))) if len(colors) == 1: text = f'Bên cửa hàng {bot_position} chỉ còn màu này thôi {prefix_name} ạ:' else: text = prefix_name.capitalize() + ' hãy chọn một màu bên dưới:' quick_replies = QuickReplies( text_before_template=text, list_quick_reply_elements=quick_reply_elements) dispatcher.utter_message(json_message=quick_replies.to_json_message()) # debug_print_content('quick_replies') # debug_print_content(quick_replies.to_json_message()) # print('*****************************************************') # print(horizontal_template.to_json_message()) # print('*****************************************************') # pprint(quick_replies.to_json_message()) return []
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: """ Trả về quick replies sizes :param dispatcher: :param tracker: :param domain: :return: """ print('_____' + self.name()) prefix_name = tracker.get_slot(Entities.prefix_name) customer_name = tracker.get_slot(Entities.customer_name) bot_position = tracker.get_slot(Entities.bot_position) shoe_id = tracker.get_slot(Entities.shoe_id) shoe_model = tracker.get_slot(Entities.shoe_model) shoe_color = tracker.get_slot(Entities.shoe_color) color_id = tracker.get_slot(Entities.color_id) # have not chosen shoe yet if shoe_model is None and shoe_id is None: err_message = f'Xin lỗi {customer_name}, {prefix_name} chưa chọn giày.' dispatcher.utter_message(text=err_message) return [FollowupAction('act_show_menu')] # if color_id: # noinspection SqlNoDataSourceInspection query = f''' select size from mainapp_shoe inner join mainapp_detailshoe on mainapp_detailshoe.shoe_id = mainapp_shoe.id where mainapp_shoe.id = {shoe_id} and mainapp_detailshoe.color_id = {color_id} and mainapp_detailshoe.quantityAvailable > 0; ''' # debug_print_content('query: ' + query) detail_shoes = SqlUtils.get_result(query, DetailShoe) if len(detail_shoes) == 0: err_code = ErrorCode.ERR_IN_ACT_CHOSE_SIZE err_message = f'Xin lỗi {prefix_name}{customer_name}, hệ thống đã xảy ra lỗi. error={err_code}' dispatcher.utter_message(text=err_message) return [] quick_reply_elements = [] for detail_shoe in detail_shoes: quick_reply_elements.append( QuickReplyElement( content_type=QuickReplyElement.TEXT, title=str(detail_shoe.size), payload=f'lấy size {detail_shoe.size}', )) if len(detail_shoes) == 1 and shoe_color: text = f'Bên {bot_position} màu {shoe_color} chỉ còn size này thôi {prefix_name} ạ:' elif len(detail_shoes) == 1: text = f'Đôi {shoe_model} chỉ còn size này thôi {prefix_name} ạ:' else: text = f'{prefix_name} muốn lấy size nào?'.capitalize() quick_replies = QuickReplies( text_before_template=text, list_quick_reply_elements=quick_reply_elements) dispatcher.utter_message(json_message=quick_replies.to_json_message()) # debug_print_content('quick_replies') # debug_print_content(quick_replies.to_json_message()) return []
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: print('_____' + self.name()) prefix_name = tracker.get_slot(Entities.prefix_name) customer_name = tracker.get_slot(Entities.customer_name) bot_position = tracker.get_slot(Entities.bot_position) messenger_id = tracker.current_state()['sender_id'] coupon_id = tracker.get_slot(Entities.coupon_id) user_api_result = MyWebApi.get_user_info(messenger_id) if user_api_result.status_code == 200: user = User(user_api_result.json()) query = f""" select mainapp_shoe.shoeName, mainapp_shoe.shoeThumbnail, mainapp_shoe.image_static, mainapp_detailshoe.newPrice, mainapp_detailshoe.size, mainapp_color.colorName, cart_cart.quantityOnCart from cart_cart inner join mainapp_detailshoe on (cart_cart.detailShoe_id = mainapp_detailshoe.id) inner join mainapp_shoe on (mainapp_shoe.id = mainapp_detailshoe.shoe_id) inner join mainapp_color on (mainapp_color.id = mainapp_detailshoe.color_id) where cart_cart.user_id = {user.user_id} and mainapp_shoe.active = 1 and mainapp_detailshoe.quantityAvailable > 0 and cart_cart.quantityOnCart <= mainapp_detailshoe.quantityAvailable ; """ shoes, detail_shoes, colors, carts = get_result( query, Shoe, DetailShoe, Color, Cart) if len(shoes) == 0: dispatcher.utter_message( text='Giỏ hàng của {} chưa có gì.'.format(prefix_name)) return [] else: dispatcher.utter_message(text='Lỗi khi call user api') return [] api_result = MyWebApi.place_order(messenger_id, coupon_id=coupon_id) debug('\n_________act place order_________') debug('status code: {}'.format(api_result.status_code)) if api_result.status_code != 200: dispatcher.utter_message( f'Xin lỗi {prefix_name}{customer_name}, đặt hàng không thành công. Mã lỗi: ' + api_result.status_code) return [] package_id = api_result.json()['orderPackage_id'] # package_id = 10 query_element = f''' select mainapp_shoe.shoeName, mainapp_shoe.shoeModel, mainapp_shoe.shoeThumbnail, mainapp_shoe.image_static, mainapp_detailshoe.size, mainapp_color.colorName, order_orderitem.itemPrice, order_orderitem.quantity from order_orderitem inner join mainapp_detailshoe on (mainapp_detailshoe.id = order_orderitem.detailShoe_id) inner join mainapp_shoe on (mainapp_detailshoe.shoe_id = mainapp_shoe.id) inner join mainapp_color on (mainapp_color.id = mainapp_detailshoe.color_id) where order_orderitem.orderPackage_id = {package_id} ; ''' shoes, detail_shoes, colors, order_items = SqlUtils.get_result( query_element, Shoe, DetailShoe, Color, OrderItem) query_package_info = f""" select order_orderpackage.id as orderPackage_id, order_orderpackage.receiver, order_orderpackage.receiverNumber, order_orderpackage.receiverAddress, order_orderpackage.dateOrder, order_orderpackage.dateDelivery, order_orderpackage.totalPayment, coupon_coupon.discountRate, coupon_coupon.discountAmount from order_orderpackage inner join coupon_coupon on (order_orderpackage.coupon_id = coupon_coupon.id) inner join account_user on (account_user.id = order_orderpackage.user_id) where order_orderpackage.id = {package_id} ; """ coupons, order_packages = SqlUtils.get_result(query_package_info, Coupon, OrderPackage) receipt_elements = [] for index in range(len(shoes)): shoe = shoes[index] detail_shoe = detail_shoes[index] color = colors[index] order_item = order_items[index] element = ReceiptElement(shoe, detail_shoe, color, order_item) receipt_elements.append(element) order_package = order_packages[0] coupon = coupons[0] receipt_template = ReceiptTemplate(order_package=order_package, coupon=coupon, receipt_elements=receipt_elements) dispatcher.utter_message( json_message=receipt_template.to_json_message()) dispatcher.utter_message( text= 'Đã đặt hàng thành công. Cảm ơn {}{} đã sử dụng dịch vụ của Witter Shoe' .format(prefix_name, customer_name)) dispatcher.utter_message( 'Đơn hàng này dự kiến giao vào ngày {} nhé {}'.format( str(order_package.dateDelivery).split(' ')[0], prefix_name)) return []
def get_mapping_color2id() -> dict: black = 1 white = 2 red = 3 yellow = 4 brown = 5 gray = 6 blue = 7 green = 8 pink = 9 colorful = 10 dict_mapping_color = { 'đen': black, 'den': black, 'black': black, 'white': white, 'trắng': white, 'trang': white, 'red': red, 'đỏ': red, 'do': red, 'yellow': yellow, 'yeallow': yellow, 'yealow': yellow, 'vàng': yellow, 'vang': yellow, 'brown': brown, 'bron': brown, 'brow': brown, 'đất': brown, 'nâu': brown, 'nau': brown, 'lau': brown, 'lâu': brown, 'gray': gray, 'grey': gray, 'xám': gray, 'xam': gray, 'gry': gray, 'blue': blue, 'blu': blue, 'xanh': blue, 'xanh lam': blue, 'lam': blue, 'xanh dương': blue, 'xanh duong': blue, 'xanh nước biển': blue, 'xanh nuoc bien': blue, 'dương': blue, 'duong': blue, 'nước biển': blue, 'nước bien': blue, 'nước': blue, 'nuoc': blue, 'nuoc bien': blue, 'pink': pink, 'pik': pink, 'ping': pink, 'hồng': pink, 'hong': pink, 'colorful': colorful, 'corlorful': colorful, 'coloful': colorful, 'colorfull': colorful, 'nhiều màu': colorful, 'nhieu mau': colorful, 'nhiều mau': colorful, 'nhieu màu': colorful, 'đa sắc': colorful, 'da sac': colorful, 'sặc sỡ': colorful, 'sắc sặc sỡ': colorful, 'sac sỡ': colorful, 'sặc so': colorful, 'sac so': colorful, 'sặc xỡ': colorful, 'xặc sỡ': colorful, 'green': green, 'gren': green, 'lục': green, 'luc': green, 'xanh lá cây': green, 'xanh la cay': green, 'lá cây': green, 'la cay': green, 'lá': green, 'la': green, 'cây': green, 'cay': green, } # update from database if any new color that haven't update this file yet query = f'select id as color_id, colorName from {Color.TABLE_NAME}' colors = SqlUtils.get_result(query, Color) dict_colors = dict() for color in colors: dict_colors[remove_accents(color.colorName).lower()] = color.color_id dict_mapping_color.update(dict_colors) return dict_mapping_color
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: """ Trả về generic template san pham cuoi va ask them vao gio hang/thanh toan :param dispatcher: :param tracker: :param domain: :return: """ # debug('\n_________act_shoe_new_shoe_________') print('_____' + self.name()) prefix_name = tracker.get_slot(Entities.prefix_name) customer_name = tracker.get_slot(Entities.customer_name) messenger_id = tracker.current_state()["sender_id"] bot_position = tracker.get_slot(Entities.bot_position) shoe_id = tracker.get_slot(Entities.shoe_id) shoe_model = tracker.get_slot(Entities.shoe_model) shoe_size = tracker.get_slot(Entities.shoe_size) color_id = tracker.get_slot(Entities.color_id) color_name = tracker.get_slot(Entities.shoe_color) coupon_id = tracker.get_slot(Entities.coupon_id) coupon_code = tracker.get_slot(Entities.coupon_code) # get_final_shoe(shoe_id int, shoe_size int, color_id int) # query = 'call get_final_shoe({}, {}, {})'.format(shoe_id, shoe_size, color_id) query = f''' select mainapp_shoe.id as shoe_id, mainapp_shoe.shoeName, mainapp_shoe.shoeModel, mainapp_shoe.shoeThumbnail, mainapp_shoe.image_static, mainapp_detailshoe.id as detailShoe_id, mainapp_detailshoe.newPrice, mainapp_detailshoe.quantityAvailable, mainapp_detailshoe.size, mainapp_color.id as color_id, mainapp_color.colorName from mainapp_shoe inner join mainapp_detailshoe on (mainapp_shoe.id = mainapp_detailshoe.shoe_id) inner join mainapp_color on (mainapp_detailshoe.color_id = mainapp_color.id) where mainapp_shoe.id = {shoe_id} and mainapp_detailshoe.size = {shoe_size} and mainapp_color.id = {color_id}; ''' shoes, detail_shoes, colors = SqlUtils.get_result( query, Shoe, DetailShoe, Color) if len(detail_shoes) == 0: err_code = ErrorCode.ERR_IN_ACT_SHOW_FINAL_CHOICE err_message = f'Xin lỗi {prefix_name}{customer_name}, hệ thống đã xảy ra lỗi. error code={err_code}' dispatcher.utter_message(text=err_message) return [] elif detail_shoes[0].quantityAvailable < 1: text = 'Xin lỗi {}, đôi {} size {} màu {} hết hàng rồi ạ' \ .format(prefix_name, shoe_model, shoe_size, color_name) dispatcher.utter_message(text=text) return [] else: text = '{} thêm món này vào giỏ hàng giúp {} nhé'.format( bot_position, prefix_name) dispatcher.utter_message(text=text) horizontal_template_elements = [] for index in range(len(shoes)): shoe = shoes[index] detail_shoe = detail_shoes[index] detail_shoe: DetailShoe buttons = [ PostBackButton( title='Thêm vào giỏ hàng' + shoe.shoeModel, str_send_to_webhook='ok luôn. thêm vào giỏ hàng'), # UrlButton( # title='Mua luôn đôi này', # url_access=MyWebUrl.get_buy_now_url(messenger_id=messenger_id, # detail_shoe_id=detail_shoe.detailShoe_id) # ), ] element = HorizontalTemplateElement( image_url=shoe.image_static, title=shoe.shoeName, subtitle=f'màu: {color_name}, size: {shoe_size}\n ' + price_format(detail_shoe.newPrice), default_action=HorizontalTemplateElement.DefaultAction( MyWebUrl.get_detail_shoe_url(shoe.shoe_id)), list_buttons=buttons, ) horizontal_template_elements.append(element) horizontal_template = HorizontalTemplate( horizontal_template_elements) dispatcher.utter_message( json_message=horizontal_template.to_json_message()) # xem tiep quick_reply_elements = [ QuickReplyElement(QuickReplyElement.TEXT, 'Xem thêm giày', 'Xem đôi giày khác'), ] text = 'Hành động khác:' # if not coupon_id: # text = '{} có muốn lấy mã giảm giá không ạ?'.format(prefix_name) # quick_reply_elements.append( # QuickReplyElement(QuickReplyElement.TEXT, 'Lấy mã giảm giá', 'cho tôi xem mã giảm giá'), # ) quick_replies = QuickReplies( text_before_template=text, list_quick_reply_elements=quick_reply_elements) dispatcher.utter_message( json_message=quick_replies.to_json_message()) # print('*****************************************************') # print(horizontal_template.to_json_message()) # print('*****************************************************') # pprint(quick_replies.to_json_message()) return []
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: """ Trả về generic template san pham cuoi va ask them vao gio hang/thanh toan :param dispatcher: :param tracker: :param domain: :return: """ # debug('\n_________act_shoe_new_shoe_________') print('_____' + self.name()) prefix_name = tracker.get_slot(Entities.prefix_name) customer_name = tracker.get_slot(Entities.customer_name) customer_id = tracker.current_state()["sender_id"] bot_position = tracker.get_slot(Entities.bot_position) shoe_id = tracker.get_slot(Entities.shoe_id) shoe_model = tracker.get_slot(Entities.shoe_model) shoe_size = tracker.get_slot(Entities.shoe_size) color_id = tracker.get_slot(Entities.color_id) color_name = tracker.get_slot(Entities.shoe_color) # get user id by messenger id query = f''' select id as user_id from account_user where messengerId = {customer_id}; ''' users = SqlUtils.get_result(query, User) if len(users) == 0: # chua tao tai khoan cho user err_code = ErrorCode.ERR_IN_ACT_ADD_TO_CARD err_message = f'Xin lỗi {prefix_name}{customer_name}, hệ thống đã xảy ra lỗi. Không thể thêm vào giỏ hàng.' \ f' error code={err_code}' dispatcher.utter_message(text=err_message) return [] # neu them vao gio hang ma chua chon size, color thi chon size, color mac dinh if shoe_size is None or color_id is None: query = f''' select mainapp_shoe.shoeName, mainapp_shoe.shoeThumbnail, mainapp_shoe.image_static, mainapp_detailshoe.newPrice, mainapp_detailshoe.size, mainapp_color.colorName, mainapp_color.id as color_id from mainapp_detailshoe inner join mainapp_shoe on (mainapp_shoe.id = mainapp_detailshoe.shoe_id) inner join mainapp_color on (mainapp_color.id = mainapp_detailshoe.color_id) where mainapp_shoe.active = 1 and mainapp_detailshoe.quantityAvailable > 0 and mainapp_shoe.id = 8 ; ''' detail_shoes, shoes, colors = SqlUtils.get_result( query, DetailShoe, Shoe, Color) if len(detail_shoes) > 0: shoe_size = detail_shoes[0].size color_id = colors[0].color_id user = users[0] debug.debug_print_content('________act add to cart________') debug.debug_print_content(f'user id: {user.user_id}') debug.debug_print_content(f'color id: {color_id}') debug.debug_print_content(f'size id: {shoe_size}') result = MyWebApi.add_to_cart(user.user_id, shoe_id, color_id, shoe_size) debug.debug_print_content(f'result: {result}') if result.status_code == 201: text = 'Thêm vào giỏ hàng thành công' else: text = 'Thêm vào giỏ hàng thất bại' dispatcher.utter_message(text=text) # xem tiep quick_reply_elements = [ QuickReplyElement(QuickReplyElement.TEXT, 'Xem giỏ hàng', 'cho xem giỏ hàng'), QuickReplyElement(QuickReplyElement.TEXT, 'Đặt hàng luôn', 'đặt hàng luôn'), QuickReplyElement(QuickReplyElement.TEXT, 'Xem lại menu', 'tôi muốn xem menu'), ] quick_replies = QuickReplies( text_before_template='Tùy chọn khác', list_quick_reply_elements=quick_reply_elements) # dispatcher.utter_message(json_message=quick_replies.to_json_message()) return []
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict[Text, Any]]: """ Trả về generic template 6 sản phẩm hot + quick replies đòi xem thêm :param dispatcher: :param tracker: :param domain: :return: """ # debug('\n_________act_shoe_new_shoe_________') print('_____' + self.name()) prefix_name = tracker.get_slot(Entities.prefix_name) customer_name = tracker.get_slot(Entities.customer_name) bot_position = tracker.get_slot(Entities.bot_position) err_message = f'Xin lỗi {prefix_name}{customer_name}, hệ thống đã xảy ra lỗi. ' query = ''' select distinct mainapp_shoe.id as shoe_id, mainapp_shoe.shoeName, mainapp_shoe.shoeModel, mainapp_shoe.viewCount, mainapp_shoe.quantitySold, mainapp_shoe.image_static, mainapp_shoe.favouriteCount, mainapp_shoe.shoeThumbnail, mainapp_category.categoryName, mainapp_detailshoe.newPrice, sum(mainapp_detailshoe.quantityAvailable) as totalQuantityAvailable, (mainapp_shoe.viewCount + mainapp_shoe.quantitySold + mainapp_shoe.favouriteCount) as hotCount, datediff(curdate(), mainapp_shoe.dateCreated) as days, ((mainapp_shoe.viewCount + mainapp_shoe.quantitySold + mainapp_shoe.favouriteCount)/datediff(curdate(), mainapp_shoe.dateCreated)) as hotRate from mainapp_shoe inner join mainapp_detailshoe on mainapp_shoe.id = mainapp_detailshoe.shoe_id inner join mainapp_category on mainapp_shoe.category_id = mainapp_category.id where active = 1 group by mainapp_shoe.id, mainapp_shoe.shoeName, mainapp_shoe.shoeModel, mainapp_shoe.viewCount, mainapp_shoe.quantitySold, mainapp_shoe.favouriteCount, mainapp_shoe.shoeThumbnail, mainapp_shoe.image_static, mainapp_category.categoryName, mainapp_detailshoe.newPrice having totalQuantityAvailable > 0 order by hotRate desc limit 0, 5; ''' shoes = SqlUtils.get_result(query, Shoe) detail_shoes = SqlUtils.get_result(query, DetailShoe) if len(shoes) == 0: err_code = ErrorCode.ERR_IN_ACT_GET_NEW_SHOE err_message = err_message + 'error code=' + err_code dispatcher.utter_message(err_message) else: dispatcher.utter_message( text='Dưới đây là top những đôi đc mọi người xem và mua nhiều nhất đó ' + prefix_name + customer_name) # horizontal_template_elements = [] # for index in range(len(shoes)): # shoe = shoes[index] # detail_shoe = detail_shoes[index] # # buttons = [ # PostBackButton( # title='Xem đôi này ' + shoe.shoeModel, # str_send_to_webhook='tôi muốn xem mẫu ' + shoe.shoeModel # ), # UrlButton( # title='Xem trên website', # url_access=MyWebUrl.get_detail_shoe_url(shoe.shoe_id) # ), # ] # # element = HorizontalTemplateElement( # image_url=shoe.shoeThumbnail, # title=shoe.shoeName, # subtitle=price_format(detail_shoe.newPrice), # default_action='', # list_buttons=buttons, # ) # horizontal_template_elements.append(element) # horizontal_template = HorizontalTemplate(horizontal_template_elements) horizontal_template = HorizontalTemplate.from_shoes_detail_shoe(shoes=shoes, detail_shoes=detail_shoes) dispatcher.utter_message(json_message=horizontal_template.to_json_message()) # xem tiep quick_reply_elements = [ QuickReplyElement(QuickReplyElement.TEXT, 'Xem thêm', 'còn đôi nào khác k?'), QuickReplyElement(QuickReplyElement.TEXT, 'Xem lại menu', 'tôi muốn xem menu'), ] quick_replies = QuickReplies(text_before_template='', list_quick_reply_elements=quick_reply_elements) dispatcher.utter_message(json_message=quick_replies.to_json_message()) # xem tiep quick_reply_elements = [ QuickReplyElement(QuickReplyElement.TEXT, 'Xem thêm', 'còn đôi nào khác k?'), QuickReplyElement(QuickReplyElement.TEXT, 'Thôi', 'tôi muốn xem menu'), ] quick_replies = QuickReplies(text_before_template='Xin hãy chọn một hành động', list_quick_reply_elements=quick_reply_elements) dispatcher.utter_message(json_message=quick_replies.to_json_message()) return []