def put(self, psid): customer = Customer.find_by_psid(psid) order = Order.query.filter_by(psid=psid, is_confirmed=False).first() vendor = Vendor.find_by_page_id(customer.page_id) catalog = Catalog.find_by_page_id(vendor.page_id) knowledge = catalog.knowledge bot = Bot(access_token=vendor.page_access_token) data = request.get_json() print(data) if not data['items']: bot.send_text_message(psid, 'انت لم تطلب شيء بعد!') return 'Order is empty', 200 order.item = data['items'] order.save() confirm_block = ButtonTemplate() confirm_block.add_web_url( **{ knowledge['buttons']['values']['Confirm_Order']: 'https://rest-bot-dev.herokuapp.com/confirm_order' }) confirm_block.add_postback( **{knowledge['buttons']['values']['Add_to_Order']: 'main_menu'}) confirm_block.add_web_url( **{ knowledge['buttons']['values']['Edit_Order']: 'https://rest-bot-dev.herokuapp.com/edit_order' }) confirm_block.set_text('تم تعديل الأوردر الخاص بك') bot.send_template_message(psid, {'payload': confirm_block.get_template()}) return 'Order was Edited', 200
def handle_messaging(data): message_type = helper.get_type_from_message(data) page_id = helper.get_vendor_from_message(data) vendor = helper.handle_vendor(page_id)[0] catalog = helper.handle_vendor(page_id)[1] blocks = catalog.blocks bot = Bot(access_token=vendor.page_access_token) sender_id = helper.get_customer_from_message(data) print(sender_id) print(message_type) if not vendor.check_customer_limit: bot.send_text_message( sender_id, 'You have reached the Maximum Customer limit for your tier.') return 'Customer Limit', 200 # if not vendor.is_open(): # bot.send_text_message( # sender_id, 'المطعم مغلق حاليا \nالرجاء المحاولة مرة أخرى خلال مواعيد العمل الرسمية من {} الى {}'.format(vendor.opening_hours.strftime('%H:%M'), vendor.closing_hours.strftime('%H:%M'))) # return 'Vendor is Closed', 200 if message_type == "text": # HANDLE TEXT MESSAGES HERE bot.send_before_message(sender_id) block = blocks['get_started'] print(bot.send_template_message(sender_id, block)) return "text", 200 elif message_type == "quick_reply": # HANDLE QUICK REPLIES HERE bot.send_before_message(sender_id) block_name = helper.quick_replies_events(data) if block_name in blocks: block = blocks[block_name] print(block) # bot.send_template_message(sender_id, block) print(bot.send_template_message(sender_id, block)) return "quick_reply", 200 elif message_type == "postback": # HANDLE POSTBACK HERE bot.send_before_message(sender_id) block_name = helper.postback_events(data) if block_name in blocks: block = blocks[block_name] print(block) # bot.send_template_message(sender_id, block) print(bot.send_template_message(sender_id, block)) return "postback", 200 else: return "ok", 200
def post(self, psid): # look for customer customer = Customer.find_by_psid(psid) vendor = customer.vendor catalog = Catalog.find_by_page_id(vendor.page_id) knowledge = catalog.knowledge bot = Bot(access_token=vendor.page_access_token) order = helper.get_order_from_customer(customer) print(order) if order.is_confirmed: print('Order is Confirmed') return 'Order is Confirmed', 200 # update customer info customer.name = request.form.get('name') customer.phone_number = request.form.get('phone_number') customer.address = request.form.get('address') customer.save() # imp # make a receipt receipt = ReceiptTemplate(recipient_name=customer.name, order_number=order.number) for item in order.items: # fill receipt with order from database receipt.add_element(title=item['name'], quantity=item['quantity'], price=item['price']) receipt.set_summary(total_cost=order.price) print(receipt.get_receipt()) print( bot.send_template_message(psid, {'payload': receipt.get_receipt()})) bot.send_text_message( psid, knowledge['browse']['values']['Order_Confirmation_Message']) order.confirm() # imp msg_id = helper.send_order_to_vendor(order, vendor.fcm_token) print(msg_id) return 'Customer info was added', 200