You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
2.0 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. import os
  2. import bot_app.fulfillment.utility as util
  3. import bot_app.fulfillment.buying_drink as buying_drink
  4. import bot_app.fulfillment.system_pic as system_pic
  5. import bot_app.fulfillment.whl_family as whl_family
  6. import bot_app.fulfillment.whl_report as whl_report
  7. from flask import Blueprint, Flask, jsonify, request, json
  8. import threading
  9. import time
  10. import requests
  11. from queue import Queue
  12. # app = Flask(__name__)
  13. fulfillment = Blueprint('fulfillment', __name__, url_prefix='/qa_bot/fulfillment')
  14. result = None
  15. @fulfillment.route('', methods=['GET', 'POST'])
  16. def index():
  17. jsonObj = request.get_json()
  18. try:
  19. handleName = jsonObj.get('queryResult').get('intent')['displayName']
  20. print('Handler:', handleName)
  21. return eval(handleName + '(jsonObj)')
  22. except Exception as e:
  23. print(str(e))
  24. return util.simple_response(fulfillmentObj=util.reset_all_contexts(fulfillmentObj=jsonObj))
  25. # return util.simple_response(str(e))
  26. @fulfillment.route("/short_call", methods=['GET', 'POST'])
  27. def five_secend_call():
  28. global result
  29. result = None
  30. curr = time.time()
  31. thread = threading.Thread(target=fetch_url, name='query_qa_thread')
  32. thread.start()
  33. while (curr + 4.5) > time.time():
  34. if not result == None:
  35. return jsonify(result.json())
  36. else:
  37. time.sleep(0.3)
  38. return jsonify({"respone": "None"})
  39. def fetch_url():
  40. global result
  41. my_headers = {'Authorization': 'EndpointKey 365cdd9c-7af2-48bd-9dfe-031986319115',
  42. 'Content-Type': 'application/json'}
  43. res = requests.post(
  44. 'https://whlqakb.azurewebsites.net/qnamaker/knowledgebases/f15e1174-339e-4a81-a95e-747143f77b02/generateAnswer'
  45. , headers=my_headers
  46. , json={"question": "outlook有問題可以找誰?"})
  47. print(res.json())
  48. result = res
  49. return res
  50. def init_app(app):
  51. app.register_blueprint(fulfillment)
  52. util.init_app(app)
  53. buying_drink.init_app(app)
  54. system_pic.init_app(app)
  55. whl_report.init_app(app)
  56. whl_family.init_app(app)