Browse Source

daily commit

master
Richard Shih 5 years ago
parent
commit
bbc2692f18
7 changed files with 131 additions and 96 deletions
  1. 2
      back_end/bot_app/__init__.py
  2. 24
      back_end/bot_app/fulfillment/buying_drink.py
  3. 14
      back_end/bot_app/fulfillment/controllers.py
  4. 41
      back_end/bot_app/fulfillment/system_pic.py
  5. 57
      back_end/bot_app/fulfillment/utility.py
  6. 87
      back_end/bot_app/fulfillment/whl_family.py
  7. 2
      back_end/run.py

2
back_end/bot_app/__init__.py

@ -14,4 +14,4 @@ def index():
@app.route('/test')
def test():
return util.sample_response('Hello World Test!!')
return util.simple_response('Hello World Test!!')

24
back_end/bot_app/fulfillment/buying_drink.py

@ -1,12 +1,13 @@
import bot_app.fulfillment.utility as util
from flask import jsonify
'''
Intent - buying_drink
'''
# 確認訂單
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)
return util.simple_response(strResp)
# 詢問飲料種類
def ask_category(fulfillment):
@ -21,16 +22,16 @@ def ask_category(fulfillment):
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'])
return util.simple_response(response_str + drink_item['drink_item'])
else:
return util.sample_response('抱歉!我們沒有' + drink_item['drink_item'])
return util.simple_response('抱歉!我們沒有' + drink_item['drink_item'])
else:
if not drink_cate == '':
return util.sample_response(response_str + ','.join(drinks.get(drink_cate)))
return util.simple_response(response_str + ','.join(drinks.get(drink_cate)))
else:
return util.sample_response(response_str + ','.join(list(drinks.keys())))
return util.simple_response(response_str + ','.join(list(drinks.keys())))
return util.sample_response('')
return util.simple_response('')
# fulfillment - 訂飲料
def ordering(fulfillment):
@ -57,7 +58,7 @@ def ordering(fulfillment):
'parameters': {}
}
}
return jsonify(jsonRep)
return util.simple_response(fulfillmentObj=jsonRep)
# strResp = '您的訂購資訊如下:\n飲料: ' + drink_item + '\n數量: ' + str(number) + '\n甜度冰塊: ' + ice_level + '' + sugar_level + '\n\n請問是否訂購?'
#
# return sample_response(strResp)
@ -65,13 +66,12 @@ def ordering(fulfillment):
# 確認地址
def ordering_delivery_info(fulfillment):
print(fulfillment)
jsonRep = {
return util.simple_response(fulfillmentObj={
'followupEventInput': {
'name': 'events_order_confirm',
'languageCode': 'zh-TW',
'parameters': {}
}
}
return jsonify(jsonRep)
})

14
back_end/bot_app/fulfillment/controllers.py

@ -1,30 +1,32 @@
import os
import bot_app.fulfillment.utility as util
import bot_app.fulfillment.buying_drink as buying_drink
import bot_app.fulfillment.system_pic as system_pic
import bot_app.fulfillment.whl_family as whl_family
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')
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 Exception as e:
print(str(e))
return util.sample_response(str(e))
return util.simple_response(str(e))
@fulfillment.route("/short_call", methods=['GET', 'POST'])
def five_secend_call():
@ -51,4 +53,4 @@ def fetch_url():
, json={"question": "outlook有問題可以找誰?"})
print(res.json())
result = res
return res
return res

41
back_end/bot_app/fulfillment/system_pic.py

@ -0,0 +1,41 @@
import bot_app.fulfillment.utility as util
'''
Intent - system_pic
'''
# 查詢系統pic
def looking_for_pic(fulfillment):
print(fulfillment)
sys_code = fulfillment.get('queryResult').get('parameters').get('sys_code')
# 取得系統pic
strRes = sys_code + '負責人是Richard Shih, Grace Liu, 請問是否幫您將問題轉達給系統負責同仁?'
# 無法取得,請user重新輸入
return util.simple_response(text_content=strRes)
# 確認轉達問題給pic
def confirm_forward(fulfillment):
print(fulfillment)
return util.simple_response(fulfillmentObj={
'followupEventInput': {
'name': 'events_forward_issue',
'languageCode': 'zh-TW',
'parameters': util.lookup_context(fulfillment, 'system_piclooking_for_pic-followup').get('parameters')
}
})
# 轉達問題給pic
def forward_issue(fulfillment):
params = fulfillment.get('queryResult').get('parameters')
employee_no = params.get('employee_no', '')
ext_no = params.get('ext_no', '')
issue = params.get('issue', '')
sys_code = params.get('sys_code')
# get user name, div, dept information from sec system by employee_no
user_name = 'Richard Shih'
strRes = '好的' + user_name + '已將您的問題 "' + issue + '" 轉達給 ' + sys_code + ' 負責人'
return util.simple_response(text_content=strRes)

57
back_end/bot_app/fulfillment/utility.py

@ -1,33 +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!!'
# }
# }
def simple_response(text_content='', fulfillmentObj=None):
if not fulfillmentObj==None:
jsonResp = fulfillmentObj
else:
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'
}
# {
# '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):

87
back_end/bot_app/fulfillment/whl_family.py

@ -1,4 +1,3 @@
# %%
import requests
from bs4 import BeautifulSoup as bs
@ -6,30 +5,29 @@ from bs4 import BeautifulSoup as bs
base_url = 'http://family.wanhai.com'
loginURL = base_url + '/Login.jsp'
account = 'M1933'
password = '1933whlM'
password = 'Whlm1933'
payload = {'Account': account, 'Password': password}
meeting_rooms = None
massage_rooms = None
session_req = None
# session_req.post(loginURL,data=payload)
# %%
# session_req.post(loginURL,data=payload)
# %%
def login(id, pw):
global session_req
if session_req == None:
if session_req is None:
session_req = requests.Session()
res = session_req.post(
loginURL, data={'Account': account, 'Password': password})
print(res.request.headers['Cookie'])
# %%
# %%
def isLogin():
global session_req
if session_req == None:
if session_req is None:
session_req = requests.Session()
route_url = base_url + '/MainPage.jsp'
res = session_req.get(route_url, allow_redirects=False)
@ -39,13 +37,12 @@ def isLogin():
else:
return False
# %%
# %%
def getEquipList(type='MEETING'):
equipList_url = base_url + '/LeaseEquip/equipListOnePage.jsp'
result = session_req.get(
equipList_url+'?file_num=61621&account_id=' + account + '&equip_type='+type)
equipList_url + '?file_num=61621&account_id=' + account + '&equip_type=' + type)
print(result.request.headers['Cookie'])
soup = bs(result.text, 'html.parser')
@ -56,9 +53,8 @@ def getEquipList(type='MEETING'):
# print(equipDict)
return equipDict
# %%
# %%
def getEquipName(equipId):
if equipId in meeting_rooms.keys():
return meeting_rooms[equipId]['name']
@ -67,15 +63,14 @@ def getEquipName(equipId):
else:
return None
# %%
# 查詢設備是否已被占用(by 特定日期/時段)
def isEquipInUsed(b_date, b_time, e_time, equip_type, equip_id):
if equip_type == 'MASSAGE':
print('Not Support Now!!!')
return False
equipUsageURL = base_url+'/LeaseEquip/equipUsage.jsp'
equipUsageURL = base_url + '/LeaseEquip/equipUsage.jsp'
payload = [('q_from_date', b_date),
('q_from_time', b_time),
('q_to_date', b_date),
@ -91,16 +86,15 @@ def isEquipInUsed(b_date, b_time, e_time, equip_type, equip_id):
return False
return False
# %%
# 查詢設備可用時段(by 特定日期)
# %%
# 查詢設備可用時段(by 特定日期
def search_available_time(equip_type, equip_id, strDate):
if equip_type == 'MASSAGE':
print('Not Support Now!!!')
return
from interval import Interval
equipUsageURL = base_url+'/LeaseEquip/equipUsage.jsp'
equipUsageURL = base_url + '/LeaseEquip/equipUsage.jsp'
payload = [('q_from_date', strDate),
('q_from_time', ''),
('q_to_date', strDate),
@ -112,16 +106,15 @@ def search_available_time(equip_type, equip_id, strDate):
# print(soup.prettify())
print('{}:\n{}'.format(equip_id, soup.select('td[nowrap]')[0].text))
# %%
# 查詢可用設備(by 特定日期/時段)
def search_available_equips(equip_type, strDate, b_time, e_time):
if equip_type == 'MASSAGE':
print('Not Support Now!!!')
return
from interval import Interval
equipUsageURL = base_url+'/LeaseEquip/equipUsage.jsp'
equipUsageURL = base_url + '/LeaseEquip/equipUsage.jsp'
payload = [('q_from_date', strDate),
('q_from_time', ''),
('q_to_date', strDate),
@ -142,7 +135,7 @@ def search_available_equips(equip_type, strDate, b_time, e_time):
target = Interval(int(b_time), int(e_time), closed=False)
periods = [x.text.split('~') for x in elem.select('b')]
periodIntvs = [Interval(x[0], x[1]) for x in list(
map(lambda bb:[int(x) for x in bb], periods))]
map(lambda bb: [int(x) for x in bb], periods))]
# print(equips[index])
# for pp in periodIntvs:
@ -158,10 +151,9 @@ def search_available_equips(equip_type, strDate, b_time, e_time):
if avail == 1:
print('{}\n'.format(equips[index]))
# %%
# 預約設備
def bookingEquip(equip_id, equip_type, strDate, b_time, e_time):
if equip_type == 'MASSAGE':
print('Not Support Now!!!')
@ -196,10 +188,9 @@ def bookingEquip(equip_id, equip_type, strDate, b_time, e_time):
print(soup.select('table tr:nth-child(2) td:nth-child(5)')[0].text)
# print(soup.prettify())
# %%
# 取消預約設備
def cancalBookingEquip(equip_type, rent_no):
if equip_type == 'MASSAGE':
print('Not Support Now!!!')
@ -221,14 +212,13 @@ def cancalBookingEquip(equip_type, rent_no):
soup = bs(session_req.post(bookingURL, data=payload).text, 'html.parser')
print(soup.prettify())
# %%
# 列出個人設備預約紀錄
def listRentedEquips():
equipList_url = base_url + '/LeaseEquip/equipListOnePage.jsp'
result = session_req.get(
equipList_url+'?file_num=61621&account_id=' + account + '&equip_type=MEETING')
equipList_url + '?file_num=61621&account_id=' + account + '&equip_type=MEETING')
soup = bs(result.text, 'html.parser')
elements = soup.select('form[name="dataForm1"] input[name^="i_rent_no"]')
elements = [x.find_parent().select('td[align="center"]') for x in elements]
@ -238,30 +228,29 @@ def listRentedEquips():
# %%
listRentedEquips()
# listRentedEquips()
# %%
equipId = 'CF15-10F-DN-01'
print(getEquipName(equipId))
inUsed = isEquipInUsed('20190606', '1900', '2000', 'MEETING', equipId)
print('Equip in used? {}'.format(inUsed))
# equipId = 'CF15-10F-DN-01'
# print(getEquipName(equipId))
# inUsed = isEquipInUsed('20190606', '1900', '2000', 'MEETING', equipId)
# print('Equip in used? {}'.format(inUsed))
# %%
search_available_equips('MEETING', '20190624', '0900', '1200')
# search_available_equips('MEETING', '20190624', '0900', '1200')
# %%
search_available_time('MEETING', 'CF01-9F', '20190624')
# search_available_time('MEETING', 'CF01-9F', '20190624')
# %%
if not isLogin():
login(account, password)
meeting_rooms = getEquipList('MEETING')
massage_rooms = getEquipList('MASSAGE')
for x in meeting_rooms:
print(x, meeting_rooms.get(x))
print('\n')
for x in massage_rooms:
print(x, massage_rooms.get(x))
# if not isLogin():
# login(account, password)
# meeting_rooms = getEquipList('MEETING')
# massage_rooms = getEquipList('MASSAGE')
#
# for x in meeting_rooms:
# print(x, meeting_rooms.get(x))
# print('\n')
# for x in massage_rooms:
# print(x, massage_rooms.get(x))
# %%
if __name__ == '__main__':

2
back_end/run.py

@ -2,4 +2,4 @@ import os
from bot_app import app
port = os.environ.get('FLASK_EXPOSE_PORT')
port = port if port != None else 8080
app.run(host='0.0.0.0', port=port, debug=True)
app.run(host='0.0.0.0', port=port)
Loading…
Cancel
Save