Skip to content

Commit 8b24896

Browse files
add
1 parent 62baa3c commit 8b24896

2 files changed

Lines changed: 123 additions & 16 deletions

File tree

1.75 KB
Binary file not shown.

restaurant_bot/index.py

Lines changed: 123 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ def process_for_facebook(data):
276276
elif action == "show.cart":
277277
sender_id = get_facebook_sender_id(data)
278278
update_user_last_trigger_time(sender_id)
279-
return show_my_cart(sender_id)
279+
return show_my_cart(sender_id, "")
280280
elif action == "cart.remove":
281281
sender_id = get_facebook_sender_id(data)
282282
update_user_last_trigger_time(sender_id)
@@ -300,10 +300,71 @@ def process_for_facebook(data):
300300
sender_id = get_facebook_sender_id(data)
301301
update_user_last_trigger_time(sender_id)
302302
return confirm_order(sender_id)
303+
elif action == "change.quantity":
304+
return process_change_quantity(data)
305+
elif action == "update.quantity":
306+
return process_update_quantity(data)
307+
pass
303308
else:
304309
return "nothing"
305310

306311

312+
# Update quantity
313+
def process_update_quantity(data):
314+
sender_id = get_facebook_sender_id(data)
315+
update_user_last_trigger_time(sender_id)
316+
317+
output_contexs = data['queryResult']['outputContexts']
318+
update_order_food_item_name = ""
319+
order_quantity_number = ""
320+
for row in output_contexs:
321+
if "parameters" in row:
322+
parameters = row['parameters']
323+
if "order_food_items" in parameters:
324+
update_order_food_item_name = str(parameters['order_food_items'])
325+
if "order_quantity_number" in parameters:
326+
order_quantity_number = str(parameters['update_quantity_number'])
327+
if len(str(update_order_food_item_name)) > 0 & len(str(order_quantity_number)) > 0:
328+
break
329+
330+
# update quantity
331+
update_tacos_items = []
332+
if sender_id in tacos_food_order:
333+
my_tacos_items = tacos_food_order[sender_id]
334+
for row in my_tacos_items:
335+
if str(row['item_name']).lower() == str(update_order_food_item_name).lower():
336+
row['quantity'] = str(order_quantity_number)
337+
update_tacos_items.append(row)
338+
tacos_food_order[sender_id] = update_tacos_items
339+
return show_my_cart(sender_id, "Here your update cart list")
340+
341+
342+
# Change quantity
343+
def process_change_quantity(data):
344+
sender_id = get_facebook_sender_id(data)
345+
update_user_last_trigger_time(sender_id)
346+
order_food_items = data['queryResult']['parameters'][
347+
'order_food_items']
348+
349+
fulfillment_text = 'confirm order'
350+
ff_response = FulfillmentResponse()
351+
fulfillment_message = ff_response.fulfillment_text(fulfillment_text)
352+
353+
fb_platform = FacebookResponse()
354+
msg = "How many items of " + order_food_items + " do you need?"
355+
replies = ['1', '2', '3', '4', '5', '6', '7', '8']
356+
fb_quick_replies = fb_platform.quick_replies(msg, replies)
357+
358+
ff_response = FulfillmentResponse()
359+
quick_replies_response = ff_response.fulfillment_messages([fb_quick_replies])
360+
reply = ff_response.main_response(fulfillment_message, quick_replies_response)
361+
if DEBUG_LOG_ENABLE:
362+
print(reply)
363+
return reply
364+
365+
pass
366+
367+
307368
# confirm order
308369
def confirm_order(sender_id, msg):
309370
fulfillment_text = 'confirm order'
@@ -423,10 +484,10 @@ def remove_my_cart(sender_id, remove_cart_item, food_category):
423484
else:
424485
tacos_food_order[sender_id] = update_tacos_items
425486

426-
return show_my_cart(sender_id)
487+
return show_my_cart(sender_id, "Here your update cart list")
427488

428489

429-
def show_my_cart(sender_id):
490+
def show_my_cart(sender_id, msg):
430491
# Tacos food order
431492
my_tacos_order = []
432493
if sender_id in tacos_food_order:
@@ -464,13 +525,13 @@ def show_my_cart(sender_id):
464525
for row in my_tacos_order:
465526
if bool(row['status']):
466527
if str(row['item_name']) == "bangladeshi tacos 1":
467-
bangladeshi_tacos_1_item = bangladeshi_tacos_1_item + int(row['quantity'])
528+
bangladeshi_tacos_1_item = bangladeshi_tacos_1_item + float(row['quantity'])
468529
print("bangladeshi_tacos_1_item " + str(bangladeshi_tacos_1_item))
469530
elif str(row['item_name']) == "bangladeshi tacos 2":
470-
bangladeshi_tacos_2_item = bangladeshi_tacos_2_item + int(row['quantity'])
531+
bangladeshi_tacos_2_item = bangladeshi_tacos_2_item + float(row['quantity'])
471532
print("bangladeshi_tacos_2_item " + str(bangladeshi_tacos_2_item))
472533
elif str(row['item_name']) == "bangladeshi tacos 3":
473-
bangladeshi_tacos_3_item = bangladeshi_tacos_3_item + int(row['quantity'])
534+
bangladeshi_tacos_3_item = bangladeshi_tacos_3_item + float(row['quantity'])
474535
print("bangladeshi_tacos_3_item " + str(bangladeshi_tacos_3_item))
475536

476537
# process response
@@ -536,11 +597,18 @@ def show_my_cart(sender_id):
536597

537598
# todo check order list is empty
538599
# process facebook response
539-
final_response = fb_platform.card_full_fillment(fb_cart_order_response)
540-
reply = ff_response.main_response(fulfillment_message, final_response)
541-
if DEBUG_LOG_ENABLE:
542-
print(reply)
543-
return reply
600+
if len(str(msg)) > 0:
601+
final_response = fb_platform.card_full_fillment(fb_cart_order_response)
602+
reply = ff_response.main_response(fulfillment_message, final_response)
603+
if DEBUG_LOG_ENABLE:
604+
print(reply)
605+
return reply
606+
else:
607+
final_response = fb_platform.card_full_fillment(fb_cart_order_response)
608+
reply = ff_response.main_response(fulfillment_message, final_response)
609+
if DEBUG_LOG_ENABLE:
610+
print(reply)
611+
return reply
544612

545613

546614
# Food category order
@@ -974,6 +1042,45 @@ def quick_replies(self, title, quick_replies_list):
9741042
"platform": self.platform
9751043
}
9761044

1045+
def quick_replies2(self, title, quick_replies_list):
1046+
if title == "":
1047+
raise Exception("Title is required for basic card in facebook.")
1048+
# quick_replies_list must contains at least one string
1049+
elif len(quick_replies_list) <= 0:
1050+
raise Exception(
1051+
"Quick replies response must contain at least on text string.")
1052+
else:
1053+
# quick replies list to store the quick replie text
1054+
quick_replies = []
1055+
for quick_reply in quick_replies_list:
1056+
# append to the list
1057+
quick_replies.append(
1058+
str(quick_reply)
1059+
)
1060+
1061+
qu = [
1062+
{
1063+
"content_type": "text",
1064+
"title": "Red",
1065+
"payload": "<POSTBACK_PAYLOAD>",
1066+
"image_url": "http://example.com/img/red.png"
1067+
}, {
1068+
"content_type": "text",
1069+
"title": "Green",
1070+
"payload": "<POSTBACK_PAYLOAD>",
1071+
"image_url": "http://example.com/img/green.png"
1072+
}
1073+
]
1074+
1075+
# return the response JSON
1076+
return {
1077+
"quickReplies": {
1078+
"title": str(title),
1079+
"quickReplies": qu
1080+
},
1081+
"platform": self.platform
1082+
}
1083+
9771084
def image_response(self, url):
9781085
# check url
9791086
if url == "":
@@ -1364,9 +1471,9 @@ def rest_api_confirm_user_order():
13641471
print(user_orders)
13651472

13661473
if len(orders) == 0:
1367-
return jsonify({'success': False})
1474+
return jsonify({'success': False, 'message': "order list not found"})
13681475
else:
1369-
return jsonify({'success': True})
1476+
return jsonify({'success': True, 'message': "success"})
13701477

13711478

13721479
@app.route('/getUserOrder/<int:sender_id>', methods=['GET'])
@@ -1443,13 +1550,13 @@ def rest_api_get_user_order(sender_id):
14431550
for row in my_tacos_order:
14441551
if bool(row['status']):
14451552
if str(row['item_name']) == "bangladeshi tacos 1":
1446-
bangladeshi_tacos_1_item = bangladeshi_tacos_1_item + int(row['quantity'])
1553+
bangladeshi_tacos_1_item = bangladeshi_tacos_1_item + float(row['quantity'])
14471554
print("bangladeshi_tacos_1_item " + str(bangladeshi_tacos_1_item))
14481555
elif str(row['item_name']) == "bangladeshi tacos 2":
1449-
bangladeshi_tacos_2_item = bangladeshi_tacos_2_item + int(row['quantity'])
1556+
bangladeshi_tacos_2_item = bangladeshi_tacos_2_item + float(row['quantity'])
14501557
print("bangladeshi_tacos_2_item " + str(bangladeshi_tacos_2_item))
14511558
elif str(row['item_name']) == "bangladeshi tacos 3":
1452-
bangladeshi_tacos_3_item = bangladeshi_tacos_3_item + int(row['quantity'])
1559+
bangladeshi_tacos_3_item = bangladeshi_tacos_3_item + float(row['quantity'])
14531560
print("bangladeshi_tacos_3_item " + str(bangladeshi_tacos_3_item))
14541561

14551562
if bangladeshi_tacos_1_item > 0:

0 commit comments

Comments
 (0)