diff --git a/back_end/Dockerfile b/back_end/Dockerfile index 51c2ffb..2021eeb 100644 --- a/back_end/Dockerfile +++ b/back_end/Dockerfile @@ -10,4 +10,4 @@ COPY . . EXPOSE 8080 -CMD [ "python", "./bot_app.py" ] +CMD [ "python", "./run.py" ] diff --git a/back_end/bot_app/__init__.py b/back_end/bot_app/__init__.py index e13818e..9eab7a5 100644 --- a/back_end/bot_app/__init__.py +++ b/back_end/bot_app/__init__.py @@ -1,4 +1,5 @@ from flask import Flask +import bot_app.fulfillment.utility as util from bot_app.fulfillment.controllers import fulfillment app = Flask(__name__) @@ -9,4 +10,8 @@ app.register_blueprint(fulfillment) @app.route('/') def index(): - return 'Service Working!!!' \ No newline at end of file + return 'Service Working!!!' + +@app.route('/test') +def test(): + return util.sample_response('Hello World Test!!') \ No newline at end of file diff --git a/back_end/bot_app/fulfillment/buying_drink.py b/back_end/bot_app/fulfillment/buying_drink.py new file mode 100644 index 0000000..ab898e7 --- /dev/null +++ b/back_end/bot_app/fulfillment/buying_drink.py @@ -0,0 +1,77 @@ +import bot_app.fulfillment.utility as util +from flask import jsonify + +# 確認訂單 +def ordering_summary(fulfillment): + context = util.lookup_context(fulfillment, 'buying_drink_ordering-followup') + params = context.get('parameters') + strResp = '您的訂購資訊如下:\n飲料: ' + params['hot_cold'] + params['drink_item.original'] + '\n數量: ' + str(params['number']) + '\n甜度冰塊: ' + params['ice_level'] + '' + params['sugar_level'] + '\n\n請問是否訂購?' + return util.sample_response(strResp) + +# 詢問飲料種類 +def ask_category(fulfillment): + params = util.lookup_context(fulfillment, 'buying_drink_dialog_context').get('parameters') + + drinks = {'咖啡': ['美式', '拿鐵', '卡布奇諾'], '茶': ['紅茶', '綠茶', '烏龍茶'], '果汁': ['芒果汁', '檸檬汁', '芭樂汁']} + + drink_cate = params.get('drink_category') + hot_cold = params.get('hot_cold') + drink_item = params.get('drink_item') + + response_str = '我們有' + if not drink_item == '': + if drink_item in sum([x for x in drinks.values()], []): + return util.sample_response(response_str + drink_item['drink_item']) + else: + return util.sample_response('抱歉!我們沒有' + drink_item['drink_item']) + else: + if not drink_cate == '': + return util.sample_response(response_str + ','.join(drinks.get(drink_cate))) + else: + return util.sample_response(response_str + ','.join(list(drinks.keys()))) + + return util.sample_response('') + +# fulfillment - 訂飲料 +def ordering(fulfillment): + params = fulfillment.get('queryResult').get('parameters') + deliver_method = params.get('deliver_method', '') + drink_item = params.get('drink_item.original', '') + ice_level = params.get('ice_level', '') + sugar_level = params.get('sugar_level', '') + number = params.get('number', '') + + if deliver_method == '外送': + jsonRep = { + 'followupEventInput': { + 'name': 'events_deliver_info', + 'languageCode': 'zh-TW', + 'parameters': {} + } + } + else: + jsonRep = { + 'followupEventInput': { + 'name': 'events_order_confirm', + 'languageCode': 'zh-TW', + 'parameters': {} + } + } + return jsonify(jsonRep) + # strResp = '您的訂購資訊如下:\n飲料: ' + drink_item + '\n數量: ' + str(number) + '\n甜度冰塊: ' + ice_level + '' + sugar_level + '\n\n請問是否訂購?' + # + # return sample_response(strResp) + +# 確認地址 +def ordering_delivery_info(fulfillment): + print(fulfillment) + jsonRep = { + 'followupEventInput': { + 'name': 'events_order_confirm', + 'languageCode': 'zh-TW', + 'parameters': {} + } + } + return jsonify(jsonRep) + + diff --git a/back_end/bot_app/fulfillment/controllers.py b/back_end/bot_app/fulfillment/controllers.py index 9997f5d..f4b8940 100644 --- a/back_end/bot_app/fulfillment/controllers.py +++ b/back_end/bot_app/fulfillment/controllers.py @@ -1,27 +1,30 @@ import os +import bot_app.fulfillment.utility as util +import bot_app.fulfillment.buying_drink as buying_drink from flask import Blueprint, Flask, jsonify, request, json import threading import time import requests from queue import Queue + # app = Flask(__name__) fulfillment = Blueprint('fulfillment',__name__,url_prefix='/qa_bot/fulfillment') result = None -@fulfillment.route('/',methods=['GET', 'POST']) +@fulfillment.route('',methods=['GET', 'POST']) def index(): jsonObj = request.get_json() try: handleName = jsonObj.get('queryResult').get('intent')['displayName'] print('Handler:', handleName) - + return eval(handleName + '(jsonObj)') - except: - return sample_response('找不到對應的fulfillment handler!!!') - + except Exception as e: + print(str(e)) + return util.sample_response(str(e)) @fulfillment.route("/short_call", methods=['GET', 'POST']) def five_secend_call(): @@ -48,125 +51,4 @@ def fetch_url(): , json={"question": "outlook有問題可以找誰?"}) print(res.json()) result = res - return res - -# 確認訂單 -def buying_drink_ordering_summary(fulfillment): - context = lookup_context(fulfillment, 'buying_drink_ordering-followup') - params = context.get('parameters') - strResp = '您的訂購資訊如下:\n飲料: ' + params['hot_cold'] + params['drink_item.original'] + '\n數量: ' + str(params['number']) + '\n甜度冰塊: ' + params['ice_level'] + '' + params['sugar_level'] + '\n\n請問是否訂購?' - return sample_response(strResp) - -# 詢問飲料種類 -def buying_drink_ask_category(fulfillment): - params = lookup_context(fulfillment, 'buying_drink_dialog_context').get('parameters') - - drinks = {'咖啡': ['美式', '拿鐵', '卡布奇諾'], '茶': ['紅茶', '綠茶', '烏龍茶'], '果汁': ['芒果汁', '檸檬汁', '芭樂汁']} - - drink_cate = params.get('drink_category') - hot_cold = params.get('hot_cold') - drink_item = params.get('drink_item') - - response_str = '我們有' - if not drink_item == '': - if drink_item in sum([x for x in drinks.values()], []): - return sample_response(response_str + drink_item['drink_item']) - else: - return sample_response('抱歉!我們沒有' + drink_item['drink_item']) - else: - if not drink_cate == '': - return sample_response(response_str + ','.join(drinks.get(drink_cate))) - else: - return sample_response(response_str + ','.join(list(drinks.keys()))) - - return sample_response('') - - -# fulfillment - 訂飲料 -def buying_drink_ordering(fulfillment): - params = fulfillment.get('queryResult').get('parameters') - deliver_method = params.get('deliver_method', '') - drink_item = params.get('drink_item.original', '') - ice_level = params.get('ice_level', '') - sugar_level = params.get('sugar_level', '') - number = params.get('number', '') - - if deliver_method == '外送': - jsonRep = { - 'followupEventInput': { - 'name': 'events_deliver_info', - 'languageCode': 'zh-TW', - 'parameters': {} - } - } - else: - jsonRep = { - 'followupEventInput': { - 'name': 'events_order_confirm', - 'languageCode': 'zh-TW', - 'parameters': {} - } - } - return jsonify(jsonRep) - # strResp = '您的訂購資訊如下:\n飲料: ' + drink_item + '\n數量: ' + str(number) + '\n甜度冰塊: ' + ice_level + '' + sugar_level + '\n\n請問是否訂購?' - # - # return sample_response(strResp) - -# 確認地址 -def buying_drink_ordering_delivery_info(fulfillment): - print(fulfillment) - jsonRep = { - 'followupEventInput': { - 'name': 'events_order_confirm', - 'languageCode': 'zh-TW', - 'parameters': {} - } - } - return jsonify(jsonRep) - - -def sample_response(text_content): - strRes = '' - jsonResp = { - 'fulfillmentText': text_content, - 'fulfillmentMessages': [ - # { - # 'image' : { - # 'imageUri':'https://yt3.ggpht.com/a/AGF-l78sCrWnJHZlRs-DP1imkaINg2KkpT5Gomkahw=s900-mo-c-c0xffffffff-rj-k-no', - # 'accessibilityText':'Hello World!!' - # } - # } - - # { - # 'card': { - # 'title': 'card title', - # 'subtitle': 'card text', - # 'imageUri': 'https://assistant.google.com/static/images/molecule/Molecule-Formation-stop.png', - # 'buttons': [ - # { - # 'text': 'button text', - # 'postback': 'https://assistant.google.com/' - # } - # ] - # } - # } - ], - 'source': 'richard-shih.idv.tw' - } - return jsonify(jsonResp) - - -def lookup_context(fulfillment,lookup_pattern): - contexts = fulfillment.get('queryResult').get('outputContexts') - search_key = fulfillment.get('session') + '/contexts/' + lookup_pattern - return next((x for x in contexts if x['name']== search_key),None) - -def main(): - pass - # port = os.environ.get('FLASK_EXPOSE_PORT') - # port = port if port != None else 8080 - # app.config['JSON_AS_ASCII'] = False - # app.run(host='0.0.0.0', port=port, debug=True) - -if __name__ == '__main__': - main() + return res \ No newline at end of file diff --git a/back_end/bot_app/fulfillment/__init__.py b/back_end/bot_app/fulfillment/system_pic.py similarity index 100% rename from back_end/bot_app/fulfillment/__init__.py rename to back_end/bot_app/fulfillment/system_pic.py diff --git a/back_end/bot_app/fulfillment/utility.py b/back_end/bot_app/fulfillment/utility.py new file mode 100644 index 0000000..1e9c9fc --- /dev/null +++ b/back_end/bot_app/fulfillment/utility.py @@ -0,0 +1,36 @@ + +from flask import jsonify +def sample_response(text_content): + strRes = '' + jsonResp = { + 'fulfillmentText': text_content, + 'fulfillmentMessages': [ + # { + # 'image' : { + # 'imageUri':'https://yt3.ggpht.com/a/AGF-l78sCrWnJHZlRs-DP1imkaINg2KkpT5Gomkahw=s900-mo-c-c0xffffffff-rj-k-no', + # 'accessibilityText':'Hello World!!' + # } + # } + + # { + # 'card': { + # 'title': 'card title', + # 'subtitle': 'card text', + # 'imageUri': 'https://assistant.google.com/static/images/molecule/Molecule-Formation-stop.png', + # 'buttons': [ + # { + # 'text': 'button text', + # 'postback': 'https://assistant.google.com/' + # } + # ] + # } + # } + ], + 'source': 'richard_shih@wanhai.com' + } + return jsonify(jsonResp) + +def lookup_context(fulfillment,lookup_pattern): + contexts = fulfillment.get('queryResult').get('outputContexts') + search_key = fulfillment.get('session') + '/contexts/' + lookup_pattern + return next((x for x in contexts if x['name']== search_key),None) \ No newline at end of file diff --git a/back_end/bot_app/fulfillment/equip_crawling.py b/back_end/bot_app/fulfillment/whl_family.py similarity index 100% rename from back_end/bot_app/fulfillment/equip_crawling.py rename to back_end/bot_app/fulfillment/whl_family.py