-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
54 lines (42 loc) · 1.55 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import os
import json
from flask import Flask, request, jsonify
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
from services import QuestionStateService, PersistService, SpreadsheetReader
from middleware import check_authentication
PROJECT_ID = os.environ.get('PROJECT_ID')
SPREADSHEET_ID = os.environ.get('SPREADSHEET_ID')
CREDENTIALS = json.loads(os.environ.get('SPREADSHEET_CREDENTIALS'))
sentry_sdk.init(
dsn=os.environ.get('SENTRY_DSN'),
integrations=[FlaskIntegration()]
)
app = Flask(__name__)
persist_service = PersistService(os.environ.get('REDIS_URL'))
@app.route("/sync")
@check_authentication
def sync():
reader = SpreadsheetReader(SPREADSHEET_ID, CREDENTIALS)
questions = reader.get_values_from_sheet()
persist_service.set_questions(questions)
return "Sincronización completada con éxito"
@app.route("/hook", methods=['POST'])
@check_authentication
def webhook():
request_dialogflow = request.get_json()
try:
session_id = request_dialogflow['session'].split('/')[-1]
request_text = request_dialogflow['queryResult']['queryText']
request_contexts = request_dialogflow['queryResult']['outputContexts']
except (KeyError, IndexError):
return 'Not a valid Dialogflow webhook payload', 400
questions = persist_service.get_questions()
question_state = QuestionStateService(
os.environ.get('PROJECT_ID'),
session_id,
request_contexts,
questions,
request_text
)
return jsonify(question_state.get_next_response())