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.

81 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. from flask import jsonify
  2. from sqlalchemy import create_engine
  3. db_engine = None
  4. def simple_response(text_content='', fulfillmentObj=None):
  5. if not fulfillmentObj == None:
  6. jsonResp = fulfillmentObj
  7. else:
  8. jsonResp = {
  9. 'fulfillmentText': text_content,
  10. 'fulfillmentMessages': [
  11. # {
  12. # 'image' : {
  13. # 'imageUri':'https://yt3.ggpht.com/a/AGF-l78sCrWnJHZlRs-DP1imkaINg2KkpT5Gomkahw=s900-mo-c-c0xffffffff-rj-k-no',
  14. # 'accessibilityText':'Hello World!!'
  15. # }
  16. # }
  17. # {
  18. # 'card': {
  19. # 'title': 'card title',
  20. # 'subtitle': 'card text',
  21. # 'imageUri': 'https://assistant.google.com/static/images/molecule/Molecule-Formation-stop.png',
  22. # 'buttons': [
  23. # {
  24. # 'text': 'button text',
  25. # 'postback': 'https://assistant.google.com/'
  26. # }
  27. # ]
  28. # }
  29. # }
  30. ],
  31. 'source': 'richard_shih@wanhai.com'
  32. }
  33. return jsonify(jsonResp)
  34. def lookup_context(fulfillment, lookup_pattern):
  35. contexts = fulfillment.get('queryResult').get('outputContexts')
  36. search_key = fulfillment.get('session') + '/contexts/' + lookup_pattern
  37. return next((x for x in contexts if x['name'] == search_key), None)
  38. # 清除所有的Output Contexts
  39. def reset_all_contexts(fulfillmentObj=None,context_list=[]):
  40. if 'queryResult' in fulfillmentObj.keys():
  41. contexts = fulfillmentObj.get('queryResult').get('outputContexts')
  42. else:
  43. contexts = fulfillmentObj.get('outputContexts')
  44. for context in contexts:
  45. if len(context_list) > 0:
  46. if context['name'] in context_list:
  47. context['lifespanCount'] = 0
  48. else:
  49. context['lifespanCount'] = 0
  50. return fulfillmentObj
  51. def get_db_conn():
  52. try:
  53. conn = db_engine.connect()
  54. return conn
  55. except Exception as e:
  56. print(str(e))
  57. def init_app(app, pre_connect=True):
  58. global db_engine
  59. db_engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'], encoding='utf8')
  60. pre_connect = app.config['DATABASE_CONNECT_OPTIONS']['PRE_CONNECT']
  61. if pre_connect:
  62. try:
  63. conn = db_engine.connect()
  64. except Exception as e:
  65. print(str(e))