Browse Source

add oracle access ability

master
shihxuancheng 5 years ago
parent
commit
ad306b4ed5
7 changed files with 58 additions and 15 deletions
  1. 3
      .gitignore
  2. 2
      back_end/Dockerfile
  3. 2
      back_end/bot_app/__init__.py
  4. 33
      back_end/bot_app/fulfillment/system_pic.py
  5. 23
      back_end/bot_app/fulfillment/utility.py
  6. 6
      back_end/config.py
  7. 4
      back_end/requirements.txt

3
.gitignore

@ -2,4 +2,5 @@
.ipynb_checkpoints/ .ipynb_checkpoints/
env/ env/
credential credential
__pycache__
__pycache__
back_end/config.py

2
back_end/Dockerfile

@ -1,4 +1,4 @@
FROM python:3.6
FROM shihxuancheng/python-oracle
LABEL MAINTAINER shihxuancheng@gmail.com LABEL MAINTAINER shihxuancheng@gmail.com
WORKDIR /usr/src/qa_bot WORKDIR /usr/src/qa_bot

2
back_end/bot_app/__init__.py

@ -8,6 +8,8 @@ app.config.from_object('config')
app.register_blueprint(fulfillment) app.register_blueprint(fulfillment)
util.init_app(app)
@app.route('/') @app.route('/')
def index(): def index():
return 'Service Working!!!' return 'Service Working!!!'

33
back_end/bot_app/fulfillment/system_pic.py

@ -1,19 +1,35 @@
import bot_app.fulfillment.utility as util import bot_app.fulfillment.utility as util
'''
Intent - system_pic
'''
# 查詢系統pic # 查詢系統pic
def looking_for_pic(fulfillment): def looking_for_pic(fulfillment):
print(fulfillment)
# print(fulfillment)
sys_code = fulfillment.get('queryResult').get('parameters').get('sys_code') sys_code = fulfillment.get('queryResult').get('parameters').get('sys_code')
# 取得系統pic # 取得系統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) return util.simple_response(text_content=strRes)
# 確認轉達問題給pic # 確認轉達問題給pic
def confirm_forward(fulfillment): def confirm_forward(fulfillment):
print(fulfillment) print(fulfillment)
@ -25,7 +41,6 @@ def confirm_forward(fulfillment):
} }
}) })
# 轉達問題給pic # 轉達問題給pic
def forward_issue(fulfillment): def forward_issue(fulfillment):
params = fulfillment.get('queryResult').get('parameters') params = fulfillment.get('queryResult').get('parameters')
@ -38,4 +53,4 @@ def forward_issue(fulfillment):
user_name = 'Richard Shih' user_name = 'Richard Shih'
strRes = '好的' + user_name + '已將您的問題 "' + issue + '" 轉達給 ' + sys_code + ' 負責人' strRes = '好的' + user_name + '已將您的問題 "' + issue + '" 轉達給 ' + sys_code + ' 負責人'
return util.simple_response(text_content=strRes)
return util.simple_response(text_content=strRes)

23
back_end/bot_app/fulfillment/utility.py

@ -1,5 +1,8 @@
from flask import jsonify from flask import jsonify
from sqlalchemy import create_engine
db_engine = None
def simple_response(text_content='', fulfillmentObj=None): def simple_response(text_content='', fulfillmentObj=None):
if not fulfillmentObj==None: if not fulfillmentObj==None:
jsonResp = fulfillmentObj jsonResp = fulfillmentObj
@ -36,4 +39,22 @@ def simple_response(text_content='', fulfillmentObj=None):
def lookup_context(fulfillment,lookup_pattern): def lookup_context(fulfillment,lookup_pattern):
contexts = fulfillment.get('queryResult').get('outputContexts') contexts = fulfillment.get('queryResult').get('outputContexts')
search_key = fulfillment.get('session') + '/contexts/' + lookup_pattern search_key = fulfillment.get('session') + '/contexts/' + lookup_pattern
return next((x for x in contexts if x['name']== search_key),None)
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))

6
back_end/config.py

@ -8,8 +8,10 @@ BASE_DIR = os.path.abspath(os.path.dirname(__file__))
# Define the database - we are working with # Define the database - we are working with
# SQLite for this example # 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 # Application threads. A common general assumption is
# using 2 per available processor cores - to handle # using 2 per available processor cores - to handle

4
back_end/requirements.txt

@ -2,4 +2,6 @@ dialogflow==0.6.0
interval==1.0.0 interval==1.0.0
requests==2.22.0 requests==2.22.0
beautifulsoup4==4.7.1 beautifulsoup4==4.7.1
flask==1.0.3
flask==1.0.3
cx-Oracle==7.2.2
SQLAlchemy==1.3.8
Loading…
Cancel
Save