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.

59 lines
2.0 KiB

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. def get_db_conn():
  39. try:
  40. conn = db_engine.connect()
  41. return conn
  42. except Exception as e:
  43. print(str(e))
  44. def init_app(app, pre_connect = True):
  45. global db_engine
  46. db_engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'], encoding='utf8')
  47. pre_connect = app.config['DATABASE_CONNECT_OPTIONS']['PRE_CONNECT']
  48. if pre_connect:
  49. try:
  50. conn = db_engine.connect()
  51. except Exception as e:
  52. print(str(e))