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.

87 lines
2.6 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
  1. import os
  2. from flask import Flask, jsonify, request, json
  3. import threading
  4. import time
  5. import requests
  6. from queue import Queue
  7. app = Flask(__name__)
  8. result = None
  9. @app.route('/')
  10. def sayHi():
  11. return 'Hello World1!!!'
  12. @app.route("/short_call",methods=['GET','POST'])
  13. def five_secend_call():
  14. global result
  15. result = None
  16. curr = time.time()
  17. thread = threading.Thread(target=fetch_url,name='query_qa_thread')
  18. thread.start()
  19. while (curr + 4.5) > time.time():
  20. if not result == None:
  21. return jsonify(result.json())
  22. else:
  23. time.sleep(0.3)
  24. return jsonify({"respone":"None"})
  25. @app.route('/qa_bot/fulfillment', methods=['GET', 'POST'])
  26. def index():
  27. jsonObj = request.get_json()
  28. # print(type(jsonObj))
  29. # print(jsonObj)
  30. print('responseId => {},\n session => {}'.format(jsonObj.get('responseId'),jsonObj.get('session')))
  31. print(request.get_data(as_text=True))
  32. return sample_response()
  33. def fetch_url():
  34. global result
  35. my_headers = {'Authorization': 'EndpointKey 365cdd9c-7af2-48bd-9dfe-031986319115','Content-Type':'application/json'}
  36. res = requests.post('https://whlqakb.azurewebsites.net/qnamaker/knowledgebases/f15e1174-339e-4a81-a95e-747143f77b02/generateAnswer'
  37. ,headers=my_headers
  38. ,json={"question":"outlook有問題可以找誰?"})
  39. print(res.json())
  40. result = res
  41. return res
  42. def sample_response():
  43. strRes = ''
  44. jsonResp = {
  45. 'fulfillmentText':'超級帥哥Richard',
  46. 'fulfillmentMessages': [
  47. # {
  48. # 'image' : {
  49. # 'imageUri':'https://yt3.ggpht.com/a/AGF-l78sCrWnJHZlRs-DP1imkaINg2KkpT5Gomkahw=s900-mo-c-c0xffffffff-rj-k-no',
  50. # 'accessibilityText':'Hello World!!'
  51. # }
  52. # }
  53. # {
  54. # 'card': {
  55. # 'title': 'card title',
  56. # 'subtitle': 'card text',
  57. # 'imageUri': 'https://assistant.google.com/static/images/molecule/Molecule-Formation-stop.png',
  58. # 'buttons': [
  59. # {
  60. # 'text': 'button text',
  61. # 'postback': 'https://assistant.google.com/'
  62. # }
  63. # ]
  64. # }
  65. # }
  66. ],
  67. 'source':'richard-shih.idv.tw'
  68. }
  69. return jsonify(jsonResp)
  70. def main():
  71. port = os.environ.get('FLASK_EXPOSE_PORT')
  72. port = port if port != None else 8080
  73. app.config['JSON_AS_ASCII'] = False
  74. app.run(host='0.0.0.0', port=port, debug=True)
  75. if __name__ == '__main__':
  76. main()