From ad306b4ed54827c1534cbae1113034cfd0ed99e9 Mon Sep 17 00:00:00 2001 From: shihxuancheng Date: Thu, 26 Sep 2019 19:16:44 +0800 Subject: [PATCH] add oracle access ability --- .gitignore | 3 +- back_end/Dockerfile | 2 +- back_end/bot_app/__init__.py | 2 ++ back_end/bot_app/fulfillment/system_pic.py | 33 ++++++++++++++++------ back_end/bot_app/fulfillment/utility.py | 23 ++++++++++++++- back_end/config.py | 6 ++-- back_end/requirements.txt | 4 ++- 7 files changed, 58 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index ef63112..ac274e6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .ipynb_checkpoints/ env/ credential -__pycache__ \ No newline at end of file +__pycache__ +back_end/config.py diff --git a/back_end/Dockerfile b/back_end/Dockerfile index 2021eeb..81d5220 100644 --- a/back_end/Dockerfile +++ b/back_end/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.6 +FROM shihxuancheng/python-oracle LABEL MAINTAINER shihxuancheng@gmail.com WORKDIR /usr/src/qa_bot diff --git a/back_end/bot_app/__init__.py b/back_end/bot_app/__init__.py index e5c1c37..69f8810 100644 --- a/back_end/bot_app/__init__.py +++ b/back_end/bot_app/__init__.py @@ -8,6 +8,8 @@ app.config.from_object('config') app.register_blueprint(fulfillment) +util.init_app(app) + @app.route('/') def index(): return 'Service Working!!!' diff --git a/back_end/bot_app/fulfillment/system_pic.py b/back_end/bot_app/fulfillment/system_pic.py index be1f158..a712e8a 100644 --- a/back_end/bot_app/fulfillment/system_pic.py +++ b/back_end/bot_app/fulfillment/system_pic.py @@ -1,19 +1,35 @@ import bot_app.fulfillment.utility as util -''' -Intent - system_pic -''' # 查詢系統pic def looking_for_pic(fulfillment): - print(fulfillment) + # print(fulfillment) sys_code = fulfillment.get('queryResult').get('parameters').get('sys_code') + # 取得系統pic - strRes = sys_code + '負責人是Richard Shih, Grace Liu, 請問是否幫您將問題轉達給系統負責同仁?' - # 無法取得,請user重新輸入 + strsql = ''' + select * + from (select c.user_name_e,c.user_name_c, b.user_email + from sec1117 a, sec1118 b,sec1102 c + where a.user_code = b.user_code + and a.user_code=c.user_code + and a.pic_type = 'B' + and a.system_code = :sys_code + order by a.pic_seq) + where rownum <= 3 + ''' + with util.get_db_conn() as conn: + res = conn.execute(strsql,sys_code=sys_code) + res = res.fetchall() + if len(res) == 0: + strRes = '無法取得指定系統負責人,請重新輸入' + else: + strRes = sys_code + '負責人是: ' + for row in res: + strRes += row[1]+'('+row[2]+'), ' + strRes += ' 請問是否幫您將問題轉給系統負責人?' return util.simple_response(text_content=strRes) - # 確認轉達問題給pic def confirm_forward(fulfillment): print(fulfillment) @@ -25,7 +41,6 @@ def confirm_forward(fulfillment): } }) - # 轉達問題給pic def forward_issue(fulfillment): params = fulfillment.get('queryResult').get('parameters') @@ -38,4 +53,4 @@ def forward_issue(fulfillment): user_name = 'Richard Shih' strRes = '好的' + user_name + '已將您的問題 "' + issue + '" 轉達給 ' + sys_code + ' 負責人' - return util.simple_response(text_content=strRes) + return util.simple_response(text_content=strRes) \ No newline at end of file diff --git a/back_end/bot_app/fulfillment/utility.py b/back_end/bot_app/fulfillment/utility.py index ef0beec..d478a08 100644 --- a/back_end/bot_app/fulfillment/utility.py +++ b/back_end/bot_app/fulfillment/utility.py @@ -1,5 +1,8 @@ from flask import jsonify +from sqlalchemy import create_engine +db_engine = None + def simple_response(text_content='', fulfillmentObj=None): if not fulfillmentObj==None: jsonResp = fulfillmentObj @@ -36,4 +39,22 @@ def simple_response(text_content='', fulfillmentObj=None): 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 + return next((x for x in contexts if x['name']== search_key),None) + +def get_db_conn(): + try: + conn = db_engine.connect() + return conn + except Exception as e: + print(str(e)) + +def init_app(app, pre_connect = True): + global db_engine + db_engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'], encoding='utf8') + pre_connect = app.config['DATABASE_CONNECT_OPTIONS']['PRE_CONNECT'] + if pre_connect: + try: + conn = db_engine.connect() + except Exception as e: + print(str(e)) + \ No newline at end of file diff --git a/back_end/config.py b/back_end/config.py index 63fc5fe..89d20f3 100644 --- a/back_end/config.py +++ b/back_end/config.py @@ -8,8 +8,10 @@ BASE_DIR = os.path.abspath(os.path.dirname(__file__)) # Define the database - we are working with # SQLite for this example -SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(BASE_DIR, 'app.db') -DATABASE_CONNECT_OPTIONS = {} +SQLALCHEMY_DATABASE_URI = 'oracle+cx_oracle://whpr:w2t2pr0614@tpe3.wanhai.com:1526/whl2' +DATABASE_CONNECT_OPTIONS = { + "PRE_CONNECT":True +} # Application threads. A common general assumption is # using 2 per available processor cores - to handle diff --git a/back_end/requirements.txt b/back_end/requirements.txt index 7667f53..9cc1947 100644 --- a/back_end/requirements.txt +++ b/back_end/requirements.txt @@ -2,4 +2,6 @@ dialogflow==0.6.0 interval==1.0.0 requests==2.22.0 beautifulsoup4==4.7.1 -flask==1.0.3 \ No newline at end of file +flask==1.0.3 +cx-Oracle==7.2.2 +SQLAlchemy==1.3.8 \ No newline at end of file