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.

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